Skip to content

Commit 96c57f8

Browse files
committed
Fix cross-compilation to Windows with clang MinGW
* Replace `select`s on flags for Windows with the appropriate compiler constraints. Also update `blake3`, which contains the same type of fix in its most recent version. * Ditch the `cmd.exe` implementation of `merge_licenses` that incorrectly matched on the target rather than the exec platform. Bash is already a requirement for Bazel at this point, so we might as well use it. * Drop the explicit C++17 standard flags since this is already the default standard in Bazel@HEAD.
1 parent 7b532fc commit 96c57f8

File tree

9 files changed

+80
-75
lines changed

9 files changed

+80
-75
lines changed

MODULE.bazel

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module(
1515

1616
bazel_dep(name = "abseil-cpp", version = "20250814.1")
1717
bazel_dep(name = "bazel_skylib", version = "1.8.2")
18-
bazel_dep(name = "blake3", version = "1.8.2")
18+
bazel_dep(name = "blake3", version = "1.8.2.bcr.1")
1919
bazel_dep(name = "googleapis-grpc-java", version = "1.0.0")
2020
bazel_dep(name = "googleapis-java", version = "1.0.0")
2121
bazel_dep(name = "googleapis", version = "0.0.0-20250604-de157ca3")
@@ -42,7 +42,14 @@ bazel_dep(name = "zstd-jni", version = "1.5.6-9")
4242
# Depend on apple_support first and then rules_cc so that the Xcode toolchain
4343
# from apple_support wins over the generic Unix toolchain from rules_cc.
4444
bazel_dep(name = "apple_support", version = "1.24.5")
45-
bazel_dep(name = "rules_cc", version = "0.2.16")
45+
bazel_dep(name = "rules_cc")
46+
47+
# rules_cc v0.2.17 is a transitive dependency, but causes Bazel tests to fail.
48+
# TODO: Remove this override when a fixed version of rules_cc is available.
49+
single_version_override(
50+
module_name = "rules_cc",
51+
version = "0.2.16",
52+
)
4653

4754
# The starlark rules in @rules_cc are hidden behind macros but docgen needs to
4855
# load the rule class directly, so we need to expose the cc_compatibility_proxy

src/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ copy_file(
191191
cc_binary(
192192
name = "read_manifest",
193193
srcs = ["read_manifest.cc"],
194+
linkopts = select({
195+
# MinGW requires -municode when using wmain.
196+
"@rules_cc//cc/compiler:clang": ["-municode"],
197+
"//conditions:default": [],
198+
}),
194199
tags = ["manual"],
195200
target_compatible_with = ["@platforms//os:windows"],
196201
visibility = ["//src/java_tools:__subpackages__"],
@@ -200,6 +205,11 @@ cc_binary(
200205
cc_binary(
201206
name = "write_manifest",
202207
srcs = ["write_manifest.cc"],
208+
linkopts = select({
209+
# MinGW requires -municode when using wmain.
210+
"@rules_cc//cc/compiler:clang": ["-municode"],
211+
"//conditions:default": [],
212+
}),
203213
tags = ["manual"],
204214
target_compatible_with = ["@platforms//os:windows"],
205215
visibility = ["//src/java_tools:__subpackages__"],

src/conditions/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,27 @@ selects.config_setting_group(
214214
visibility = ["//visibility:public"],
215215
)
216216

217+
config_setting(
218+
name = "windows_clang_cl",
219+
constraint_values = ["@platforms//os:windows"],
220+
flag_values = {"@rules_cc//cc/compiler:compiler": "clang-cl"},
221+
)
222+
223+
config_setting(
224+
name = "windows_msvc_cl",
225+
constraint_values = ["@platforms//os:windows"],
226+
flag_values = {"@rules_cc//cc/compiler:compiler": "msvc-cl"},
227+
)
228+
229+
selects.config_setting_group(
230+
name = "windows_msvc_like",
231+
match_any = [
232+
":windows_clang_cl",
233+
":windows_msvc_cl",
234+
],
235+
visibility = ["//visibility:public"],
236+
)
237+
217238
config_setting(
218239
name = "arm",
219240
constraint_values = ["@platforms//cpu:arm"],

src/main/cpp/BUILD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ cc_binary(
106106
"//conditions:default": [],
107107
}),
108108
copts = select({
109-
"//src/conditions:windows": ["/wd4018"],
109+
"//src/conditions:windows_msvc_like": ["/wd4018"],
110110
"//conditions:default": ["-Wno-sign-compare"],
111111
}),
112112
linkopts = select({
@@ -168,9 +168,12 @@ cc_library(
168168
# and the double % get reduced down to 1 by the compiler. A
169169
# forward slash is used because \b is a special character,
170170
# backspace.
171-
"//src/conditions:windows": [
171+
"//src/conditions:windows_msvc_like": [
172172
"/DBAZEL_SYSTEM_BAZELRC_PATH#\\\"%%ProgramData%%/bazel.bazelrc\\\"",
173173
],
174+
"@platforms//os:windows": [
175+
"-DBAZEL_SYSTEM_BAZELRC_PATH=\\\"%%ProgramData%%/bazel.bazelrc\\\"",
176+
],
174177
# For posix platforms, this can include environment variables in the
175178
# form ${var_name}. Braces are required.
176179
"//conditions:default": [

src/main/java/com/google/devtools/build/lib/runtime/commands/license/merge_licenses.bzl

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""A platform-independent build rule that merges license files."""
15+
"""A build rule that merges license files."""
1616

17-
def _windows_action(ctx, files):
18-
cmd = "(FOR %F IN (%SRCS%) DO ((SET X=%F)&ECHO ===== !X:\\=/! =====&TYPE %F&ECHO.&ECHO.)) > %OUT%"
19-
ctx.actions.run(
20-
inputs = depset(direct = files),
21-
outputs = [ctx.outputs.out],
22-
executable = "cmd.exe",
23-
arguments = ["/V:ON", "/E:ON", "/Q", "/C", cmd],
24-
env = {
25-
"OUT": ctx.outputs.out.path.replace("/", "\\"),
26-
"SRCS": " ".join([f.path.replace("/", "\\") for f in files]),
27-
},
28-
)
17+
def _impl(ctx):
18+
files = []
19+
for src in ctx.files.srcs:
20+
for substr in ["ASSEMBLY_EXCEPTION", "DISCLAIMER", "LICENSE", "license", "THIRD_PARTY_README"]:
21+
if substr in src.path:
22+
files.append(src)
23+
break
24+
if not files:
25+
fail("expected some sources")
2926

30-
def _bash_action(ctx, files):
3127
cmd = "for f in $SRCS; do echo ===== $f ===== && cat $f && echo && echo ; done > $OUT"
3228
ctx.actions.run_shell(
3329
inputs = depset(direct = files),
@@ -38,40 +34,12 @@ def _bash_action(ctx, files):
3834
"SRCS": " ".join([f.path for f in files]),
3935
},
4036
)
41-
42-
def _impl(ctx):
43-
files = []
44-
for src in ctx.files.srcs:
45-
for substr in ["ASSEMBLY_EXCEPTION", "DISCLAIMER", "LICENSE", "license", "THIRD_PARTY_README"]:
46-
if substr in src.path:
47-
files.append(src)
48-
break
49-
if not files:
50-
fail("expected some sources")
51-
if ctx.attr.is_windows:
52-
_windows_action(ctx, files)
53-
else:
54-
_bash_action(ctx, files)
55-
5637
return [DefaultInfo(files = depset(direct = [ctx.outputs.out]))]
5738

58-
_merge_licenses = rule(
39+
merge_licenses = rule(
5940
implementation = _impl,
6041
attrs = {
6142
"srcs": attr.label_list(allow_files = True, mandatory = True),
6243
"out": attr.output(mandatory = True),
63-
"is_windows": attr.bool(mandatory = True),
6444
},
6545
)
66-
67-
def merge_licenses(name, srcs, out, **kwargs):
68-
_merge_licenses(
69-
name = name,
70-
srcs = srcs,
71-
out = out,
72-
is_windows = select({
73-
"@bazel_tools//src/conditions:windows": True,
74-
"//conditions:default": False,
75-
}),
76-
**kwargs
77-
)

src/tools/launcher/util/launcher_util.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#endif
1818
#include <windows.h>
1919

20-
// For rand_s function, https://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
21-
#define _CRT_RAND_S
2220
#include <fcntl.h>
2321
#include <io.h>
2422
#include <stdarg.h>
@@ -27,8 +25,10 @@
2725
#include <string.h>
2826

2927
#include <algorithm>
28+
#include <random>
3029
#include <sstream>
3130
#include <string>
31+
#include <string_view>
3232

3333
#include "src/main/cpp/util/path_platform.h"
3434
#include "src/main/native/windows/file.h"
@@ -200,17 +200,17 @@ bool SetEnv(const wstring& env_name, const wstring& value) {
200200
return SetEnvironmentVariableW(env_name.c_str(), value.c_str());
201201
}
202202

203-
wstring GetRandomStr(size_t len) {
204-
static const wchar_t alphabet[] =
205-
L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
206-
wstring rand_str;
207-
rand_str.reserve(len);
208-
unsigned int x;
209-
for (size_t i = 0; i < len; i++) {
210-
rand_s(&x);
211-
rand_str += alphabet[x % wcslen(alphabet)];
212-
}
213-
return rand_str;
203+
std::wstring GetRandomStr(size_t len) {
204+
static constexpr std::wstring_view alphabet =
205+
L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
206+
std::random_device rd;
207+
std::uniform_int_distribution<size_t> dist(0, alphabet.size() - 1);
208+
std::wstring rand_str;
209+
rand_str.reserve(len);
210+
for (size_t i = 0; i < len; ++i) {
211+
rand_str += alphabet[dist(rd)];
212+
}
213+
return rand_str;
214214
}
215215

216216
bool NormalizePath(const wstring& path, wstring* result) {

third_party/ijar/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <stdint.h>
2323
#include <string.h>
2424

25-
#ifdef _WIN32
25+
#if defined(_WIN32) && !defined(__MINGW32__)
2626
#define PATH_MAX 4096
2727
typedef int mode_t;
2828
#endif // _WIN32

tools/cpp/modules_tools/BUILD

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ config_setting(
2929
constraint_values = ["@platforms//os:windows"],
3030
)
3131

32-
COPTS = select({
33-
":windows": ["/std:c++17"],
34-
"//conditions:default": ["-std=c++17"],
35-
})
36-
3732
cc_library(
3833
name = "common",
3934
srcs = [
@@ -43,22 +38,19 @@ cc_library(
4338
"common/common.h",
4439
"common/json.hpp",
4540
],
46-
copts = COPTS,
4741
includes = ["."],
4842
)
4943

5044
cc_library(
5145
name = "aggregate-ddi-lib",
5246
srcs = ["aggregate-ddi/aggregate-ddi.cc"],
5347
hdrs = ["aggregate-ddi/aggregate-ddi.h"],
54-
copts = COPTS,
5548
deps = [":common"],
5649
)
5750

5851
cc_binary(
5952
name = "aggregate-ddi",
6053
srcs = ["aggregate-ddi/main.cc"],
61-
copts = COPTS,
6254
deps = [
6355
":aggregate-ddi-lib",
6456
],
@@ -68,14 +60,12 @@ cc_library(
6860
name = "generate-modmap-lib",
6961
srcs = ["generate-modmap/generate-modmap.cc"],
7062
hdrs = ["generate-modmap/generate-modmap.h"],
71-
copts = COPTS,
7263
deps = [":common"],
7364
)
7465

7566
cc_binary(
7667
name = "generate-modmap",
7768
srcs = ["generate-modmap/main.cc"],
78-
copts = COPTS,
7969
deps = [":generate-modmap-lib"],
8070
)
8171

@@ -92,7 +82,6 @@ filegroup(
9282
cc_test(
9383
name = "generate-modmap_test",
9484
srcs = ["generate-modmap/generate-modmap_test.cc"],
95-
copts = COPTS,
9685
deps = [
9786
":generate-modmap-lib",
9887
"@com_google_googletest//:gtest_main",
@@ -102,7 +91,6 @@ cc_test(
10291
cc_test(
10392
name = "aggregate-ddi_test",
10493
srcs = ["aggregate-ddi/aggregate-ddi_test.cc"],
105-
copts = COPTS,
10694
deps = [
10795
":aggregate-ddi-lib",
10896
"@com_google_googletest//:gtest_main",
@@ -112,7 +100,6 @@ cc_test(
112100
cc_test(
113101
name = "common_test",
114102
srcs = ["common/common_test.cc"],
115-
copts = COPTS,
116103
deps = [
117104
":common",
118105
"@com_google_googletest//:gtest_main",
@@ -122,7 +109,6 @@ cc_test(
122109
cc_test(
123110
name = "json_test",
124111
srcs = ["common/json_test.cc"],
125-
copts = COPTS,
126112
deps = [
127113
":common",
128114
"@com_google_googletest//:gtest_main",

tools/test/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ filegroup(
4343
cc_binary(
4444
name = "tw",
4545
srcs = ["windows/tw_main.cc"],
46+
linkopts = select({
47+
# MinGW requires -municode when using wmain.
48+
"@rules_cc//cc/compiler:clang": ["-municode"],
49+
"//conditions:default": [],
50+
}),
4651
target_compatible_with = ["@platforms//os:windows"],
4752
visibility = ["//visibility:private"],
4853
deps = [":tw_lib"],
@@ -51,6 +56,11 @@ cc_binary(
5156
cc_binary(
5257
name = "xml",
5358
srcs = ["windows/xml_main.cc"],
59+
linkopts = select({
60+
# MinGW requires -municode when using wmain.
61+
"@rules_cc//cc/compiler:clang": ["-municode"],
62+
"//conditions:default": [],
63+
}),
5464
target_compatible_with = ["@platforms//os:windows"],
5565
visibility = ["//visibility:private"],
5666
deps = [":tw_lib"],

0 commit comments

Comments
 (0)