Skip to content

Commit 3b661c0

Browse files
committed
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`.
1 parent fe41fc4 commit 3b661c0

File tree

4 files changed

+183
-15
lines changed

4 files changed

+183
-15
lines changed

cc/private/toolchain/BUILD.windows.tpl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ cc_toolchain_config(
124124
cxx_builtin_include_directories = [%{cxx_builtin_include_directories}],
125125
tool_paths = {%{tool_paths}},
126126
tool_bin_path = "%{tool_bin_path}",
127+
c_flags = [%{c_flags}],
128+
conly_flags = [%{conly_flags}],
129+
cxx_flags = [%{cxx_flags}],
130+
link_flags = [%{link_flags}],
131+
win32_winnt_flag = "%{win32_winnt_flag}",
127132
)
128133

129134
toolchain(
@@ -170,6 +175,11 @@ cc_toolchain_config(
170175
tool_bin_path = "%{tool_bin_path}",
171176
default_compile_flags = ["-m32"],
172177
default_link_flags = ["-m32"],
178+
c_flags = [%{c_flags}],
179+
conly_flags = [%{conly_flags}],
180+
cxx_flags = [%{cxx_flags}],
181+
link_flags = [%{link_flags}],
182+
win32_winnt_flag = "%{win32_winnt_flag}",
173183
)
174184

175185
toolchain(
@@ -214,6 +224,11 @@ cc_toolchain_config(
214224
tool_bin_path = "%{mingw_tool_bin_path}",
215225
cxx_builtin_include_directories = [%{mingw_cxx_builtin_include_directories}],
216226
tool_paths = {%{mingw_tool_paths}},
227+
c_flags = [%{c_flags}],
228+
conly_flags = [%{conly_flags}],
229+
cxx_flags = [%{cxx_flags}],
230+
link_flags = [%{link_flags}],
231+
win32_winnt_flag = "%{win32_winnt_flag}",
217232
)
218233

219234
toolchain(
@@ -260,6 +275,11 @@ cc_toolchain_config(
260275
tool_paths = {%{mingw_tool_paths}},
261276
default_compile_flags = ["-m32"],
262277
default_link_flags = ["-m32"],
278+
c_flags = [%{c_flags}],
279+
conly_flags = [%{conly_flags}],
280+
cxx_flags = [%{cxx_flags}],
281+
link_flags = [%{link_flags}],
282+
win32_winnt_flag = "%{win32_winnt_flag}",
263283
)
264284

265285
toolchain(
@@ -330,6 +350,11 @@ cc_toolchain_config(
330350
dbg_mode_debug_flag = "%{dbg_mode_debug_flag_x64}",
331351
fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_x64}",
332352
supports_parse_showincludes = %{msvc_parse_showincludes_x64},
353+
c_flags = [%{c_flags}],
354+
conly_flags = [%{conly_flags}],
355+
cxx_flags = [%{cxx_flags}],
356+
link_flags = [%{link_flags}],
357+
win32_winnt_flag = "%{win32_winnt_flag}",
333358
)
334359

335360
toolchain(
@@ -399,6 +424,11 @@ cc_toolchain_config(
399424
dbg_mode_debug_flag = "%{dbg_mode_debug_flag_x86}",
400425
fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_x86}",
401426
supports_parse_showincludes = %{msvc_parse_showincludes_x86},
427+
c_flags = [%{c_flags}],
428+
conly_flags = [%{conly_flags}],
429+
cxx_flags = [%{cxx_flags}],
430+
link_flags = [%{link_flags}],
431+
win32_winnt_flag = "%{win32_winnt_flag}",
402432
)
403433

404434
toolchain(
@@ -468,6 +498,11 @@ cc_toolchain_config(
468498
dbg_mode_debug_flag = "%{dbg_mode_debug_flag_arm}",
469499
fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_arm}",
470500
supports_parse_showincludes = %{msvc_parse_showincludes_arm},
501+
c_flags = [%{c_flags}],
502+
conly_flags = [%{conly_flags}],
503+
cxx_flags = [%{cxx_flags}],
504+
link_flags = [%{link_flags}],
505+
win32_winnt_flag = "%{win32_winnt_flag}",
471506
)
472507

473508
toolchain(
@@ -537,6 +572,11 @@ cc_toolchain_config(
537572
dbg_mode_debug_flag = "%{dbg_mode_debug_flag_arm64}",
538573
fastbuild_mode_debug_flag = "%{fastbuild_mode_debug_flag_arm64}",
539574
supports_parse_showincludes = %{msvc_parse_showincludes_arm64},
575+
c_flags = [%{c_flags}],
576+
conly_flags = [%{conly_flags}],
577+
cxx_flags = [%{cxx_flags}],
578+
link_flags = [%{link_flags}],
579+
win32_winnt_flag = "%{win32_winnt_flag}",
540580
)
541581

542582
toolchain(
@@ -604,6 +644,11 @@ cc_toolchain_config(
604644
dbg_mode_debug_flag = "%{clang_cl_dbg_mode_debug_flag_x64}",
605645
fastbuild_mode_debug_flag = "%{clang_cl_fastbuild_mode_debug_flag_x64}",
606646
supports_parse_showincludes = %{clang_cl_parse_showincludes_x64},
647+
c_flags = [%{c_flags}],
648+
conly_flags = [%{conly_flags}],
649+
cxx_flags = [%{cxx_flags}],
650+
link_flags = [%{link_flags}],
651+
win32_winnt_flag = "%{win32_winnt_flag}",
607652
)
608653

609654
toolchain(
@@ -672,6 +717,11 @@ cc_toolchain_config(
672717
dbg_mode_debug_flag = "%{clang_cl_dbg_mode_debug_flag_arm64}",
673718
fastbuild_mode_debug_flag = "%{clang_cl_fastbuild_mode_debug_flag_arm64}",
674719
supports_parse_showincludes = %{clang_cl_parse_showincludes_arm64},
720+
c_flags = [%{c_flags}],
721+
conly_flags = [%{conly_flags}],
722+
cxx_flags = [%{cxx_flags}],
723+
link_flags = [%{link_flags}],
724+
win32_winnt_flag = "%{win32_winnt_flag}",
675725
)
676726

677727
toolchain(

cc/private/toolchain/cc_configure.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ cc_autoconf = repository_rule(
125125
"BAZEL_TARGET_SYSTEM",
126126
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN",
127127
"BAZEL_USE_LLVM_NATIVE_COVERAGE",
128+
"BAZEL_WIN32_WINNT",
128129
"BAZEL_LLVM",
129130
"BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS",
130131
"USE_CLANG_CL",

cc/private/toolchain/windows_cc_configure.bzl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ load(
2121
"auto_configure_warning_maybe",
2222
"escape_string",
2323
"execute",
24+
"get_starlark_list",
2425
"resolve_labels",
26+
"split_escaped",
2527
"write_builtin_include_directory_paths",
2628
)
2729

@@ -848,6 +850,42 @@ def _get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, target_arc
848850
"%{msvc_deps_scanner_wrapper_path_" + target_arch + "}": "msvc_deps_scanner_wrapper_" + target_arch + ".bat",
849851
}
850852

853+
def _get_copts(repository_ctx):
854+
"""Get the variables we need to populate the msys/mingw toolchains."""
855+
conly_opts = split_escaped(_get_env_var(
856+
repository_ctx,
857+
"BAZEL_CONLYOPTS",
858+
"",
859+
), ":")
860+
c_opts = split_escaped(_get_env_var(
861+
repository_ctx,
862+
"BAZEL_COPTS",
863+
"",
864+
), ":")
865+
cxx_opts = split_escaped(_get_env_var(
866+
repository_ctx,
867+
"BAZEL_CXXOPTS",
868+
"",
869+
), ":")
870+
link_opts = split_escaped(_get_env_var(
871+
repository_ctx,
872+
"BAZEL_LINKOPTS",
873+
"",
874+
), ":")
875+
win32_winnt_opts = _get_env_var(
876+
repository_ctx,
877+
"BAZEL_WIN32_WINNT",
878+
"/D_WIN32_WINNT=0x0601",
879+
)
880+
copts_vars = {
881+
"%{c_flags}": get_starlark_list(c_opts),
882+
"%{conly_flags}": get_starlark_list(conly_opts),
883+
"%{cxx_flags}": get_starlark_list(cxx_opts),
884+
"%{link_flags}": get_starlark_list(link_opts),
885+
"%{win32_winnt_flag}": escape_string(win32_winnt_opts),
886+
}
887+
return copts_vars
888+
851889
def configure_windows_toolchain(repository_ctx):
852890
"""Configure C++ toolchain on Windows.
853891
@@ -882,6 +920,7 @@ def configure_windows_toolchain(repository_ctx):
882920
template_vars.update(msvc_vars_x64)
883921
template_vars.update(_get_clang_cl_vars(repository_ctx, paths, msvc_vars_x64, "x64"))
884922
template_vars.update(_get_msys_mingw_vars(repository_ctx))
923+
template_vars.update(_get_copts(repository_ctx))
885924
template_vars.update(_get_msvc_vars(repository_ctx, paths, "x86", msvc_vars_x64))
886925
template_vars.update(_get_msvc_vars(repository_ctx, paths, "arm", msvc_vars_x64))
887926
msvc_vars_arm64 = _get_msvc_vars(repository_ctx, paths, "arm64", msvc_vars_x64)

cc/private/toolchain/windows_cc_toolchain_config.bzl

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ all_link_actions = [
8888
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
8989
]
9090

91+
lto_index_actions = [
92+
ACTION_NAMES.lto_index_for_executable,
93+
ACTION_NAMES.lto_index_for_dynamic_library,
94+
ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
95+
]
96+
9197
def _use_msvc_toolchain(ctx):
9298
return ctx.attr.cpu in ["x64_windows", "arm64_windows"] and (ctx.attr.compiler == "msvc-cl" or ctx.attr.compiler == "clang-cl")
9399

@@ -579,14 +585,42 @@ def _impl(ctx):
579585
ACTION_NAMES.cpp20_module_compile,
580586
ACTION_NAMES.cpp20_module_codegen,
581587
],
582-
flag_groups = [
588+
flag_groups = ([
589+
flag_group(
590+
flags = ctx.attr.c_flags,
591+
),
592+
] if ctx.attr.c_flags else []) + [
583593
flag_group(
584594
flags = ["%{user_compile_flags}"],
585595
iterate_over = "user_compile_flags",
586596
expand_if_available = "user_compile_flags",
587597
),
588598
],
589599
),
600+
flag_set(
601+
actions = [ACTION_NAMES.c_compile],
602+
flag_groups = ([
603+
flag_group(
604+
flags = ctx.attr.conly_flags,
605+
),
606+
] if ctx.attr.conly_flags else []),
607+
),
608+
flag_set(
609+
actions = [
610+
ACTION_NAMES.linkstamp_compile,
611+
ACTION_NAMES.cpp_compile,
612+
ACTION_NAMES.cpp_header_parsing,
613+
ACTION_NAMES.cpp_module_compile,
614+
ACTION_NAMES.cpp_module_codegen,
615+
ACTION_NAMES.lto_backend,
616+
ACTION_NAMES.clif_match,
617+
],
618+
flag_groups = ([
619+
flag_group(
620+
flags = ctx.attr.cxx_flags,
621+
),
622+
] if ctx.attr.cxx_flags else []),
623+
),
590624
],
591625
)
592626

@@ -725,9 +759,32 @@ def _impl(ctx):
725759
),
726760
],
727761
),
762+
flag_set(
763+
actions = all_link_actions + lto_index_actions,
764+
flag_groups = ([
765+
flag_group(
766+
flags = ctx.attr.link_flags,
767+
),
768+
] if ctx.attr.link_flags else []),
769+
),
728770
],
729771
)
730772

773+
default_compile_flags_list = [
774+
"/DNOMINMAX",
775+
"/D_CRT_SECURE_NO_DEPRECATE",
776+
"/D_CRT_SECURE_NO_WARNINGS",
777+
"/bigobj",
778+
"/Zm500",
779+
"/EHsc",
780+
"/wd4351",
781+
"/wd4291",
782+
"/wd4250",
783+
"/wd4996",
784+
]
785+
if ctx.attr.win32_winnt_flag:
786+
default_compile_flags_list.append(ctx.attr.win32_winnt_flag)
787+
731788
default_compile_flags_feature = feature(
732789
name = "default_compile_flags",
733790
enabled = True,
@@ -750,19 +807,7 @@ def _impl(ctx):
750807
],
751808
flag_groups = [
752809
flag_group(
753-
flags = [
754-
"/DNOMINMAX",
755-
"/D_WIN32_WINNT=0x0601",
756-
"/D_CRT_SECURE_NO_DEPRECATE",
757-
"/D_CRT_SECURE_NO_WARNINGS",
758-
"/bigobj",
759-
"/Zm500",
760-
"/EHsc",
761-
"/wd4351",
762-
"/wd4291",
763-
"/wd4250",
764-
"/wd4996",
765-
],
810+
flags = default_compile_flags_list,
766811
),
767812
],
768813
),
@@ -1533,14 +1578,42 @@ def _impl(ctx):
15331578
ACTION_NAMES.lto_backend,
15341579
ACTION_NAMES.clif_match,
15351580
],
1536-
flag_groups = [
1581+
flag_groups = ([
1582+
flag_group(
1583+
flags = ctx.attr.c_flags,
1584+
),
1585+
] if ctx.attr.c_flags else []) + [
15371586
flag_group(
15381587
flags = ["%{user_compile_flags}"],
15391588
iterate_over = "user_compile_flags",
15401589
expand_if_available = "user_compile_flags",
15411590
),
15421591
],
15431592
),
1593+
flag_set(
1594+
actions = [ACTION_NAMES.c_compile],
1595+
flag_groups = ([
1596+
flag_group(
1597+
flags = ctx.attr.conly_flags,
1598+
),
1599+
] if ctx.attr.conly_flags else []),
1600+
),
1601+
flag_set(
1602+
actions = [
1603+
ACTION_NAMES.linkstamp_compile,
1604+
ACTION_NAMES.cpp_compile,
1605+
ACTION_NAMES.cpp_header_parsing,
1606+
ACTION_NAMES.cpp_module_compile,
1607+
ACTION_NAMES.cpp_module_codegen,
1608+
ACTION_NAMES.lto_backend,
1609+
ACTION_NAMES.clif_match,
1610+
],
1611+
flag_groups = ([
1612+
flag_group(
1613+
flags = ctx.attr.cxx_flags,
1614+
),
1615+
] if ctx.attr.cxx_flags else []),
1616+
),
15441617
],
15451618
)
15461619

@@ -1641,14 +1714,18 @@ cc_toolchain_config = rule(
16411714
"abi_libc_version": attr.string(),
16421715
"abi_version": attr.string(),
16431716
"archiver_flags": attr.string_list(default = []),
1717+
"c_flags": attr.string_list(),
16441718
"compiler": attr.string(),
1719+
"conly_flags": attr.string_list(),
16451720
"cpu": attr.string(mandatory = True),
16461721
"cxx_builtin_include_directories": attr.string_list(),
1722+
"cxx_flags": attr.string_list(),
16471723
"dbg_mode_debug_flag": attr.string(default = ""),
16481724
"default_compile_flags": attr.string_list(default = []),
16491725
"default_link_flags": attr.string_list(default = []),
16501726
"fastbuild_mode_debug_flag": attr.string(default = ""),
16511727
"host_system_name": attr.string(),
1728+
"link_flags": attr.string_list(),
16521729
"msvc_cl_path": attr.string(default = "vc_installation_error.bat"),
16531730
"msvc_env_include": attr.string(default = "msvc_not_found"),
16541731
"msvc_env_lib": attr.string(default = "msvc_not_found"),
@@ -1663,6 +1740,7 @@ cc_toolchain_config = rule(
16631740
"tool_bin_path": attr.string(default = "not_found"),
16641741
"tool_paths": attr.string_dict(),
16651742
"toolchain_identifier": attr.string(),
1743+
"win32_winnt_flag": attr.string(default = "/D_WIN32_WINNT=0x0601"),
16661744
},
16671745
provides = [CcToolchainConfigInfo],
16681746
)

0 commit comments

Comments
 (0)