diff --git a/WORKSPACE b/WORKSPACE.bazel similarity index 96% rename from WORKSPACE rename to WORKSPACE.bazel index b0eae5d3..fc7b727d 100644 --- a/WORKSPACE +++ b/WORKSPACE.bazel @@ -45,8 +45,7 @@ http_archive( load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") # If you use WORKSPACE.bazel, use the following line instead of the bare gazelle_dependencies(): -# gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel") -gazelle_dependencies() +gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel") http_archive( name = "com_google_protobuf", diff --git a/cc/BUILD.bazel b/cc/BUILD.bazel index 8d23f414..2a91fa83 100644 --- a/cc/BUILD.bazel +++ b/cc/BUILD.bazel @@ -54,6 +54,19 @@ config_setting( visibility = ["//visibility:public"], ) +# Disable warnings as errors with --@rules_swiftnav//cc:warnings_as_errors=false +bool_flag( + name = "warnings_as_errors", + build_setting_default = True, + visibility = ["//visibility:public"], +) + +config_setting( + name = "_disable_warnings_as_errors", + flag_values = {":warnings_as_errors": "false"}, + visibility = ["//visibility:public"], +) + # Allows us to experiment with building the codebase with different standards. string_flag( name = "cxx_standard", diff --git a/cc/defs.bzl b/cc/defs.bzl index 325de3f4..6870ae47 100644 --- a/cc/defs.bzl +++ b/cc/defs.bzl @@ -51,6 +51,9 @@ def _common_cc_opts(nocopts, pedantic = False): Label("//cc/constraints:gcc-6"): [copt for copt in GCC6_COPTS if copt not in nocopts], Label("//cc/constraints:gcc-5"): [copt for copt in GCC5_COPTS if copt not in nocopts], "//conditions:default": [copt for copt in DEFAULT_COPTS if copt not in nocopts], + }) + select({ + Label("//cc:_disable_warnings_as_errors"): [], + "//conditions:default": ["-Werror"], }) + ["-pedantic"] if pedantic else [] # Options specific to c++ code (exceptions, rtti, etc..) @@ -72,16 +75,6 @@ def _common_cxx_opts(exceptions = False, rtti = False, standard = None): def _construct_local_includes(local_includes): return [construct_local_include(path) for path in local_includes] -# Some options like -Werror are set using toolchain features -# See: https://bazel.build/docs/cc-toolchain-config-reference#features -def _default_features(): - return select({ - # treat_warnings_as_errors passes the option -fatal-warnings - # to the linker which ld on mac does not understand. - "@platforms//os:macos": [], - "//conditions:default": ["treat_warnings_as_errors"], - }) - # Disable building when --//:disable_tests=true or when building on windows def _test_compatible_with(): return select({ @@ -194,8 +187,6 @@ def swift_c_library(**kwargs): kwargs["copts"] = copts + c_standard + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - kwargs["tags"] = [LIBRARY] + kwargs.get("tags", []) native.cc_library(**kwargs) @@ -247,8 +238,6 @@ def swift_cc_library(**kwargs): kwargs["copts"] = copts + cxxopts + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - kwargs["tags"] = [LIBRARY] + kwargs.get("tags", []) native.cc_library(**kwargs) @@ -293,8 +282,6 @@ def swift_c_tool_library(**kwargs): kwargs["copts"] = copts + c_standard + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - native.cc_library(**kwargs) def swift_cc_tool_library(**kwargs): @@ -340,8 +327,6 @@ def swift_cc_tool_library(**kwargs): kwargs["copts"] = copts + cxxopts + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - native.cc_library(**kwargs) def swift_c_binary(**kwargs): @@ -382,8 +367,6 @@ def swift_c_binary(**kwargs): kwargs["copts"] = copts + c_standard + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - kwargs["tags"] = [BINARY] + kwargs.get("tags", []) native.cc_binary(**kwargs) @@ -430,8 +413,6 @@ def swift_cc_binary(**kwargs): kwargs["copts"] = copts + cxxopts + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - kwargs["tags"] = [BINARY] + kwargs.get("tags", []) native.cc_binary(**kwargs) @@ -473,8 +454,6 @@ def swift_c_tool(**kwargs): kwargs["copts"] = copts + c_standard + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - native.cc_binary(**kwargs) def swift_cc_tool(**kwargs): @@ -517,8 +496,6 @@ def swift_cc_tool(**kwargs): kwargs["copts"] = copts + cxxopts + kwargs.get("copts", []) - kwargs["features"] = _default_features() + kwargs.get("features", []) - native.cc_binary(**kwargs) def swift_cc_test_library(**kwargs): diff --git a/cc/repositories.bzl b/cc/repositories.bzl index a8c1d1c4..193b1312 100644 --- a/cc/repositories.bzl +++ b/cc/repositories.bzl @@ -11,18 +11,20 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") -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" +AARCH64_DARWIN_LLVM = "https://github.com/swift-nav/swift-toolchains/releases/download/llvm-14.0.0/clang%2Bllvm-14.0.0-arm64-apple-darwin.tar.gz" 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" +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" + def swift_cc_toolchain(): maybe( http_archive, - name = "x86_64-linux-llvm", + name = "aarch64-darwin-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", + url = AARCH64_DARWIN_LLVM, + strip_prefix = "clang+llvm-14.0.0-arm64-apple-darwin", + sha256 = "f826ee92c3fedb92bad2f9f834d96f6b9db3192871bfe434124bca848ba9a2a3", ) maybe( @@ -34,6 +36,16 @@ def swift_cc_toolchain(): sha256 = "cf5af0f32d78dcf4413ef6966abbfd5b1445fe80bba57f2ff8a08f77e672b9b3", ) + maybe( + http_archive, + 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", + ) + def register_swift_cc_toolchains(): - native.register_toolchains("@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux") + 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") diff --git a/cc/toolchains/llvm/aarch64-darwin/BUILD.bazel b/cc/toolchains/llvm/aarch64-darwin/BUILD.bazel new file mode 100644 index 00000000..df83853a --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/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", + "@aarch64-darwin-llvm//:ar", + ], +) + +filegroup( + name = "as_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:as", + ], +) + +filegroup( + name = "compiler_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:clang", + "@aarch64-darwin-llvm//:include", + ], +) + +filegroup( + name = "dwp_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:dwp", + ], +) + +filegroup( + name = "linker_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:ar", + "@aarch64-darwin-llvm//:clang", + "@aarch64-darwin-llvm//:lib", + ], +) + +filegroup( + name = "objcopy_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:objcopy", + ], +) + +filegroup( + name = "strip_files", + srcs = [ + ":wrappers", + "@aarch64-darwin-llvm//:strip", + ], +) + +filegroup( + name = "all_files", + srcs = [ + "linker_files", + ":compiler_files", + "@aarch64-darwin-llvm//: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 = "darwin-aarch64", + is_darwin = True, + target_cpu = "darwin", + target_libc = "macosx", + target_system_name = "aarch64-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-aarch64-darwin", + toolchain_path_prefix = "external/aarch64-darwin-llvm", +) + +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", + ], + target_settings = None, + toolchain = ":cc-clang-aarch64-darwin", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/clang b/cc/toolchains/llvm/aarch64-darwin/wrappers/clang new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/clang @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/clang-cpp b/cc/toolchains/llvm/aarch64-darwin/wrappers/clang-cpp new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/clang-cpp @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-cov b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-cov new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-cov @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-nm b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-nm new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-nm @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objcopy b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objcopy new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objcopy @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objdump b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objdump new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-objdump @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-profdata b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-profdata new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-profdata @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-strip b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-strip new file mode 120000 index 00000000..22cb46f8 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-darwin/wrappers/llvm-strip @@ -0,0 +1 @@ +wrapper \ No newline at end of file diff --git a/cc/toolchains/llvm/aarch64-darwin/wrappers/wrapper b/cc/toolchains/llvm/aarch64-darwin/wrappers/wrapper new file mode 100755 index 00000000..f7ad6956 --- /dev/null +++ b/cc/toolchains/llvm/aarch64-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/aarch64-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/cc_toolchain_config.bzl b/cc/toolchains/llvm/cc_toolchain_config.bzl index e13145a3..392a6c88 100644 --- a/cc/toolchains/llvm/cc_toolchain_config.bzl +++ b/cc/toolchains/llvm/cc_toolchain_config.bzl @@ -35,6 +35,12 @@ def cc_toolchain_config( "-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247 "-fstack-protector", "-fno-omit-frame-pointer", + # Math + # This controls whether the compiler allows contracting floating point operations. + # It was flipped from off to on in clang-14. + # For now we'd like to preserve the legacy behavior. + # See [BUILD-666] for more details. + "-ffp-contract=off", # Diagnostics "-fcolor-diagnostics", "-Wall", @@ -91,6 +97,7 @@ def cc_toolchain_config( # https://developer.apple.com/forums/thread/719961 "-undefined", "dynamic_lookup", + "-Wl,-no_fixup_chains", ]) else: use_lld = True diff --git a/cc/toolchains/llvm/llvm.BUILD.bzl b/cc/toolchains/llvm/llvm.BUILD.bzl index 3c175d51..413d4c69 100755 --- a/cc/toolchains/llvm/llvm.BUILD.bzl +++ b/cc/toolchains/llvm/llvm.BUILD.bzl @@ -30,9 +30,11 @@ filegroup( filegroup( name = "ld", - srcs = [ + # Hack to workaround us not building lld + # for aarch64-darwin. + srcs = glob([ "bin/ld.lld", - ], + ]), ) filegroup( diff --git a/clang_format/BUILD.bazel b/clang_format/BUILD.bazel index 466add66..cb1b024c 100644 --- a/clang_format/BUILD.bazel +++ b/clang_format/BUILD.bazel @@ -19,6 +19,7 @@ choose_clang_format( filegroup( name = "_clang_format_bin", srcs = select({ + "//platforms:aarch64-darwin": ["@aarch64-darwin-llvm//: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"],