diff --git a/cc/repositories.bzl b/cc/repositories.bzl index e6cb75ee..a8c1d1c4 100644 --- a/cc/repositories.bzl +++ b/cc/repositories.bzl @@ -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") diff --git a/cc/toolchains/BUILD.bazel b/cc/toolchains/BUILD.bazel index e760c819..e69de29b 100644 --- a/cc/toolchains/BUILD.bazel +++ b/cc/toolchains/BUILD.bazel @@ -1,121 +0,0 @@ -# Copyright (C) 2022 Swift Navigation Inc. -# Contact: Swift Navigation -# -# 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", -) diff --git a/cc/toolchains/llvm/BUILD.bazel b/cc/toolchains/llvm/BUILD.bazel new file mode 100644 index 00000000..15894856 --- /dev/null +++ b/cc/toolchains/llvm/BUILD.bazel @@ -0,0 +1,11 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# 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"]) diff --git a/cc/toolchains/cc_toolchain_config.bzl b/cc/toolchains/llvm/cc_toolchain_config.bzl similarity index 55% rename from cc/toolchains/cc_toolchain_config.bzl rename to cc/toolchains/llvm/cc_toolchain_config.bzl index 5a65afa5..e13145a3 100644 --- a/cc/toolchains/cc_toolchain_config.bzl +++ b/cc/toolchains/llvm/cc_toolchain_config.bzl @@ -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, @@ -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 @@ -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. @@ -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, ) diff --git a/cc/toolchains/llvm.BUILD.bzl b/cc/toolchains/llvm/llvm.BUILD.bzl similarity index 100% rename from cc/toolchains/llvm.BUILD.bzl rename to cc/toolchains/llvm/llvm.BUILD.bzl diff --git a/cc/toolchains/llvm/x86_64-darwin/BUILD.bazel b/cc/toolchains/llvm/x86_64-darwin/BUILD.bazel new file mode 100644 index 00000000..e92a6b75 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin/BUILD.bazel @@ -0,0 +1,150 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# 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/toolchains/llvm:cc_toolchain_config.bzl", "cc_toolchain_config") + +filegroup( + name = "wrappers", + srcs = glob([ + "wrappers/**", + ]), + visibility = ["//visibility:public"], +) + +filegroup( + name = "ar_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:ar", + ], +) + +filegroup( + name = "as_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:as", + ], +) + +filegroup( + name = "compiler_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:clang", + "@x86_64-darwin-llvm//:include", + ], +) + +filegroup( + name = "dwp_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:dwp", + ], +) + +filegroup( + name = "linker_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:ar", + "@x86_64-darwin-llvm//:clang", + "@x86_64-darwin-llvm//:ld", + "@x86_64-darwin-llvm//:lib", + ], +) + +filegroup( + name = "objcopy_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:objcopy", + ], +) + +filegroup( + name = "strip_files", + srcs = [ + ":wrappers", + "@x86_64-darwin-llvm//:strip", + ], +) + +filegroup( + name = "all_files", + srcs = [ + "linker_files", + ":compiler_files", + "@x86_64-darwin-llvm//:bin", + ], +) + +cc_toolchain_config( + name = "local-x86_64-darwin", + abi_libc_version = "darwin_x86_64", + abi_version = "darwin_x86_64", + builtin_sysroot = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk", + compiler = "clang", + cxx_builtin_include_directories = [ + "%sysroot%/usr/include", + "%sysroot%/System/Library/Frameworks", + ], + host_system_name = "darwin-x86_64", + is_darwin = True, + target_cpu = "darwin", + target_libc = "macosx", + target_system_name = "x86_64-apple-macosx", + 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-x86_64-darwin", + toolchain_path_prefix = "external/x86_64-darwin-llvm", +) + +cc_toolchain( + name = "cc-clang-x86_64-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-x86_64-darwin", +) + +toolchain( + name = "cc-toolchain-x86_64-darwin", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + target_settings = None, + toolchain = ":cc-clang-x86_64-darwin", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/cc/toolchains/wrappers/clang b/cc/toolchains/llvm/x86_64-darwin/wrappers/clang similarity index 100% rename from cc/toolchains/wrappers/clang rename to cc/toolchains/llvm/x86_64-darwin/wrappers/clang diff --git a/cc/toolchains/wrappers/clang-cpp b/cc/toolchains/llvm/x86_64-darwin/wrappers/clang-cpp similarity index 100% rename from cc/toolchains/wrappers/clang-cpp rename to cc/toolchains/llvm/x86_64-darwin/wrappers/clang-cpp diff --git a/cc/toolchains/wrappers/llvm-cov b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-cov similarity index 100% rename from cc/toolchains/wrappers/llvm-cov rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-cov diff --git a/cc/toolchains/wrappers/llvm-nm b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-nm similarity index 100% rename from cc/toolchains/wrappers/llvm-nm rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-nm diff --git a/cc/toolchains/wrappers/llvm-objcopy b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-objcopy similarity index 100% rename from cc/toolchains/wrappers/llvm-objcopy rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-objcopy diff --git a/cc/toolchains/wrappers/llvm-objdump b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-objdump similarity index 100% rename from cc/toolchains/wrappers/llvm-objdump rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-objdump diff --git a/cc/toolchains/wrappers/llvm-profdata b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-profdata similarity index 100% rename from cc/toolchains/wrappers/llvm-profdata rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-profdata diff --git a/cc/toolchains/wrappers/llvm-strip b/cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-strip similarity index 100% rename from cc/toolchains/wrappers/llvm-strip rename to cc/toolchains/llvm/x86_64-darwin/wrappers/llvm-strip diff --git a/cc/toolchains/llvm/x86_64-darwin/wrappers/wrapper b/cc/toolchains/llvm/x86_64-darwin/wrappers/wrapper new file mode 100755 index 00000000..e8a60c3e --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin/wrappers/wrapper @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# 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]}") +toolchain_bindir=external/x86_64-darwin-llvm/bin + +if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then + # We're running under _execroot_, call the real tool. + exec "${toolchain_bindir}"/"${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 + diff --git a/cc/toolchains/llvm/x86_64-linux/BUILD.bazel b/cc/toolchains/llvm/x86_64-linux/BUILD.bazel new file mode 100644 index 00000000..d3c68877 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/BUILD.bazel @@ -0,0 +1,149 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# 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/toolchains/llvm:cc_toolchain_config.bzl", "cc_toolchain_config") + +filegroup( + name = "wrappers", + srcs = glob([ + "wrappers/**", + ]), + visibility = ["//visibility:public"], +) + +filegroup( + name = "ar_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:ar", + ], +) + +filegroup( + name = "as_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:as", + ], +) + +filegroup( + name = "compiler_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:clang", + "@x86_64-linux-llvm//:include", + ], +) + +filegroup( + name = "dwp_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:dwp", + ], +) + +filegroup( + name = "linker_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:ar", + "@x86_64-linux-llvm//:clang", + "@x86_64-linux-llvm//:ld", + "@x86_64-linux-llvm//:lib", + ], +) + +filegroup( + name = "objcopy_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:objcopy", + ], +) + +filegroup( + name = "strip_files", + srcs = [ + ":wrappers", + "@x86_64-linux-llvm//:strip", + ], +) + +filegroup( + name = "all_files", + srcs = [ + "linker_files", + ":compiler_files", + "@x86_64-linux-llvm//:bin", + ], +) + +cc_toolchain_config( + name = "local-x86_64-linux", + abi_libc_version = "glibc_unknown", + abi_version = "clang", + compiler = "clang", + cxx_builtin_include_directories = [ + "/include", + "/usr/include", + "/usr/local/include", + ], + host_system_name = "linux-x86_64", + target_cpu = "k8", + target_libc = "glibc_unknown", + target_system_name = "x86_64-unknown-linux-gnu", + 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", + }, + toolchain_identifier = "clang-x86_64-linux", + toolchain_path_prefix = "external/x86_64-linux-llvm", +) + +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", +) diff --git a/cc/toolchains/wrappers/ld.lld b/cc/toolchains/llvm/x86_64-linux/wrappers/clang similarity index 100% rename from cc/toolchains/wrappers/ld.lld rename to cc/toolchains/llvm/x86_64-linux/wrappers/clang diff --git a/cc/toolchains/wrappers/llvm-ar b/cc/toolchains/llvm/x86_64-linux/wrappers/clang-cpp similarity index 100% rename from cc/toolchains/wrappers/llvm-ar rename to cc/toolchains/llvm/x86_64-linux/wrappers/clang-cpp diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/ld.lld b/cc/toolchains/llvm/x86_64-linux/wrappers/ld.lld new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/ld.lld @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-ar b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-ar new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-ar @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-cov b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-cov new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-cov @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-nm b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-nm new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-nm @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objcopy b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objcopy new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objcopy @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objdump b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objdump new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-objdump @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-profdata b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-profdata new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-profdata @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-strip b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-strip new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/llvm-strip @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/wrappers/wrapper b/cc/toolchains/llvm/x86_64-linux/wrappers/wrapper similarity index 91% rename from cc/toolchains/wrappers/wrapper rename to cc/toolchains/llvm/x86_64-linux/wrappers/wrapper index b73fdcd5..14658085 100755 --- a/cc/toolchains/wrappers/wrapper +++ b/cc/toolchains/llvm/x86_64-linux/wrappers/wrapper @@ -34,7 +34,7 @@ if [[ -n "$SWIFTNAV_VERBOSE_TOOLCHAIN" ]]; then fi tool_name=$(basename "${BASH_SOURCE[0]}") -toolchain_bindir=external/llvm-distribution/bin +toolchain_bindir=external/x86_64-linux-llvm/bin if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then # We're running under _execroot_, call the real tool. @@ -45,10 +45,10 @@ elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then # 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/wrappers/wrapper. + # _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]%/*/*/*/*/*/*}" + execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}" tool="${execroot_path}/${toolchain_bindir}/${tool_name}" exec "${tool}" "${@}" else diff --git a/clang_format/BUILD.bazel b/clang_format/BUILD.bazel index e92d216b..466add66 100644 --- a/clang_format/BUILD.bazel +++ b/clang_format/BUILD.bazel @@ -19,7 +19,8 @@ choose_clang_format( filegroup( name = "_clang_format_bin", srcs = select({ - "@platforms//os:linux": ["@llvm-distribution//:clang-format"], + "//platforms:x86_64-darwin": ["@x86_64-darwin-llvm//:clang-format"], + "//platforms:x86_64-linux": ["@x86_64-linux-llvm//:clang-format"], "//conditions:default": [":clang_format_bin"], }), visibility = ["//visibility:public"], diff --git a/clang_tidy/BUILD.bazel b/clang_tidy/BUILD.bazel index f97431be..4ae49ffd 100644 --- a/clang_tidy/BUILD.bazel +++ b/clang_tidy/BUILD.bazel @@ -27,7 +27,8 @@ filegroup( name = "clang_tidy_executable_default", srcs = select( { - "@platforms//os:linux": ["@llvm-distribution//:clang-tidy"], + "//platforms:x86_64-darwin": ["@x86_64-darwin-llvm//:clang-tidy"], + "//platforms:x86_64-linux": ["@x86_64-linux-llvm//:clang-tidy"], "//conditions:default": [":clang_tidy_bin"], }, ), diff --git a/platforms/BUILD.bazel b/platforms/BUILD.bazel new file mode 100644 index 00000000..c23eb909 --- /dev/null +++ b/platforms/BUILD.bazel @@ -0,0 +1,35 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# 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("@bazel_skylib//lib:selects.bzl", "selects") + +selects.config_setting_group( + name = "aarch64-darwin", + match_all = [ + "@platforms//cpu:aarch64", + "@platforms//os:macos", + ], +) + +selects.config_setting_group( + name = "x86_64-darwin", + match_all = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], +) + +selects.config_setting_group( + name = "x86_64-linux", + match_all = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) diff --git a/third_party/netcdf-c.BUILD b/third_party/netcdf-c.BUILD index 161ec233..6c9d769d 100644 --- a/third_party/netcdf-c.BUILD +++ b/third_party/netcdf-c.BUILD @@ -12,7 +12,6 @@ netcdf-c@4.9.0 """ -load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_swiftnav//tools:configure_file.bzl", "configure_file") load("@rules_swiftnav//third_party/netcdf-c:aarch64-darwin-config.bzl", "AARCH64_DARWIN_CONFIG") load("@rules_swiftnav//third_party/netcdf-c:x86_64-darwin-config.bzl", "X86_64_DARWIN_CONFIG") @@ -21,30 +20,6 @@ load("@rules_swiftnav//third_party/netcdf-c:attr.bzl", "attr") load("@rules_swiftnav//third_party/netcdf-c:ncx.bzl", "ncx") load("@rules_swiftnav//third_party/netcdf-c:putget.bzl", "putget") -selects.config_setting_group( - name = "aarch64-darwin", - match_all = [ - "@platforms//cpu:aarch64", - "@platforms//os:macos", - ], -) - -selects.config_setting_group( - name = "x86_64-darwin", - match_all = [ - "@platforms//cpu:x86_64", - "@platforms//os:macos", - ], -) - -selects.config_setting_group( - name = "x86_64-linux", - match_all = [ - "@platforms//cpu:x86_64", - "@platforms//os:linux", - ], -) - configure_file( name = "netcdf_dispatch", out = "netcdf_dispatch.h", @@ -157,9 +132,9 @@ genrule( outs = ["config.h"], cmd = select( { - ":aarch64-darwin": "cat <<'EOF' > $@ {}EOF".format(AARCH64_DARWIN_CONFIG), - ":x86_64-darwin": "cat <<'EOF' > $@ {}EOF".format(X86_64_DARWIN_CONFIG), - ":x86_64-linux": "cat <<'EOF' > $@ {}EOF".format(X86_64_LINUX_CONFIG), + "@rules_swiftnav//platforms:aarch64-darwin": "cat <<'EOF' > $@ {}EOF".format(AARCH64_DARWIN_CONFIG), + "@rules_swiftnav//platforms:x86_64-darwin": "cat <<'EOF' > $@ {}EOF".format(X86_64_DARWIN_CONFIG), + "@rules_swiftnav//platforms:x86_64-linux": "cat <<'EOF' > $@ {}EOF".format(X86_64_LINUX_CONFIG), }, no_match_error = "Currently only aarch64-darwin, x86_64-darwin, and x86_64-linux are supported.", ),