Skip to content

Provide clang 20.1.7 toolchain [AP-4010] #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions cc/constraints/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ constraint_value(
constraint_setting = ":toolchain",
)

constraint_value(
name = "llvm20_toolchain",
constraint_setting = ":toolchain",
)

constraint_value(
name = "yocto_generic_toolchain",
constraint_setting = ":toolchain",
Expand Down
54 changes: 54 additions & 0 deletions cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ AARCH64_LINUX_LLVM = "https://github.com/llvm/llvm-project/releases/download/llv

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"

# LLVM 20.1.7 URLs
AARCH64_DARWIN_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-macOS-ARM64.tar.xz"

X86_64_DARWIN_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-macOS-X64.tar.xz"

AARCH64_LINUX_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-Linux-ARM64.tar.xz"

X86_64_LINUX_LLVM20 = "https://github.com/llvm/llvm-project/releases/download/llvmorg-20.1.7/LLVM-20.1.7-Linux-X64.tar.xz"

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"

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

def swift_cc_toolchain_llvm20():
"""Define LLVM 20.1.7 toolchain repositories."""
maybe(
http_archive,
name = "aarch64-darwin-llvm20",
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
url = AARCH64_DARWIN_LLVM20,
strip_prefix = "LLVM-20.1.7-macOS-ARM64",
# TODO: Add sha256 hash for integrity
)

maybe(
http_archive,
name = "x86_64-darwin-llvm20",
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
url = X86_64_DARWIN_LLVM20,
strip_prefix = "LLVM-20.1.7-macOS-X64",
# TODO: Add sha256 hash for integrity
)

maybe(
http_archive,
name = "aarch64-linux-llvm20",
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
url = AARCH64_LINUX_LLVM20,
strip_prefix = "LLVM-20.1.7-Linux-ARM64",
# TODO: Add sha256 hash for integrity
)

maybe(
http_archive,
name = "x86_64-linux-llvm20",
build_file = Label("@rules_swiftnav//cc/toolchains/llvm20:llvm.BUILD.bzl"),
url = X86_64_LINUX_LLVM20,
strip_prefix = "LLVM-20.1.7-Linux-X64",
# TODO: Add sha256 hash for integrity
)

def aarch64_sysroot():
maybe(
http_archive,
Expand Down Expand Up @@ -124,6 +171,13 @@ def register_swift_cc_toolchains():
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton2")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton3")

def register_swift_cc_toolchains_llvm20():
"""Register LLVM 20.1.7 toolchains."""
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/aarch64-darwin:cc-toolchain-aarch64-darwin")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/x86_64-darwin:cc-toolchain-x86_64-darwin")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/aarch64-linux:cc-toolchain-aarch64-linux")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm20/x86_64-linux:cc-toolchain-x86_64-linux")

def aarch64_linux_musl_toolchain():
http_archive(
name = "aarch64-linux-musl",
Expand Down
11 changes: 11 additions & 0 deletions cc/toolchains/llvm20/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

package(default_visibility = ["//visibility:public"])
152 changes: 152 additions & 0 deletions cc/toolchains/llvm20/aarch64-darwin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

load("@rules_swiftnav//cc/toolchains/llvm20:cc_toolchain_config.bzl", "cc_toolchain_config")
load("@rules_swiftnav//cc/toolchains/llvm20:target_triplets.bzl", "AARCH64_DARWIN")

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

filegroup(
name = "wrappers",
srcs = glob([
"wrappers/**",
]),
visibility = ["//visibility:public"],
)

filegroup(
name = "ar_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:ar",
],
)

filegroup(
name = "as_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:as",
],
)

filegroup(
name = "compiler_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:clang",
"@aarch64-darwin-llvm20//:include",
],
)

filegroup(
name = "dwp_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:dwp",
],
)

filegroup(
name = "linker_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:ar",
"@aarch64-darwin-llvm20//:clang",
"@aarch64-darwin-llvm20//:lib",
],
)

filegroup(
name = "objcopy_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:objcopy",
],
)

filegroup(
name = "strip_files",
srcs = [
":wrappers",
"@aarch64-darwin-llvm20//:strip",
],
)

filegroup(
name = "all_files",
srcs = [
"linker_files",
":compiler_files",
"@aarch64-darwin-llvm20//:bin",
],
)

cc_toolchain_config(
name = "local-aarch64-darwin",
abi_libc_version = "darwin_aarch64",
abi_version = "darwin_aarch64",
builtin_sysroot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk",
compiler = "clang",
cxx_builtin_include_directories = [
"%sysroot%/usr/include",
"%sysroot%/System/Library/Frameworks",
],
host_system_name = AARCH64_DARWIN,
is_darwin = True,
target_cpu = "darwin",
target_libc = "macosx",
target_system_name = AARCH64_DARWIN,
tool_paths = {
"ar": "/usr/bin/libtool",
"cpp": "wrappers/clang-cpp",
"gcc": "wrappers/clang",
"gcov": "wrappers/llvm-profdata",
"llvm-cov": "wrappers/llvm-cov",
"llvm-profdata": "wrappers/llvm-profdata",
"ld": "/usr/bin/ld",
"nm": "wrappers/llvm-nm",
"objcopy": "wrappers/llvm-objcopy",
"objdump": "wrappers/llvm-objdump",
"strip": "wrappers/llvm-strip",
},
toolchain_identifier = "clang-aarch64-darwin",
toolchain_path_prefix = "external/aarch64-darwin-llvm20",
use_lld = False,
)

cc_toolchain(
name = "cc-clang-aarch64-darwin",
all_files = ":all_files",
ar_files = ":ar_files",
as_files = ":as_files",
compiler_files = ":compiler_files",
dwp_files = ":dwp_files",
linker_files = ":linker_files",
objcopy_files = ":objcopy_files",
strip_files = ":strip_files",
toolchain_config = ":local-aarch64-darwin",
)

toolchain(
name = "cc-toolchain-aarch64-darwin",
exec_compatible_with = [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
],
target_compatible_with = [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
"@rules_swiftnav//cc/constraints:llvm20_toolchain",
],
target_settings = None,
toolchain = ":cc-clang-aarch64-darwin",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
64 changes: 64 additions & 0 deletions cc/toolchains/llvm20/aarch64-darwin/wrappers/clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

# Locates the actual tool paths relative to the location this script is
# executed from.
#
# This is necessary because we download the toolchain using
# http_archive, which bazel places in _execroot_/external/_repo_name_.
#
# Unfortunately we cannot provide this location as the argument of tool_path
# when configuring the toolchain, because tool_path only takes a relative path.
#
# This is the fairly common workaround to handle this.
#
# TODO: [BUILD-549] - Remove need for wrapper files
#
# Recent versions of Bazel may have introduced a mechanism that removes
# the need for this workaround: https://github.com/bazelbuild/bazel/issues/7746

set -e

# Use --actionv_env=SWIFTNAV_VERBOSE_TOOLCHAIN to enable
if [[ -n "$SWIFTNAV_VERBOSE_TOOLCHAIN" ]]; then
set -x
fi

tool_name=$(basename "${BASH_SOURCE[0]}")
# In case the tool label is changed, a change in here is very likely needed
# This establishes backwards compatibility with the old WORKSPACE file
toolchain_bindir=external/aarch64-darwin-llvm20/bin
toolchain_bindir_as_bzlmod=external/rules_swiftnav~~swift_cc_toolchain_extension~aarch64-darwin-llvm20/bin

if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
# We're running under _execroot_, call the real tool.
exec "${toolchain_bindir}"/"${tool_name}" "$@"
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
# We're running under _execroot_, call the real tool.
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
# This branch exists because some users of the toolchain,
# namely rules_foreign_cc, will change CWD and call $CC (this script)
# with its absolute path.
#
# To deal with this we find the tool relative to this script, which is at
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
#
# If the wrapper is relocated then this line needs to be adjusted.
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
exec "${tool}" "${@}"
else
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
exit 5
fi

64 changes: 64 additions & 0 deletions cc/toolchains/llvm20/aarch64-darwin/wrappers/clang-cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

# Locates the actual tool paths relative to the location this script is
# executed from.
#
# This is necessary because we download the toolchain using
# http_archive, which bazel places in _execroot_/external/_repo_name_.
#
# Unfortunately we cannot provide this location as the argument of tool_path
# when configuring the toolchain, because tool_path only takes a relative path.
#
# This is the fairly common workaround to handle this.
#
# TODO: [BUILD-549] - Remove need for wrapper files
#
# Recent versions of Bazel may have introduced a mechanism that removes
# the need for this workaround: https://github.com/bazelbuild/bazel/issues/7746

set -e

# Use --actionv_env=SWIFTNAV_VERBOSE_TOOLCHAIN to enable
if [[ -n "$SWIFTNAV_VERBOSE_TOOLCHAIN" ]]; then
set -x
fi

tool_name=$(basename "${BASH_SOURCE[0]}")
# In case the tool label is changed, a change in here is very likely needed
# This establishes backwards compatibility with the old WORKSPACE file
toolchain_bindir=external/aarch64-darwin-llvm20/bin
toolchain_bindir_as_bzlmod=external/rules_swiftnav~~swift_cc_toolchain_extension~aarch64-darwin-llvm20/bin

if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
# We're running under _execroot_, call the real tool.
exec "${toolchain_bindir}"/"${tool_name}" "$@"
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
# We're running under _execroot_, call the real tool.
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
# This branch exists because some users of the toolchain,
# namely rules_foreign_cc, will change CWD and call $CC (this script)
# with its absolute path.
#
# To deal with this we find the tool relative to this script, which is at
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
#
# If the wrapper is relocated then this line needs to be adjusted.
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
exec "${tool}" "${@}"
else
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
exit 5
fi

Loading
Loading