From ddb70013e4cec8b7b1d42e7e4316a4ce5c8b5714 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Fri, 29 Nov 2024 21:48:09 +0100 Subject: [PATCH 1/6] vendor large parts of @bazel_tools//tools/cpp into @rules_nixpkgs_core//private/cc_toolchain Most of @bazel_tools//tools/cpp was removed from @bazel_tools and moved into a private package in rules_cc in Bazel 8. --- core/BUILD.bazel | 2 +- core/private/BUILD.bazel | 0 core/private/cc_toolchain/BUILD.bazel | 38 + core/private/cc_toolchain/BUILD.tpl | 144 ++ core/private/cc_toolchain/README.md | 4 + .../armeabi_cc_toolchain_config.bzl | 83 + .../cc_toolchain/cc_toolchain_config.bzl | 1491 +++++++++++++ .../generate_system_module_map.sh | 35 + .../private/cc_toolchain/lib_cc_configure.bzl | 142 ++ .../cc_toolchain/linux_cc_wrapper.sh.tpl | 54 + .../cc_toolchain/osx_cc_wrapper.sh.tpl | 138 ++ .../cc_toolchain/unix_cc_toolchain_config.bzl | 1942 +++++++++++++++++ core/private/get_cpu_value.bzl | 48 - core/util.bzl | 2 +- nixpkgs/nixpkgs.bzl | 2 +- toolchains/cc/cc.bzl | 20 +- toolchains/java/java.bzl | 2 +- toolchains/posix/posix.bzl | 2 +- 18 files changed, 4085 insertions(+), 64 deletions(-) create mode 100644 core/private/BUILD.bazel create mode 100644 core/private/cc_toolchain/BUILD.bazel create mode 100644 core/private/cc_toolchain/BUILD.tpl create mode 100644 core/private/cc_toolchain/README.md create mode 100644 core/private/cc_toolchain/armeabi_cc_toolchain_config.bzl create mode 100644 core/private/cc_toolchain/cc_toolchain_config.bzl create mode 100755 core/private/cc_toolchain/generate_system_module_map.sh create mode 100644 core/private/cc_toolchain/lib_cc_configure.bzl create mode 100644 core/private/cc_toolchain/linux_cc_wrapper.sh.tpl create mode 100644 core/private/cc_toolchain/osx_cc_wrapper.sh.tpl create mode 100644 core/private/cc_toolchain/unix_cc_toolchain_config.bzl delete mode 100644 core/private/get_cpu_value.bzl diff --git a/core/BUILD.bazel b/core/BUILD.bazel index f19c53c0b..ed7c2de43 100644 --- a/core/BUILD.bazel +++ b/core/BUILD.bazel @@ -24,9 +24,9 @@ bzl_library( bzl_library( name = "nixpkgs", srcs = [ + "//private/cc_toolchain:srcs", "nixpkgs.bzl", "util.bzl", - "private/get_cpu_value.bzl", ], deps = [ ":bazel_tools", diff --git a/core/private/BUILD.bazel b/core/private/BUILD.bazel new file mode 100644 index 000000000..e69de29bb diff --git a/core/private/cc_toolchain/BUILD.bazel b/core/private/cc_toolchain/BUILD.bazel new file mode 100644 index 000000000..62b78f907 --- /dev/null +++ b/core/private/cc_toolchain/BUILD.bazel @@ -0,0 +1,38 @@ +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # Apache 2.0 + +filegroup( + name = "empty", + srcs = [], +) + +filegroup( + name = "bzl_srcs", + srcs = glob(["**/*.bzl"]), +) + +filegroup( + name = "srcs", + srcs = glob(["**"]), +) + +filegroup( + name = "lib_cc_configure", + srcs = ["lib_cc_configure.bzl"], +) diff --git a/core/private/cc_toolchain/BUILD.tpl b/core/private/cc_toolchain/BUILD.tpl new file mode 100644 index 000000000..99896dc93 --- /dev/null +++ b/core/private/cc_toolchain/BUILD.tpl @@ -0,0 +1,144 @@ +# Copyright 2016 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This becomes the BUILD file for @local_config_cc// under non-BSD unixes. + +load(":cc_toolchain_config.bzl", "cc_toolchain_config") +load(":armeabi_cc_toolchain_config.bzl", "armeabi_cc_toolchain_config") +load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite") + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # Apache 2.0 + +cc_library(name = "empty_lib") + +# Label flag for extra libraries to be linked into every binary. +# TODO(bazel-team): Support passing flag multiple times to build a list. +label_flag( + name = "link_extra_libs", + build_setting_default = ":empty_lib", +) + +# The final extra library to be linked into every binary target. This collects +# the above flag, but may also include more libraries depending on config. +cc_library( + name = "link_extra_lib", + deps = [ + ":link_extra_libs", + ], +) + +cc_library( + name = "malloc", +) + +filegroup( + name = "empty", + srcs = [], +) + +filegroup( + name = "cc_wrapper", + srcs = ["cc_wrapper.sh"], +) + +filegroup( + name = "validate_static_library", + srcs = ["validate_static_library.sh"], +) + +filegroup( + name = "deps_scanner_wrapper", + srcs = ["deps_scanner_wrapper.sh"], +) +filegroup( + name = "compiler_deps", + srcs = glob(["extra_tools/**"], allow_empty = True) + [%{cc_compiler_deps}], +) + +# This is the entry point for --crosstool_top. Toolchains are found +# by lopping off the name of --crosstool_top and searching for +# the "${CPU}" entry in the toolchains attribute. +cc_toolchain_suite( + name = "toolchain", + toolchains = { + "%{name}|%{compiler}": ":cc-compiler-%{name}", + "%{name}": ":cc-compiler-%{name}", + "armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a", + "armeabi-v7a": ":cc-compiler-armeabi-v7a", + }, +) + +cc_toolchain( + name = "cc-compiler-%{name}", + toolchain_identifier = "%{cc_toolchain_identifier}", + toolchain_config = ":%{cc_toolchain_identifier}", + all_files = ":compiler_deps", + ar_files = ":compiler_deps", + as_files = ":compiler_deps", + compiler_files = ":compiler_deps", + dwp_files = ":empty", + linker_files = ":compiler_deps", + objcopy_files = ":empty", + strip_files = ":empty", + supports_header_parsing = 1, + supports_param_files = 1, + module_map = %{modulemap}, +) + +cc_toolchain_config( + name = "%{cc_toolchain_identifier}", + cpu = "%{target_cpu}", + compiler = "%{compiler}", + toolchain_identifier = "%{cc_toolchain_identifier}", + host_system_name = "%{host_system_name}", + target_system_name = "%{target_system_name}", + target_libc = "%{target_libc}", + abi_version = "%{abi_version}", + abi_libc_version = "%{abi_libc_version}", + cxx_builtin_include_directories = [%{cxx_builtin_include_directories}], + tool_paths = {%{tool_paths}}, + compile_flags = [%{compile_flags}], + opt_compile_flags = [%{opt_compile_flags}], + dbg_compile_flags = [%{dbg_compile_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + link_libs = [%{link_libs}], + opt_link_flags = [%{opt_link_flags}], + unfiltered_compile_flags = [%{unfiltered_compile_flags}], + coverage_compile_flags = [%{coverage_compile_flags}], + coverage_link_flags = [%{coverage_link_flags}], + supports_start_end_lib = %{supports_start_end_lib}, + extra_flags_per_feature = %{extra_flags_per_feature}, +) + +# Android tooling requires a default toolchain for the armeabi-v7a cpu. +cc_toolchain( + name = "cc-compiler-armeabi-v7a", + toolchain_identifier = "stub_armeabi-v7a", + toolchain_config = ":stub_armeabi-v7a", + all_files = ":empty", + ar_files = ":empty", + as_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 1, +) + +armeabi_cc_toolchain_config(name = "stub_armeabi-v7a") diff --git a/core/private/cc_toolchain/README.md b/core/private/cc_toolchain/README.md new file mode 100644 index 000000000..5a6870e21 --- /dev/null +++ b/core/private/cc_toolchain/README.md @@ -0,0 +1,4 @@ +# Vendored files from `@bazel_tools//tools/cpp` that were removed in Bazel 8 + +This directory contains files that rules_nixpkgs depends on that once lived in `@bazel_tools`. +Do not add other files here unless needed. \ No newline at end of file diff --git a/core/private/cc_toolchain/armeabi_cc_toolchain_config.bzl b/core/private/cc_toolchain/armeabi_cc_toolchain_config.bzl new file mode 100644 index 000000000..7d4baadc4 --- /dev/null +++ b/core/private/cc_toolchain/armeabi_cc_toolchain_config.bzl @@ -0,0 +1,83 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A Starlark cc_toolchain configuration rule""" + +load( + "@rules_cc//cc:cc_toolchain_config_lib.bzl", + "feature", + "tool_path", +) + +def _impl(ctx): + toolchain_identifier = "stub_armeabi-v7a" + host_system_name = "armeabi-v7a" + target_system_name = "armeabi-v7a" + target_cpu = "armeabi-v7a" + target_libc = "armeabi-v7a" + compiler = "compiler" + abi_version = "armeabi-v7a" + abi_libc_version = "armeabi-v7a" + cc_target_os = None + builtin_sysroot = None + action_configs = [] + + supports_pic_feature = feature(name = "supports_pic", enabled = True) + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + features = [supports_dynamic_linker_feature, supports_pic_feature] + + cxx_builtin_include_directories = [] + artifact_name_patterns = [] + make_variables = [] + + tool_paths = [ + tool_path(name = "ar", path = "/bin/false"), + tool_path(name = "compat-ld", path = "/bin/false"), + tool_path(name = "cpp", path = "/bin/false"), + tool_path(name = "dwp", path = "/bin/false"), + tool_path(name = "gcc", path = "/bin/false"), + tool_path(name = "gcov", path = "/bin/false"), + tool_path(name = "ld", path = "/bin/false"), + tool_path(name = "llvm-profdata", path = "/bin/false"), + tool_path(name = "nm", path = "/bin/false"), + tool_path(name = "objcopy", path = "/bin/false"), + tool_path(name = "objdump", path = "/bin/false"), + tool_path(name = "strip", path = "/bin/false"), + ] + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = action_configs, + artifact_name_patterns = artifact_name_patterns, + cxx_builtin_include_directories = cxx_builtin_include_directories, + toolchain_identifier = toolchain_identifier, + host_system_name = host_system_name, + target_system_name = target_system_name, + target_cpu = target_cpu, + target_libc = target_libc, + compiler = compiler, + abi_version = abi_version, + abi_libc_version = abi_libc_version, + tool_paths = tool_paths, + make_variables = make_variables, + builtin_sysroot = builtin_sysroot, + cc_target_os = cc_target_os, + ) + +armeabi_cc_toolchain_config = rule( + implementation = _impl, + attrs = {}, + provides = [CcToolchainConfigInfo], +) diff --git a/core/private/cc_toolchain/cc_toolchain_config.bzl b/core/private/cc_toolchain/cc_toolchain_config.bzl new file mode 100644 index 000000000..265fce6a4 --- /dev/null +++ b/core/private/cc_toolchain/cc_toolchain_config.bzl @@ -0,0 +1,1491 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A Starlark cc_toolchain configuration rule""" + +load( + "@rules_cc//cc:action_names.bzl", + _ASSEMBLE_ACTION_NAME = "ASSEMBLE_ACTION_NAME", + _CLIF_MATCH_ACTION_NAME = "CLIF_MATCH_ACTION_NAME", + _CPP_COMPILE_ACTION_NAME = "CPP_COMPILE_ACTION_NAME", + _CPP_HEADER_PARSING_ACTION_NAME = "CPP_HEADER_PARSING_ACTION_NAME", + _CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME = "CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME", + _CPP_LINK_EXECUTABLE_ACTION_NAME = "CPP_LINK_EXECUTABLE_ACTION_NAME", + _CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME = "CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME", + _CPP_MODULE_CODEGEN_ACTION_NAME = "CPP_MODULE_CODEGEN_ACTION_NAME", + _CPP_MODULE_COMPILE_ACTION_NAME = "CPP_MODULE_COMPILE_ACTION_NAME", + _C_COMPILE_ACTION_NAME = "C_COMPILE_ACTION_NAME", + _LINKSTAMP_COMPILE_ACTION_NAME = "LINKSTAMP_COMPILE_ACTION_NAME", + _LTO_BACKEND_ACTION_NAME = "LTO_BACKEND_ACTION_NAME", + _PREPROCESS_ASSEMBLE_ACTION_NAME = "PREPROCESS_ASSEMBLE_ACTION_NAME", +) +load( + "@rules_cc//cc:cc_toolchain_config_lib.bzl", + "action_config", + "feature", + "flag_group", + "flag_set", + "tool", + "tool_path", + "with_feature_set", +) + +all_compile_actions = [ + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, +] + +all_cpp_compile_actions = [ + _CPP_COMPILE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, +] + +preprocessor_compile_actions = [ + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, +] + +codegen_compile_actions = [ + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, +] + +all_link_actions = [ + _CPP_LINK_EXECUTABLE_ACTION_NAME, + _CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME, + _CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME, +] + +def _impl(ctx): + if ctx.attr.disable_static_cc_toolchains: + fail("@rules_cc//cc/private/toolchain:default-toolchain, as well as the cc_toolchains it points " + + "to have been removed. See https://github.com/bazelbuild/bazel/issues/8546.") + + if (ctx.attr.cpu == "darwin"): + toolchain_identifier = "local_darwin" + elif (ctx.attr.cpu == "freebsd"): + toolchain_identifier = "local_freebsd" + elif (ctx.attr.cpu == "local"): + toolchain_identifier = "local_linux" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"): + toolchain_identifier = "local_windows_clang" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw"): + toolchain_identifier = "local_windows_mingw" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + toolchain_identifier = "local_windows_msys64" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + toolchain_identifier = "local_windows_msys64_mingw64" + elif (ctx.attr.cpu == "armeabi-v7a"): + toolchain_identifier = "stub_armeabi-v7a" + elif (ctx.attr.cpu == "x64_windows_msvc"): + toolchain_identifier = "vc_14_0_x64" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + host_system_name = "armeabi-v7a" + elif (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows" or + ctx.attr.cpu == "x64_windows_msvc"): + host_system_name = "local" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + target_system_name = "armeabi-v7a" + elif (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows" or + ctx.attr.cpu == "x64_windows_msvc"): + target_system_name = "local" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + target_cpu = "armeabi-v7a" + elif (ctx.attr.cpu == "darwin"): + target_cpu = "darwin" + elif (ctx.attr.cpu == "freebsd"): + target_cpu = "freebsd" + elif (ctx.attr.cpu == "local"): + target_cpu = "local" + elif (ctx.attr.cpu == "x64_windows"): + target_cpu = "x64_windows" + elif (ctx.attr.cpu == "x64_windows_msvc"): + target_cpu = "x64_windows_msvc" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + target_libc = "armeabi-v7a" + elif (ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows"): + target_libc = "local" + elif (ctx.attr.cpu == "darwin"): + target_libc = "macosx" + elif (ctx.attr.cpu == "x64_windows_msvc"): + target_libc = "msvcrt140" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "x64_windows_msvc"): + compiler = "cl" + elif (ctx.attr.cpu == "armeabi-v7a" or + ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local"): + compiler = "compiler" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"): + compiler = "windows_clang" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw"): + compiler = "windows_mingw" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + compiler = "windows_msys64" + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + compiler = "windows_msys64_mingw64" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + abi_version = "armeabi-v7a" + elif (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows" or + ctx.attr.cpu == "x64_windows_msvc"): + abi_version = "local" + else: + fail("Unreachable") + + if (ctx.attr.cpu == "armeabi-v7a"): + abi_libc_version = "armeabi-v7a" + elif (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows" or + ctx.attr.cpu == "x64_windows_msvc"): + abi_libc_version = "local" + else: + fail("Unreachable") + + cc_target_os = None + + builtin_sysroot = None + + objcopy_embed_data_action = None + if (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local"): + objcopy_embed_data_action = action_config( + action_name = "objcopy_embed_data", + enabled = True, + tools = [tool(path = "/usr/bin/objcopy")], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"): + objcopy_embed_data_action = action_config( + action_name = "objcopy_embed_data", + enabled = True, + tools = [tool(path = "C:/Program Files (x86)/LLVM/bin/objcopy")], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw"): + objcopy_embed_data_action = action_config( + action_name = "objcopy_embed_data", + enabled = True, + tools = [tool(path = "C:/mingw/bin/objcopy")], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + objcopy_embed_data_action = action_config( + action_name = "objcopy_embed_data", + enabled = True, + tools = [tool(path = "C:/tools/msys64/mingw64/bin/objcopy")], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + objcopy_embed_data_action = action_config( + action_name = "objcopy_embed_data", + enabled = True, + tools = [tool(path = "C:/tools/msys64/usr/bin/objcopy")], + ) + + c_compile_action = action_config( + action_name = _C_COMPILE_ACTION_NAME, + implies = [ + "compiler_input_flags", + "compiler_output_flags", + "default_compile_flags", + "user_compile_flags", + "sysroot", + "unfiltered_compile_flags", + ], + tools = [tool(path = "wrapper/bin/msvc_cl.bat")], + ) + + cpp_compile_action = action_config( + action_name = _CPP_COMPILE_ACTION_NAME, + implies = [ + "compiler_input_flags", + "compiler_output_flags", + "default_compile_flags", + "user_compile_flags", + "sysroot", + "unfiltered_compile_flags", + ], + tools = [tool(path = "wrapper/bin/msvc_cl.bat")], + ) + + if (ctx.attr.cpu == "armeabi-v7a"): + action_configs = [] + elif (ctx.attr.cpu == "x64_windows_msvc"): + action_configs = [c_compile_action, cpp_compile_action] + elif (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows"): + action_configs = [objcopy_embed_data_action] + else: + fail("Unreachable") + + random_seed_feature = feature(name = "random_seed", enabled = True) + + compiler_output_flags_feature = feature( + name = "compiler_output_flags", + flag_sets = [ + flag_set( + actions = [_ASSEMBLE_ACTION_NAME], + flag_groups = [ + flag_group( + flags = ["/Fo%{output_file}", "/Zi"], + expand_if_available = "output_file", + expand_if_not_available = "output_assembly_file", + ), + ], + ), + flag_set( + actions = [ + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/Fo%{output_file}"], + expand_if_available = "output_file", + expand_if_not_available = "output_assembly_file", + ), + flag_group( + flags = ["/Fa%{output_file}"], + expand_if_available = "output_file", + ), + flag_group( + flags = ["/P", "/Fi%{output_file}"], + expand_if_available = "output_file", + ), + ], + ), + ], + ) + + default_link_flags_feature = None + if (ctx.attr.cpu == "local"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-Wl,-z,relro,-z,now", + "-no-canonical-prefixes", + "-pass-exit-codes", + ], + ), + ], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])], + with_features = [with_feature_set(features = ["opt"])], + ), + ], + ) + elif (ctx.attr.cpu == "freebsd"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-Wl,-z,relro,-z,now", + "-no-canonical-prefixes", + ], + ), + ], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])], + with_features = [with_feature_set(features = ["opt"])], + ), + ], + ) + elif (ctx.attr.cpu == "darwin"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-undefined", + "dynamic_lookup", + "-headerpad_max_install_names", + "-no-canonical-prefixes", + ], + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-lstdc++"])], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows_msvc"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-m64"])], + ), + ], + ) + + unfiltered_compile_flags_feature = None + if (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd"): + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-no-canonical-prefixes", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ], + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "local"): + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-no-canonical-prefixes", + "-fno-canonical-system-headers", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ], + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows_msvc"): + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["%{unfiltered_compile_flags}"], + iterate_over = "unfiltered_compile_flags", + expand_if_available = "unfiltered_compile_flags", + ), + ], + ), + ], + ) + + supports_pic_feature = feature(name = "supports_pic", enabled = True) + + default_compile_flags_feature = None + if (ctx.attr.cpu == "darwin"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-D_FORTIFY_SOURCE=1", + "-fstack-protector", + "-fcolor-diagnostics", + "-Wall", + "-Wthread-safety", + "-Wself-assign", + "-fno-omit-frame-pointer", + ], + ), + ], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-g"])], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-g0", + "-O2", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + ), + ], + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = [ + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-std=c++0x"])], + ), + ], + ) + elif (ctx.attr.cpu == "local"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-U_FORTIFY_SOURCE", + "-D_FORTIFY_SOURCE=1", + "-fstack-protector", + "-Wall", + "-Wunused-but-set-parameter", + "-Wno-free-nonheap-object", + "-fno-omit-frame-pointer", + ], + ), + ], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-g"])], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-g0", + "-O2", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + ), + ], + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = [ + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-std=c++0x"])], + ), + ], + ) + elif (ctx.attr.cpu == "freebsd"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-U_FORTIFY_SOURCE", + "-D_FORTIFY_SOURCE=1", + "-fstack-protector", + "-Wall", + "-fno-omit-frame-pointer", + ], + ), + ], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-g"])], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-g0", + "-O2", + "-DNDEBUG", + "-ffunction-sections", + "-fdata-sections", + ], + ), + ], + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = [ + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-std=c++0x"])], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows_msvc"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = [ + "-m64", + "/D__inline__=__inline", + "/DCOMPILER_MSVC", + "/DNOGDI", + "/DNOMINMAX", + "/DPRAGMA_SUPPORTED", + "/D_WIN32_WINNT=0x0601", + "/D_CRT_SECURE_NO_DEPRECATE", + "/D_CRT_SECURE_NO_WARNINGS", + "/D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS", + "/D_USE_MATH_DEFINES", + "/nologo", + "/bigobj", + "/Zm500", + "/J", + "/Gy", + "/GF", + "/W3", + "/EHsc", + "/wd4351", + "/wd4291", + "/wd4250", + "/wd4996", + ], + ), + ], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/DDEBUG=1", "-g", "/Od", "-Xcompilation-mode=dbg"], + ), + ], + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/DNDEBUG", "/Od", "-Xcompilation-mode=fastbuild"], + ), + ], + with_features = [with_feature_set(features = ["fastbuild"])], + ), + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/DNDEBUG", "/O2", "-Xcompilation-mode=opt"], + ), + ], + with_features = [with_feature_set(features = ["opt"])], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang" or + ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw" or + ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-std=c++0x"])], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _LINKSTAMP_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [flag_group(flags = ["-std=gnu++0x"])], + ), + ], + ) + + opt_feature = feature(name = "opt") + + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + + objcopy_embed_flags_feature = feature( + name = "objcopy_embed_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = ["objcopy_embed_data"], + flag_groups = [flag_group(flags = ["-I", "binary"])], + ), + ], + ) + + dbg_feature = feature(name = "dbg") + + user_compile_flags_feature = None + if (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local"): + user_compile_flags_feature = feature( + name = "user_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows_msvc"): + user_compile_flags_feature = feature( + name = "user_compile_flags", + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + + sysroot_feature = None + if (ctx.attr.cpu == "darwin" or + ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local"): + sysroot_feature = feature( + name = "sysroot", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _LINKSTAMP_COMPILE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _LTO_BACKEND_ACTION_NAME, + _CLIF_MATCH_ACTION_NAME, + _CPP_LINK_EXECUTABLE_ACTION_NAME, + _CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME, + _CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "x64_windows_msvc"): + sysroot_feature = feature( + name = "sysroot", + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + _CPP_LINK_EXECUTABLE_ACTION_NAME, + _CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME, + _CPP_LINK_NODEPS_DYNAMIC_LIBRARY_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + iterate_over = "sysroot", + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + + include_paths_feature = feature( + name = "include_paths", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/I%{quote_include_paths}"], + iterate_over = "quote_include_paths", + ), + flag_group( + flags = ["/I%{include_paths}"], + iterate_over = "include_paths", + ), + flag_group( + flags = ["/I%{system_include_paths}"], + iterate_over = "system_include_paths", + ), + ], + ), + ], + ) + + dependency_file_feature = feature( + name = "dependency_file", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/DEPENDENCY_FILE", "%{dependency_file}"], + expand_if_available = "dependency_file", + ), + ], + ), + ], + ) + + compiler_input_flags_feature = feature( + name = "compiler_input_flags", + flag_sets = [ + flag_set( + actions = [ + _ASSEMBLE_ACTION_NAME, + _PREPROCESS_ASSEMBLE_ACTION_NAME, + _C_COMPILE_ACTION_NAME, + _CPP_COMPILE_ACTION_NAME, + _CPP_HEADER_PARSING_ACTION_NAME, + _CPP_MODULE_COMPILE_ACTION_NAME, + _CPP_MODULE_CODEGEN_ACTION_NAME, + ], + flag_groups = [ + flag_group( + flags = ["/c", "%{source_file}"], + expand_if_available = "source_file", + ), + ], + ), + ], + ) + + fastbuild_feature = feature(name = "fastbuild") + + features = None + if (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + features = [ + default_compile_flags_feature, + default_link_flags_feature, + supports_dynamic_linker_feature, + objcopy_embed_flags_feature, + ] + elif (ctx.attr.cpu == "darwin"): + features = [ + default_compile_flags_feature, + default_link_flags_feature, + supports_dynamic_linker_feature, + supports_pic_feature, + objcopy_embed_flags_feature, + dbg_feature, + opt_feature, + user_compile_flags_feature, + sysroot_feature, + unfiltered_compile_flags_feature, + ] + elif (ctx.attr.cpu == "freebsd" or + ctx.attr.cpu == "local"): + features = [ + default_compile_flags_feature, + default_link_flags_feature, + supports_dynamic_linker_feature, + supports_pic_feature, + objcopy_embed_flags_feature, + opt_feature, + dbg_feature, + user_compile_flags_feature, + sysroot_feature, + unfiltered_compile_flags_feature, + ] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang" or + ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw" or + ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + features = [ + default_compile_flags_feature, + supports_dynamic_linker_feature, + objcopy_embed_flags_feature, + ] + elif (ctx.attr.cpu == "x64_windows_msvc"): + features = [ + default_link_flags_feature, + random_seed_feature, + default_compile_flags_feature, + include_paths_feature, + dependency_file_feature, + user_compile_flags_feature, + sysroot_feature, + unfiltered_compile_flags_feature, + compiler_output_flags_feature, + compiler_input_flags_feature, + dbg_feature, + fastbuild_feature, + opt_feature, + ] + elif (ctx.attr.cpu == "armeabi-v7a"): + features = [supports_dynamic_linker_feature, supports_pic_feature] + + if (ctx.attr.cpu == "armeabi-v7a"): + cxx_builtin_include_directories = [] + elif (ctx.attr.cpu == "darwin"): + cxx_builtin_include_directories = ["/"] + elif (ctx.attr.cpu == "freebsd"): + cxx_builtin_include_directories = ["/usr/lib/clang", "/usr/local/include", "/usr/include"] + elif (ctx.attr.cpu == "local" or + ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"): + cxx_builtin_include_directories = ["/usr/lib/gcc/", "/usr/local/include", "/usr/include"] + elif (ctx.attr.cpu == "x64_windows_msvc"): + cxx_builtin_include_directories = [ + "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/INCLUDE", + "C:/Program Files (x86)/Windows Kits/10/include/", + "C:/Program Files (x86)/Windows Kits/8.1/include/", + "C:/Program Files (x86)/GnuWin32/include/", + "C:/python_27_amd64/files/include", + ] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw"): + cxx_builtin_include_directories = ["C:/mingw/include", "C:/mingw/lib/gcc"] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + cxx_builtin_include_directories = ["C:/tools/msys64/mingw64/x86_64-w64-mingw32/include"] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + cxx_builtin_include_directories = ["C:/tools/msys64/", "/usr/"] + else: + fail("Unreachable") + + artifact_name_patterns = [] + + make_variables = [] + + if (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64_mingw64"): + tool_paths = [ + tool_path( + name = "ar", + path = "C:/tools/msys64/mingw64/bin/ar", + ), + tool_path( + name = "compat-ld", + path = "C:/tools/msys64/mingw64/bin/ld", + ), + tool_path( + name = "cpp", + path = "C:/tools/msys64/mingw64/bin/cpp", + ), + tool_path( + name = "dwp", + path = "C:/tools/msys64/mingw64/bin/dwp", + ), + tool_path( + name = "gcc", + path = "C:/tools/msys64/mingw64/bin/gcc", + ), + tool_path( + name = "gcov", + path = "C:/tools/msys64/mingw64/bin/gcov", + ), + tool_path( + name = "ld", + path = "C:/tools/msys64/mingw64/bin/ld", + ), + tool_path( + name = "nm", + path = "C:/tools/msys64/mingw64/bin/nm", + ), + tool_path( + name = "objcopy", + path = "C:/tools/msys64/mingw64/bin/objcopy", + ), + tool_path( + name = "objdump", + path = "C:/tools/msys64/mingw64/bin/objdump", + ), + tool_path( + name = "strip", + path = "C:/tools/msys64/mingw64/bin/strip", + ), + ] + elif (ctx.attr.cpu == "armeabi-v7a"): + tool_paths = [ + tool_path(name = "ar", path = "/bin/false"), + tool_path(name = "compat-ld", path = "/bin/false"), + tool_path(name = "cpp", path = "/bin/false"), + tool_path(name = "dwp", path = "/bin/false"), + tool_path(name = "gcc", path = "/bin/false"), + tool_path(name = "gcov", path = "/bin/false"), + tool_path(name = "ld", path = "/bin/false"), + tool_path(name = "nm", path = "/bin/false"), + tool_path(name = "objcopy", path = "/bin/false"), + tool_path(name = "objdump", path = "/bin/false"), + tool_path(name = "strip", path = "/bin/false"), + ] + elif (ctx.attr.cpu == "freebsd"): + tool_paths = [ + tool_path(name = "ar", path = "/usr/bin/ar"), + tool_path(name = "compat-ld", path = "/usr/bin/ld"), + tool_path(name = "cpp", path = "/usr/bin/cpp"), + tool_path(name = "dwp", path = "/usr/bin/dwp"), + tool_path(name = "gcc", path = "/usr/bin/clang"), + tool_path(name = "gcov", path = "/usr/bin/gcov"), + tool_path(name = "ld", path = "/usr/bin/ld"), + tool_path(name = "nm", path = "/usr/bin/nm"), + tool_path(name = "objcopy", path = "/usr/bin/objcopy"), + tool_path(name = "objdump", path = "/usr/bin/objdump"), + tool_path(name = "strip", path = "/usr/bin/strip"), + ] + elif (ctx.attr.cpu == "local"): + tool_paths = [ + tool_path(name = "ar", path = "/usr/bin/ar"), + tool_path(name = "compat-ld", path = "/usr/bin/ld"), + tool_path(name = "cpp", path = "/usr/bin/cpp"), + tool_path(name = "dwp", path = "/usr/bin/dwp"), + tool_path(name = "gcc", path = "/usr/bin/gcc"), + tool_path(name = "gcov", path = "/usr/bin/gcov"), + tool_path(name = "ld", path = "/usr/bin/ld"), + tool_path(name = "nm", path = "/usr/bin/nm"), + tool_path(name = "objcopy", path = "/usr/bin/objcopy"), + tool_path(name = "objdump", path = "/usr/bin/objdump"), + tool_path(name = "strip", path = "/usr/bin/strip"), + ] + elif (ctx.attr.cpu == "darwin"): + tool_paths = [ + tool_path(name = "ar", path = "/usr/bin/libtool"), + tool_path(name = "compat-ld", path = "/usr/bin/ld"), + tool_path(name = "cpp", path = "/usr/bin/cpp"), + tool_path(name = "dwp", path = "/usr/bin/dwp"), + tool_path(name = "gcc", path = "osx_cc_wrapper.sh"), + tool_path(name = "gcov", path = "/usr/bin/gcov"), + tool_path(name = "ld", path = "/usr/bin/ld"), + tool_path(name = "nm", path = "/usr/bin/nm"), + tool_path(name = "objcopy", path = "/usr/bin/objcopy"), + tool_path(name = "objdump", path = "/usr/bin/objdump"), + tool_path(name = "strip", path = "/usr/bin/strip"), + ] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_clang"): + tool_paths = [ + tool_path(name = "ar", path = "C:/mingw/bin/ar"), + tool_path( + name = "compat-ld", + path = "C:/Program Files (x86)/LLVM/bin/ld", + ), + tool_path( + name = "cpp", + path = "C:/Program Files (x86)/LLVM/bin/cpp", + ), + tool_path( + name = "dwp", + path = "C:/Program Files (x86)/LLVM/bin/dwp", + ), + tool_path( + name = "gcc", + path = "C:/Program Files (x86)/LLVM/bin/clang", + ), + tool_path( + name = "gcov", + path = "C:/Program Files (x86)/LLVM/bin/gcov", + ), + tool_path( + name = "ld", + path = "C:/Program Files (x86)/LLVM/bin/ld", + ), + tool_path( + name = "nm", + path = "C:/Program Files (x86)/LLVM/bin/nm", + ), + tool_path( + name = "objcopy", + path = "C:/Program Files (x86)/LLVM/bin/objcopy", + ), + tool_path( + name = "objdump", + path = "C:/Program Files (x86)/LLVM/bin/objdump", + ), + tool_path( + name = "strip", + path = "C:/Program Files (x86)/LLVM/bin/strip", + ), + ] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_mingw"): + tool_paths = [ + tool_path(name = "ar", path = "C:/mingw/bin/ar"), + tool_path(name = "compat-ld", path = "C:/mingw/bin/ld"), + tool_path(name = "cpp", path = "C:/mingw/bin/cpp"), + tool_path(name = "dwp", path = "C:/mingw/bin/dwp"), + tool_path(name = "gcc", path = "C:/mingw/bin/gcc"), + tool_path(name = "gcov", path = "C:/mingw/bin/gcov"), + tool_path(name = "ld", path = "C:/mingw/bin/ld"), + tool_path(name = "nm", path = "C:/mingw/bin/nm"), + tool_path(name = "objcopy", path = "C:/mingw/bin/objcopy"), + tool_path(name = "objdump", path = "C:/mingw/bin/objdump"), + tool_path(name = "strip", path = "C:/mingw/bin/strip"), + ] + elif (ctx.attr.cpu == "x64_windows" and ctx.attr.compiler == "windows_msys64"): + tool_paths = [ + tool_path(name = "ar", path = "C:/tools/msys64/usr/bin/ar"), + tool_path( + name = "compat-ld", + path = "C:/tools/msys64/usr/bin/ld", + ), + tool_path( + name = "cpp", + path = "C:/tools/msys64/usr/bin/cpp", + ), + tool_path( + name = "dwp", + path = "C:/tools/msys64/usr/bin/dwp", + ), + tool_path( + name = "gcc", + path = "C:/tools/msys64/usr/bin/gcc", + ), + tool_path( + name = "gcov", + path = "C:/tools/msys64/usr/bin/gcov", + ), + tool_path(name = "ld", path = "C:/tools/msys64/usr/bin/ld"), + tool_path(name = "nm", path = "C:/tools/msys64/usr/bin/nm"), + tool_path( + name = "objcopy", + path = "C:/tools/msys64/usr/bin/objcopy", + ), + tool_path( + name = "objdump", + path = "C:/tools/msys64/usr/bin/objdump", + ), + tool_path( + name = "strip", + path = "C:/tools/msys64/usr/bin/strip", + ), + ] + elif (ctx.attr.cpu == "x64_windows_msvc"): + tool_paths = [ + tool_path(name = "ar", path = "wrapper/bin/msvc_link.bat"), + tool_path(name = "cpp", path = "wrapper/bin/msvc_cl.bat"), + tool_path(name = "gcc", path = "wrapper/bin/msvc_cl.bat"), + tool_path(name = "gcov", path = "wrapper/bin/msvc_nop.bat"), + tool_path(name = "ld", path = "wrapper/bin/msvc_link.bat"), + tool_path(name = "nm", path = "wrapper/bin/msvc_nop.bat"), + tool_path( + name = "objcopy", + path = "wrapper/bin/msvc_nop.bat", + ), + tool_path( + name = "objdump", + path = "wrapper/bin/msvc_nop.bat", + ), + tool_path( + name = "strip", + path = "wrapper/bin/msvc_nop.bat", + ), + ] + else: + fail("Unreachable") + + out = ctx.actions.declare_file(ctx.label.name) + ctx.actions.write(out, "Fake executable") + return [ + cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = action_configs, + artifact_name_patterns = artifact_name_patterns, + cxx_builtin_include_directories = cxx_builtin_include_directories, + toolchain_identifier = toolchain_identifier, + host_system_name = host_system_name, + target_system_name = target_system_name, + target_cpu = target_cpu, + target_libc = target_libc, + compiler = compiler, + abi_version = abi_version, + abi_libc_version = abi_libc_version, + tool_paths = tool_paths, + make_variables = make_variables, + builtin_sysroot = builtin_sysroot, + cc_target_os = cc_target_os, + ), + DefaultInfo( + executable = out, + ), + ] + +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "compiler": attr.string(), + "cpu": attr.string(mandatory = True), + "disable_static_cc_toolchains": attr.bool(), + }, + provides = [CcToolchainConfigInfo], + executable = True, +) diff --git a/core/private/cc_toolchain/generate_system_module_map.sh b/core/private/cc_toolchain/generate_system_module_map.sh new file mode 100755 index 000000000..deb52c2f6 --- /dev/null +++ b/core/private/cc_toolchain/generate_system_module_map.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# Copyright 2020 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu + +echo 'module "crosstool" [system] {' + +if [[ "$OSTYPE" == darwin* ]]; then + for dir in $@; do + find "$dir" -type f \( -name "*.h" -o -name "*.def" -o -path "*/c++/*" \) \ + | LANG=C sort -u | while read -r header; do + echo " textual header \"${header}\"" + done + done +else + for dir in $@; do + find -L "${dir}" -type f 2>/dev/null | LANG=C sort -u | while read -r header; do + echo " textual header \"${header}\"" + done + done +fi + +echo "}" diff --git a/core/private/cc_toolchain/lib_cc_configure.bzl b/core/private/cc_toolchain/lib_cc_configure.bzl new file mode 100644 index 000000000..38d976adf --- /dev/null +++ b/core/private/cc_toolchain/lib_cc_configure.bzl @@ -0,0 +1,142 @@ +# pylint: disable=g-bad-file-header +# Copyright 2016 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Base library for configuring the C++ toolchain. + +This file contains parts of `@bazel_tools//tools/cpp:lib_cc_configure.bzl` that are needed by rules_nixpkgs and that were removed from Bazel. +""" + + +load("@bazel_skylib//lib:versions.bzl", "versions") + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L108-L112 +def auto_configure_fail(msg): + """Output failure message when auto configuration fails.""" + red = "\033[0;31m" + no_color = "\033[0m" + fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg)) + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L114-L120 +def auto_configure_warning(msg): + """Output warning message during auto configuration.""" + yellow = "\033[1;33m" + no_color = "\033[0m" + + # buildifier: disable=print + print("\n%sAuto-Configuration Warning:%s %s\n" % (yellow, no_color, msg)) + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L122-L141 +def get_env_var(repository_ctx, name, default = None, enable_warning = True): + """Find an environment variable in system path. Doesn't %-escape the value! + + Args: + repository_ctx: The repository context. + name: Name of the environment variable. + default: Default value to be used when such environment variable is not present. + enable_warning: Show warning if the variable is not present. + Returns: + value of the environment variable or default. + """ + + if name in repository_ctx.os.environ: + return repository_ctx.os.environ[name] + if default != None: + if enable_warning: + auto_configure_warning("'%s' environment variable is not set, using '%s' as default" % (name, default)) + return default + auto_configure_fail("'%s' environment variable is not set" % name) + return None + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L271-L275 +def get_starlark_list(values): + """Convert a list of string into a string that can be passed to a rule attribute.""" + if not values: + return "" + return "\"" + "\",\n \"".join(values) + "\"" + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L225 +def get_cpu_value(repository_ctx): + """Compute the cpu_value based on the OS name. Doesn't %-escape the result! + + Args: + repository_ctx: The repository context. + Returns: + One of (darwin, freebsd, x64_windows, ppc, s390x, arm, aarch64, k8, piii) + """ + os_name = repository_ctx.os.name + arch = repository_ctx.os.arch + if os_name.startswith("mac os"): + # Check if we are on x86_64 or arm64 and return the corresponding cpu value. + if arch == "aarch64": + return "darwin_arm64" + + # NOTE(cb) for backward compatibility return "darwin" for Bazel < 7 on x86_64 + if versions.is_at_least("7.0.0", versions.get()): + return "darwin_x86_64" + else: + return "darwin" + + if os_name.find("freebsd") != -1: + return "freebsd" + if os_name.find("openbsd") != -1: + return "openbsd" + if os_name.find("windows") != -1: + if arch == "aarch64": + return "arm64_windows" + else: + return "x64_windows" + + if arch in ["power", "ppc64le", "ppc", "ppc64"]: + return "ppc" + if arch in ["s390x"]: + return "s390x" + if arch in ["mips64"]: + return "mips64" + if arch in ["riscv64"]: + return "riscv64" + if arch in ["arm", "armv7l"]: + return "arm" + if arch in ["aarch64"]: + return "aarch64" + return "k8" if arch in ["amd64", "x86_64", "x64"] else "piii" + +# symbol once lived in @bazel_tools and is now private within rules_cc +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L282-L303 +def write_builtin_include_directory_paths(repository_ctx, cc, directories, file_suffix = ""): + """Generate output file named 'builtin_include_directory_paths' in the root of the repository.""" + if get_env_var(repository_ctx, "BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS", "0", False) == "1": + repository_ctx.file( + "builtin_include_directory_paths" + file_suffix, + """This file is generated by cc_configure and normally contains builtin include directories +that C++ compiler reported. But because BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS was set to 1, +header include directory paths are intentionally not put there. +""", + ) + else: + repository_ctx.file( + "builtin_include_directory_paths" + file_suffix, + """This file is generated by cc_configure and contains builtin include directories +that %s reported. This file is a dependency of every compilation action and +changes to it will be reflected in the action cache key. When some of these +paths change, Bazel will make sure to rerun the action, even though none of +declared action inputs or the action commandline changes. + +%s +""" % (cc, "\n".join(directories)), + ) diff --git a/core/private/cc_toolchain/linux_cc_wrapper.sh.tpl b/core/private/cc_toolchain/linux_cc_wrapper.sh.tpl new file mode 100644 index 000000000..629741e55 --- /dev/null +++ b/core/private/cc_toolchain/linux_cc_wrapper.sh.tpl @@ -0,0 +1,54 @@ +#!/bin/bash +# +# Copyright 2015 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Ship the environment to the C++ action +# +set -eu + +OUTPUT= + +function parse_option() { + local -r opt="$1" + if [[ "${OUTPUT}" = "1" ]]; then + OUTPUT=$opt + elif [[ "$opt" = "-o" ]]; then + # output is coming + OUTPUT=1 + fi +} + +# let parse the option list +for i in "$@"; do + if [[ "$i" = @* && -r "${i:1}" ]]; then + while IFS= read -r opt + do + parse_option "$opt" + done < "${i:1}" || exit 1 + else + parse_option "$i" + fi +done + +# Set-up the environment +%{env} + +# Call the C++ compiler +%{cc} "$@" + +# Generate an empty file if header processing succeeded. +if [[ "${OUTPUT}" == *.h.processed ]]; then + echo -n > "${OUTPUT}" +fi diff --git a/core/private/cc_toolchain/osx_cc_wrapper.sh.tpl b/core/private/cc_toolchain/osx_cc_wrapper.sh.tpl new file mode 100644 index 000000000..4fbd546c4 --- /dev/null +++ b/core/private/cc_toolchain/osx_cc_wrapper.sh.tpl @@ -0,0 +1,138 @@ +#!/bin/bash +# +# Copyright 2015 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# OS X relpath is not really working. This is a wrapper script around gcc +# to simulate relpath behavior. +# +# This wrapper uses install_name_tool to replace all paths in the binary +# (bazel-out/.../path/to/original/library.so) by the paths relative to +# the binary. It parses the command line to behave as rpath is supposed +# to work. +# +# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac +# on how to set those paths for Mach-O binaries. +# +set -eu + +LIBS= +LIB_PATHS= +LIB_DIRS= +RPATHS= +OUTPUT= + +function parse_option() { + local -r opt="$1" + if [[ "${OUTPUT}" = "1" ]]; then + OUTPUT=$opt + elif [[ "$opt" =~ ^-l(.*)$ ]]; then + LIBS="${BASH_REMATCH[1]} $LIBS" + elif [[ "$opt" =~ ^(.*)\.so$ ]]; then + LIB_PATHS="${opt} $LIB_PATHS" + elif [[ "$opt" =~ ^(.*)\.dylib$ ]]; then + LIB_PATHS="${opt} $LIB_PATHS" + elif [[ "$opt" =~ ^-L(.*)$ ]]; then + LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS" + elif [[ "$opt" =~ ^\@loader_path/(.*)$ ]]; then + RPATHS="${BASH_REMATCH[1]} ${RPATHS}" + elif [[ "$opt" = "-o" ]]; then + # output is coming + OUTPUT=1 + fi +} + +# let parse the option list +for i in "$@"; do + if [[ "$i" = @* && -r "${i:1}" ]]; then + while IFS= read -r opt + do + parse_option "$opt" + done < "${i:1}" || exit 1 + else + parse_option "$i" + fi +done + +# Set-up the environment +%{env} + +# Call the C++ compiler +%{cc} "$@" + +# Generate an empty file if header processing succeeded. +if [[ "${OUTPUT}" == *.h.processed ]]; then + echo -n > "${OUTPUT}" +fi + +function get_library_path() { + for libdir in ${LIB_DIRS}; do + if [ -f ${libdir}/lib$1.so ]; then + echo "${libdir}/lib$1.so" + elif [ -f ${libdir}/lib$1.dylib ]; then + echo "${libdir}/lib$1.dylib" + fi + done +} + +# A convenient method to return the actual path even for non symlinks +# and multi-level symlinks. +function get_realpath() { + local previous="$1" + local next=$(readlink "${previous}") + while [ -n "${next}" ]; do + previous="${next}" + next=$(readlink "${previous}") + done + echo "${previous}" +} + +# Get the path of a lib inside a tool +function get_otool_path() { + # the lib path is the path of the original lib relative to the workspace + get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|' +} + +function call_install_name() { + /usr/bin/xcrun install_name_tool -change $(get_otool_path "$1") \ + "@loader_path/$2/$3" "${OUTPUT}" +} + +# Do replacements in the output +for rpath in ${RPATHS}; do + for lib in ${LIBS}; do + unset libname + if [ -f "$(/usr/bin/dirname ${OUTPUT})/${rpath}/lib${lib}.so" ]; then + libname="lib${lib}.so" + elif [ -f "$(/usr/bin/dirname ${OUTPUT})/${rpath}/lib${lib}.dylib" ]; then + libname="lib${lib}.dylib" + fi + # ${libname-} --> return $libname if defined, or undefined otherwise. This is to make + # this set -e friendly + if [[ -n "${libname-}" ]]; then + libpath=$(get_library_path ${lib}) + if [ -n "${libpath}" ]; then + call_install_name "${libpath}" "${rpath}" "${libname}" + fi + fi + done + for libpath in ${LIB_PATHS}; do + if [ -f "$libpath" ]; then + libname=$(/usr/bin/basename "$libpath") + if [ -f "$(/usr/bin/dirname ${OUTPUT})/${rpath}/${libname}" ]; then + call_install_name "${libpath}" "${rpath}" "${libname}" + fi + fi + done +done diff --git a/core/private/cc_toolchain/unix_cc_toolchain_config.bzl b/core/private/cc_toolchain/unix_cc_toolchain_config.bzl new file mode 100644 index 000000000..e500999bf --- /dev/null +++ b/core/private/cc_toolchain/unix_cc_toolchain_config.bzl @@ -0,0 +1,1942 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A Starlark cc_toolchain configuration rule""" + +load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") +load( + "@rules_cc//cc:cc_toolchain_config_lib.bzl", + "action_config", + "artifact_name_pattern", + "env_entry", + "env_set", + "feature", + "feature_set", + "flag_group", + "flag_set", + "tool", + "tool_path", + "variable_with_value", + "with_feature_set", +) + +def _target_os_version(ctx): + platform_type = ctx.fragments.apple.single_arch_platform.platform_type + xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] + return xcode_config.minimum_os_for_platform_type(platform_type) + +def layering_check_features(compiler, extra_flags_per_feature, is_macos): + if compiler != "clang": + return [] + return [ + feature( + name = "use_module_maps", + requires = [feature_set(features = ["module_maps"])], + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ], + flag_groups = [ + flag_group( + # macOS requires -Xclang because of a bug in Apple Clang + flags = (["-Xclang"] if is_macos else []) + [ + "-fmodule-name=%{module_name}", + ] + (["-Xclang"] if is_macos else []) + [ + "-fmodule-map-file=%{module_map_file}", + ] + extra_flags_per_feature.get("use_module_maps", []), + ), + ], + ), + ], + ), + + # Tell blaze we support module maps in general, so they will be generated + # for all c/c++ rules. + # Note: not all C++ rules support module maps; thus, do not imply this + # feature from other features - instead, require it. + feature(name = "module_maps", enabled = True), + feature( + name = "layering_check", + implies = ["use_module_maps"], + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ], + flag_groups = [ + flag_group(flags = [ + "-fmodules-strict-decluse", + "-Wprivate-header", + ]), + flag_group( + iterate_over = "dependent_module_map_files", + flags = (["-Xclang"] if is_macos else []) + [ + "-fmodule-map-file=%{dependent_module_map_files}", + ], + ), + ], + ), + ], + ), + ] + +def parse_headers_support(parse_headers_tool_path): + """ + Returns action configurations and features for parsing headers. + + Args: + parse_headers_tool_path: The path to the tool used for parsing headers. + + Returns: + A tuple containing a list of action configurations and a list of features. + """ + if not parse_headers_tool_path: + return [], [] + action_configs = [ + action_config( + action_name = ACTION_NAMES.cpp_header_parsing, + tools = [ + tool(path = parse_headers_tool_path), + ], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group( + flags = [ + # Note: This treats all headers as C++ headers, which may lead to + # parsing failures for C headers that are not valid C++. + # For such headers, use features = ["-parse_headers"] to selectively + # disable parsing. + "-xc++-header", + "-fsyntax-only", + ], + ), + ], + ), + ], + implies = [ + # Copied from the legacy feature definition in CppActionConfigs.java. + "legacy_compile_flags", + "user_compile_flags", + "sysroot", + "unfiltered_compile_flags", + "compiler_input_flags", + "compiler_output_flags", + ], + ), + ] + features = [ + feature(name = "parse_headers"), + ] + return action_configs, features + +all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.clif_match, +] + +preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.clif_match, +] + +codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.lto_backend, +] + +all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +lto_index_actions = [ + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, +] + +def _sanitizer_feature(name = "", specific_compile_flags = [], specific_link_flags = []): + return feature( + name = name, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group(flags = [ + "-fno-omit-frame-pointer", + "-fno-sanitize-recover=all", + ] + specific_compile_flags), + ], + ), + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group(flags = specific_link_flags), + ], + ), + ], + ) + +def _impl(ctx): + is_linux = ctx.attr.target_libc != "macosx" + + tool_paths = [ + tool_path(name = name, path = path) + for name, path in ctx.attr.tool_paths.items() + ] + action_configs = [] + + llvm_cov = ctx.attr.tool_paths.get("llvm-cov") + if llvm_cov: + llvm_cov_action = action_config( + action_name = ACTION_NAMES.llvm_cov, + tools = [ + tool( + path = llvm_cov, + ), + ], + ) + action_configs.append(llvm_cov_action) + + objcopy = ctx.attr.tool_paths.get("objcopy") + if objcopy: + objcopy_action = action_config( + action_name = ACTION_NAMES.objcopy_embed_data, + tools = [ + tool( + path = objcopy, + ), + ], + ) + action_configs.append(objcopy_action) + + validate_static_library = ctx.attr.tool_paths.get("validate_static_library") + if validate_static_library: + validate_static_library_action = action_config( + action_name = ACTION_NAMES.validate_static_library, + tools = [ + tool( + path = validate_static_library, + ), + ], + ) + action_configs.append(validate_static_library_action) + + symbol_check = feature( + name = "symbol_check", + implies = [ACTION_NAMES.validate_static_library], + ) + else: + symbol_check = None + + deps_scanner = "cpp-module-deps-scanner_not_found" + if "cpp-module-deps-scanner" in ctx.attr.tool_paths: + deps_scanner = ctx.attr.tool_paths["cpp-module-deps-scanner"] + cc = ctx.attr.tool_paths.get("gcc") + compile_implies = [ + # keep same with c++-compile + "legacy_compile_flags", + "user_compile_flags", + "sysroot", + "unfiltered_compile_flags", + "compiler_input_flags", + "compiler_output_flags", + ] + cpp_module_scan_deps = action_config( + action_name = ACTION_NAMES.cpp_module_deps_scanning, + tools = [ + tool( + path = deps_scanner, + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp_module_scan_deps) + + cpp20_module_compile = action_config( + action_name = ACTION_NAMES.cpp20_module_compile, + tools = [ + tool( + path = cc, + ), + ], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group( + flags = [ + "-x", + "c++-module" if ctx.attr.compiler == "clang" else "c++", + ], + ), + ], + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp20_module_compile) + + cpp20_module_codegen = action_config( + action_name = ACTION_NAMES.cpp20_module_codegen, + tools = [ + tool( + path = cc, + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp20_module_codegen) + + supports_pic_feature = feature( + name = "supports_pic", + enabled = True, + ) + supports_start_end_lib_feature = feature( + name = "supports_start_end_lib", + enabled = True, + ) + + gcc_quoting_for_param_files_feature = feature( + name = "gcc_quoting_for_param_files", + enabled = True, + ) + + static_link_cpp_runtimes_feature = feature( + name = "static_link_cpp_runtimes", + enabled = False, + ) + + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + # Security hardening requires optimization. + # We need to undef it as some distributions now have it enabled by default. + flags = ["-U_FORTIFY_SOURCE"], + ), + ], + with_features = [ + with_feature_set( + not_features = ["thin_lto"], + ), + ], + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.compile_flags, + ), + ] if ctx.attr.compile_flags else []), + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.dbg_compile_flags, + ), + ] if ctx.attr.dbg_compile_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.opt_compile_flags, + ), + ] if ctx.attr.opt_compile_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ([ + flag_group( + flags = ctx.attr.conly_flags, + ), + ] if ctx.attr.conly_flags else []), + ), + flag_set( + actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend], + flag_groups = ([ + flag_group( + flags = ctx.attr.cxx_flags, + ), + ] if ctx.attr.cxx_flags else []), + ), + ], + ) + + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.link_flags, + ), + ] if ctx.attr.link_flags else []), + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.opt_link_flags, + ), + ] if ctx.attr.opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ), + ], + env_sets = [ + env_set( + actions = all_link_actions + lto_index_actions + [ACTION_NAMES.cpp_link_static_library], + env_entries = ([ + env_entry( + # Required for hermetic links on macOS + key = "ZERO_AR_DATE", + value = "1", + ), + ]), + ), + ], + ) + + dbg_feature = feature(name = "dbg") + + opt_feature = feature(name = "opt") + + sysroot_feature = feature( + name = "sysroot", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ] + all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + + compiler_input_flags_feature = feature( + name = "compiler_input_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ACTION_NAMES.lto_backend, + ], + flag_groups = [ + flag_group( + flags = ["-c", "%{source_file}"], + expand_if_available = "source_file", + ), + ], + ), + ], + ) + + compiler_output_flags_feature = feature( + name = "compiler_output_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ACTION_NAMES.lto_backend, + ], + flag_groups = [ + flag_group( + flags = ["-S"], + expand_if_available = "output_assembly_file", + ), + flag_group( + flags = ["-E"], + expand_if_available = "output_preprocess_file", + ), + flag_group( + flags = ["-o", "%{output_file}"], + expand_if_available = "output_file", + ), + ], + ), + ], + ) + + fdo_optimize_feature = feature( + name = "fdo_optimize", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [ + flag_group( + flags = [ + "-fprofile-use=%{fdo_profile_path}", + "-fprofile-correction", + ], + expand_if_available = "fdo_profile_path", + ), + ], + ), + ], + provides = ["profile"], + ) + + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + + user_compile_flags_feature = feature( + name = "user_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.unfiltered_compile_flags, + ), + ] if ctx.attr.unfiltered_compile_flags else []), + ), + ], + ) + + library_search_directories_feature = feature( + name = "library_search_directories", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["-L%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_available = "library_search_directories", + ), + ], + ), + ], + ) + + static_libgcc_feature = feature( + name = "static_libgcc", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ], + flag_groups = [flag_group(flags = ["-static-libgcc"])], + with_features = [ + with_feature_set(features = ["static_link_cpp_runtimes"]), + ], + ), + ], + ) + + pic_feature = feature( + name = "pic", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group(flags = ["-fPIC"], expand_if_available = "pic"), + ], + ), + ], + ) + + per_object_debug_info_feature = feature( + name = "per_object_debug_info", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group( + flags = ["-gsplit-dwarf", "-g"], + expand_if_available = "per_object_debug_info_file", + ), + ], + ), + ], + ) + + preprocessor_defines_feature = feature( + name = "preprocessor_defines", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = ["-D%{preprocessor_defines}"], + iterate_over = "preprocessor_defines", + ), + ], + ), + ], + ) + + cs_fdo_optimize_feature = feature( + name = "cs_fdo_optimize", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.lto_backend], + flag_groups = [ + flag_group( + flags = [ + "-fprofile-use=%{fdo_profile_path}", + "-Wno-profile-instr-unprofiled", + "-Wno-profile-instr-out-of-date", + "-fprofile-correction", + ], + expand_if_available = "fdo_profile_path", + ), + ], + ), + ], + provides = ["csprofile"], + ) + + autofdo_feature = feature( + name = "autofdo", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [ + flag_group( + flags = [ + "-fauto-profile=%{fdo_profile_path}", + "-fprofile-correction", + ], + expand_if_available = "fdo_profile_path", + ), + ], + ), + ], + provides = ["profile"], + ) + + if is_linux: + runtime_library_search_directories_feature = feature( + name = "runtime_library_search_directories", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$EXEC_ORIGIN/%{runtime_library_search_directories}", + ], + expand_if_true = "is_cc_test", + ), + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + expand_if_false = "is_cc_test", + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + ], + with_features = [ + with_feature_set(features = ["static_link_cpp_runtimes"]), + ], + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "$ORIGIN/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = + "runtime_library_search_directories", + ), + ], + with_features = [ + with_feature_set( + not_features = ["static_link_cpp_runtimes"], + ), + ], + ), + ], + ) + set_install_name_feature = feature( + name = "set_soname", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ], + flag_groups = [ + flag_group( + flags = [ + "-Wl,-soname,%{runtime_solib_name}", + ], + expand_if_available = "runtime_solib_name", + ), + ], + ), + ], + ) + else: + runtime_library_search_directories_feature = feature( + name = "runtime_library_search_directories", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + iterate_over = "runtime_library_search_directories", + flag_groups = [ + flag_group( + flags = [ + "-Xlinker", + "-rpath", + "-Xlinker", + "@loader_path/%{runtime_library_search_directories}", + ], + ), + ], + expand_if_available = "runtime_library_search_directories", + ), + ], + ), + ], + ) + set_install_name_feature = feature( + name = "set_install_name", + enabled = ctx.fragments.cpp.do_not_use_macos_set_install_name, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ], + flag_groups = [ + flag_group( + flags = [ + "-Wl,-install_name,@rpath/%{runtime_solib_name}", + ], + expand_if_available = "runtime_solib_name", + ), + ], + ), + ], + ) + + fission_support_feature = feature( + name = "fission_support", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["-Wl,--gdb-index"], + expand_if_available = "is_using_fission", + ), + ], + ), + ], + ) + + shared_flag_feature = feature( + name = "shared_flag", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, + ], + flag_groups = [flag_group(flags = ["-shared"])], + ), + ], + ) + + random_seed_feature = feature( + name = "random_seed", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group( + flags = ["-frandom-seed=%{output_file}"], + expand_if_available = "output_file", + ), + ], + ), + ], + ) + + includes_feature = feature( + name = "includes", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ], + flag_groups = [ + flag_group( + flags = ["-include", "%{includes}"], + iterate_over = "includes", + expand_if_available = "includes", + ), + ], + ), + ], + ) + + fdo_instrument_feature = feature( + name = "fdo_instrument", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ] + all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = [ + "-fprofile-generate=%{fdo_instrument_path}", + "-fno-data-sections", + ], + expand_if_available = "fdo_instrument_path", + ), + ], + ), + ], + provides = ["profile"], + ) + + cs_fdo_instrument_feature = feature( + name = "cs_fdo_instrument", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.lto_backend, + ] + all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = [ + "-fcs-profile-generate=%{cs_fdo_instrument_path}", + ], + expand_if_available = "cs_fdo_instrument_path", + ), + ], + ), + ], + provides = ["csprofile"], + ) + + include_paths_feature = feature( + name = "include_paths", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ], + flag_groups = [ + flag_group( + flags = ["-iquote", "%{quote_include_paths}"], + iterate_over = "quote_include_paths", + ), + flag_group( + flags = ["-I%{include_paths}"], + iterate_over = "include_paths", + ), + flag_group( + flags = ["-isystem", "%{system_include_paths}"], + iterate_over = "system_include_paths", + ), + ], + ), + ], + ) + + external_include_paths_feature = feature( + name = "external_include_paths", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ], + flag_groups = [ + flag_group( + flags = ["-isystem", "%{external_include_paths}"], + iterate_over = "external_include_paths", + expand_if_available = "external_include_paths", + ), + ], + ), + ], + ) + + strip_debug_symbols_feature = feature( + name = "strip_debug_symbols", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["-Wl,-S"], + expand_if_available = "strip_debug_symbols", + ), + ], + ), + ], + ) + + build_interface_libraries_feature = feature( + name = "build_interface_libraries", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, + ], + flag_groups = [ + flag_group( + flags = [ + "%{generate_interface_library}", + "%{interface_library_builder_path}", + "%{interface_library_input_path}", + "%{interface_library_output_path}", + ], + expand_if_available = "generate_interface_library", + ), + ], + with_features = [ + with_feature_set( + features = ["supports_interface_shared_libraries"], + ), + ], + ), + ], + ) + + libraries_to_link_common_flag_groups = [ + flag_group( + flags = ["-Wl,-whole-archive"], + expand_if_true = + "libraries_to_link.is_whole_archive", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "interface_library", + ), + ), + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + flag_group( + flags = ["-l%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "dynamic_library", + ), + ), + flag_group( + flags = ["-l:%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "versioned_dynamic_library", + ), + ), + flag_group( + flags = ["-Wl,-no-whole-archive"], + expand_if_true = "libraries_to_link.is_whole_archive", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "static_library", + ), + ), + ] + + libraries_to_link_feature = feature( + name = "libraries_to_link", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ] + lto_index_actions, + flag_groups = [ + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["-Wl,--start-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ] + libraries_to_link_common_flag_groups + [ + flag_group( + flags = ["-Wl,--end-lib"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + flag_group( + flags = ["-Wl,@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + ], + ), + # Object file groups may contain symbols that aren't referenced in the same target that + # produces the object files and must thus not be wrapped in --start-lib/--end-lib when + # linking a nodeps dynamic library. + flag_set( + actions = [ACTION_NAMES.cpp_link_nodeps_dynamic_library], + flag_groups = [ + flag_group( + iterate_over = "libraries_to_link", + flag_groups = libraries_to_link_common_flag_groups, + ), + flag_group( + flags = ["-Wl,@%{thinlto_param_file}"], + expand_if_true = "thinlto_param_file", + ), + ], + ), + ], + ) + + user_link_flags_feature = feature( + name = "user_link_flags", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + ], + ), + ], + ) + + default_link_libs_feature = feature( + name = "default_link_libs", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else [], + ), + ], + ) + + fdo_prefetch_hints_feature = feature( + name = "fdo_prefetch_hints", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.lto_backend, + ], + flag_groups = [ + flag_group( + flags = [ + "-mllvm", + "-prefetch-hints-file=%{fdo_prefetch_hints_path}", + ], + expand_if_available = "fdo_prefetch_hints_path", + ), + ], + ), + ], + ) + + linkstamps_feature = feature( + name = "linkstamps", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["%{linkstamp_paths}"], + iterate_over = "linkstamp_paths", + expand_if_available = "linkstamp_paths", + ), + ], + ), + ], + ) + + libtool_feature = feature( + name = "libtool", + enabled = not is_linux, + ) + + archiver_flags_feature = feature( + name = "archiver_flags", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + flags = [ + "rcsD" if is_linux else "rcs", + "%{output_execpath}", + ], + expand_if_available = "output_execpath", + ), + ], + with_features = [ + with_feature_set( + not_features = ["libtool"], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + flags = [ + "-static", + "-o", + "%{output_execpath}", + ], + expand_if_available = "output_execpath", + ), + ], + with_features = [ + with_feature_set( + features = ["libtool"], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = ([ + flag_group( + flags = ctx.attr.archive_flags, + ), + ] if ctx.attr.archive_flags else []), + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + flags = ["%{user_archiver_flags}"], + iterate_over = "user_archiver_flags", + expand_if_available = "user_archiver_flags", + ), + ], + ), + ], + ) + + force_pic_flags_feature = feature( + name = "force_pic_flags", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.lto_index_for_executable, + ], + flag_groups = [ + flag_group( + flags = ["-pie"], + expand_if_available = "force_pic", + ), + ], + ), + ], + ) + + dependency_file_feature = feature( + name = "dependency_file", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = ["-MD", "-MF", "%{dependency_file}"], + expand_if_available = "dependency_file", + ), + ], + ), + ], + ) + + serialized_diagnostics_file_feature = feature( + name = "serialized_diagnostics_file", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.clif_match, + ], + flag_groups = [ + flag_group( + flags = ["--serialize-diagnostics", "%{serialized_diagnostics_file}"], + expand_if_available = "serialized_diagnostics_file", + ), + ], + ), + ], + ) + + dynamic_library_linker_tool_feature = feature( + name = "dynamic_library_linker_tool", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, + ], + flag_groups = [ + flag_group( + flags = [" + cppLinkDynamicLibraryToolPath + "], + expand_if_available = "generate_interface_library", + ), + ], + with_features = [ + with_feature_set( + features = ["supports_interface_shared_libraries"], + ), + ], + ), + ], + ) + + generate_linkmap_feature = feature( + name = "generate_linkmap", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_link_executable, + ], + flag_groups = [ + flag_group( + flags = [ + "-Wl,-Map=%{output_execpath}.map" if is_linux else "-Wl,-map,%{output_execpath}.map", + ], + expand_if_available = "output_execpath", + ), + ], + ), + ], + ) + + output_execpath_flags_feature = feature( + name = "output_execpath_flags", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["-o", "%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], + ), + ], + ) + + # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The + # generated code contains references to gcov symbols, and the dynamic linker + # can't resolve them unless the library is linked against gcov. + coverage_feature = feature( + name = "coverage", + provides = ["profile"], + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ], + flag_groups = ([ + flag_group(flags = ctx.attr.coverage_compile_flags), + ] if ctx.attr.coverage_compile_flags else []), + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group(flags = ctx.attr.coverage_link_flags), + ] if ctx.attr.coverage_link_flags else []), + ), + ], + ) + + thinlto_feature = feature( + name = "thin_lto", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ] + all_link_actions + lto_index_actions, + flag_groups = [ + flag_group(flags = ["-flto=thin"]), + flag_group( + expand_if_available = "lto_indexing_bitcode_file", + flags = [ + "-Xclang", + "-fthin-link-bitcode=%{lto_indexing_bitcode_file}", + ], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.linkstamp_compile], + flag_groups = [flag_group(flags = ["-DBUILD_LTO_TYPE=thin"])], + ), + flag_set( + actions = lto_index_actions, + flag_groups = [ + flag_group(flags = [ + "-flto=thin", + "-Wl,-plugin-opt,thinlto-index-only%{thinlto_optional_params_file}", + "-Wl,-plugin-opt,thinlto-emit-imports-files", + "-Wl,-plugin-opt,thinlto-prefix-replace=%{thinlto_prefix_replace}", + ]), + flag_group( + expand_if_available = "thinlto_object_suffix_replace", + flags = [ + "-Wl,-plugin-opt,thinlto-object-suffix-replace=%{thinlto_object_suffix_replace}", + ], + ), + flag_group( + expand_if_available = "thinlto_merged_object_file", + flags = [ + "-Wl,-plugin-opt,obj-path=%{thinlto_merged_object_file}", + ], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.lto_backend], + flag_groups = [ + flag_group(flags = [ + "-c", + "-fthinlto-index=%{thinlto_index}", + "-o", + "%{thinlto_output_object_file}", + "-x", + "ir", + "%{thinlto_input_bitcode_file}", + ]), + ], + ), + ], + ) + + treat_warnings_as_errors_feature = feature( + name = "treat_warnings_as_errors", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-Werror"])], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group( + flags = ["-Wl,-fatal-warnings"] if is_linux else ["-Wl,-fatal_warnings"], + )], + ), + ], + ) + + archive_param_file_feature = feature( + name = "archive_param_file", + enabled = True, + ) + + asan_feature = _sanitizer_feature( + name = "asan", + specific_compile_flags = [ + "-fsanitize=address", + "-fno-common", + ], + specific_link_flags = [ + "-fsanitize=address", + ], + ) + + tsan_feature = _sanitizer_feature( + name = "tsan", + specific_compile_flags = [ + "-fsanitize=thread", + ], + specific_link_flags = [ + "-fsanitize=thread", + ], + ) + + ubsan_feature = _sanitizer_feature( + name = "ubsan", + specific_compile_flags = [ + "-fsanitize=undefined", + ], + specific_link_flags = [ + "-fsanitize=undefined", + ], + ) + + # If you have Xcode + the CLT installed the version defaults can be + # too old for some standard C apis such as thread locals + macos_minimum_os_feature = feature( + name = "macos_minimum_os", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions + all_link_actions, + flag_groups = [flag_group(flags = ["-mmacosx-version-min={}".format(_target_os_version(ctx))])], + ), + ], + ) + + # Kept for backwards compatibility with the crosstool that moved. Without + # linking the objc runtime binaries don't link CoreFoundation for free, + # which breaks abseil. + macos_default_link_flags_feature = feature( + name = "macos_default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = [ + "-no-canonical-prefixes", + "-fobjc-link-runtime", + ])], + ), + ], + ) + + # Tell bazel we support C++ modules now + cpp_modules_feature = feature( + name = "cpp_modules", + # set default value to False + # to enable the feature + # use --features=cpp_modules + # or add cpp_modules to features attr + enabled = False, + ) + + cpp_module_modmap_file_feature = feature( + name = "cpp_module_modmap_file", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group( + flags = ["@%{cpp_module_modmap_file}" if ctx.attr.compiler == "clang" else "-fmodule-mapper=%{cpp_module_modmap_file}"], + expand_if_available = "cpp_module_modmap_file", + ), + ], + ), + ], + enabled = True, + ) + if ctx.attr.compiler == "clang": + flag_groups = [ + flag_group( + flags = ["-fmodule-output=%{cpp_module_output_file}"], + expand_if_available = "cpp_module_output_file", + ), + ] + else: + flag_groups = [] + cpp20_module_compile_flags_feature = feature( + name = "cpp20_module_compile_flags", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp20_module_compile, + ], + flag_groups = flag_groups, + ), + ], + enabled = True, + ) + + # TODO(#8303): Mac crosstool should also declare every feature. + if is_linux: + # Linux artifact name patterns are the default. + artifact_name_patterns = [] + features = [ + cpp_modules_feature, + cpp_module_modmap_file_feature, + cpp20_module_compile_flags_feature, + dependency_file_feature, + serialized_diagnostics_file_feature, + random_seed_feature, + pic_feature, + per_object_debug_info_feature, + preprocessor_defines_feature, + includes_feature, + include_paths_feature, + external_include_paths_feature, + fdo_instrument_feature, + cs_fdo_instrument_feature, + cs_fdo_optimize_feature, + thinlto_feature, + fdo_prefetch_hints_feature, + autofdo_feature, + build_interface_libraries_feature, + dynamic_library_linker_tool_feature, + generate_linkmap_feature, + shared_flag_feature, + linkstamps_feature, + output_execpath_flags_feature, + runtime_library_search_directories_feature, + library_search_directories_feature, + libtool_feature, + archiver_flags_feature, + force_pic_flags_feature, + fission_support_feature, + strip_debug_symbols_feature, + coverage_feature, + supports_pic_feature, + asan_feature, + tsan_feature, + ubsan_feature, + gcc_quoting_for_param_files_feature, + static_link_cpp_runtimes_feature, + ] + ( + [ + supports_start_end_lib_feature, + ] if ctx.attr.supports_start_end_lib else [] + ) + [ + default_compile_flags_feature, + default_link_flags_feature, + libraries_to_link_feature, + user_link_flags_feature, + default_link_libs_feature, + static_libgcc_feature, + fdo_optimize_feature, + supports_dynamic_linker_feature, + dbg_feature, + opt_feature, + user_compile_flags_feature, + sysroot_feature, + compiler_input_flags_feature, + compiler_output_flags_feature, + unfiltered_compile_flags_feature, + treat_warnings_as_errors_feature, + archive_param_file_feature, + set_install_name_feature, + ] + layering_check_features(ctx.attr.compiler, ctx.attr.extra_flags_per_feature, is_macos = False) + else: + # macOS artifact name patterns differ from the defaults only for dynamic + # libraries. + artifact_name_patterns = [ + artifact_name_pattern( + category_name = "dynamic_library", + prefix = "lib", + extension = ".dylib", + ), + ] + features = [ + cpp_modules_feature, + cpp_module_modmap_file_feature, + cpp20_module_compile_flags_feature, + macos_minimum_os_feature, + macos_default_link_flags_feature, + dependency_file_feature, + runtime_library_search_directories_feature, + set_install_name_feature, + libtool_feature, + archiver_flags_feature, + asan_feature, + tsan_feature, + ubsan_feature, + gcc_quoting_for_param_files_feature, + static_link_cpp_runtimes_feature, + ] + ( + [ + supports_start_end_lib_feature, + ] if ctx.attr.supports_start_end_lib else [] + ) + [ + coverage_feature, + default_compile_flags_feature, + default_link_flags_feature, + user_link_flags_feature, + default_link_libs_feature, + external_include_paths_feature, + fdo_optimize_feature, + dbg_feature, + opt_feature, + user_compile_flags_feature, + sysroot_feature, + compiler_input_flags_feature, + compiler_output_flags_feature, + unfiltered_compile_flags_feature, + treat_warnings_as_errors_feature, + archive_param_file_feature, + generate_linkmap_feature, + ] + layering_check_features(ctx.attr.compiler, ctx.attr.extra_flags_per_feature, is_macos = True) + + parse_headers_action_configs, parse_headers_features = parse_headers_support( + parse_headers_tool_path = ctx.attr.tool_paths.get("parse_headers"), + ) + action_configs += parse_headers_action_configs + features += parse_headers_features + + if symbol_check: + features.append(symbol_check) + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = action_configs, + artifact_name_patterns = artifact_name_patterns, + cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, + toolchain_identifier = ctx.attr.toolchain_identifier, + host_system_name = ctx.attr.host_system_name, + target_system_name = ctx.attr.target_system_name, + target_cpu = ctx.attr.cpu, + target_libc = ctx.attr.target_libc, + compiler = ctx.attr.compiler, + abi_version = ctx.attr.abi_version, + abi_libc_version = ctx.attr.abi_libc_version, + tool_paths = tool_paths, + builtin_sysroot = ctx.attr.builtin_sysroot, + ) + +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "abi_libc_version": attr.string(mandatory = True), + "abi_version": attr.string(mandatory = True), + "archive_flags": attr.string_list(), + "builtin_sysroot": attr.string(), + "compile_flags": attr.string_list(), + "compiler": attr.string(mandatory = True), + "conly_flags": attr.string_list(), + "coverage_compile_flags": attr.string_list(), + "coverage_link_flags": attr.string_list(), + "cpu": attr.string(mandatory = True), + "cxx_builtin_include_directories": attr.string_list(), + "cxx_flags": attr.string_list(), + "dbg_compile_flags": attr.string_list(), + "extra_flags_per_feature": attr.string_list_dict(), + "host_system_name": attr.string(mandatory = True), + "link_flags": attr.string_list(), + "link_libs": attr.string_list(), + "opt_compile_flags": attr.string_list(), + "opt_link_flags": attr.string_list(), + "supports_start_end_lib": attr.bool(), + "target_libc": attr.string(mandatory = True), + "target_system_name": attr.string(mandatory = True), + "tool_paths": attr.string_dict(), + "toolchain_identifier": attr.string(mandatory = True), + "unfiltered_compile_flags": attr.string_list(), + "_xcode_config": attr.label(default = configuration_field( + fragment = "apple", + name = "xcode_config_label", + )), + }, + fragments = ["apple", "cpp"], + provides = [CcToolchainConfigInfo], +) diff --git a/core/private/get_cpu_value.bzl b/core/private/get_cpu_value.bzl deleted file mode 100644 index f2fcc5776..000000000 --- a/core/private/get_cpu_value.bzl +++ /dev/null @@ -1,48 +0,0 @@ -load("@bazel_skylib//lib:versions.bzl", "versions") - -# see https://github.com/tweag/rules_nixpkgs/pull/613 -# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L225 -def get_cpu_value(repository_ctx): - """Compute the cpu_value based on the OS name. Doesn't %-escape the result! - - Args: - repository_ctx: The repository context. - Returns: - One of (darwin, freebsd, x64_windows, ppc, s390x, arm, aarch64, k8, piii) - """ - os_name = repository_ctx.os.name - arch = repository_ctx.os.arch - if os_name.startswith("mac os"): - # Check if we are on x86_64 or arm64 and return the corresponding cpu value. - if arch == "aarch64": - return "darwin_arm64" - - # NOTE(cb) for backward compatibility return "darwin" for Bazel < 7 on x86_64 - if versions.is_at_least("7.0.0", versions.get()): - return "darwin_x86_64" - else: - return "darwin" - - if os_name.find("freebsd") != -1: - return "freebsd" - if os_name.find("openbsd") != -1: - return "openbsd" - if os_name.find("windows") != -1: - if arch == "aarch64": - return "arm64_windows" - else: - return "x64_windows" - - if arch in ["power", "ppc64le", "ppc", "ppc64"]: - return "ppc" - if arch in ["s390x"]: - return "s390x" - if arch in ["mips64"]: - return "mips64" - if arch in ["riscv64"]: - return "riscv64" - if arch in ["arm", "armv7l"]: - return "arm" - if arch in ["aarch64"]: - return "aarch64" - return "k8" if arch in ["amd64", "x86_64", "x64"] else "piii" diff --git a/core/util.bzl b/core/util.bzl index e9103d6f7..87cf8a4eb 100644 --- a/core/util.bzl +++ b/core/util.bzl @@ -1,6 +1,6 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_skylib//lib:versions.bzl", "versions") -load("//:private/get_cpu_value.bzl", "get_cpu_value") +load("//private/cc_toolchain:lib_cc_configure.bzl", "get_cpu_value") def fail_on_err(return_value, prefix = None): """Fail if the given return value indicates an error. diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index f16aed784..fc3756cab 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -113,7 +113,7 @@ nixpkgs_package( """ load( - "@rules_nixpkgs_core//:private/get_cpu_value.bzl", + "@rules_nixpkgs_core//:private/cc_toolchain/lib_cc_configure.bzl", "get_cpu_value", ) load( diff --git a/toolchains/cc/cc.bzl b/toolchains/cc/cc.bzl index 4181da05b..3ad594769 100644 --- a/toolchains/cc/cc.bzl +++ b/toolchains/cc/cc.bzl @@ -23,14 +23,12 @@ using `nixpkgs_cc_configure(..., cc_lang = "cuda")` or similar. load("@bazel_skylib//lib:sets.bzl", "sets") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load( - "@rules_nixpkgs_core//:private/get_cpu_value.bzl", + "@rules_nixpkgs_core//private/cc_toolchain:lib_cc_configure.bzl", "get_cpu_value", -) -load( - "@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_starlark_list", "write_builtin_include_directory_paths", ) + load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") load( "@rules_nixpkgs_core//:util.bzl", @@ -47,7 +45,7 @@ def _parse_cc_toolchain_info(content, filename): filename: string, The path to the `CC_TOOLCHAIN_INFO` file, used for error reporting. Returns: - struct, The substitutions for `@bazel_tools//tools/cpp:BUILD.tpl`. + struct, The substitutions for `@rules_nixpkgs_core//private/cc_toolchain:BUILD.tpl`. """ # Parse the content of CC_TOOLCHAIN_INFO. @@ -234,22 +232,22 @@ _nixpkgs_cc_toolchain_config = repository_rule( "fail_not_supported": attr.bool(), "cross_cpu": attr.string(), "_unix_cc_toolchain_config": attr.label( - default = Label("@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:unix_cc_toolchain_config.bzl"), ), "_armeabi_cc_toolchain_config": attr.label( - default = Label("@bazel_tools//tools/cpp:armeabi_cc_toolchain_config.bzl"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:armeabi_cc_toolchain_config.bzl"), ), "_generate_system_module_map": attr.label( - default = Label("@bazel_tools//tools/cpp:generate_system_module_map.sh"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:generate_system_module_map.sh"), ), "_osx_cc_wrapper": attr.label( - default = Label("@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:osx_cc_wrapper.sh.tpl"), ), "_linux_cc_wrapper": attr.label( - default = Label("@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:linux_cc_wrapper.sh.tpl"), ), "_build": attr.label( - default = Label("@bazel_tools//tools/cpp:BUILD.tpl"), + default = Label("@rules_nixpkgs_core//private/cc_toolchain:BUILD.tpl"), ), }, ) diff --git a/toolchains/java/java.bzl b/toolchains/java/java.bzl index fb229dd8d..f902cb08c 100644 --- a/toolchains/java/java.bzl +++ b/toolchains/java/java.bzl @@ -8,7 +8,7 @@ """ load( - "@rules_nixpkgs_core//:private/get_cpu_value.bzl", + "@rules_nixpkgs_core//private/cc_toolchain:lib_cc_configure.bzl", "get_cpu_value", ) load( diff --git a/toolchains/posix/posix.bzl b/toolchains/posix/posix.bzl index a0c04470c..c3a662432 100644 --- a/toolchains/posix/posix.bzl +++ b/toolchains/posix/posix.bzl @@ -8,7 +8,7 @@ Rules for importing a POSIX toolchain from Nixpkgs. """ load( - "@rules_nixpkgs_core//:private/get_cpu_value.bzl", + "@rules_nixpkgs_core//private/cc_toolchain:lib_cc_configure.bzl", "get_cpu_value", ) load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package") From 9aeeec05934023691badf4e81ba36fe3fbcb05ce Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Fri, 29 Nov 2024 22:04:32 +0100 Subject: [PATCH 2/6] drop extra_flags_per_feature in call to cc_toolchain_config This is only available in Bazel 8 and later. See bazelbuild/bazel#9e913fbfa2e3930742fc30dfb60ac5e2694c70cf --- core/private/cc_toolchain/BUILD.tpl | 1 - 1 file changed, 1 deletion(-) diff --git a/core/private/cc_toolchain/BUILD.tpl b/core/private/cc_toolchain/BUILD.tpl index 99896dc93..a17985916 100644 --- a/core/private/cc_toolchain/BUILD.tpl +++ b/core/private/cc_toolchain/BUILD.tpl @@ -122,7 +122,6 @@ cc_toolchain_config( coverage_compile_flags = [%{coverage_compile_flags}], coverage_link_flags = [%{coverage_link_flags}], supports_start_end_lib = %{supports_start_end_lib}, - extra_flags_per_feature = %{extra_flags_per_feature}, ) # Android tooling requires a default toolchain for the armeabi-v7a cpu. From 85fb94fcd915435aa4c7f97bf7ca8606d7f59aa8 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Fri, 29 Nov 2024 22:26:31 +0100 Subject: [PATCH 3/6] drop conly_flags in call to cc_toolchain_config This is only available in Bazel 7 and later. See bazelbuild/bazel#b272cef7d4b2d1be4e3f82b737c3c14d5e768aea --- core/private/cc_toolchain/BUILD.tpl | 1 - 1 file changed, 1 deletion(-) diff --git a/core/private/cc_toolchain/BUILD.tpl b/core/private/cc_toolchain/BUILD.tpl index a17985916..b9e82e3f4 100644 --- a/core/private/cc_toolchain/BUILD.tpl +++ b/core/private/cc_toolchain/BUILD.tpl @@ -113,7 +113,6 @@ cc_toolchain_config( compile_flags = [%{compile_flags}], opt_compile_flags = [%{opt_compile_flags}], dbg_compile_flags = [%{dbg_compile_flags}], - conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], link_libs = [%{link_libs}], From 32c48867931852b2678425323b29207e3c0f4646 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Tue, 17 Dec 2024 18:27:27 +0100 Subject: [PATCH 4/6] conditionally use unix_cc_toolchain_config.bzl from bazel_tools or rules_cc Bazel 7 and prior requires a matching unix_cc_toolchain_config (with the appropriate features for that Bazel version). That one is bound to the Bazel version and can be loaded from @bazel_tools up to Bazel 8, where it was moved to rules_cc. --- toolchains/cc/cc.bzl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/toolchains/cc/cc.bzl b/toolchains/cc/cc.bzl index 3ad594769..bfd204255 100644 --- a/toolchains/cc/cc.bzl +++ b/toolchains/cc/cc.bzl @@ -140,8 +140,16 @@ def _nixpkgs_cc_toolchain_config_impl(repository_ctx): # `@bazel_tools//tools/cpp:unix_cc_configure.bzl`. # Uses the corresponding templates from `@bazel_tools` as well, see the # private attributes of the `_nixpkgs_cc_toolchain_config` rule. + + bazel_version_match_8, _ = is_bazel_version_at_least("8.0.0") + unix_toolchain_config = repository_ctx.attr._unix_cc_toolchain_config + if not bazel_version_match_8: + # Bazel 7 and prior requires a matching unix_cc_toolchain_config + # (with the appropriate features for that Bazel version) + unix_toolchain_config = Label("@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl") + repository_ctx.symlink( - repository_ctx.path(repository_ctx.attr._unix_cc_toolchain_config), + repository_ctx.path(unix_toolchain_config), "cc_toolchain_config.bzl", ) repository_ctx.symlink( From 07e23e1720a6e0110982940b2ba89e052a099666 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Tue, 17 Dec 2024 19:01:30 +0100 Subject: [PATCH 5/6] remove unused load --- nixpkgs/nixpkgs.bzl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl index fc3756cab..ff66d2c9d 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl @@ -112,10 +112,6 @@ nixpkgs_package( ``` """ -load( - "@rules_nixpkgs_core//:private/cc_toolchain/lib_cc_configure.bzl", - "get_cpu_value", -) load( "@rules_nixpkgs_cc//:cc.bzl", _nixpkgs_cc_configure = "nixpkgs_cc_configure", From 598f403d55d809051caf7edf165ba8cd40862c29 Mon Sep 17 00:00:00 2001 From: Malte Poll Date: Tue, 17 Dec 2024 19:52:45 +0100 Subject: [PATCH 6/6] expose @rules_nixpkgs_core//private/cc_toolchain in Workspace mode --- core/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/BUILD.bazel b/core/BUILD.bazel index ed7c2de43..1d610ab3d 100644 --- a/core/BUILD.bazel +++ b/core/BUILD.bazel @@ -4,7 +4,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "srcs", - srcs = glob(["**"]), + srcs = glob(["**"]) + ["//private/cc_toolchain:srcs"], visibility = ["//visibility:public"], )