Skip to content
Merged
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
22 changes: 17 additions & 5 deletions cc/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,29 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

LLVM_DISTRIBUTION_URL = "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"
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"

X86_64_DARWIN_LLVM = "https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang%2Bllvm-14.0.0-x86_64-apple-darwin.tar.xz"

def swift_cc_toolchain():
maybe(
http_archive,
name = "llvm-distribution",
build_file = Label("//cc/toolchains:llvm.BUILD.bzl"),
url = LLVM_DISTRIBUTION_URL,
name = "x86_64-linux-llvm",
build_file = Label("//cc/toolchains/llvm:llvm.BUILD.bzl"),
url = X86_64_LINUX_LLVM,
strip_prefix = "clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04",
sha256 = "61582215dafafb7b576ea30cc136be92c877ba1f1c31ddbbd372d6d65622fef5",
)

maybe(
http_archive,
name = "x86_64-darwin-llvm",
build_file = Label("//cc/toolchains/llvm:llvm.BUILD.bzl"),
url = X86_64_DARWIN_LLVM,
strip_prefix = "clang+llvm-14.0.0-x86_64-apple-darwin",
sha256 = "cf5af0f32d78dcf4413ef6966abbfd5b1445fe80bba57f2ff8a08f77e672b9b3",
)

def register_swift_cc_toolchains():
native.register_toolchains("@rules_swiftnav//cc/toolchains:cc-toolchain-x86_64-linux")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux")
native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin:cc-toolchain-x86_64-darwin")
121 changes: 0 additions & 121 deletions cc/toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,121 +0,0 @@
# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <dev@swift-nav.com>
#
# 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"])

load(":cc_toolchain_config.bzl", "cc_toolchain_config")

filegroup(name = "empty")

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

filegroup(
name = "ar_files",
srcs = [
":wrappers",
"@llvm-distribution//:ar",
],
)

filegroup(
name = "as_files",
srcs = [
":wrappers",
"@llvm-distribution//:as",
],
)

filegroup(
name = "compiler_files",
srcs = [
":wrappers",
"@llvm-distribution//:clang",
"@llvm-distribution//:include",
],
)

filegroup(
name = "dwp_files",
srcs = [
":wrappers",
"@llvm-distribution//:dwp",
],
)

filegroup(
name = "linker_files",
srcs = [
":wrappers",
"@llvm-distribution//:ar",
"@llvm-distribution//:clang",
"@llvm-distribution//:ld",
"@llvm-distribution//:lib",
],
)

filegroup(
name = "objcopy_files",
srcs = [
":wrappers",
"@llvm-distribution//:objcopy",
],
)

filegroup(
name = "strip_files",
srcs = [
":wrappers",
"@llvm-distribution//:strip",
],
)

filegroup(
name = "all_files",
srcs = [
"linker_files",
":compiler_files",
"@llvm-distribution//:bin",
],
)

cc_toolchain_config(name = "local-x86_64-linux")

cc_toolchain(
name = "cc-clang-x86_64-linux",
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-x86_64-linux",
)

toolchain(
name = "cc-toolchain-x86_64-linux",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_settings = None,
toolchain = ":cc-clang-x86_64-linux",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
11 changes: 11 additions & 0 deletions cc/toolchains/llvm/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 <dev@swift-nav.com>
#
# 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"])
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,21 @@ load(
unix_cc_toolchain_config = "cc_toolchain_config",
)

def cc_toolchain_config(name):
# These variables are passed directly through to unix_cc_toolchain_config
# below. As far as I can tell they are just metadata that doesn't affect
# the build.
host_system_name = "linux-x86_64"
toolchain_identifier = "clang-x86_64-linux"
target_cpu = "k8"
target_libc = "glibc_unknown"
compiler = "clang"
abi_version = "clang"
abi_libc_version = "glibc_unknown"

cxx_builtin_include_directories = [
"/include",
"/usr/include",
"/usr/local/include",
]

tool_paths = {
"ar": "wrappers/llvm-ar",
"cpp": "wrappers/clang-cpp",
"gcc": "wrappers/clang",
"gcov": "wrappers/llvm-profdata",
"llvm-cov": "wrappers/llvm-cov",
"llvm-profdata": "wrappers/llvm-profdata",
"ld": "wrappers/ld.ldd",
"nm": "wrappers/llvm-nm",
"objcopy": "wrappers/llvm-objcopy",
"objdump": "wrappers/llvm-objdump",
"strip": "wrappers/llvm-strip",
}

target_system_name = "x86_64-unknown-linux-gnu"

def cc_toolchain_config(
name,
host_system_name,
toolchain_identifier,
toolchain_path_prefix,
target_cpu,
target_libc,
compiler,
abi_version,
abi_libc_version,
cxx_builtin_include_directories,
tool_paths,
target_system_name,
builtin_sysroot = None,
is_darwin = False):
# Default compiler flags:
compile_flags = [
"--target=" + target_system_name,
Expand Down Expand Up @@ -93,32 +74,79 @@ def cc_toolchain_config(name):
"--target=" + target_system_name,
"-lm",
"-no-canonical-prefixes",
# Below this line, assumes libc++ & lld
"-l:libc++.a",
"-l:libc++abi.a",
"-l:libunwind.a",
# Compiler runtime features.
"-rtlib=compiler-rt",
# To support libunwind
# It's ok to assume posix when using this toolchain
"-lpthread",
"-ldl",
]

# linux/lld only!
link_flags.extend([
"-fuse-ld=lld",
"-Wl,--build-id=md5",
"-Wl,--hash-style=gnu",
"-Wl,-z,relro,-z,now",
])

# Similar to link_flags, but placed later in the command line such that
# unused symbols are not stripped.
link_libs = []

if is_darwin:
# Mach-O support in lld is experimental, so on mac
# we use the system linker.
use_lld = False
link_flags.extend([
"-headerpad_max_install_names",
# This will issue a warning on macOS ventura; see:
# https://github.com/python/cpython/issues/97524
# https://developer.apple.com/forums/thread/719961
"-undefined",
"dynamic_lookup",
])
else:
use_lld = True
link_flags.extend([
"-fuse-ld=lld",
"-Wl,--build-id=md5",
"-Wl,--hash-style=gnu",
"-Wl,-z,relro,-z,now",
])

if use_lld:
link_flags.extend([
# Below this line, assumes libc++ & lld
"-l:libc++.a",
"-l:libc++abi.a",
"-l:libunwind.a",
# Compiler runtime features.
"-rtlib=compiler-rt",
# To support libunwind
# It's ok to assume posix when using this toolchain
"-lpthread",
"-ldl",
])
else:
# The comments below were copied directly from:
# https://github.com/grailbio/bazel-toolchain/blob/795d76fd03e0b17c0961f0981a8512a00cba4fa2/toolchain/cc_toolchain_config.bzl#L202

# The only known mechanism to static link libraries in ld64 is to
# not have the corresponding .dylib files in the library search
# path. The link time sandbox does not include the .dylib files, so
# anything we pick up from the toolchain should be statically
# linked. However, several system libraries on macOS dynamically
# link libc++ and libc++abi, so static linking them becomes a problem.
# We need to ensure that they are dynamic linked from the system
# sysroot and not static linked from the toolchain, so explicitly
# have the sysroot directory on the search path and then add the
# toolchain directory back after we are done.
link_flags.extend([
"-L{}/usr/lib".format(builtin_sysroot),
"-lc++",
"-lc++abi",
])

# Let's provide the path to the toolchain library directory
# explicitly as part of the search path to make it easy for a user
# to pick up something. This also makes the behavior consistent with
# targets when a user explicitly depends on something like
# libomp.dylib, which adds this directory to the search path, and would
# (unintentionally) lead to static linking of libraries from the
# toolchain.
link_flags.extend([
"-L{}/lib".format(toolchain_path_prefix),
])

# linux/lld only
opt_link_flags = ["-Wl,--gc-sections"]
opt_link_flags = ["-Wl,--gc-sections"] if not is_darwin else []

# Unfiltered compiler flags; these are placed at the end of the command
# line, so take precendence over any user supplied flags through --copts or
Expand All @@ -140,8 +168,7 @@ def cc_toolchain_config(name):
coverage_compile_flags = ["-fprofile-instr-generate", "-fcoverage-mapping"]
coverage_link_flags = ["-fprofile-instr-generate"]

# true if using lld
supports_start_end_lib = True
supports_start_end_lib = use_lld

# Calls https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl
# Which defines the rule that actually sets up the cc toolchain.
Expand All @@ -168,4 +195,5 @@ def cc_toolchain_config(name):
coverage_compile_flags = coverage_compile_flags,
coverage_link_flags = coverage_link_flags,
supports_start_end_lib = supports_start_end_lib,
builtin_sysroot = builtin_sysroot,
)
File renamed without changes.
Loading