Skip to content

Commit 63ff83e

Browse files
committed
Merge the rules
1 parent e3a884c commit 63ff83e

File tree

10 files changed

+156
-90
lines changed

10 files changed

+156
-90
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Required by googletest 1.17.
22
common --cxxopt=-std=c++17
3+
common --incompatible_autoload_externally=+@rules_cc

cc/private/rules_impl/cc_toolchain.bzl

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ load("//cc/common:cc_helper.bzl", "cc_helper")
1919
load("//cc/common:semantics.bzl", "semantics")
2020
load("//cc/private/rules_impl/fdo:fdo_context.bzl", "create_fdo_context")
2121
load("//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo")
22+
load("//cc/toolchains:cc_toolchain_info.bzl", "ToolConfigInfo")
23+
load("//cc/toolchains/impl:collect.bzl", "collect_action_types")
24+
load("//cc/toolchains/impl:toolchain_config.bzl", "CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS", "cc_toolchain_config_impl_helper")
2225
load(":cc_toolchain_provider_helper.bzl", "get_cc_toolchain_provider")
2326

2427
ToolchainInfo = platform_common.ToolchainInfo
@@ -74,49 +77,111 @@ def _single_file(ctx, attr_name):
7477
return files[0]
7578
return None
7679

80+
# Taken from https://bazel.build/docs/cc-toolchain-config-reference#actions
81+
# TODO: This is best-effort. Update this with the correct file groups once we
82+
# work out what actions correspond to what file groups.
83+
LEGACY_FILE_GROUPS = {
84+
"ar_files": [
85+
Label("//cc/toolchains/actions:ar_actions"),
86+
],
87+
"as_files": [
88+
Label("//cc/toolchains/actions:assembly_actions"),
89+
],
90+
"compiler_files": [
91+
Label("//cc/toolchains/actions:cc_flags_make_variable"),
92+
Label("//cc/toolchains/actions:c_compile"),
93+
Label("//cc/toolchains/actions:cpp_compile"),
94+
Label("//cc/toolchains/actions:cpp_header_parsing"),
95+
],
96+
# There are no actions listed for coverage and objcopy in action_names.bzl.
97+
"coverage_files": [],
98+
"dwp_files": [
99+
Label("//cc/toolchains/actions:dwp"),
100+
],
101+
"linker_files": [
102+
Label("//cc/toolchains/actions:cpp_link_dynamic_library"),
103+
Label("//cc/toolchains/actions:cpp_link_nodeps_dynamic_library"),
104+
Label("//cc/toolchains/actions:cpp_link_executable"),
105+
],
106+
"objcopy_files": [],
107+
"strip_files": [
108+
Label("//cc/toolchains/actions:strip"),
109+
],
110+
}
111+
77112
def _attributes(ctx):
78113
grep_includes = None
79114
if not semantics.is_bazel:
80115
grep_includes = _single_file(ctx, "_grep_includes")
81116

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

84-
all_files = _files(ctx, "all_files")
119+
if ctx.attr.toolchain_config:
120+
for key in CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS.keys():
121+
if getattr(ctx.attr, key):
122+
fail("Must not pass %s when passing `toolchain_config`" % key)
123+
124+
cc_toolchain_config_info = ctx.attr.toolchain_config[CcToolchainConfigInfo]
125+
all_files = _files(ctx, "all_files")
126+
127+
legacy_file_groups = {
128+
"as_files": _files(ctx, "as_files"),
129+
"ar_files": _files(ctx, "ar_files"),
130+
"dwp_files": _files(ctx, "dwp_files"),
131+
"compiler_files": _files(ctx, "compiler_files"),
132+
"strip_files": _files(ctx, "strip_files"),
133+
"objcopy_files": _files(ctx, "objcopy_files"),
134+
"coverage_files": _files(ctx, "coverage_files") or all_files,
135+
}
136+
137+
linker_files = _files(ctx, "linker_files")
138+
139+
else:
140+
if not ctx.attr.tool_map:
141+
fail("Must pass `tool_map` when not passing `toolchain_config`")
142+
toolchain_config_info, cc_toolchain_config_info = cc_toolchain_config_impl_helper(ctx)
143+
144+
legacy_file_groups = {}
145+
for group, actions in LEGACY_FILE_GROUPS.items():
146+
legacy_file_groups[group] = depset(transitive = [
147+
toolchain_config_info.files[action]
148+
for action in collect_action_types(actions).to_list()
149+
if action in toolchain_config_info.files
150+
])
151+
152+
all_files = depset(transitive = [legacy_file_groups.values()])
153+
legacy_file_groups["coverage_files"] = legacy_file_groups["coverage_files"] or all_files
154+
linker_files = legacy_file_groups.pop("linker_files")
155+
85156
return struct(
86157
supports_param_files = ctx.attr.supports_param_files,
87158
runtime_solib_dir_base = "_solib__" + cc_common.escape_label(label = ctx.label),
88-
cc_toolchain_config_info = _provider(ctx.attr.toolchain_config, CcToolchainConfigInfo),
159+
cc_toolchain_config_info = cc_toolchain_config_info,
89160
static_runtime_lib = ctx.attr.static_runtime_lib,
90161
dynamic_runtime_lib = ctx.attr.dynamic_runtime_lib,
91162
supports_header_parsing = ctx.attr.supports_header_parsing,
92163
all_files = all_files,
93-
compiler_files = _files(ctx, "compiler_files"),
94-
strip_files = _files(ctx, "strip_files"),
95-
objcopy_files = _files(ctx, "objcopy_files"),
96164
link_dynamic_library_tool = ctx.file._link_dynamic_library_tool,
97165
grep_includes = grep_includes,
98166
aggregate_ddi = _single_file(ctx, "_aggregate_ddi"),
99167
generate_modmap = _single_file(ctx, "_generate_modmap"),
100168
module_map = ctx.attr.module_map,
101-
as_files = _files(ctx, "as_files"),
102-
ar_files = _files(ctx, "ar_files"),
103-
dwp_files = _files(ctx, "dwp_files"),
104169
module_map_artifact = _single_file(ctx, "module_map"),
105-
all_files_including_libc = depset(transitive = [_files(ctx, "all_files"), _files(ctx, latebound_libc)]),
170+
all_files_including_libc = depset(transitive = [all_files, _files(ctx, latebound_libc)]),
106171
zipper = ctx.file._zipper,
107172
linker_files = _full_inputs_for_link(
108173
ctx,
109-
_files(ctx, "linker_files"),
174+
linker_files,
110175
_files(ctx, latebound_libc),
111176
),
112177
cc_toolchain_label = ctx.label,
113-
coverage_files = _files(ctx, "coverage_files") or all_files,
114178
compiler_files_without_includes = _files(ctx, "compiler_files_without_includes"),
115179
libc = _files(ctx, latebound_libc),
116180
libc_top_label = _label(ctx, latebound_libc),
117181
if_so_builder = ctx.file._interface_library_builder,
118182
allowlist_for_layering_check = _package_specification_provider(ctx, "disabling_parse_headers_and_layering_check_allowed"),
119183
build_info_files = _provider(ctx.attr._build_info_translator, OutputGroupInfo),
184+
**legacy_file_groups
120185
)
121186

122187
def _cc_toolchain_impl(ctx):
@@ -200,7 +265,6 @@ crosstool_config.toolchain.
200265
),
201266
"all_files": attr.label(
202267
allow_files = True,
203-
mandatory = True,
204268
doc = """
205269
Collection of all cc_toolchain artifacts. These artifacts will be added as inputs to all
206270
rules_cc related actions (with the exception of actions that are using more precise sets of
@@ -214,7 +278,6 @@ rules using C++ toolchain.</p>""",
214278
),
215279
"compiler_files": attr.label(
216280
allow_files = True,
217-
mandatory = True,
218281
doc = """
219282
Collection of all cc_toolchain artifacts required for compile actions.""",
220283
),
@@ -226,13 +289,11 @@ input discovery is supported (currently Google-only).""",
226289
),
227290
"strip_files": attr.label(
228291
allow_files = True,
229-
mandatory = True,
230292
doc = """
231293
Collection of all cc_toolchain artifacts required for strip actions.""",
232294
),
233295
"objcopy_files": attr.label(
234296
allow_files = True,
235-
mandatory = True,
236297
doc = """
237298
Collection of all cc_toolchain artifacts required for objcopy actions.""",
238299
),
@@ -248,13 +309,11 @@ Collection of all cc_toolchain artifacts required for archiving actions.""",
248309
),
249310
"linker_files": attr.label(
250311
allow_files = True,
251-
mandatory = True,
252312
doc = """
253313
Collection of all cc_toolchain artifacts required for linking actions.""",
254314
),
255315
"dwp_files": attr.label(
256316
allow_files = True,
257-
mandatory = True,
258317
doc = """
259318
Collection of all cc_toolchain artifacts required for dwp actions.""",
260319
),
@@ -307,7 +366,6 @@ Set to True when cc_toolchain supports header parsing actions.""",
307366
),
308367
"toolchain_config": attr.label(
309368
allow_files = False,
310-
mandatory = True,
311369
providers = [CcToolchainConfigInfo],
312370
doc = """
313371
The label of the rule providing <code>cc_toolchain_config_info</code>.""",
@@ -340,5 +398,12 @@ The label of the rule providing <code>cc_toolchain_config_info</code>.""",
340398
default = semantics.BUILD_INFO_TRANLATOR_LABEL,
341399
providers = [OutputGroupInfo],
342400
),
401+
} | CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS | {
402+
# Override tool_map to make it optional.
403+
"tool_map": attr.label(
404+
cfg = "exec",
405+
providers = [ToolConfigInfo],
406+
),
407+
"_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
343408
} | semantics.cpp_modules_tools(), # buildifier: disable=unsorted-dict-items
344409
)

cc/toolchains/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ bzl_library(
2222
"//cc:action_names_bzl",
2323
"//cc:cc_toolchain_config_lib_bzl",
2424
"//cc:find_cc_toolchain_bzl",
25+
"//cc/common:cc_helper_bzl",
26+
"//cc/common:semantics_bzl",
2527
"//cc/private:paths_bzl",
2628
"//cc/private/rules_impl:cc_flags_supplier_lib_bzl",
2729
"//cc/private/rules_impl:native_bzl",
2830
"//cc/private/rules_impl:toolchain_rules",
2931
"//cc/private/rules_impl/fdo:fdo_rules",
3032
"//cc/toolchains/impl:toolchain_impl_rules",
33+
"@bazel_features//:features",
3134
"@bazel_skylib//rules/directory:glob",
3235
"@cc_compatibility_proxy//:proxy_bzl",
3336
],

cc/toolchains/cc_toolchain_info.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# Once it's stabilized, we *may* consider opening up parts of the API, or we may
1919
# decide to just require users to use the public user-facing rules.
2020
visibility([
21+
"//cc/private/rules_impl/...",
2122
"//cc/toolchains/...",
2223
"//tests/rule_based_toolchain/...",
2324
])

cc/toolchains/impl/collect.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load(
2222
)
2323

2424
visibility([
25+
"//cc/private/rules_impl/...",
2526
"//cc/toolchains/...",
2627
"//tests/rule_based_toolchain/...",
2728
])

cc/toolchains/impl/toolchain_config.bzl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ load(":legacy_converter.bzl", "convert_toolchain")
2929
load(":toolchain_config_info.bzl", "toolchain_config_info")
3030

3131
visibility([
32+
"//cc/private/rules_impl/...",
3233
"//cc/toolchains/...",
3334
"//tests/rule_based_toolchain/...",
3435
])
@@ -50,7 +51,14 @@ cc_legacy_file_group = rule(
5051
},
5152
)
5253

53-
def _cc_toolchain_config_impl(ctx):
54+
def cc_toolchain_config_impl_helper(ctx):
55+
"""Main implementation for _cc_toolchain_config_impl, reused for rules-based toolchains
56+
57+
Args:
58+
ctx: Rule context
59+
Returns:
60+
toolchain_config_info and cc_toolchain_config_info providers"""
61+
5462
if ctx.attr.features:
5563
fail("Features is a reserved attribute in bazel. Did you mean 'known_features' or 'enabled_features'?")
5664

@@ -66,7 +74,7 @@ def _cc_toolchain_config_impl(ctx):
6674

6775
legacy = convert_toolchain(toolchain_config)
6876

69-
return [
77+
return (
7078
toolchain_config,
7179
cc_common.create_cc_toolchain_config_info(
7280
ctx = ctx,
@@ -90,6 +98,13 @@ def _cc_toolchain_config_impl(ctx):
9098
abi_version = "",
9199
abi_libc_version = "",
92100
),
101+
)
102+
103+
def _cc_toolchain_config_impl(ctx):
104+
toolchain_config, cc_toolchain_config_info = cc_toolchain_config_impl_helper(ctx)
105+
return [
106+
toolchain_config,
107+
cc_toolchain_config_info,
93108
# This allows us to support all_files.
94109
# If all_files was simply an alias to
95110
# //cc/toolchains/actions:all_actions,
@@ -98,19 +113,21 @@ def _cc_toolchain_config_impl(ctx):
98113
DefaultInfo(files = depset(transitive = toolchain_config.files.values())),
99114
]
100115

116+
CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS = {
117+
# Attributes new to this rule.
118+
"compiler": attr.string(default = ""),
119+
"cpu": attr.string(default = ""),
120+
"tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
121+
"args": attr.label_list(providers = [ArgsListInfo]),
122+
"known_features": attr.label_list(providers = [FeatureSetInfo]),
123+
"enabled_features": attr.label_list(providers = [FeatureSetInfo]),
124+
"artifact_name_patterns": attr.label_list(providers = [ArtifactNamePatternInfo]),
125+
"make_variables": attr.label_list(providers = [MakeVariableInfo]),
126+
}
127+
101128
cc_toolchain_config = rule(
102129
implementation = _cc_toolchain_config_impl,
103-
# @unsorted-dict-items
104-
attrs = {
105-
# Attributes new to this rule.
106-
"compiler": attr.string(default = ""),
107-
"cpu": attr.string(default = ""),
108-
"tool_map": attr.label(providers = [ToolConfigInfo], mandatory = True),
109-
"args": attr.label_list(providers = [ArgsListInfo]),
110-
"known_features": attr.label_list(providers = [FeatureSetInfo]),
111-
"enabled_features": attr.label_list(providers = [FeatureSetInfo]),
112-
"artifact_name_patterns": attr.label_list(providers = [ArtifactNamePatternInfo]),
113-
"make_variables": attr.label_list(providers = [MakeVariableInfo]),
130+
attrs = CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS | {
114131
"_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"),
115132
},
116133
provides = [ToolchainConfigInfo],

0 commit comments

Comments
 (0)