Skip to content

Commit d798741

Browse files
committed
remove tool constraints
resolves #74
1 parent 2f22aa8 commit d798741

4 files changed

Lines changed: 40 additions & 44 deletions

File tree

MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

toolchain/templates/toolchain.bazel

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
22
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
33
load("@toolchains_arm_gnu//toolchain:config.bzl", "cc_arm_gnu_toolchain_config")
4+
load("@toolchains_arm_gnu//toolchain:toolchain.bzl", "host_from")
45
load("@toolchains_arm_gnu//toolchain:transitions.bzl", "toolchain_transition_library")
56

67
HOSTS = %hosts%
78

8-
def _normalize(x):
9-
mapping = {
10-
"osx": "macos",
11-
"darwin": "macos",
12-
"aarch64": "arm64",
13-
}
14-
return mapping.get(x, default=x)
15-
16-
17-
def _host_os_cpu():
18-
host_constraints = HOST_CONSTRAINTS
19-
if "os" not in host_constraints:
20-
host_constraints = reversed(host_constraints)
21-
22-
return struct(
23-
os = _normalize(Label(host_constraints[0]).name),
24-
cpu = _normalize(Label(host_constraints[1]).name),
25-
)
26-
279
def _toolchain_variants(name, additional_link_libraries, target_compatible_with):
2810
"""
2911
List of toolchain variants to create.
@@ -118,11 +100,8 @@ def %toolchain_name%_toolchain(
118100
],
119101
)
120102

121-
host = _host_os_cpu()
122-
123103
for host_repo, exec_compatible_with in HOSTS.items():
124-
os, cpu = [Label(x).name for x in exec_compatible_with]
125-
if _normalize(os) == host.os and _normalize(cpu) == host.cpu:
104+
if host_repo == "%toolchain_name%_{}".format(host_from(HOST_CONSTRAINTS)):
126105
native.alias(
127106
name = name,
128107
actual = ":{}_{}".format(name, host_repo),

toolchain/templates/top.BUILD

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,30 @@ repository, i.e., the targets defined here appear in the workspace as
44
"@arm_none_eabi//:*" for arm-none-eabi toolchains.
55
"""
66

7-
load("@toolchains_arm_gnu//toolchain:toolchain.bzl", "hosts", "tools")
87
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
8+
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
9+
load(
10+
"@toolchains_arm_gnu//toolchain:toolchain.bzl",
11+
"host_from",
12+
"hosts",
13+
"tools",
14+
)
915

1016
package(default_visibility = ["//visibility:public"])
1117

12-
TOOLS = tools + ["bin"]
13-
14-
[
15-
config_setting(
16-
name = host,
17-
constraint_values = constraint_values,
18-
)
19-
for host, constraint_values in hosts["%toolchain_prefix%"].items()
20-
]
18+
TOOLS = tools
19+
host = host_from(HOST_CONSTRAINTS)
2120

2221
[
2322
native_binary(
2423
name = tool,
25-
src = select({
26-
host: "@%toolchain_name%_{}//:{}".format(host, tool)
27-
for host in hosts["%toolchain_prefix%"].keys()
28-
}),
24+
src = "@%toolchain_name%_{}//:{}".format(host, tool),
2925
out = tool,
30-
target_compatible_with = select({
31-
host: constraint_values
32-
for host, constraint_values in hosts["%toolchain_prefix%"].items()
33-
} | {
34-
"//conditions:default": ["@platforms//:incompatible"],
35-
}),
26+
target_compatible_with = (
27+
[]
28+
if host in hosts["%toolchain_prefix%"].keys() else
29+
["@platforms//:incompatible"]
30+
),
3631
)
3732
for tool in TOOLS
3833
]

toolchain/toolchain.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ hosts = {
7070
},
7171
}
7272

73+
def host_from(host_constraints):
74+
if "os" != Label(host_constraints[0]).package:
75+
host_constraints = reversed(host_constraints)
76+
77+
mapping = {
78+
"osx": "darwin",
79+
"macos": "darwin",
80+
"x86_64": "amd64",
81+
"aarch64": "arm64",
82+
}
83+
84+
normalize = lambda x: mapping.get(x, default = x)
85+
86+
os, cpu = [normalize(Label(x).name) for x in host_constraints]
87+
88+
if os == "darwin":
89+
return "darwin_x86_64" if cpu == "amd64" else "darwin_arm64"
90+
elif os == "linux":
91+
return "linux_x86_64" if cpu == "amd64" else "linux_aarch64"
92+
else:
93+
return "windows_x86_64"
94+
7395
def _arm_gnu_toolchain(
7496
name,
7597
toolchain = "",

0 commit comments

Comments
 (0)