Skip to content

Commit 2707b82

Browse files
jkurland-rokufmeum
authored andcommitted
Allow user provided platform constraints
1 parent 1d9ac58 commit 2707b82

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

toolchain/extensions/llvm.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ def _root_dict(roots, cls, name, strip_target):
3434

3535
return res
3636

37+
def _constraint_dict(tags, name):
38+
constraints = {}
39+
40+
# Gather all the additional constraints for each target
41+
for tag in tags:
42+
targets = list(tag.targets)
43+
if not targets:
44+
targets = [""]
45+
for target in targets:
46+
constraints_for_target = constraints.setdefault(target, [])
47+
constraints_for_target.extend([str(c) for c in tag.constraints])
48+
49+
return constraints
50+
3751
def _llvm_impl_(module_ctx):
3852
for mod in module_ctx.modules:
3953
if not mod.is_root:
@@ -49,6 +63,14 @@ def _llvm_impl_(module_ctx):
4963
}
5064
attrs["toolchain_roots"] = _root_dict([root for root in mod.tags.toolchain_root if root.name == name], "toolchain_root", name, True)
5165
attrs["sysroot"] = _root_dict([sysroot for sysroot in mod.tags.sysroot if sysroot.name == name], "sysroot", name, False)
66+
attrs["extra_exec_compatible_with"] = _constraint_dict(
67+
[tag for tag in mod.tags.extra_exec_compatible_with if tag.name == name],
68+
name,
69+
)
70+
attrs["extra_target_compatible_with"] = _constraint_dict(
71+
[tag for tag in mod.tags.extra_target_compatible_with if tag.name == name],
72+
name,
73+
)
5274

5375
llvm_toolchain(
5476
**attrs
@@ -95,5 +117,19 @@ llvm = module_extension(
95117
"path": attr.string(doc = "Absolute path to the sysroot."),
96118
},
97119
),
120+
"extra_exec_compatible_with": tag_class(
121+
attrs = {
122+
"name": attr.string(doc = "Same name as the toolchain tag.", default = "llvm_toolchain"),
123+
"targets": attr.string_list(doc = "Specific targets, if any; empty list means this applies to all."),
124+
"constraints": attr.label_list(doc = "List of extra constraints to add to exec_compatible_with for the generated toolchains."),
125+
},
126+
),
127+
"extra_target_compatible_with": tag_class(
128+
attrs = {
129+
"name": attr.string(doc = "Same name as the toolchain tag.", default = "llvm_toolchain"),
130+
"targets": attr.string_list(doc = "Specific targets, if any; empty list means this applies to all."),
131+
"constraints": attr.label_list(doc = "List of extra constraints to add to target_compatible_with for the generated toolchains."),
132+
},
133+
),
98134
},
99135
)

toolchain/internal/configure.bzl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def _join(path1, path2):
5959
def llvm_config_impl(rctx):
6060
_check_os_arch_keys(rctx.attr.sysroot)
6161
_check_os_arch_keys(rctx.attr.cxx_builtin_include_directories)
62+
_check_os_arch_keys(rctx.attr.extra_exec_compatible_with)
63+
_check_os_arch_keys(rctx.attr.extra_target_compatible_with)
6264

6365
os = _os(rctx)
6466
if os == "windows":
@@ -166,6 +168,8 @@ def llvm_config_impl(rctx):
166168
unfiltered_compile_flags_dict = rctx.attr.unfiltered_compile_flags,
167169
llvm_version = llvm_version,
168170
extra_compiler_files = rctx.attr.extra_compiler_files,
171+
extra_exec_compatible_with = rctx.attr.extra_exec_compatible_with,
172+
extra_target_compatible_with = rctx.attr.extra_target_compatible_with,
169173
)
170174
exec_dl_ext = "dylib" if os == "darwin" else "so"
171175
cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str(
@@ -380,11 +384,11 @@ toolchain(
380384
exec_compatible_with = [
381385
"@platforms//cpu:{exec_arch}",
382386
"@platforms//os:{exec_os_bzl}",
383-
],
387+
] + {extra_exec_compatible_with_specific} + {extra_exec_compatible_with_all_targets},
384388
target_compatible_with = [
385389
"@platforms//cpu:{target_arch}",
386390
"@platforms//os:{target_os_bzl}",
387-
],
391+
] + {extra_target_compatible_with_specific} + {extra_target_compatible_with_all_targets},
388392
target_settings = {target_settings},
389393
toolchain = ":cc-clang-{suffix}",
390394
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
@@ -543,6 +547,10 @@ cc_toolchain(
543547
]),
544548
extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "",
545549
major_llvm_version = major_llvm_version,
550+
extra_exec_compatible_with_specific = toolchain_info.extra_exec_compatible_with.get(target_pair, []),
551+
extra_target_compatible_with_specific = toolchain_info.extra_target_compatible_with.get(target_pair, []),
552+
extra_exec_compatible_with_all_targets = toolchain_info.extra_exec_compatible_with.get("", []),
553+
extra_target_compatible_with_all_targets = toolchain_info.extra_target_compatible_with.get("", []),
546554
)
547555

548556
def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, exec_dl_ext):

toolchain/internal/repo.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ llvm_config_attrs.update({
271271
default = False,
272272
doc = "Use absolute paths in the toolchain. Avoids sandbox overhead.",
273273
),
274+
"extra_exec_compatible_with": attr.string_list_dict(
275+
mandatory = False,
276+
doc = "Extra constraints to be added to exec_compatible_with for each target",
277+
),
278+
"extra_target_compatible_with": attr.string_list_dict(
279+
mandatory = False,
280+
doc = "Extra constraints to be added to target_compatible_with for each target",
281+
),
274282
"_cc_toolchain_config_bzl": attr.label(
275283
default = "//toolchain:cc_toolchain_config.bzl",
276284
),

0 commit comments

Comments
 (0)