From 3b661c091d6feb1027a2439b6372a0a930a1b845 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Sat, 5 Jul 2025 16:02:17 -0700 Subject: [PATCH 1/2] Make windows toolchain configurable with BAZEL_*OPTS This brings the same functionality that exists in Linux to Windows. When cross compiling with transitions, --copts and friends apply to both the host OS, and the target of cross compilation. This necessitates moving all flags to the toolchain. There is no way to do this today for the windows toolchains without writing a toolchain from scratch, which isn't ideal for a couple of small flag changes. While we are here, make `/D_WIN32_WINNT=0x0601` configurable too in the same way, with `BAZEL_WIN32_WINNT`. --- cc/private/toolchain/BUILD.windows.tpl | 50 ++++++++ cc/private/toolchain/cc_configure.bzl | 1 + cc/private/toolchain/windows_cc_configure.bzl | 39 +++++++ .../toolchain/windows_cc_toolchain_config.bzl | 108 +++++++++++++++--- 4 files changed, 183 insertions(+), 15 deletions(-) diff --git a/cc/private/toolchain/BUILD.windows.tpl b/cc/private/toolchain/BUILD.windows.tpl index 75fbd0a8..2ff8c3a2 100644 --- a/cc/private/toolchain/BUILD.windows.tpl +++ b/cc/private/toolchain/BUILD.windows.tpl @@ -124,6 +124,11 @@ cc_toolchain_config( cxx_builtin_include_directories = [%{cxx_builtin_include_directories}], tool_paths = {%{tool_paths}}, tool_bin_path = "%{tool_bin_path}", + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -170,6 +175,11 @@ cc_toolchain_config( tool_bin_path = "%{tool_bin_path}", default_compile_flags = ["-m32"], default_link_flags = ["-m32"], + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -214,6 +224,11 @@ cc_toolchain_config( tool_bin_path = "%{mingw_tool_bin_path}", cxx_builtin_include_directories = [%{mingw_cxx_builtin_include_directories}], tool_paths = {%{mingw_tool_paths}}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -260,6 +275,11 @@ cc_toolchain_config( tool_paths = {%{mingw_tool_paths}}, default_compile_flags = ["-m32"], default_link_flags = ["-m32"], + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -330,6 +350,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{dbg_mode_debug_flag_x64}", fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_x64}", supports_parse_showincludes = %{msvc_parse_showincludes_x64}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -399,6 +424,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{dbg_mode_debug_flag_x86}", fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_x86}", supports_parse_showincludes = %{msvc_parse_showincludes_x86}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -468,6 +498,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{dbg_mode_debug_flag_arm}", fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_arm}", supports_parse_showincludes = %{msvc_parse_showincludes_arm}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -537,6 +572,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{dbg_mode_debug_flag_arm64}", fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_arm64}", supports_parse_showincludes = %{msvc_parse_showincludes_arm64}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -604,6 +644,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{clang_cl_dbg_mode_debug_flag_x64}", fastbuild_mode_debug_flag = "%{clang_cl_fastbuild_mode_debug_flag_x64}", supports_parse_showincludes = %{clang_cl_parse_showincludes_x64}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( @@ -672,6 +717,11 @@ cc_toolchain_config( dbg_mode_debug_flag = "%{clang_cl_dbg_mode_debug_flag_arm64}", fastbuild_mode_debug_flag = "%{clang_cl_fastbuild_mode_debug_flag_arm64}", supports_parse_showincludes = %{clang_cl_parse_showincludes_arm64}, + c_flags = [%{c_flags}], + conly_flags = [%{conly_flags}], + cxx_flags = [%{cxx_flags}], + link_flags = [%{link_flags}], + win32_winnt_flag = "%{win32_winnt_flag}", ) toolchain( diff --git a/cc/private/toolchain/cc_configure.bzl b/cc/private/toolchain/cc_configure.bzl index ce0dac5c..475a9dd5 100644 --- a/cc/private/toolchain/cc_configure.bzl +++ b/cc/private/toolchain/cc_configure.bzl @@ -125,6 +125,7 @@ cc_autoconf = repository_rule( "BAZEL_TARGET_SYSTEM", "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", "BAZEL_USE_LLVM_NATIVE_COVERAGE", + "BAZEL_WIN32_WINNT", "BAZEL_LLVM", "BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS", "USE_CLANG_CL", diff --git a/cc/private/toolchain/windows_cc_configure.bzl b/cc/private/toolchain/windows_cc_configure.bzl index 66ca5fb1..7581ce7e 100644 --- a/cc/private/toolchain/windows_cc_configure.bzl +++ b/cc/private/toolchain/windows_cc_configure.bzl @@ -21,7 +21,9 @@ load( "auto_configure_warning_maybe", "escape_string", "execute", + "get_starlark_list", "resolve_labels", + "split_escaped", "write_builtin_include_directory_paths", ) @@ -848,6 +850,42 @@ def _get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, target_arc "%{msvc_deps_scanner_wrapper_path_" + target_arch + "}": "msvc_deps_scanner_wrapper_" + target_arch + ".bat", } +def _get_copts(repository_ctx): + """Get the variables we need to populate the msys/mingw toolchains.""" + conly_opts = split_escaped(_get_env_var( + repository_ctx, + "BAZEL_CONLYOPTS", + "", + ), ":") + c_opts = split_escaped(_get_env_var( + repository_ctx, + "BAZEL_COPTS", + "", + ), ":") + cxx_opts = split_escaped(_get_env_var( + repository_ctx, + "BAZEL_CXXOPTS", + "", + ), ":") + link_opts = split_escaped(_get_env_var( + repository_ctx, + "BAZEL_LINKOPTS", + "", + ), ":") + win32_winnt_opts = _get_env_var( + repository_ctx, + "BAZEL_WIN32_WINNT", + "/D_WIN32_WINNT=0x0601", + ) + copts_vars = { + "%{c_flags}": get_starlark_list(c_opts), + "%{conly_flags}": get_starlark_list(conly_opts), + "%{cxx_flags}": get_starlark_list(cxx_opts), + "%{link_flags}": get_starlark_list(link_opts), + "%{win32_winnt_flag}": escape_string(win32_winnt_opts), + } + return copts_vars + def configure_windows_toolchain(repository_ctx): """Configure C++ toolchain on Windows. @@ -882,6 +920,7 @@ def configure_windows_toolchain(repository_ctx): template_vars.update(msvc_vars_x64) template_vars.update(_get_clang_cl_vars(repository_ctx, paths, msvc_vars_x64, "x64")) template_vars.update(_get_msys_mingw_vars(repository_ctx)) + template_vars.update(_get_copts(repository_ctx)) template_vars.update(_get_msvc_vars(repository_ctx, paths, "x86", msvc_vars_x64)) template_vars.update(_get_msvc_vars(repository_ctx, paths, "arm", msvc_vars_x64)) msvc_vars_arm64 = _get_msvc_vars(repository_ctx, paths, "arm64", msvc_vars_x64) diff --git a/cc/private/toolchain/windows_cc_toolchain_config.bzl b/cc/private/toolchain/windows_cc_toolchain_config.bzl index d8cf50e1..f1c97abe 100644 --- a/cc/private/toolchain/windows_cc_toolchain_config.bzl +++ b/cc/private/toolchain/windows_cc_toolchain_config.bzl @@ -88,6 +88,12 @@ all_link_actions = [ 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 _use_msvc_toolchain(ctx): return ctx.attr.cpu in ["x64_windows", "arm64_windows"] and (ctx.attr.compiler == "msvc-cl" or ctx.attr.compiler == "clang-cl") @@ -579,7 +585,11 @@ def _impl(ctx): ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.cpp20_module_codegen, ], - flag_groups = [ + flag_groups = ([ + flag_group( + flags = ctx.attr.c_flags, + ), + ] if ctx.attr.c_flags else []) + [ flag_group( flags = ["%{user_compile_flags}"], iterate_over = "user_compile_flags", @@ -587,6 +597,30 @@ def _impl(ctx): ), ], ), + 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 = [ + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = ([ + flag_group( + flags = ctx.attr.cxx_flags, + ), + ] if ctx.attr.cxx_flags else []), + ), ], ) @@ -725,9 +759,32 @@ def _impl(ctx): ), ], ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.link_flags, + ), + ] if ctx.attr.link_flags else []), + ), ], ) + default_compile_flags_list = [ + "/DNOMINMAX", + "/D_CRT_SECURE_NO_DEPRECATE", + "/D_CRT_SECURE_NO_WARNINGS", + "/bigobj", + "/Zm500", + "/EHsc", + "/wd4351", + "/wd4291", + "/wd4250", + "/wd4996", + ] + if ctx.attr.win32_winnt_flag: + default_compile_flags_list.append(ctx.attr.win32_winnt_flag) + default_compile_flags_feature = feature( name = "default_compile_flags", enabled = True, @@ -750,19 +807,7 @@ def _impl(ctx): ], flag_groups = [ flag_group( - flags = [ - "/DNOMINMAX", - "/D_WIN32_WINNT=0x0601", - "/D_CRT_SECURE_NO_DEPRECATE", - "/D_CRT_SECURE_NO_WARNINGS", - "/bigobj", - "/Zm500", - "/EHsc", - "/wd4351", - "/wd4291", - "/wd4250", - "/wd4996", - ], + flags = default_compile_flags_list, ), ], ), @@ -1533,7 +1578,11 @@ def _impl(ctx): ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ], - flag_groups = [ + flag_groups = ([ + flag_group( + flags = ctx.attr.c_flags, + ), + ] if ctx.attr.c_flags else []) + [ flag_group( flags = ["%{user_compile_flags}"], iterate_over = "user_compile_flags", @@ -1541,6 +1590,30 @@ def _impl(ctx): ), ], ), + 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 = [ + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ], + flag_groups = ([ + flag_group( + flags = ctx.attr.cxx_flags, + ), + ] if ctx.attr.cxx_flags else []), + ), ], ) @@ -1641,14 +1714,18 @@ cc_toolchain_config = rule( "abi_libc_version": attr.string(), "abi_version": attr.string(), "archiver_flags": attr.string_list(default = []), + "c_flags": attr.string_list(), "compiler": attr.string(), + "conly_flags": attr.string_list(), "cpu": attr.string(mandatory = True), "cxx_builtin_include_directories": attr.string_list(), + "cxx_flags": attr.string_list(), "dbg_mode_debug_flag": attr.string(default = ""), "default_compile_flags": attr.string_list(default = []), "default_link_flags": attr.string_list(default = []), "fastbuild_mode_debug_flag": attr.string(default = ""), "host_system_name": attr.string(), + "link_flags": attr.string_list(), "msvc_cl_path": attr.string(default = "vc_installation_error.bat"), "msvc_env_include": attr.string(default = "msvc_not_found"), "msvc_env_lib": attr.string(default = "msvc_not_found"), @@ -1663,6 +1740,7 @@ cc_toolchain_config = rule( "tool_bin_path": attr.string(default = "not_found"), "tool_paths": attr.string_dict(), "toolchain_identifier": attr.string(), + "win32_winnt_flag": attr.string(default = "/D_WIN32_WINNT=0x0601"), }, provides = [CcToolchainConfigInfo], ) From 731963a00fe647ed57dfefd64eaad76890888861 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Tue, 22 Jul 2025 20:28:53 -0700 Subject: [PATCH 2/2] Deduplicate the default field value for BAZEL_WIN32_WINNT --- cc/private/toolchain/BUILD.windows.tpl | 20 +++++++++---------- cc/private/toolchain/windows_cc_configure.bzl | 7 +++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cc/private/toolchain/BUILD.windows.tpl b/cc/private/toolchain/BUILD.windows.tpl index 2ff8c3a2..8f025403 100644 --- a/cc/private/toolchain/BUILD.windows.tpl +++ b/cc/private/toolchain/BUILD.windows.tpl @@ -128,7 +128,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -179,7 +179,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -228,7 +228,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -279,7 +279,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -354,7 +354,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -428,7 +428,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -502,7 +502,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -576,7 +576,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -648,7 +648,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( @@ -721,7 +721,7 @@ cc_toolchain_config( conly_flags = [%{conly_flags}], cxx_flags = [%{cxx_flags}], link_flags = [%{link_flags}], - win32_winnt_flag = "%{win32_winnt_flag}", +%{win32_winnt_flag} ) toolchain( diff --git a/cc/private/toolchain/windows_cc_configure.bzl b/cc/private/toolchain/windows_cc_configure.bzl index 7581ce7e..b6f8e260 100644 --- a/cc/private/toolchain/windows_cc_configure.bzl +++ b/cc/private/toolchain/windows_cc_configure.bzl @@ -875,15 +875,18 @@ def _get_copts(repository_ctx): win32_winnt_opts = _get_env_var( repository_ctx, "BAZEL_WIN32_WINNT", - "/D_WIN32_WINNT=0x0601", + None, ) copts_vars = { "%{c_flags}": get_starlark_list(c_opts), "%{conly_flags}": get_starlark_list(conly_opts), "%{cxx_flags}": get_starlark_list(cxx_opts), "%{link_flags}": get_starlark_list(link_opts), - "%{win32_winnt_flag}": escape_string(win32_winnt_opts), + # None means the user didn't provide the override. + # Fall back to the default in that case. + "%{win32_winnt_flag}": ' win32_winnt_flag = "' + escape_string(win32_winnt_opts) + '", ' if win32_winnt_opts != None else "", } + return copts_vars def configure_windows_toolchain(repository_ctx):