Skip to content

Commit 5e65e32

Browse files
authored
Add extra_{exec,target}_compatible_with extensions (#240)
This adds `extra_exec_compatible_with` and `extra_target_compatible_with` as extensions so that users can control whether the toolchains generated by hermetic_cc_toolchain are selected. This is intended to fix #205
1 parent d76afac commit 5e65e32

File tree

6 files changed

+82
-12
lines changed

6 files changed

+82
-12
lines changed

toolchain/defs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def toolchains(
6666
host_platform_sha256 = HOST_PLATFORM_SHA256,
6767
host_platform_ext = _HOST_PLATFORM_EXT,
6868
exec_platforms = {},
69+
extra_exec_compatible_with = [],
70+
extra_target_compatible_with = [],
6971
extra_target_settings = []):
7072
"""
7173
Download zig toolchain and declare bazel toolchains.
@@ -86,6 +88,8 @@ def toolchains(
8688
zig_sdk_repository(
8789
name = "zig_sdk",
8890
exec_platforms = exec_platforms,
91+
extra_exec_compatible_with = extra_exec_compatible_with,
92+
extra_target_compatible_with = extra_target_compatible_with,
8993
extra_target_settings = extra_target_settings,
9094
)
9195

toolchain/ext.bzl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,27 @@ _extra_target_settings = tag_class(
2222
doc = "Each setting is added to every toolchain to make them more restrictive",
2323
)
2424

25+
_extra_target_compatible_with = tag_class(
26+
attrs = {
27+
"constraints": attr.label_list(),
28+
},
29+
doc = "Extra constraints added to every toolchain's `target_compatible_with`",
30+
)
31+
32+
_extra_exec_compatible_with = tag_class(
33+
attrs = {
34+
"constraints": attr.label_list(),
35+
},
36+
doc = "Extra constraints added to every toolchain's `exec_compatible_with`",
37+
)
38+
2539
def _toolchains_impl(mctx):
2640
exec_platforms = {}
2741
root_direct_deps = []
2842
root_direct_dev_deps = []
2943
is_non_dev_dependency = mctx.root_module_has_non_dev_dependency
44+
extra_exec_compatible_with = []
45+
extra_target_compatible_with = []
3046
extra_target_settings = []
3147

3248
for mod in mctx.modules:
@@ -40,7 +56,18 @@ def _toolchains_impl(mctx):
4056
for tag in mod.tags.extra_target_settings:
4157
extra_target_settings += tag.settings
4258

43-
repos = zig_toolchains(exec_platforms = exec_platforms, extra_target_settings = extra_target_settings)
59+
for tag in mod.tags.extra_exec_compatible_with:
60+
extra_exec_compatible_with += tag.constraints
61+
62+
for tag in mod.tags.extra_target_compatible_with:
63+
extra_target_compatible_with += tag.constraints
64+
65+
repos = zig_toolchains(
66+
exec_platforms = exec_platforms,
67+
extra_exec_compatible_with = extra_exec_compatible_with,
68+
extra_target_compatible_with = extra_target_compatible_with,
69+
extra_target_settings = extra_target_settings,
70+
)
4471

4572
root_direct_deps = list(repos.public) if is_non_dev_dependency else []
4673
root_direct_dev_deps = list(repos.public) if not is_non_dev_dependency else []
@@ -59,6 +86,8 @@ toolchains = module_extension(
5986
implementation = _toolchains_impl,
6087
tag_classes = {
6188
"exec_platform": _exec_platform,
89+
"extra_exec_compatible_with": _extra_exec_compatible_with,
90+
"extra_target_compatible_with": _extra_target_compatible_with,
6291
"extra_target_settings": _extra_target_settings,
6392
},
6493
)

toolchain/libc_aware/toolchain/BUILD.bazel.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ package(
77
default_visibility = ["//visibility:public"],
88
)
99

10-
declare_libc_aware_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})
10+
declare_libc_aware_toolchains(
11+
configs = {configs},
12+
extra_exec_compatible_with = {extra_exec_compatible_with},
13+
extra_target_compatible_with = {extra_target_compatible_with},
14+
extra_target_settings = {extra_target_settings},
15+
)

toolchain/private/repositories.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ load("@hermetic_cc_toolchain//toolchain:utils.bzl", "quote")
22
load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "transform_arch_name", "transform_os_name")
33

44
def _define_zig_toolchains(repository_ctx, configs, package = ""):
5+
extra_exec_compatible_with = "[" + " ".join([quote(str(setting)) + "," for setting in repository_ctx.attr.extra_exec_compatible_with]) + "]"
6+
extra_target_compatible_with = "[" + " ".join([quote(str(setting)) + "," for setting in repository_ctx.attr.extra_target_compatible_with]) + "]"
57
extra_target_settings = "[" + " ".join([quote(str(setting)) + "," for setting in repository_ctx.attr.extra_target_settings]) + "]"
68

79
repository_ctx.template(
@@ -10,6 +12,8 @@ def _define_zig_toolchains(repository_ctx, configs, package = ""):
1012
executable = False,
1113
substitutions = {
1214
"{configs}": repr(configs),
15+
"{extra_exec_compatible_with}": extra_exec_compatible_with,
16+
"{extra_target_compatible_with}": extra_target_compatible_with,
1317
"{extra_target_settings}": extra_target_settings,
1418
},
1519
)
@@ -20,6 +24,8 @@ def _define_zig_toolchains(repository_ctx, configs, package = ""):
2024
executable = False,
2125
substitutions = {
2226
"{configs}": repr(configs),
27+
"{extra_exec_compatible_with}": extra_exec_compatible_with,
28+
"{extra_target_compatible_with}": extra_target_compatible_with,
2329
"{extra_target_settings}": extra_target_settings,
2430
},
2531
)
@@ -96,6 +102,12 @@ zig_sdk_repository = repository_rule(
96102
doc = "Dictionary, where the keys are oses and the values are lists of supported architectures",
97103
mandatory = True,
98104
),
105+
"extra_exec_compatible_with": attr.label_list(
106+
doc = "Additional constraints added to the `exec_compatible_with` attribute of each generated toolchain",
107+
),
108+
"extra_target_compatible_with": attr.label_list(
109+
doc = "Additional constraints added to the `target_compatible_with` attribute of each generated toolchain",
110+
),
99111
"extra_target_settings": attr.label_list(
100112
doc = "Additional settings to add to the generated toolchains, to make them more restrictive",
101113
),

toolchain/toolchain/BUILD.bazel.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ package(
44
default_visibility = ["//visibility:public"],
55
)
66

7-
declare_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})
7+
declare_toolchains(
8+
configs = {configs},
9+
extra_exec_compatible_with = {extra_exec_compatible_with},
10+
extra_target_compatible_with = {extra_target_compatible_with},
11+
extra_target_settings = {extra_target_settings},
12+
)

toolchain/toolchain/defs.bzl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "target_structs")
22

3-
def declare_toolchains(configs = "", extra_target_settings = []):
3+
def declare_toolchains(
4+
configs = "",
5+
extra_exec_compatible_with = [],
6+
extra_target_compatible_with = [],
7+
extra_target_settings = []):
48
for target_config in target_structs():
59
gotarget = target_config.gotarget
610
zigtarget = target_config.zigtarget
@@ -12,11 +16,15 @@ def declare_toolchains(configs = "", extra_target_settings = []):
1216
if hasattr(target_config, "libc_constraint"):
1317
extra_constraints = ["@zig_sdk//libc:unconstrained"]
1418

15-
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs, extra_target_settings)
19+
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs, extra_exec_compatible_with, extra_target_compatible_with, extra_target_settings)
1620

1721
_declare_zig_toolchain("zig", configs)
1822

19-
def declare_libc_aware_toolchains(configs = "", extra_target_settings = []):
23+
def declare_libc_aware_toolchains(
24+
configs = "",
25+
extra_exec_compatible_with = [],
26+
extra_target_compatible_with = [],
27+
extra_target_settings = []):
2028
for target_config in target_structs():
2129
gotarget = target_config.gotarget
2230
zigtarget = target_config.zigtarget
@@ -25,15 +33,22 @@ def declare_libc_aware_toolchains(configs = "", extra_target_settings = []):
2533
# is only selected if libc is not expicitly set and another one that is
2634
# only selected if the specific libc variant is selected.
2735
if hasattr(target_config, "libc_constraint"):
28-
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs, extra_target_settings)
36+
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs, extra_exec_compatible_with, extra_target_compatible_with, extra_target_settings)
2937

30-
def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs, extra_target_settings):
38+
def _declare_toolchain(
39+
gotarget,
40+
zigtarget,
41+
target_compatible_with,
42+
configs,
43+
extra_exec_compatible_with,
44+
extra_target_compatible_with,
45+
extra_target_settings):
3146
# register two kinds of toolchain targets: Go and Zig conventions.
3247
# Go convention: amd64/arm64, linux/darwin
3348
native.toolchain(
3449
name = gotarget,
35-
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
36-
target_compatible_with = target_compatible_with,
50+
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)] + extra_exec_compatible_with,
51+
target_compatible_with = target_compatible_with + extra_target_compatible_with,
3752
target_settings = extra_target_settings,
3853
toolchain = "{}//:{}_cc".format(configs, zigtarget),
3954
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
@@ -42,8 +57,8 @@ def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs, ext
4257
# Zig convention: x86_64/aarch64, linux/macos
4358
native.toolchain(
4459
name = zigtarget,
45-
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
46-
target_compatible_with = target_compatible_with,
60+
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)] + extra_exec_compatible_with,
61+
target_compatible_with = target_compatible_with + extra_target_compatible_with,
4762
target_settings = extra_target_settings,
4863
toolchain = "{}//:{}_cc".format(configs, zigtarget),
4964
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",

0 commit comments

Comments
 (0)