diff --git a/cc/BUILD.bazel b/cc/BUILD.bazel index 2a91fa8..4383b13 100644 --- a/cc/BUILD.bazel +++ b/cc/BUILD.bazel @@ -94,3 +94,15 @@ config_setting( flag_values = {":cxx_standard": "23"}, visibility = ["//visibility:public"], ) + +bool_flag( + name = "enable_sysroot", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "_enable_sysroot", + flag_values = {":enable_sysroot": "true"}, + visibility = ["//visibility:public"], +) diff --git a/cc/repositories.bzl b/cc/repositories.bzl index b573358..3684438 100644 --- a/cc/repositories.bzl +++ b/cc/repositories.bzl @@ -69,11 +69,27 @@ filegroup( url = "https://github.com/swift-nav/swift-toolchains/releases/download/bullseye-aarch64-sysroot-v1/debian_bullseye_aarch64_sysroot.tar.xz", ) +def x86_64_sysroot(): + maybe( + http_archive, + name = "x86_64-sysroot", + # sha256 = "9bd27c7ec6aa4bd3d4df60cd04228669bcf366aec32d4b4dc7b504de2f63121e", + build_file_content = """ +filegroup( + name = "x86_64-sysroot", + srcs = glob(["*/**"]), + visibility = ["//visibility:public"], +) + """, + url = "https://github.com/swift-nav/swift-toolchains/releases/download/bullseye-x86_64-sysroot-v1/debian_bullseye_x86_64_sysroot.tar.xz", + ) + def register_swift_cc_toolchains(): native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/aarch64-darwin:cc-toolchain-aarch64-darwin") native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin:cc-toolchain-x86_64-darwin") 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-aarch64-linux:cc-toolchain-x86_64-aarch64-linux") + native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin-x86_64-linux:cc-toolchain-x86_64-darwin-x86_64-linux") def aarch64_linux_musl_toolchain(): http_archive( diff --git a/cc/toolchains/llvm/cc_toolchain_config.bzl b/cc/toolchains/llvm/cc_toolchain_config.bzl index 41d9da1..c3b1029 100644 --- a/cc/toolchains/llvm/cc_toolchain_config.bzl +++ b/cc/toolchains/llvm/cc_toolchain_config.bzl @@ -35,7 +35,7 @@ def cc_toolchain_config( if not is_target_triplet(target_system_name): fail(target_system_name + " is not a target tripplet") - cross_compile = host_system_name != target_system_name + use_libstdcpp = builtin_sysroot != None and not is_darwin # Default compiler flags: compile_flags = [ @@ -82,7 +82,7 @@ def cc_toolchain_config( # The whole codebase should build with c++14 "-std=c++14", # Use bundled libc++ for hermeticity if not cross compiling - ] + ["-stdlib=libstdc++"] if cross_compile else ["-stdlib=libc++"] + ] + ["-stdlib=libstdc++"] if use_libstdcpp else ["-stdlib=libc++"] link_flags = [ "--target=" + target_system_name, @@ -124,7 +124,7 @@ def cc_toolchain_config( "-ldl", ]) - if cross_compile: + if use_libstdcpp: link_flags.extend([ # Use libstdc++ from the sysroot when cross compiling "-l:libstdc++.a", diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/BUILD.bazel b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/BUILD.bazel new file mode 100644 index 0000000..9f015e3 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/BUILD.bazel @@ -0,0 +1,157 @@ +# 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") +load("//cc/toolchains/llvm:target_triplets.bzl", "X86_64_DARWIN", "X86_64_LINUX") + +filegroup( + name = "wrappers", + srcs = glob([ + "wrappers/**", + ]), + visibility = ["//visibility:public"], +) + +filegroup( + name = "ar_files", + srcs = [ + ":wrappers", + "@x86_64-sysroot", + "@x86_64-darwin-llvm//:ar", + ], +) + +filegroup( + name = "as_files", + srcs = [ + ":wrappers", + "@x86_64-sysroot", + "@x86_64-darwin-llvm//:as", + ], +) + +filegroup( + name = "compiler_files", + srcs = [ + ":wrappers", + "@x86_64-sysroot", + "@x86_64-darwin-llvm//:clang", + "@x86_64-darwin-llvm//:include", + ], +) + +filegroup( + name = "dwp_files", + srcs = [ + ":wrappers", + "@x86_64-sysroot", + "@x86_64-darwin-llvm//:dwp", + ], +) + +filegroup( + name = "linker_files", + srcs = [ + ":wrappers", + "@x86_64-sysroot", + "@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-sysroot", + "@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 = "external/x86_64-sysroot", + compiler = "clang", + cxx_builtin_include_directories = [ + "%sysroot%/usr/include", + ], + host_system_name = X86_64_DARWIN, + is_darwin = False, + target_cpu = "k8", + target_libc = "glibc_unknown", + target_system_name = X86_64_LINUX, + 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-darwin-x86_64-linux", + toolchain_path_prefix = "external/x86_64-darwin-llvm", +) + +cc_toolchain( + name = "cc-clang-x86_64-darwin-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-darwin", +) + +toolchain( + name = "cc-toolchain-x86_64-darwin-x86_64-linux", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + "//cc/constraints:glibc_2_31", + ], + target_settings = None, + toolchain = ":cc-clang-x86_64-darwin-x86_64-linux", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang-cpp b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang-cpp new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/clang-cpp @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/ld.ldd b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/ld.ldd new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/ld.ldd @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-ar b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-ar new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-ar @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-cov b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-cov new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-cov @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-nm b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-nm new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-nm @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objcopy b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objcopy new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objcopy @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objdump b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objdump new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-objdump @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-profdata b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-profdata new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-profdata @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-strip b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-strip new file mode 120000 index 0000000..22cb46f --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/llvm-strip @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/wrapper b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/wrappers/wrapper new file mode 100755 index 0000000..e8a60c3 --- /dev/null +++ b/cc/toolchains/llvm/x86_64-darwin-x86_64-linux/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 index 5cad905..408e9cd 100644 --- a/cc/toolchains/llvm/x86_64-linux/BUILD.bazel +++ b/cc/toolchains/llvm/x86_64-linux/BUILD.bazel @@ -26,7 +26,10 @@ filegroup( srcs = [ ":wrappers", "@x86_64-linux-llvm//:ar", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -34,7 +37,10 @@ filegroup( srcs = [ ":wrappers", "@x86_64-linux-llvm//:as", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -43,7 +49,10 @@ filegroup( ":wrappers", "@x86_64-linux-llvm//:clang", "@x86_64-linux-llvm//:include", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -51,7 +60,10 @@ filegroup( srcs = [ ":wrappers", "@x86_64-linux-llvm//:dwp", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -62,7 +74,10 @@ filegroup( "@x86_64-linux-llvm//:clang", "@x86_64-linux-llvm//:ld", "@x86_64-linux-llvm//:lib", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -70,7 +85,10 @@ filegroup( srcs = [ ":wrappers", "@x86_64-linux-llvm//:objcopy", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) filegroup( @@ -87,19 +105,29 @@ filegroup( "linker_files", ":compiler_files", "@x86_64-linux-llvm//:bin", - ], + ] + select({ + "//cc:_enable_sysroot": ["@x86_64-sysroot"], + "//conditions:default": [], + }), ) cc_toolchain_config( name = "local-x86_64-linux", abi_libc_version = "glibc_unknown", abi_version = "clang", + builtin_sysroot = select({ + "//cc:_enable_sysroot": "external/x86_64-sysroot", + "//conditions:default": None, + }), compiler = "clang", - cxx_builtin_include_directories = [ - "/include", - "/usr/include", - "/usr/local/include", - ], + cxx_builtin_include_directories = select({ + "//cc:_enable_sysroot": ["%sysroot%/usr/include"], + "//conditions:default": [ + "/include", + "/usr/include", + "/usr/local/include", + ], + }), host_system_name = X86_64_LINUX, target_cpu = "k8", target_libc = "glibc_unknown",