Skip to content

Commit 0b11998

Browse files
dzbarskycopybara-github
authored andcommitted
cc_toolchain's tool_map should be cfg-exec-configured
Copybara Import from #555 BEGIN_PUBLIC cc_toolchain's tool_map should be cfg-exec-configured (#555) However, it's not enough for `tool_map` to be on `cc_toolchain_config` - it must be on the toolchain itself to properly share the same exec platform as the action requesting the toolchain. Therefore we rearrange the implementation of the `cc_toolchain` macro to pass it to the rule, and teach the rule to accept it from either the attribute or the `cc_toolchain_config.` Eventually, `cc_toolchain_config` can be retired, though it will be a long journey. Closes #555 END_PUBLIC COPYBARA_INTEGRATE_REVIEW=#555 from dzbarsky:zbarsky/tool_map c964c48 PiperOrigin-RevId: 901427534 Change-Id: I18ae3b4d1bf7806717cadc5e31c743be9bd31e67
1 parent 2993fd1 commit 0b11998

15 files changed

Lines changed: 258 additions & 104 deletions

File tree

.bazelci/presubmit.yml

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ tasks:
220220
- "--enable_bzlmod"
221221
- "--ignore_dev_dependency"
222222

223-
ubuntu_rule_based_toolchains:
223+
# Rules-based toolchains
224+
ubuntu_rule_based_toolchains_bazel_7:
225+
name: Ubuntu rule-based toolchains (Bazel 7)
226+
bazel: 7.2.1
224227
name: Ubuntu rule-based toolchains
225228
platform: ubuntu1804
226229
working_directory: examples/rule_based_toolchain
@@ -231,8 +234,53 @@ tasks:
231234
test_targets:
232235
- "//..."
233236

234-
macos_intel_rule_based_toolchains:
235-
name: macOS rule-based toolchains
237+
ubuntu_rule_based_toolchains_bazel_8:
238+
name: Ubuntu rule-based toolchains (Bazel 8)
239+
bazel: 8.5.0
240+
platform: ubuntu1804
241+
working_directory: examples/rule_based_toolchain
242+
build_flags:
243+
- "--enable_bzlmod"
244+
build_targets:
245+
- "//..."
246+
test_targets:
247+
- "//..."
248+
249+
ubuntu_rule_based_toolchains_bazel_9:
250+
name: Ubuntu rule-based toolchains (Bazel 9)
251+
bazel: 9.0.0rc3
252+
platform: ubuntu1804
253+
working_directory: examples/rule_based_toolchain
254+
build_flags:
255+
- "--enable_bzlmod"
256+
build_targets:
257+
- "//..."
258+
test_targets:
259+
- "//..."
260+
261+
macos_rule_based_toolchains_bazel_7:
262+
name: macOS rule-based toolchains (Bazel 7)
263+
bazel: 7.2.1
264+
platform: macos
265+
working_directory: examples/rule_based_toolchain
266+
build_flags:
267+
- "--enable_bzlmod"
268+
build_targets:
269+
- "//..."
270+
271+
macos_rule_based_toolchains_bazel_8:
272+
name: macOS rule-based toolchains (Bazel 8)
273+
bazel: 8.5.0
274+
platform: macos
275+
working_directory: examples/rule_based_toolchain
276+
build_flags:
277+
- "--enable_bzlmod"
278+
build_targets:
279+
- "//..."
280+
281+
macos_rule_based_toolchains_bazel_9:
282+
name: macOS rule-based toolchains (Bazel 9)
283+
bazel: 9.0.0rc3
236284
platform: macos
237285
working_directory: examples/rule_based_toolchain
238286
build_flags:

cc/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ filegroup(
7171

7272
filegroup(
7373
name = "srcs",
74+
testonly = 1,
7475
srcs = glob([
7576
"**/*.bzl",
7677
"**/BUILD",
7778
]) + [
79+
"//cc/common:srcs",
7880
"//cc/private:srcs",
81+
"//cc/toolchains:srcs",
82+
"@bazel_skylib//:test_deps",
7983
],
8084
visibility = ["//visibility:public"],
8185
)
@@ -194,6 +198,7 @@ bzl_library(
194198
deps = [
195199
":build_settings_bzl",
196200
":cc_postmark_bzl",
201+
"//cc/toolchains:toolchain_rules",
197202
"@cc_compatibility_proxy//:proxy_bzl",
198203
],
199204
)

cc/private/rules_impl/cc_toolchain.bzl

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ 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", "ActionTypeSetInfo", "FeatureSetInfo", "ToolConfigInfo")
23+
load("//cc/toolchains:legacy_file_group.bzl", "LEGACY_FILE_GROUPS")
24+
load("//cc/toolchains/impl:collect.bzl", "collect_action_types")
25+
load("//cc/toolchains/impl:toolchain_config.bzl", "CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS", "cc_toolchain_config_impl_helper")
2226
load(":cc_toolchain_provider_helper.bzl", "get_cc_toolchain_provider")
2327

2428
ToolchainInfo = platform_common.ToolchainInfo
2529
TemplateVariableInfo = platform_common.TemplateVariableInfo
2630

31+
LEGACY_ACTION_SET_DEPS = [
32+
action
33+
for actions in LEGACY_FILE_GROUPS.values()
34+
for action in actions
35+
]
36+
2737
def _files(ctx, attr_name):
2838
attr = getattr(ctx.attr, attr_name, None)
2939
files = []
@@ -81,42 +91,81 @@ def _attributes(ctx):
8191

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

84-
all_files = _files(ctx, "all_files")
94+
if ctx.attr.toolchain_config:
95+
for key in CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS.keys():
96+
if getattr(ctx.attr, key):
97+
fail("Must not pass %s when passing `toolchain_config`" % key)
98+
99+
cc_toolchain_config_info = ctx.attr.toolchain_config[CcToolchainConfigInfo]
100+
all_files = _files(ctx, "all_files")
101+
102+
legacy_file_groups = {
103+
"as_files": _files(ctx, "as_files"),
104+
"ar_files": _files(ctx, "ar_files"),
105+
"dwp_files": _files(ctx, "dwp_files"),
106+
"compiler_files": _files(ctx, "compiler_files"),
107+
"strip_files": _files(ctx, "strip_files"),
108+
"objcopy_files": _files(ctx, "objcopy_files"),
109+
"coverage_files": _files(ctx, "coverage_files") or all_files,
110+
}
111+
112+
linker_files = _files(ctx, "linker_files")
113+
114+
else:
115+
if not ctx.attr.tool_map:
116+
fail("Must pass `tool_map` when not passing `toolchain_config`")
117+
toolchain_config_info, cc_toolchain_config_info = cc_toolchain_config_impl_helper(ctx)
118+
119+
legacy_action_set_lookup = {
120+
target.label: target
121+
for target in ctx.attr._legacy_action_sets
122+
}
123+
124+
legacy_file_groups = {}
125+
for group, actions in LEGACY_FILE_GROUPS.items():
126+
action_targets = [
127+
legacy_action_set_lookup[action]
128+
for action in actions
129+
]
130+
legacy_file_groups[group] = depset(transitive = [
131+
toolchain_config_info.files[action]
132+
for action in collect_action_types(action_targets).to_list()
133+
if action in toolchain_config_info.files
134+
])
135+
136+
all_files = depset(transitive = legacy_file_groups.values())
137+
legacy_file_groups["coverage_files"] = legacy_file_groups["coverage_files"] or all_files
138+
linker_files = legacy_file_groups.pop("linker_files")
139+
85140
return struct(
86141
supports_param_files = ctx.attr.supports_param_files,
87142
runtime_solib_dir_base = "_solib__" + cc_common.escape_label(label = ctx.label),
88-
cc_toolchain_config_info = _provider(ctx.attr.toolchain_config, CcToolchainConfigInfo),
143+
cc_toolchain_config_info = cc_toolchain_config_info,
89144
static_runtime_lib = ctx.attr.static_runtime_lib,
90145
dynamic_runtime_lib = ctx.attr.dynamic_runtime_lib,
91146
supports_header_parsing = ctx.attr.supports_header_parsing,
92147
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"),
96148
link_dynamic_library_tool = ctx.file._link_dynamic_library_tool,
97149
grep_includes = grep_includes,
98150
aggregate_ddi = _single_file(ctx, "_aggregate_ddi"),
99151
generate_modmap = _single_file(ctx, "_generate_modmap"),
100152
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"),
104153
module_map_artifact = _single_file(ctx, "module_map"),
105-
all_files_including_libc = depset(transitive = [_files(ctx, "all_files"), _files(ctx, latebound_libc)]),
154+
all_files_including_libc = depset(transitive = [all_files, _files(ctx, latebound_libc)]),
106155
zipper = ctx.file._zipper,
107156
linker_files = _full_inputs_for_link(
108157
ctx,
109-
_files(ctx, "linker_files"),
158+
linker_files,
110159
_files(ctx, latebound_libc),
111160
),
112161
cc_toolchain_label = ctx.label,
113-
coverage_files = _files(ctx, "coverage_files") or all_files,
114162
compiler_files_without_includes = _files(ctx, "compiler_files_without_includes"),
115163
libc = _files(ctx, latebound_libc),
116164
libc_top_label = _label(ctx, latebound_libc),
117165
if_so_builder = ctx.file._interface_library_builder,
118166
allowlist_for_layering_check = _package_specification_provider(ctx, "disabling_parse_headers_and_layering_check_allowed"),
119167
build_info_files = _provider(ctx.attr._build_info_translator, OutputGroupInfo),
168+
**legacy_file_groups
120169
)
121170

122171
def _cc_toolchain_impl(ctx):
@@ -200,7 +249,6 @@ crosstool_config.toolchain.
200249
),
201250
"all_files": attr.label(
202251
allow_files = True,
203-
mandatory = True,
204252
doc = """
205253
Collection of all cc_toolchain artifacts. These artifacts will be added as inputs to all
206254
rules_cc related actions (with the exception of actions that are using more precise sets of
@@ -214,7 +262,6 @@ rules using C++ toolchain.</p>""",
214262
),
215263
"compiler_files": attr.label(
216264
allow_files = True,
217-
mandatory = True,
218265
doc = """
219266
Collection of all cc_toolchain artifacts required for compile actions.""",
220267
),
@@ -226,13 +273,11 @@ input discovery is supported (currently Google-only).""",
226273
),
227274
"strip_files": attr.label(
228275
allow_files = True,
229-
mandatory = True,
230276
doc = """
231277
Collection of all cc_toolchain artifacts required for strip actions.""",
232278
),
233279
"objcopy_files": attr.label(
234280
allow_files = True,
235-
mandatory = True,
236281
doc = """
237282
Collection of all cc_toolchain artifacts required for objcopy actions.""",
238283
),
@@ -248,13 +293,11 @@ Collection of all cc_toolchain artifacts required for archiving actions.""",
248293
),
249294
"linker_files": attr.label(
250295
allow_files = True,
251-
mandatory = True,
252296
doc = """
253297
Collection of all cc_toolchain artifacts required for linking actions.""",
254298
),
255299
"dwp_files": attr.label(
256300
allow_files = True,
257-
mandatory = True,
258301
doc = """
259302
Collection of all cc_toolchain artifacts required for dwp actions.""",
260303
),
@@ -307,7 +350,6 @@ Set to True when cc_toolchain supports header parsing actions.""",
307350
),
308351
"toolchain_config": attr.label(
309352
allow_files = False,
310-
mandatory = True,
311353
providers = [CcToolchainConfigInfo],
312354
doc = """
313355
The label of the rule providing <code>cc_toolchain_config_info</code>.""",
@@ -340,5 +382,16 @@ The label of the rule providing <code>cc_toolchain_config_info</code>.""",
340382
default = semantics.BUILD_INFO_TRANLATOR_LABEL,
341383
providers = [OutputGroupInfo],
342384
),
385+
"_legacy_action_sets": attr.label_list(
386+
default = LEGACY_ACTION_SET_DEPS,
387+
providers = [ActionTypeSetInfo],
388+
),
389+
} | CC_TOOLCHAIN_CONFIG_PUBLIC_ATTRS | {
390+
# Override tool_map to make it optional and exec-configured.
391+
"tool_map": attr.label(
392+
cfg = "exec",
393+
providers = [ToolConfigInfo],
394+
),
395+
"_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features", providers = [FeatureSetInfo]),
343396
} | semantics.cpp_modules_tools(), # buildifier: disable=unsorted-dict-items
344397
)

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)