Skip to content

Commit 659118c

Browse files
authored
Provide clang 20.1.7 toolchain [AP-4010] (#160)
Introduction of hermetic `clang20` toolchains
1 parent 182ac20 commit 659118c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+6522
-2
lines changed

cc/constraints/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ constraint_value(
1414
constraint_setting = ":toolchain",
1515
)
1616

17+
constraint_value(
18+
name = "llvm20_toolchain",
19+
constraint_setting = ":toolchain",
20+
)
21+
1722
constraint_value(
1823
name = "yocto_generic_toolchain",
1924
constraint_setting = ":toolchain",

cc/repositories.bzl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ AARCH64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llv
2020

2121
X86_64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
2222

23+
# LLVM 20.1.7 URLs
24+
AARCH64_DARWIN_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-macOS-ARM64.tar.xz"
25+
26+
X86_64_DARWIN_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-macOS-X64.tar.xz"
27+
28+
AARCH64_LINUX_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-Linux-ARM64.tar.xz"
29+
30+
X86_64_LINUX_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-Linux-X64.tar.xz"
31+
2332
X86_64_LINUX_UCRT_LLVM_MINGW = "https://github.com/mstorsjo/llvm-mingw/releases/download/20241203/llvm-mingw-20241203-ucrt-ubuntu-20.04-x86_64.tar.xz"
2433

2534
AARCH64_LINUX_MUSL = "https://github.com/swift-nav/swift-toolchains/releases/download/musl-cross-11.2.0/aarch64-linux-musl-cross.tar.gz"
@@ -85,6 +94,44 @@ def swift_cc_toolchain():
8594
sha256 = "61582215dafafb7b576ea30cc136be92c877ba1f1c31ddbbd372d6d65622fef5",
8695
)
8796

97+
def swift_cc_toolchain_llvm20():
98+
"""Define LLVM 20.1.7 toolchain repositories."""
99+
maybe(
100+
http_archive,
101+
name = "aarch64-darwin-llvm20",
102+
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
103+
url = AARCH64_DARWIN_LLVM20,
104+
strip_prefix = "LLVM-20.1.7-macOS-ARM64",
105+
# TODO: Add sha256 hash for integrity
106+
)
107+
108+
maybe(
109+
http_archive,
110+
name = "x86_64-darwin-llvm20",
111+
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
112+
url = X86_64_DARWIN_LLVM20,
113+
strip_prefix = "LLVM-20.1.7-macOS-X64",
114+
sha256 = "8494c98a774051a40bfe1187a2d6442f4bc107598998bbe1673d9bb1572cfd6f",
115+
)
116+
117+
maybe(
118+
http_archive,
119+
name = "aarch64-linux-llvm20",
120+
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
121+
url = AARCH64_LINUX_LLVM20,
122+
strip_prefix = "LLVM-20.1.7-Linux-ARM64",
123+
sha256 = "832f2802a29457dc758f56e26e98558c6cd0e45fcd07186f540cb6e7f4e59385",
124+
)
125+
126+
maybe(
127+
http_archive,
128+
name = "x86_64-linux-llvm20",
129+
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
130+
url = X86_64_LINUX_LLVM20,
131+
strip_prefix = "LLVM-20.1.7-Linux-X64",
132+
sha256 = "8494c98a774051a40bfe1187a2d6442f4bc107598998bbe1673d9bb1572cfd6f",
133+
)
134+
88135
def aarch64_sysroot():
89136
maybe(
90137
http_archive,
@@ -124,6 +171,13 @@ def register_swift_cc_toolchains():
124171
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton2")
125172
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton3")
126173

174+
def register_swift_cc_toolchains_llvm20():
175+
"""Register LLVM 20.1.7 toolchains."""
176+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/aarch64-darwin:cc-toolchain-aarch64-darwin")
177+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/x86_64-darwin:cc-toolchain-x86_64-darwin")
178+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/aarch64-linux:cc-toolchain-aarch64-linux")
179+
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/x86_64-linux:cc-toolchain-x86_64-linux")
180+
127181
def aarch64_linux_musl_toolchain():
128182
http_archive(
129183
name = "aarch64-linux-musl",

cc/toolchains/llvm/cc_toolchain_config.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def cc_toolchain_config(
3232
is_darwin = False,
3333
use_lld = True):
3434
if not is_target_triplet(host_system_name):
35-
fail(host_system_name + " is not a target tripplet")
35+
fail(host_system_name + " is not a target triplet")
3636

3737
if not is_target_triplet(target_system_name):
38-
fail(target_system_name + " is not a target tripplet")
38+
fail(target_system_name + " is not a target triplet")
3939

4040
is_cross_compile = host_system_name != target_system_name
4141

cc/toolchains/llvm20/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2025 Swift Navigation Inc.
2+
# Contact: Swift Navigation <[email protected]>
3+
#
4+
# This source is subject to the license found in the file 'LICENSE' which must
5+
# be be distributed together with this source. All other rights reserved.
6+
#
7+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10+
11+
package(default_visibility = ["//visibility:public"])
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright (C) 2025 Swift Navigation Inc.
2+
# Contact: Swift Navigation <[email protected]>
3+
#
4+
# This source is subject to the license found in the file 'LICENSE' which must
5+
# be be distributed together with this source. All other rights reserved.
6+
#
7+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
8+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
9+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
10+
11+
load("@rules_swiftnav//cc/toolchains/llvm20:cc_toolchain_config.bzl", "cc_toolchain_config")
12+
load("@rules_swiftnav//cc/toolchains/llvm20:target_triplets.bzl", "AARCH64_DARWIN")
13+
14+
package(default_visibility = ["//visibility:public"])
15+
16+
filegroup(
17+
name = "wrappers",
18+
srcs = glob([
19+
"wrappers/**",
20+
]),
21+
visibility = ["//visibility:public"],
22+
)
23+
24+
filegroup(
25+
name = "ar_files",
26+
srcs = [
27+
":wrappers",
28+
"@aarch64-darwin-llvm20//:ar",
29+
],
30+
)
31+
32+
filegroup(
33+
name = "as_files",
34+
srcs = [
35+
":wrappers",
36+
"@aarch64-darwin-llvm20//:as",
37+
],
38+
)
39+
40+
filegroup(
41+
name = "compiler_files",
42+
srcs = [
43+
":wrappers",
44+
"@aarch64-darwin-llvm20//:clang",
45+
"@aarch64-darwin-llvm20//:include",
46+
],
47+
)
48+
49+
filegroup(
50+
name = "dwp_files",
51+
srcs = [
52+
":wrappers",
53+
"@aarch64-darwin-llvm20//:dwp",
54+
],
55+
)
56+
57+
filegroup(
58+
name = "linker_files",
59+
srcs = [
60+
":wrappers",
61+
"@aarch64-darwin-llvm20//:ar",
62+
"@aarch64-darwin-llvm20//:clang",
63+
"@aarch64-darwin-llvm20//:lib",
64+
],
65+
)
66+
67+
filegroup(
68+
name = "objcopy_files",
69+
srcs = [
70+
":wrappers",
71+
"@aarch64-darwin-llvm20//:objcopy",
72+
],
73+
)
74+
75+
filegroup(
76+
name = "strip_files",
77+
srcs = [
78+
":wrappers",
79+
"@aarch64-darwin-llvm20//:strip",
80+
],
81+
)
82+
83+
filegroup(
84+
name = "all_files",
85+
srcs = [
86+
"linker_files",
87+
":compiler_files",
88+
"@aarch64-darwin-llvm20//:bin",
89+
],
90+
)
91+
92+
cc_toolchain_config(
93+
name = "local-aarch64-darwin",
94+
abi_libc_version = "darwin_aarch64",
95+
abi_version = "darwin_aarch64",
96+
builtin_sysroot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
97+
compiler = "clang",
98+
cxx_builtin_include_directories = [
99+
"%sysroot%/usr/include",
100+
"%sysroot%/System/Library/Frameworks",
101+
],
102+
host_system_name = AARCH64_DARWIN,
103+
is_darwin = True,
104+
target_cpu = "darwin",
105+
target_libc = "macosx",
106+
target_system_name = AARCH64_DARWIN,
107+
tool_paths = {
108+
"ar": "/usr/bin/libtool",
109+
"cpp": "wrappers/clang-cpp",
110+
"gcc": "wrappers/clang",
111+
"gcov": "wrappers/llvm-profdata",
112+
"llvm-cov": "wrappers/llvm-cov",
113+
"llvm-profdata": "wrappers/llvm-profdata",
114+
"ld": "/usr/bin/ld",
115+
"nm": "wrappers/llvm-nm",
116+
"objcopy": "wrappers/llvm-objcopy",
117+
"objdump": "wrappers/llvm-objdump",
118+
"strip": "wrappers/llvm-strip",
119+
},
120+
toolchain_identifier = "clang-aarch64-darwin",
121+
toolchain_path_prefix = "external/aarch64-darwin-llvm20",
122+
use_lld = False,
123+
)
124+
125+
cc_toolchain(
126+
name = "cc-clang-aarch64-darwin",
127+
all_files = ":all_files",
128+
ar_files = ":ar_files",
129+
as_files = ":as_files",
130+
compiler_files = ":compiler_files",
131+
dwp_files = ":dwp_files",
132+
linker_files = ":linker_files",
133+
objcopy_files = ":objcopy_files",
134+
strip_files = ":strip_files",
135+
toolchain_config = ":local-aarch64-darwin",
136+
)
137+
138+
toolchain(
139+
name = "cc-toolchain-aarch64-darwin",
140+
exec_compatible_with = [
141+
"@platforms//cpu:aarch64",
142+
"@platforms//os:macos",
143+
],
144+
target_compatible_with = [
145+
"@platforms//cpu:aarch64",
146+
"@platforms//os:macos",
147+
"@rules_swiftnav//cc/constraints:llvm20_toolchain",
148+
],
149+
target_settings = None,
150+
toolchain = ":cc-clang-aarch64-darwin",
151+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
152+
)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (C) 2025 Swift Navigation Inc.
4+
# Contact: Swift Navigation <[email protected]>
5+
#
6+
# This source is subject to the license found in the file 'LICENSE' which must
7+
# be be distributed together with this source. All other rights reserved.
8+
#
9+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
11+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12+
13+
# Locates the actual tool paths relative to the location this script is
14+
# executed from.
15+
#
16+
# This is necessary because we download the toolchain using
17+
# http_archive, which bazel places in _execroot_/external/_repo_name_.
18+
#
19+
# Unfortunately we cannot provide this location as the argument of tool_path
20+
# when configuring the toolchain, because tool_path only takes a relative path.
21+
#
22+
# This is the fairly common workaround to handle this.
23+
#
24+
# TODO: [BUILD-549] - Remove need for wrapper files
25+
#
26+
# Recent versions of Bazel may have introduced a mechanism that removes
27+
# the need for this workaround: https://github.com/bazelbuild/bazel/issues/7746
28+
29+
set -e
30+
31+
# Use --actionv_env=SWIFTNAV_VERBOSE_TOOLCHAIN to enable
32+
if [[ -n "$SWIFTNAV_VERBOSE_TOOLCHAIN" ]]; then
33+
set -x
34+
fi
35+
36+
tool_name=$(basename "${BASH_SOURCE[0]}")
37+
# In case the tool label is changed, a change in here is very likely needed
38+
# This establishes backwards compatibility with the old WORKSPACE file
39+
toolchain_bindir=external/aarch64-darwin-llvm20/bin
40+
toolchain_bindir_as_bzlmod=external/rules_swiftnav~~swift_cc_toolchain_extension~aarch64-darwin-llvm20/bin
41+
42+
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
43+
# We're running under _execroot_, call the real tool.
44+
exec "${toolchain_bindir}"/"${tool_name}" "$@"
45+
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
46+
# We're running under _execroot_, call the real tool.
47+
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
48+
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
49+
# This branch exists because some users of the toolchain,
50+
# namely rules_foreign_cc, will change CWD and call $CC (this script)
51+
# with its absolute path.
52+
#
53+
# To deal with this we find the tool relative to this script, which is at
54+
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
55+
#
56+
# If the wrapper is relocated then this line needs to be adjusted.
57+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
58+
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59+
exec "${tool}" "${@}"
60+
else
61+
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
62+
exit 5
63+
fi
64+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (C) 2025 Swift Navigation Inc.
4+
# Contact: Swift Navigation <[email protected]>
5+
#
6+
# This source is subject to the license found in the file 'LICENSE' which must
7+
# be be distributed together with this source. All other rights reserved.
8+
#
9+
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10+
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
11+
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12+
13+
# Locates the actual tool paths relative to the location this script is
14+
# executed from.
15+
#
16+
# This is necessary because we download the toolchain using
17+
# http_archive, which bazel places in _execroot_/external/_repo_name_.
18+
#
19+
# Unfortunately we cannot provide this location as the argument of tool_path
20+
# when configuring the toolchain, because tool_path only takes a relative path.
21+
#
22+
# This is the fairly common workaround to handle this.
23+
#
24+
# TODO: [BUILD-549] - Remove need for wrapper files
25+
#
26+
# Recent versions of Bazel may have introduced a mechanism that removes
27+
# the need for this workaround: https://github.com/bazelbuild/bazel/issues/7746
28+
29+
set -e
30+
31+
# Use --actionv_env=SWIFTNAV_VERBOSE_TOOLCHAIN to enable
32+
if [[ -n "$SWIFTNAV_VERBOSE_TOOLCHAIN" ]]; then
33+
set -x
34+
fi
35+
36+
tool_name=$(basename "${BASH_SOURCE[0]}")
37+
# In case the tool label is changed, a change in here is very likely needed
38+
# This establishes backwards compatibility with the old WORKSPACE file
39+
toolchain_bindir=external/aarch64-darwin-llvm20/bin
40+
toolchain_bindir_as_bzlmod=external/rules_swiftnav~~swift_cc_toolchain_extension~aarch64-darwin-llvm20/bin
41+
42+
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
43+
# We're running under _execroot_, call the real tool.
44+
exec "${toolchain_bindir}"/"${tool_name}" "$@"
45+
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
46+
# We're running under _execroot_, call the real tool.
47+
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
48+
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
49+
# This branch exists because some users of the toolchain,
50+
# namely rules_foreign_cc, will change CWD and call $CC (this script)
51+
# with its absolute path.
52+
#
53+
# To deal with this we find the tool relative to this script, which is at
54+
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
55+
#
56+
# If the wrapper is relocated then this line needs to be adjusted.
57+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
58+
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59+
exec "${tool}" "${@}"
60+
else
61+
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
62+
exit 5
63+
fi
64+

0 commit comments

Comments
 (0)