-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hermetic_cc_toolchain for a hermetic cc toolchain (#12135)
* Add bazel-zig-cc for a hermetic cc toolchain * gazelle * Remove llvm * remove wl * Add new URLs for renamed repo * gazelle * Update to v2.0.0-rc1 * bump to rc2 * Some PR feedback * use v2.0.0 from rc2 * Disable hermetic builds for mac and windows. * bump bazel version, add darwin hack * fix * Add the no-op emtpy cc toolchain code * typo and additional copy * update protobuf and fix vaticle warning * Revert "update protobuf and fix vaticle warning" This reverts commit 7bb4b6b. --------- Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b6258cc
commit 7c92a1b
Showing
5 changed files
with
159 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6.1.0 | ||
6.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# From Bazel's perspective, this is almost equivalent to always specifying | ||
# --extra_toolchains on every bazel <...> command-line invocation. It also | ||
# means there is no way to disable the toolchain with the command line. This is | ||
# useful if you find bazel-hermetic-cc useful enough to compile for all of your | ||
# targets and tools. | ||
# | ||
# With BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 Bazel stops detecting the default | ||
# host toolchain. Configuring toolchains is complicated enough, and the | ||
# auto-detection (read: fallback to non-hermetic toolchain) is a footgun best | ||
# avoided. This option is not documented in bazel, so may break. If you intend | ||
# to use the hermetic toolchain exclusively, it won't hurt. | ||
build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 | ||
|
||
# This snippet instructs Bazel to use the registered "new kinds of toolchains". | ||
# This flag not needed after this issue is closed https://github.com/bazelbuild/bazel/issues/7260 | ||
build --incompatible_enable_cc_toolchain_resolution | ||
|
||
# Add a no-op warning for users still using --config=llvm | ||
build:llvm --unconditional_warning="llvm config is no longer used as clang is now the default compiler" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain") | ||
load( | ||
"@bazel_tools//tools/cpp:lib_cc_configure.bzl", | ||
"get_cpu_value", | ||
"resolve_labels", | ||
) | ||
|
||
""" | ||
This file is a copy of https://github.com/bazelbuild/bazel/blob/master/tools/cpp/cc_configure.bzl | ||
with some minor changes. The original file is licensed under Apache 2.0 license. The gist of this | ||
is that we want darwin to register the local toolchain and disregard the environment variable of | ||
BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN. We must support a local toolchain for darwin until | ||
hermetic_cc_toolchain supports darwin's sysroot in a hermetic way. | ||
""" | ||
|
||
def cc_autoconf_toolchains_impl(repository_ctx): | ||
"""Generate BUILD file with 'toolchain' targets for the local host C++ toolchain. | ||
Args: | ||
repository_ctx: repository context | ||
""" | ||
|
||
cpu_value = get_cpu_value(repository_ctx) | ||
|
||
if cpu_value.startswith("darwin"): | ||
paths = resolve_labels(repository_ctx, [ | ||
"@bazel_tools//tools/cpp:BUILD.toolchains.tpl", | ||
]) | ||
repository_ctx.template( | ||
"BUILD", | ||
paths["@bazel_tools//tools/cpp:BUILD.toolchains.tpl"], | ||
{"%{name}": get_cpu_value(repository_ctx)}, | ||
) | ||
else: | ||
repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.") | ||
|
||
|
||
def cc_autoconf_impl(repository_ctx, overriden_tools = dict()): | ||
"""Generate BUILD file with 'cc_toolchain' targets for the local host C++ toolchain. | ||
Args: | ||
repository_ctx: repository context | ||
overriden_tools: dict of tool paths to use instead of autoconfigured tools | ||
""" | ||
cpu_value = get_cpu_value(repository_ctx) | ||
|
||
if cpu_value.startswith("darwin"): | ||
print("Configuring local C++ toolchain for Darwin. This is non-hermetic and builds may " + | ||
"not be reproducible. Consider building on linux for a hermetic build.") | ||
configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools) | ||
else: | ||
paths = resolve_labels(repository_ctx, [ | ||
"@bazel_tools//tools/cpp:BUILD.empty.tpl", | ||
"@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl", | ||
]) | ||
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl") | ||
repository_ctx.template("BUILD", paths["@bazel_tools//tools/cpp:BUILD.empty.tpl"], { | ||
"%{cpu}": get_cpu_value(repository_ctx), | ||
}) | ||
|
||
cc_autoconf_toolchains = repository_rule( | ||
environ = [ | ||
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", | ||
], | ||
implementation = cc_autoconf_toolchains_impl, | ||
configure = True, | ||
) | ||
|
||
cc_autoconf = repository_rule( | ||
environ = [ | ||
"ABI_LIBC_VERSION", | ||
"ABI_VERSION", | ||
"BAZEL_COMPILER", | ||
"BAZEL_HOST_SYSTEM", | ||
"BAZEL_CONLYOPTS", | ||
"BAZEL_CXXOPTS", | ||
"BAZEL_LINKOPTS", | ||
"BAZEL_LINKLIBS", | ||
"BAZEL_LLVM_COV", | ||
"BAZEL_LLVM_PROFDATA", | ||
"BAZEL_PYTHON", | ||
"BAZEL_SH", | ||
"BAZEL_TARGET_CPU", | ||
"BAZEL_TARGET_LIBC", | ||
"BAZEL_TARGET_SYSTEM", | ||
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", | ||
"BAZEL_USE_LLVM_NATIVE_COVERAGE", | ||
"BAZEL_LLVM", | ||
"BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS", | ||
"USE_CLANG_CL", | ||
"CC", | ||
"CC_CONFIGURE_DEBUG", | ||
"CC_TOOLCHAIN_NAME", | ||
"CPLUS_INCLUDE_PATH", | ||
"DEVELOPER_DIR", | ||
"GCOV", | ||
"LIBTOOL", | ||
"HOMEBREW_RUBY_PATH", | ||
"SYSTEMROOT", | ||
"USER", | ||
], | ||
implementation = cc_autoconf_impl, | ||
configure = True, | ||
) | ||
|
||
def configure_nonhermetic_darwin(): | ||
"""A C++ configuration rules that generate the crosstool file.""" | ||
cc_autoconf_toolchains(name = "local_config_cc_toolchains") | ||
cc_autoconf(name = "local_config_cc") | ||
native.bind(name = "cc_toolchain", actual = "@local_config_cc//:toolchain") | ||
native.register_toolchains( | ||
# Use register_toolchain's target pattern expansion to register all toolchains in the package. | ||
"@local_config_cc_toolchains//:all", | ||
) |