Skip to content

Commit 43263dc

Browse files
fmeumiancha1992
authored andcommitted
Fix cross-compilation to Windows with clang MinGW (#28751)
* 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. Get Bazel to build with `clang` (not `clang-cl`) on Windows and non-Windows platforms. No - [ ] I have added tests for the new use cases (if any). - [ ] I have updated the documentation (if applicable). RELNOTES: None Closes #28751. PiperOrigin-RevId: 874799593 Change-Id: Ia836e93d7307a30045c18e052228906c7be9ed92
1 parent 141d9d2 commit 43263dc

File tree

10 files changed

+82
-101
lines changed

10 files changed

+82
-101
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")
@@ -41,7 +41,14 @@ bazel_dep(name = "zstd-jni", version = "1.5.6-9")
4141
# Depend on apple_support first and then rules_cc so that the Xcode toolchain
4242
# from apple_support wins over the generic Unix toolchain from rules_cc.
4343
bazel_dep(name = "apple_support", version = "1.24.5")
44-
bazel_dep(name = "rules_cc", version = "0.2.14")
44+
bazel_dep(name = "rules_cc")
45+
46+
# rules_cc v0.2.17 is a transitive dependency, but causes Bazel tests to fail.
47+
# TODO: Remove this override when a fixed version of rules_cc is available.
48+
single_version_override(
49+
module_name = "rules_cc",
50+
version = "0.2.16",
51+
)
4552

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

MODULE.bazel.lock

Lines changed: 4 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ copy_file(
195195
cc_binary(
196196
name = "read_manifest",
197197
srcs = ["read_manifest.cc"],
198+
linkopts = select({
199+
# MinGW requires -municode when using wmain.
200+
"@rules_cc//cc/compiler:clang": ["-municode"],
201+
"//conditions:default": [],
202+
}),
198203
tags = ["manual"],
199204
target_compatible_with = ["@platforms//os:windows"],
200205
visibility = ["//src/java_tools:__subpackages__"],
@@ -204,6 +209,11 @@ cc_binary(
204209
cc_binary(
205210
name = "write_manifest",
206211
srcs = ["write_manifest.cc"],
212+
linkopts = select({
213+
# MinGW requires -municode when using wmain.
214+
"@rules_cc//cc/compiler:clang": ["-municode"],
215+
"//conditions:default": [],
216+
}),
207217
tags = ["manual"],
208218
target_compatible_with = ["@platforms//os:windows"],
209219
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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
#ifndef WIN32_LEAN_AND_MEAN
1616
#define WIN32_LEAN_AND_MEAN
1717
#endif
18-
#include <windows.h>
18+
#include "src/tools/launcher/util/launcher_util.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>
2523
#include <stdio.h>
2624
#include <stdlib.h>
2725
#include <string.h>
26+
#include <windows.h>
2827

2928
#include <algorithm>
29+
#include <random>
3030
#include <sstream>
3131
#include <string>
32+
#include <string_view>
3233

3334
#include "src/main/cpp/util/path_platform.h"
3435
#include "src/main/native/windows/file.h"
35-
#include "src/tools/launcher/util/launcher_util.h"
3636

3737
namespace bazel {
3838
namespace launcher {
@@ -200,15 +200,15 @@ 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[] =
203+
std::wstring GetRandomStr(size_t len) {
204+
static constexpr std::wstring_view alphabet =
205205
L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
206-
wstring rand_str;
206+
std::random_device rd; // NOLINT(runtime/random_device) - Windows-only code
207+
std::uniform_int_distribution<size_t> dist(0, alphabet.size() - 1);
208+
std::wstring rand_str;
207209
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)];
210+
for (size_t i = 0; i < len; ++i) {
211+
rand_str += alphabet[dist(rd)];
212212
}
213213
return rand_str;
214214
}

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",

0 commit comments

Comments
 (0)