Skip to content

Commit 5020492

Browse files
authored
Add path to add extra settings to toolchains (#213)
This will allow toolchains to be configurably more restrictive, e.g. by requiring a setting to be enabled in order to select the hermetic toolchains. This could be a solution to #205.
1 parent e906f27 commit 5020492

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

toolchain/defs.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
22
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_user_netrc", "use_netrc")
33
load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "target_structs", "transform_os_name", "zig_tool_path")
44
load("@hermetic_cc_toolchain//toolchain/private:repositories.bzl", "zig_sdk_repository")
5+
load("@hermetic_cc_toolchain//toolchain:utils.bzl", "quote")
56
load(
67
"@hermetic_cc_toolchain//toolchain/private:zig_sdk.bzl",
78
"HOST_PLATFORM_SHA256",
@@ -64,7 +65,8 @@ def toolchains(
6465
url_formats = [],
6566
host_platform_sha256 = HOST_PLATFORM_SHA256,
6667
host_platform_ext = _HOST_PLATFORM_EXT,
67-
exec_platforms = {}):
68+
exec_platforms = {},
69+
extra_target_settings = []):
6870
"""
6971
Download zig toolchain and declare bazel toolchains.
7072
The platforms are not registered automatically, that should be done by
@@ -84,6 +86,7 @@ def toolchains(
8486
zig_sdk_repository(
8587
name = "zig_sdk",
8688
exec_platforms = exec_platforms,
89+
extra_target_settings = extra_target_settings,
8790
)
8891

8992
# create configs for the HOST
@@ -115,9 +118,6 @@ def toolchains(
115118
private = private_repos,
116119
)
117120

118-
def _quote(s):
119-
return "'" + s.replace("'", "'\\''") + "'"
120-
121121
def _zig_repository_impl(repository_ctx):
122122
exec_os = repository_ctx.attr.exec_os
123123
exec_arch = repository_ctx.attr.exec_arch
@@ -166,8 +166,8 @@ def _zig_repository_impl(repository_ctx):
166166
Label(src),
167167
executable = False,
168168
substitutions = {
169-
"{zig_sdk_path}": _quote("external/zig_sdk"),
170-
"{os}": _quote(exec_os),
169+
"{zig_sdk_path}": quote("external/zig_sdk"),
170+
"{os}": quote(exec_os),
171171
"{exec_os}": exec_os,
172172
"{exec_cpu}": exec_arch,
173173
},

toolchain/ext.bzl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ _exec_platform = tag_class(
1515
doc = "Zig execution platform tuple",
1616
)
1717

18+
_extra_target_settings = tag_class(
19+
attrs = {
20+
"settings": attr.label_list(),
21+
},
22+
doc = "Each setting is added to every toolchain to make them more restrictive",
23+
)
24+
1825
def _toolchains_impl(mctx):
1926
exec_platforms = {}
2027
root_direct_deps = []
2128
root_direct_dev_deps = []
2229
is_non_dev_dependency = mctx.root_module_has_non_dev_dependency
30+
extra_target_settings = []
2331

2432
for mod in mctx.modules:
2533
if mod.is_root:
@@ -29,7 +37,10 @@ def _toolchains_impl(mctx):
2937
_archs.append(ep.arch)
3038
exec_platforms[ep.os] = _archs
3139

32-
repos = zig_toolchains(exec_platforms = exec_platforms)
40+
for tag in mod.tags.extra_target_settings:
41+
extra_target_settings += tag.settings
42+
43+
repos = zig_toolchains(exec_platforms = exec_platforms, extra_target_settings = extra_target_settings)
3344

3445
root_direct_deps = list(repos.public) if is_non_dev_dependency else []
3546
root_direct_dev_deps = list(repos.public) if not is_non_dev_dependency else []
@@ -46,5 +57,8 @@ def _toolchains_impl(mctx):
4657

4758
toolchains = module_extension(
4859
implementation = _toolchains_impl,
49-
tag_classes = {"exec_platform": _exec_platform},
60+
tag_classes = {
61+
"exec_platform": _exec_platform,
62+
"extra_target_settings": _extra_target_settings,
63+
},
5064
)

toolchain/libc_aware/toolchain/BUILD.bazel.tmpl

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

10-
declare_libc_aware_toolchains(configs = {configs})
10+
declare_libc_aware_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})

toolchain/private/repositories.bzl

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

34
def _define_zig_toolchains(repository_ctx, configs, package = ""):
5+
extra_target_settings = "[" + " ".join([quote(str(setting)) + "," for setting in repository_ctx.attr.extra_target_settings]) + "]"
6+
47
repository_ctx.template(
58
"toolchain/{}BUILD".format(package),
69
Label("//toolchain/toolchain:BUILD.bazel.tmpl"),
710
executable = False,
811
substitutions = {
912
"{configs}": repr(configs),
13+
"{extra_target_settings}": extra_target_settings,
1014
},
1115
)
1216

@@ -16,6 +20,7 @@ def _define_zig_toolchains(repository_ctx, configs, package = ""):
1620
executable = False,
1721
substitutions = {
1822
"{configs}": repr(configs),
23+
"{extra_target_settings}": extra_target_settings,
1924
},
2025
)
2126

@@ -91,6 +96,9 @@ zig_sdk_repository = repository_rule(
9196
doc = "Dictionary, where the keys are oses and the values are lists of supported architectures",
9297
mandatory = True,
9398
),
99+
"extra_target_settings": attr.label_list(
100+
doc = "Additional settings to add to the generated toolchains, to make them more restrictive",
101+
),
94102
},
95103
implementation = _zig_sdk_repository_impl,
96104
)

toolchain/toolchain/BUILD.bazel.tmpl

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

7-
declare_toolchains(configs = {configs})
7+
declare_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})

toolchain/toolchain/defs.bzl

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

3-
def declare_toolchains(configs = ""):
3+
def declare_toolchains(configs = "", extra_target_settings = []):
44
for target_config in target_structs():
55
gotarget = target_config.gotarget
66
zigtarget = target_config.zigtarget
@@ -12,11 +12,11 @@ def declare_toolchains(configs = ""):
1212
if hasattr(target_config, "libc_constraint"):
1313
extra_constraints = ["@zig_sdk//libc:unconstrained"]
1414

15-
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs)
15+
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs, extra_target_settings)
1616

1717
_declare_zig_toolchain("zig", configs)
1818

19-
def declare_libc_aware_toolchains(configs = ""):
19+
def declare_libc_aware_toolchains(configs = "", extra_target_settings = []):
2020
for target_config in target_structs():
2121
gotarget = target_config.gotarget
2222
zigtarget = target_config.zigtarget
@@ -25,15 +25,16 @@ def declare_libc_aware_toolchains(configs = ""):
2525
# is only selected if libc is not expicitly set and another one that is
2626
# only selected if the specific libc variant is selected.
2727
if hasattr(target_config, "libc_constraint"):
28-
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs)
28+
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs, extra_target_settings)
2929

30-
def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs):
30+
def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs, extra_target_settings):
3131
# register two kinds of toolchain targets: Go and Zig conventions.
3232
# Go convention: amd64/arm64, linux/darwin
3333
native.toolchain(
3434
name = gotarget,
3535
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
3636
target_compatible_with = target_compatible_with,
37+
target_settings = extra_target_settings,
3738
toolchain = "{}//:{}_cc".format(configs, zigtarget),
3839
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
3940
)
@@ -43,6 +44,7 @@ def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs):
4344
name = zigtarget,
4445
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
4546
target_compatible_with = target_compatible_with,
47+
target_settings = extra_target_settings,
4648
toolchain = "{}//:{}_cc".format(configs, zigtarget),
4749
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
4850
)

toolchain/utils.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def quote(s):
2+
return "'" + s.replace("'", "'\\''") + "'"

0 commit comments

Comments
 (0)