Skip to content
Open
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
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 2 additions & 23 deletions toolchain/templates/toolchain.bazel
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
load("@toolchains_arm_gnu//toolchain:config.bzl", "cc_arm_gnu_toolchain_config")
load("@toolchains_arm_gnu//toolchain:toolchain.bzl", "host_from")
load("@toolchains_arm_gnu//toolchain:transitions.bzl", "toolchain_transition_library")

HOSTS = %hosts%

def _normalize(x):
mapping = {
"osx": "macos",
"darwin": "macos",
"aarch64": "arm64",
}
return mapping.get(x, default=x)


def _host_os_cpu():
host_constraints = HOST_CONSTRAINTS
if "os" not in host_constraints:
host_constraints = reversed(host_constraints)

return struct(
os = _normalize(Label(host_constraints[0]).name),
cpu = _normalize(Label(host_constraints[1]).name),
)

def _toolchain_variants(name, additional_link_libraries, target_compatible_with):
"""
List of toolchain variants to create.
Expand Down Expand Up @@ -118,11 +100,8 @@ def %toolchain_name%_toolchain(
],
)

host = _host_os_cpu()

for host_repo, exec_compatible_with in HOSTS.items():
os, cpu = [Label(x).name for x in exec_compatible_with]
if _normalize(os) == host.os and _normalize(cpu) == host.cpu:
if host_repo == "%toolchain_name%_{}".format(host_from(HOST_CONSTRAINTS)):
native.alias(
name = name,
actual = ":{}_{}".format(name, host_repo),
Expand Down
35 changes: 15 additions & 20 deletions toolchain/templates/top.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@ repository, i.e., the targets defined here appear in the workspace as
"@arm_none_eabi//:*" for arm-none-eabi toolchains.
"""

load("@toolchains_arm_gnu//toolchain:toolchain.bzl", "hosts", "tools")
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
load(
"@toolchains_arm_gnu//toolchain:toolchain.bzl",
"host_from",
"hosts",
"tools",
)

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

TOOLS = tools + ["bin"]

[
config_setting(
name = host,
constraint_values = constraint_values,
)
for host, constraint_values in hosts["%toolchain_prefix%"].items()
]
TOOLS = tools
host = host_from(HOST_CONSTRAINTS)

[
native_binary(
name = tool,
src = select({
host: "@%toolchain_name%_{}//:{}".format(host, tool)
for host in hosts["%toolchain_prefix%"].keys()
}),
src = "@%toolchain_name%_{}//:{}".format(host, tool),
out = tool,
target_compatible_with = select({
host: constraint_values
for host, constraint_values in hosts["%toolchain_prefix%"].items()
} | {
"//conditions:default": ["@platforms//:incompatible"],
}),
target_compatible_with = (
[]
if host in hosts["%toolchain_prefix%"].keys() else
["@platforms//:incompatible"]
),
)
for tool in TOOLS
]
22 changes: 22 additions & 0 deletions toolchain/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ hosts = {
},
}

def host_from(host_constraints):
if "os" != Label(host_constraints[0]).package:
host_constraints = reversed(host_constraints)

mapping = {
"osx": "darwin",
"macos": "darwin",
"x86_64": "amd64",
"aarch64": "arm64",
}

normalize = lambda x: mapping.get(x, default = x)

os, cpu = [normalize(Label(x).name) for x in host_constraints]

if os == "darwin":
return "darwin_x86_64" if cpu == "amd64" else "darwin_arm64"
elif os == "linux":
return "linux_x86_64" if cpu == "amd64" else "linux_aarch64"
else:
return "windows_x86_64"

def _arm_gnu_toolchain(
name,
toolchain = "",
Expand Down