From e8c306d0e1f7d559dc17250d3f1ca4a2572514e0 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Sat, 5 Jul 2025 15:49:42 -0700 Subject: [PATCH 1/2] Add BAZEL_COPTS to mirror --copts This lets us fully replace --copts, --cxxopts, --conlyopts, and --linkopts with --repo_env. When triggering transitions to other platforms, these flags apply to all platforms. This is especially problematic when cross compiling for a different operating system. Signed-off-by: Austin Schuh --- cc/private/toolchain/BUILD.tpl | 1 + cc/private/toolchain/cc_configure.bzl | 1 + cc/private/toolchain/unix_cc_configure.bzl | 7 +++++++ cc/private/toolchain/unix_cc_toolchain_config.bzl | 9 +++++++++ 4 files changed, 18 insertions(+) diff --git a/cc/private/toolchain/BUILD.tpl b/cc/private/toolchain/BUILD.tpl index 522d67e4..afc8a257 100644 --- a/cc/private/toolchain/BUILD.tpl +++ b/cc/private/toolchain/BUILD.tpl @@ -115,6 +115,7 @@ cc_toolchain_config( opt_compile_flags = [%{opt_compile_flags}], dbg_compile_flags = [%{dbg_compile_flags}], conly_flags = [%{conly_flags}], + c_flags = [%{c_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], link_libs = [%{link_libs}], diff --git a/cc/private/toolchain/cc_configure.bzl b/cc/private/toolchain/cc_configure.bzl index ce0dac5c..8cbf8749 100644 --- a/cc/private/toolchain/cc_configure.bzl +++ b/cc/private/toolchain/cc_configure.bzl @@ -113,6 +113,7 @@ cc_autoconf = repository_rule( "BAZEL_COMPILER", "BAZEL_HOST_SYSTEM", "BAZEL_CONLYOPTS", + "BAZEL_COPTS", "BAZEL_CXXOPTS", "BAZEL_LINKOPTS", "BAZEL_LINKLIBS", diff --git a/cc/private/toolchain/unix_cc_configure.bzl b/cc/private/toolchain/unix_cc_configure.bzl index 6a6f38a3..50270a19 100644 --- a/cc/private/toolchain/unix_cc_configure.bzl +++ b/cc/private/toolchain/unix_cc_configure.bzl @@ -459,6 +459,12 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overridden_tools): }, ) + c_opts = split_escaped(get_env_var( + repository_ctx, + "BAZEL_COPTS", + "", + False, + ), ":") conly_opts = split_escaped(get_env_var( repository_ctx, "BAZEL_CONLYOPTS", @@ -662,6 +668,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overridden_tools): False, )), "%{conly_flags}": get_starlark_list(conly_opts), + "%{c_flags}": get_starlark_list(c_opts), "%{coverage_compile_flags}": coverage_compile_flags, "%{coverage_link_flags}": coverage_link_flags, "%{cxx_builtin_include_directories}": get_starlark_list(builtin_include_directories), diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl index fcc7a24a..7f2eb1ac 100644 --- a/cc/private/toolchain/unix_cc_toolchain_config.bzl +++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl @@ -407,6 +407,14 @@ def _impl(ctx): ] if ctx.attr.opt_compile_flags else []), with_features = [with_feature_set(features = ["opt"])], ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.c_flags, + ), + ] if ctx.attr.c_flags else []), + ), flag_set( actions = [ACTION_NAMES.c_compile], flag_groups = ([ @@ -1920,6 +1928,7 @@ cc_toolchain_config = rule( "compile_flags": attr.string_list(), "compiler": attr.string(mandatory = True), "conly_flags": attr.string_list(), + "c_flags": attr.string_list(), "coverage_compile_flags": attr.string_list(), "coverage_link_flags": attr.string_list(), "cpu": attr.string(mandatory = True), From cf2ec1e3d6fcc212a4de3470278680b98aaeb016 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Sat, 5 Jul 2025 15:55:23 -0700 Subject: [PATCH 2/2] Buildifier --- cc/private/toolchain/unix_cc_toolchain_config.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl index 7f2eb1ac..dbf3b620 100644 --- a/cc/private/toolchain/unix_cc_toolchain_config.bzl +++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl @@ -1925,10 +1925,10 @@ cc_toolchain_config = rule( "abi_version": attr.string(mandatory = True), "archive_flags": attr.string_list(), "builtin_sysroot": attr.string(), + "c_flags": attr.string_list(), "compile_flags": attr.string_list(), "compiler": attr.string(mandatory = True), "conly_flags": attr.string_list(), - "c_flags": attr.string_list(), "coverage_compile_flags": attr.string_list(), "coverage_link_flags": attr.string_list(), "cpu": attr.string(mandatory = True),