Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions cc/private/rules_impl/cc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ load("//cc/common:cc_helper.bzl", "cc_helper")
load("//cc/common:semantics.bzl", "semantics")
load("//cc/private/rules_impl/fdo:fdo_context.bzl", "create_fdo_context")
load("//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo")
load("//cc/toolchains:cc_toolchain_info.bzl", "ToolConfigInfo")
load("//cc/toolchains/impl:collect.bzl", "collect_action_types")
load("//cc/toolchains/impl:toolchain_config.bzl", "CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS", "cc_toolchain_config_impl_helper")
load(":cc_toolchain_provider_helper.bzl", "get_cc_toolchain_provider")

ToolchainInfo = platform_common.ToolchainInfo
Expand Down Expand Up @@ -74,49 +77,111 @@ def _single_file(ctx, attr_name):
return files[0]
return None

# Taken from https://bazel.build/docs/cc-toolchain-config-reference#actions
# TODO: This is best-effort. Update this with the correct file groups once we
# work out what actions correspond to what file groups.
_LEGACY_FILE_GROUPS = {
"ar_files": [
Label("//cc/toolchains/actions:ar_actions"),
],
"as_files": [
Label("//cc/toolchains/actions:assembly_actions"),
],
"compiler_files": [
Label("//cc/toolchains/actions:cc_flags_make_variable"),
Label("//cc/toolchains/actions:c_compile"),
Label("//cc/toolchains/actions:cpp_compile"),
Label("//cc/toolchains/actions:cpp_header_parsing"),
],
# There are no actions listed for coverage and objcopy in action_names.bzl.
"coverage_files": [],
"dwp_files": [
Label("//cc/toolchains/actions:dwp"),
],
"linker_files": [
Label("//cc/toolchains/actions:cpp_link_dynamic_library"),
Label("//cc/toolchains/actions:cpp_link_nodeps_dynamic_library"),
Label("//cc/toolchains/actions:cpp_link_executable"),
],
"objcopy_files": [],
"strip_files": [
Label("//cc/toolchains/actions:strip"),
],
}

def _attributes(ctx):
grep_includes = None
if not semantics.is_bazel:
grep_includes = _single_file(ctx, "_grep_includes")

latebound_libc = _latebound_libc(ctx, "libc_top", "_libc_top")

all_files = _files(ctx, "all_files")
if ctx.attr.toolchain_config:
for key in CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS.keys():
if getattr(ctx.attr, key):
fail("Must not pass %s when passing `toolchain_config`" % key)

cc_toolchain_config_info = ctx.attr.toolchain_config[CcToolchainConfigInfo]
all_files = _files(ctx, "all_files")

legacy_file_groups = {
"as_files": _files(ctx, "as_files"),
"ar_files": _files(ctx, "ar_files"),
"dwp_files": _files(ctx, "dwp_files"),
"compiler_files": _files(ctx, "compiler_files"),
"strip_files": _files(ctx, "strip_files"),
"objcopy_files": _files(ctx, "objcopy_files"),
"coverage_files": _files(ctx, "coverage_files") or all_files,
}

linker_files = _files(ctx, "linker_files")

else:
if not ctx.attr.tool_map:
fail("Must pass `tool_map` when not passing `toolchain_config`")
toolchain_config_info, cc_toolchain_config_info = cc_toolchain_config_impl_helper(ctx)

legacy_file_groups = {}
for group, actions in _LEGACY_FILE_GROUPS.items():
legacy_file_groups[group] = depset(transitive = [
toolchain_config_info.files[action]
for action in collect_action_types(actions).to_list()
if action in toolchain_config_info.files
])

all_files = depset(transitive = [legacy_file_groups.values()])
legacy_file_groups["coverage_files"] = legacy_file_groups["coverage_files"] or all_files
linker_files = legacy_file_groups.pop("linker_files")

return struct(
supports_param_files = ctx.attr.supports_param_files,
runtime_solib_dir_base = "_solib__" + cc_common.escape_label(label = ctx.label),
cc_toolchain_config_info = _provider(ctx.attr.toolchain_config, CcToolchainConfigInfo),
cc_toolchain_config_info = cc_toolchain_config_info,
static_runtime_lib = ctx.attr.static_runtime_lib,
dynamic_runtime_lib = ctx.attr.dynamic_runtime_lib,
supports_header_parsing = ctx.attr.supports_header_parsing,
all_files = all_files,
compiler_files = _files(ctx, "compiler_files"),
strip_files = _files(ctx, "strip_files"),
objcopy_files = _files(ctx, "objcopy_files"),
link_dynamic_library_tool = ctx.file._link_dynamic_library_tool,
grep_includes = grep_includes,
aggregate_ddi = _single_file(ctx, "_aggregate_ddi"),
generate_modmap = _single_file(ctx, "_generate_modmap"),
module_map = ctx.attr.module_map,
as_files = _files(ctx, "as_files"),
ar_files = _files(ctx, "ar_files"),
dwp_files = _files(ctx, "dwp_files"),
module_map_artifact = _single_file(ctx, "module_map"),
all_files_including_libc = depset(transitive = [_files(ctx, "all_files"), _files(ctx, latebound_libc)]),
all_files_including_libc = depset(transitive = [all_files, _files(ctx, latebound_libc)]),
zipper = ctx.file._zipper,
linker_files = _full_inputs_for_link(
ctx,
_files(ctx, "linker_files"),
linker_files,
_files(ctx, latebound_libc),
),
cc_toolchain_label = ctx.label,
coverage_files = _files(ctx, "coverage_files") or all_files,
compiler_files_without_includes = _files(ctx, "compiler_files_without_includes"),
libc = _files(ctx, latebound_libc),
libc_top_label = _label(ctx, latebound_libc),
if_so_builder = ctx.file._interface_library_builder,
allowlist_for_layering_check = _package_specification_provider(ctx, "disabling_parse_headers_and_layering_check_allowed"),
build_info_files = _provider(ctx.attr._build_info_translator, OutputGroupInfo),
**legacy_file_groups
)

def _cc_toolchain_impl(ctx):
Expand Down Expand Up @@ -200,7 +265,6 @@ crosstool_config.toolchain.
),
"all_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts. These artifacts will be added as inputs to all
rules_cc related actions (with the exception of actions that are using more precise sets of
Expand All @@ -214,7 +278,6 @@ rules using C++ toolchain.</p>""",
),
"compiler_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts required for compile actions.""",
),
Expand All @@ -226,13 +289,11 @@ input discovery is supported (currently Google-only).""",
),
"strip_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts required for strip actions.""",
),
"objcopy_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts required for objcopy actions.""",
),
Expand All @@ -248,13 +309,11 @@ Collection of all cc_toolchain artifacts required for archiving actions.""",
),
"linker_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts required for linking actions.""",
),
"dwp_files": attr.label(
allow_files = True,
mandatory = True,
doc = """
Collection of all cc_toolchain artifacts required for dwp actions.""",
),
Expand Down Expand Up @@ -307,7 +366,6 @@ Set to True when cc_toolchain supports header parsing actions.""",
),
"toolchain_config": attr.label(
allow_files = False,
mandatory = True,
providers = [CcToolchainConfigInfo],
doc = """
The label of the rule providing <code>cc_toolchain_config_info</code>.""",
Expand Down Expand Up @@ -340,5 +398,12 @@ The label of the rule providing <code>cc_toolchain_config_info</code>.""",
default = semantics.BUILD_INFO_TRANLATOR_LABEL,
providers = [OutputGroupInfo],
),
} | CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS | {
# Override tool_map to make it optional.
"tool_map": attr.label(
cfg = "exec",
providers = [ToolConfigInfo],
),
"_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
} | semantics.cpp_modules_tools(), # buildifier: disable=unsorted-dict-items
)
1 change: 1 addition & 0 deletions cc/toolchains/cc_toolchain_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Once it's stabilized, we *may* consider opening up parts of the API, or we may
# decide to just require users to use the public user-facing rules.
visibility([
"//cc/private/rules_impl/...",
"//cc/toolchains/...",
"//tests/rule_based_toolchain/...",
])
Expand Down
1 change: 1 addition & 0 deletions cc/toolchains/impl/collect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ load(
)

visibility([
"//cc/private/rules_impl/...",
"//cc/toolchains/...",
"//tests/rule_based_toolchain/...",
])
Expand Down
58 changes: 28 additions & 30 deletions cc/toolchains/impl/toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,30 @@
load("//cc/common:cc_common.bzl", "cc_common")
load(
"//cc/toolchains:cc_toolchain_info.bzl",
"ActionTypeSetInfo",
"ArgsListInfo",
"ArtifactNamePatternInfo",
"FeatureSetInfo",
"MakeVariableInfo",
"ToolConfigInfo",
"ToolchainConfigInfo",
)
load(":collect.bzl", "collect_action_types")
load(":legacy_converter.bzl", "convert_toolchain")
load(":toolchain_config_info.bzl", "toolchain_config_info")

visibility([
"//cc/private/rules_impl/...",
"//cc/toolchains/...",
"//tests/rule_based_toolchain/...",
])

def _cc_legacy_file_group_impl(ctx):
files = ctx.attr.config[ToolchainConfigInfo].files
def cc_toolchain_config_impl_helper(ctx):
"""Main implementation for _cc_toolchain_config_impl, reused for rules-based toolchains

return [DefaultInfo(files = depset(transitive = [
files[action]
for action in collect_action_types(ctx.attr.actions).to_list()
if action in files
]))]
Args:
ctx: Rule context
Returns:
toolchain_config_info and cc_toolchain_config_info providers"""

cc_legacy_file_group = rule(
implementation = _cc_legacy_file_group_impl,
attrs = {
"actions": attr.label_list(providers = [ActionTypeSetInfo], mandatory = True),
"config": attr.label(providers = [ToolchainConfigInfo], mandatory = True),
},
)

def _cc_toolchain_config_impl(ctx):
if ctx.attr.features:
fail("Features is a reserved attribute in bazel. Did you mean 'known_features' or 'enabled_features'?")

Expand All @@ -66,7 +55,7 @@ def _cc_toolchain_config_impl(ctx):

legacy = convert_toolchain(toolchain_config)

return [
return (
toolchain_config,
cc_common.create_cc_toolchain_config_info(
ctx = ctx,
Expand All @@ -90,6 +79,13 @@ def _cc_toolchain_config_impl(ctx):
abi_version = "",
abi_libc_version = "",
),
)

def _cc_toolchain_config_impl(ctx):
toolchain_config, cc_toolchain_config_info = cc_toolchain_config_impl_helper(ctx)
return [
toolchain_config,
cc_toolchain_config_info,
# This allows us to support all_files.
# If all_files was simply an alias to
# //cc/toolchains/actions:all_actions,
Expand All @@ -98,19 +94,21 @@ def _cc_toolchain_config_impl(ctx):
DefaultInfo(files = depset(transitive = toolchain_config.files.values())),
]

CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS = {
# Attributes new to this rule.
"compiler": attr.string(default = ""),
"cpu": attr.string(default = ""),
"tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
"args": attr.label_list(providers = [ArgsListInfo]),
"known_features": attr.label_list(providers = [FeatureSetInfo]),
"enabled_features": attr.label_list(providers = [FeatureSetInfo]),
"artifact_name_patterns": attr.label_list(providers = [ArtifactNamePatternInfo]),
"make_variables": attr.label_list(providers = [MakeVariableInfo]),
}

cc_toolchain_config = rule(
implementation = _cc_toolchain_config_impl,
# @unsorted-dict-items
attrs = {
# Attributes new to this rule.
"compiler": attr.string(default = ""),
"cpu": attr.string(default = ""),
"tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
"args": attr.label_list(providers = [ArgsListInfo]),
"known_features": attr.label_list(providers = [FeatureSetInfo]),
"enabled_features": attr.label_list(providers = [FeatureSetInfo]),
"artifact_name_patterns": attr.label_list(providers = [ArtifactNamePatternInfo]),
"make_variables": attr.label_list(providers = [MakeVariableInfo]),
attrs = CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS | {
"_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
},
provides = [ToolchainConfigInfo],
Expand Down
Loading