Skip to content
Merged
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
4 changes: 4 additions & 0 deletions toolchain/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def toolchains(
host_platform_sha256 = HOST_PLATFORM_SHA256,
host_platform_ext = _HOST_PLATFORM_EXT,
exec_platforms = {},
extra_exec_compatible_with = [],
extra_target_compatible_with = [],
extra_target_settings = []):
"""
Download zig toolchain and declare bazel toolchains.
Expand All @@ -86,6 +88,8 @@ def toolchains(
zig_sdk_repository(
name = "zig_sdk",
exec_platforms = exec_platforms,
extra_exec_compatible_with = extra_exec_compatible_with,
extra_target_compatible_with = extra_target_compatible_with,
extra_target_settings = extra_target_settings,
)

Expand Down
31 changes: 30 additions & 1 deletion toolchain/ext.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@ _extra_target_settings = tag_class(
doc = "Each setting is added to every toolchain to make them more restrictive",
)

_extra_target_compatible_with = tag_class(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with these two new tag classes, do you think we still need _extra_target_settings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly either way, for the project that motivated me to send this PR I definitely won't need it now that I can specify with target_compatible_with, but I could imagine situations where it might be convenient to control which toolchain is used with settings rather than constraints.

attrs = {
"constraints": attr.label_list(),
},
doc = "Extra constraints added to every toolchain's `target_compatible_with`",
)

_extra_exec_compatible_with = tag_class(
attrs = {
"constraints": attr.label_list(),
},
doc = "Extra constraints added to every toolchain's `exec_compatible_with`",
)

def _toolchains_impl(mctx):
exec_platforms = {}
root_direct_deps = []
root_direct_dev_deps = []
is_non_dev_dependency = mctx.root_module_has_non_dev_dependency
extra_exec_compatible_with = []
extra_target_compatible_with = []
extra_target_settings = []

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

repos = zig_toolchains(exec_platforms = exec_platforms, extra_target_settings = extra_target_settings)
for tag in mod.tags.extra_exec_compatible_with:
extra_exec_compatible_with += tag.constraints

for tag in mod.tags.extra_target_compatible_with:
extra_target_compatible_with += tag.constraints

repos = zig_toolchains(
exec_platforms = exec_platforms,
extra_exec_compatible_with = extra_exec_compatible_with,
extra_target_compatible_with = extra_target_compatible_with,
extra_target_settings = extra_target_settings,
)

root_direct_deps = list(repos.public) if is_non_dev_dependency else []
root_direct_dev_deps = list(repos.public) if not is_non_dev_dependency else []
Expand All @@ -59,6 +86,8 @@ toolchains = module_extension(
implementation = _toolchains_impl,
tag_classes = {
"exec_platform": _exec_platform,
"extra_exec_compatible_with": _extra_exec_compatible_with,
"extra_target_compatible_with": _extra_target_compatible_with,
"extra_target_settings": _extra_target_settings,
},
)
7 changes: 6 additions & 1 deletion toolchain/libc_aware/toolchain/BUILD.bazel.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ package(
default_visibility = ["//visibility:public"],
)

declare_libc_aware_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})
declare_libc_aware_toolchains(
configs = {configs},
extra_exec_compatible_with = {extra_exec_compatible_with},
extra_target_compatible_with = {extra_target_compatible_with},
extra_target_settings = {extra_target_settings},
)
12 changes: 12 additions & 0 deletions toolchain/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ load("@hermetic_cc_toolchain//toolchain:utils.bzl", "quote")
load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "transform_arch_name", "transform_os_name")

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

repository_ctx.template(
Expand All @@ -10,6 +12,8 @@ def _define_zig_toolchains(repository_ctx, configs, package = ""):
executable = False,
substitutions = {
"{configs}": repr(configs),
"{extra_exec_compatible_with}": extra_exec_compatible_with,
"{extra_target_compatible_with}": extra_target_compatible_with,
"{extra_target_settings}": extra_target_settings,
},
)
Expand All @@ -20,6 +24,8 @@ def _define_zig_toolchains(repository_ctx, configs, package = ""):
executable = False,
substitutions = {
"{configs}": repr(configs),
"{extra_exec_compatible_with}": extra_exec_compatible_with,
"{extra_target_compatible_with}": extra_target_compatible_with,
"{extra_target_settings}": extra_target_settings,
},
)
Expand Down Expand Up @@ -96,6 +102,12 @@ zig_sdk_repository = repository_rule(
doc = "Dictionary, where the keys are oses and the values are lists of supported architectures",
mandatory = True,
),
"extra_exec_compatible_with": attr.label_list(
doc = "Additional constraints added to the `exec_compatible_with` attribute of each generated toolchain",
),
"extra_target_compatible_with": attr.label_list(
doc = "Additional constraints added to the `target_compatible_with` attribute of each generated toolchain",
),
"extra_target_settings": attr.label_list(
doc = "Additional settings to add to the generated toolchains, to make them more restrictive",
),
Expand Down
7 changes: 6 additions & 1 deletion toolchain/toolchain/BUILD.bazel.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ package(
default_visibility = ["//visibility:public"],
)

declare_toolchains(configs = {configs}, extra_target_settings = {extra_target_settings})
declare_toolchains(
configs = {configs},
extra_exec_compatible_with = {extra_exec_compatible_with},
extra_target_compatible_with = {extra_target_compatible_with},
extra_target_settings = {extra_target_settings},
)
33 changes: 24 additions & 9 deletions toolchain/toolchain/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "target_structs")

def declare_toolchains(configs = "", extra_target_settings = []):
def declare_toolchains(
configs = "",
extra_exec_compatible_with = [],
extra_target_compatible_with = [],
extra_target_settings = []):
for target_config in target_structs():
gotarget = target_config.gotarget
zigtarget = target_config.zigtarget
Expand All @@ -12,11 +16,15 @@ def declare_toolchains(configs = "", extra_target_settings = []):
if hasattr(target_config, "libc_constraint"):
extra_constraints = ["@zig_sdk//libc:unconstrained"]

_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs, extra_target_settings)
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints, configs, extra_exec_compatible_with, extra_target_compatible_with, extra_target_settings)

_declare_zig_toolchain("zig", configs)

def declare_libc_aware_toolchains(configs = "", extra_target_settings = []):
def declare_libc_aware_toolchains(
configs = "",
extra_exec_compatible_with = [],
extra_target_compatible_with = [],
extra_target_settings = []):
for target_config in target_structs():
gotarget = target_config.gotarget
zigtarget = target_config.zigtarget
Expand All @@ -25,15 +33,22 @@ def declare_libc_aware_toolchains(configs = "", extra_target_settings = []):
# is only selected if libc is not expicitly set and another one that is
# only selected if the specific libc variant is selected.
if hasattr(target_config, "libc_constraint"):
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs, extra_target_settings)
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + [target_config.libc_constraint], configs, extra_exec_compatible_with, extra_target_compatible_with, extra_target_settings)

def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs, extra_target_settings):
def _declare_toolchain(
gotarget,
zigtarget,
target_compatible_with,
configs,
extra_exec_compatible_with,
extra_target_compatible_with,
extra_target_settings):
# register two kinds of toolchain targets: Go and Zig conventions.
# Go convention: amd64/arm64, linux/darwin
native.toolchain(
name = gotarget,
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
target_compatible_with = target_compatible_with,
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)] + extra_exec_compatible_with,
target_compatible_with = target_compatible_with + extra_target_compatible_with,
target_settings = extra_target_settings,
toolchain = "{}//:{}_cc".format(configs, zigtarget),
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
Expand All @@ -42,8 +57,8 @@ def _declare_toolchain(gotarget, zigtarget, target_compatible_with, configs, ext
# Zig convention: x86_64/aarch64, linux/macos
native.toolchain(
name = zigtarget,
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)],
target_compatible_with = target_compatible_with,
exec_compatible_with = ["{}//:exec_os".format(configs), "{}//:exec_cpu".format(configs)] + extra_exec_compatible_with,
target_compatible_with = target_compatible_with + extra_target_compatible_with,
target_settings = extra_target_settings,
toolchain = "{}//:{}_cc".format(configs, zigtarget),
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
Expand Down