Skip to content

Commit

Permalink
Sync cc toolchain from Bazel's tools/cpp
Browse files Browse the repository at this point in the history
- @bazel_tools//tools/cpp:toolchain_type is not moved.
- Added cc/toolchains/toolchain_config_utils.bzl for exposing util functions for cc toolchain configuration.

Closes #247

Working towards bazelbuild/bazel#23809

PiperOrigin-RevId: 681913803
Change-Id: I16ff456a212ab0e579c137bd74344255f4e49bd8
  • Loading branch information
meteorcloudy authored and copybara-github committed Oct 3, 2024
1 parent f364500 commit c2549f6
Show file tree
Hide file tree
Showing 38 changed files with 2,905 additions and 688 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bazel-*
/bazel-*
MODULE.bazel.lock
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "stardoc", version = "0.7.0")

cc_configure = use_extension("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure_extension")
use_repo(cc_configure, "local_config_cc_toolchains")
cc_configure = use_extension("//cc:extensions.bzl", "cc_configure_extension")
use_repo(cc_configure, "local_config_cc", "local_config_cc_toolchains")

register_toolchains("@local_config_cc_toolchains//:all")

Expand Down
5 changes: 5 additions & 0 deletions cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ bzl_library(

cc_toolchain_alias(name = "current_cc_toolchain")

cc_toolchain_alias(
name = "optional_current_cc_toolchain",
mandatory = False,
)

cc_libc_top_alias(name = "current_libc_top")
19 changes: 18 additions & 1 deletion cc/action_names.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ CPP_MODULE_CODEGEN_ACTION_NAME = "c++-module-codegen"
# Name of the C++ header parsing action.
CPP_HEADER_PARSING_ACTION_NAME = "c++-header-parsing"

# Name of the C++ deps scanning action.
CPP_MODULE_DEPS_SCANNING_ACTION_NAME = "c++-module-deps-scanning"

# Name of the C++ module compile action.
CPP20_MODULE_COMPILE_ACTION_NAME = "c++20-module-compile"
CPP20_MODULE_CODEGEN_ACTION_NAME = "c++20-module-codegen"

# Name of the C++ module compile action.
CPP_MODULE_COMPILE_ACTION_NAME = "c++-module-compile"

Expand All @@ -42,6 +49,8 @@ ASSEMBLE_ACTION_NAME = "assemble"
# Name of the assembly preprocessing action.
PREPROCESS_ASSEMBLE_ACTION_NAME = "preprocess-assemble"

LLVM_COV = "llvm-cov"

# Name of the action producing ThinLto index.
LTO_INDEXING_ACTION_NAME = "lto-indexing"

Expand Down Expand Up @@ -85,22 +94,29 @@ OBJC_EXECUTABLE_ACTION_NAME = "objc-executable"
# A string constant for the objc fully-link link action.
OBJC_FULLY_LINK_ACTION_NAME = "objc-fully-link"

# A string constant for the clif action.
# A string constant for the clif actions.
CLIF_MATCH_ACTION_NAME = "clif-match"

# A string constant for the obj copy actions.
OBJ_COPY_ACTION_NAME = "objcopy_embed_data"

# A string constant for the validation action for cc_static_library.
VALIDATE_STATIC_LIBRARY = "validate-static-library"

ACTION_NAMES = struct(
c_compile = C_COMPILE_ACTION_NAME,
cpp_compile = CPP_COMPILE_ACTION_NAME,
linkstamp_compile = LINKSTAMP_COMPILE_ACTION_NAME,
cc_flags_make_variable = CC_FLAGS_MAKE_VARIABLE_ACTION_NAME,
cpp_module_codegen = CPP_MODULE_CODEGEN_ACTION_NAME,
cpp_header_parsing = CPP_HEADER_PARSING_ACTION_NAME,
cpp_module_deps_scanning = CPP_MODULE_DEPS_SCANNING_ACTION_NAME,
cpp20_module_compile = CPP20_MODULE_COMPILE_ACTION_NAME,
cpp20_module_codegen = CPP20_MODULE_CODEGEN_ACTION_NAME,
cpp_module_compile = CPP_MODULE_COMPILE_ACTION_NAME,
assemble = ASSEMBLE_ACTION_NAME,
preprocess_assemble = PREPROCESS_ASSEMBLE_ACTION_NAME,
llvm_cov = LLVM_COV,
lto_indexing = LTO_INDEXING_ACTION_NAME,
lto_backend = LTO_BACKEND_ACTION_NAME,
lto_index_for_executable = LTO_INDEX_FOR_EXECUTABLE_ACTION_NAME,
Expand All @@ -117,6 +133,7 @@ ACTION_NAMES = struct(
objcpp_compile = OBJCPP_COMPILE_ACTION_NAME,
clif_match = CLIF_MATCH_ACTION_NAME,
objcopy_embed_data = OBJ_COPY_ACTION_NAME,
validate_static_library = VALIDATE_STATIC_LIBRARY,
)

# Names of actions that parse or compile C++ code.
Expand Down
16 changes: 8 additions & 8 deletions cc/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Example:
name = "foo",
srcs = ["foo.cc"],
copts = select({
"@rules_cc//cc/compiler:gcc": [...],
"@rules_cc//cc/compiler:clang": [...],
"@rules_cc//cc/compiler:msvc-cl": [...],
"//cc/compiler:gcc": [...],
"//cc/compiler:clang": [...],
"//cc/compiler:msvc-cl": [...],
# Fallback case for an undetected compiler.
"//conditions:default": [...],
}),
Expand All @@ -47,25 +47,25 @@ licenses(["notice"])

config_setting(
name = "clang",
flag_values = {"@bazel_tools//tools/cpp:compiler": "clang"},
flag_values = {"@rules_cc//cc/private/toolchain:compiler": "clang"},
)

config_setting(
name = "clang-cl",
flag_values = {"@bazel_tools//tools/cpp:compiler": "clang-cl"},
flag_values = {"@rules_cc//cc/private/toolchain:compiler": "clang-cl"},
)

config_setting(
name = "gcc",
flag_values = {"@bazel_tools//tools/cpp:compiler": "gcc"},
flag_values = {"@rules_cc//cc/private/toolchain:compiler": "gcc"},
)

config_setting(
name = "mingw-gcc",
flag_values = {"@bazel_tools//tools/cpp:compiler": "mingw-gcc"},
flag_values = {"@rules_cc//cc/private/toolchain:compiler": "mingw-gcc"},
)

config_setting(
name = "msvc-cl",
flag_values = {"@bazel_tools//tools/cpp:compiler": "msvc-cl"},
flag_values = {"@rules_cc//cc/private/toolchain:compiler": "msvc-cl"},
)
7 changes: 3 additions & 4 deletions cc/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
# limitations under the License.
"""Module extension for cc auto configuration."""

load("@bazel_tools//tools/osx:xcode_configure.bzl", "xcode_configure")
load("//cc/private/toolchain:cc_configure.bzl", "cc_autoconf", "cc_autoconf_toolchains")

def _cc_configure_impl(_):
def _cc_configure_extension_impl(ctx):
cc_autoconf_toolchains(name = "local_config_cc_toolchains")
cc_autoconf(name = "local_config_cc")
xcode_configure("@bazel_tools//tools/osx:xcode_locator.m")
return ctx.extension_metadata(reproducible = True)

cc_configure = module_extension(implementation = _cc_configure_impl)
cc_configure_extension = module_extension(implementation = _cc_configure_extension_impl)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
# limitations under the License.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite")
load(":cc_toolchain_config.bzl", "cc_toolchain_config")

package(default_visibility = ["//visibility:public"])

load(":cc_toolchain_config.bzl", "cc_toolchain_config")
cc_library(
name = "link_extra_lib",
)

cc_library(
name = "malloc",
Expand All @@ -30,8 +33,8 @@ filegroup(
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"local": ":local",
"local|local": ":local",
"%{cpu}|local": ":local",
"%{cpu}": ":local",
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This becomes the BUILD file for @local_config_cc// under FreeBSD.
# This becomes the BUILD file for @local_config_cc// under FreeBSD and OpenBSD.

load(":cc_toolchain_config.bzl", "cc_toolchain_config")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite")

package(default_visibility = ["//visibility:public"])

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite")
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
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",
Expand All @@ -32,10 +50,12 @@ filegroup(
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"armeabi-v7a": ":cc-compiler-armeabi-v7a",
"armeabi-v7a|compiler": ":cc-compiler-armeabi-v7a",
"freebsd": ":cc-compiler-freebsd",
"freebsd|compiler": ":cc-compiler-freebsd",
"openbsd|compiler": ":cc-compiler-openbsd",
"armeabi-v7a": ":cc-compiler-armeabi-v7a",
"freebsd": ":cc-compiler-freebsd",
"openbsd": ":cc-compiler-openbsd",
},
)

Expand Down Expand Up @@ -70,7 +90,41 @@ toolchain(
"@platforms//os:freebsd",
],
toolchain = ":cc-compiler-freebsd",
toolchain_type = "@rules_cc//cc:toolchain_type",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
name = "cc-compiler-openbsd",
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 = 0,
toolchain_config = ":local_openbsd",
toolchain_identifier = "local_openbsd",
)

cc_toolchain_config(
name = "local_openbsd",
cpu = "openbsd",
)

toolchain(
name = "cc-toolchain-openbsd",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:openbsd",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:openbsd",
],
toolchain = ":cc-compiler-openbsd",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
Expand Down Expand Up @@ -99,11 +153,11 @@ toolchain(
"@platforms//cpu:arm",
],
target_compatible_with = [
"@platforms//cpu:arm",
"@platforms//cpu:armv7",
"@platforms//os:android",
],
toolchain = ":cc-compiler-armeabi-v7a",
toolchain_type = "@rules_cc//cc:toolchain_type",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

filegroup(
Expand Down
6 changes: 3 additions & 3 deletions cc/private/toolchain/BUILD.toolchains.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ toolchain(
exec_compatible_with = HOST_CONSTRAINTS,
target_compatible_with = HOST_CONSTRAINTS,
toolchain = "@local_config_cc//:cc-compiler-%{name}",
toolchain_type = "@rules_cc//cc:toolchain_type",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

toolchain(
name = "cc-toolchain-armeabi-v7a",
exec_compatible_with = HOST_CONSTRAINTS,
target_compatible_with = [
"@platforms//cpu:arm",
"@platforms//cpu:armv7",
"@platforms//os:android",
],
toolchain = "@local_config_cc//:cc-compiler-armeabi-v7a",
toolchain_type = "@rules_cc//cc:toolchain_type",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
39 changes: 35 additions & 4 deletions cc/private/toolchain/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,34 @@
# 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-FreeBSD unixes.

package(default_visibility = ["//visibility:public"])
# 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",
)
Expand All @@ -36,6 +54,15 @@ filegroup(
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}],
Expand Down Expand Up @@ -66,7 +93,9 @@ cc_toolchain(
linker_files = ":compiler_deps",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = %{supports_param_files},
supports_header_parsing = 1,
supports_param_files = 1,
module_map = %{modulemap},
)

cc_toolchain_config(
Expand All @@ -84,6 +113,7 @@ 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}],
Expand All @@ -92,6 +122,7 @@ 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.
Expand Down
Loading

0 comments on commit c2549f6

Please sign in to comment.