Skip to content

Commit a58ddea

Browse files
authored
Cherry pick win arm64 (bazelbuild#14794)
* Enable native support for Windows on arm64 (Part 1) Contains following changes to third_party: - Extended def_parser to handle ARM64 binaries - Add grpc patch to workaround build issues Closes: bazelbuild#14689 Partial commit for third_party/*, see bazelbuild#14689. Signed-off-by: Yun Peng <[email protected]> * Enable native support for Windows on arm64 This PR will enable cross-compilation of Bazel binaries for win/arm64 from win/x64 `bazel build -c opt --cpu=x64_arm64_windows //src:bazel.exe` Generated bazel executable can be used for native compilation in win/arm64 `bazel.exe build //main:hello-world` Following changes are included - Add win/arm64 JDK 17 - Fix AutoCpuConverter.java to identify win/arm64 CPU - Extend build_bazel_binaries.yml to cross-compile for win/arm64 - Fix msvc toolchain to look for tools in HostX86 directory as well - add clang-cl support for windows/arm64 host - Extend host_windows config to handle windows x64 and arm64 hosts. Closes bazelbuild#14340. PiperOrigin-RevId: 425919351 * [windows/arm64] Add missing JDK toolchain for java build Extend configurations to add JDK 11 and 17 for windows/arm64 platforms. This should fix the Java builds on windows/arm64 Closes bazelbuild#14700. PiperOrigin-RevId: 427737536 * add missing openjdk11_windows_arm64_archive * Deduplicate urls parsed to reduce crawl requests bazelbuild#14700 added couple more URLs to fetch JDK package and seems to be causing some infrastructure as discussed in bazelbuild#14700. This patch workaround the issue by removing the duplicated URLs and reduce the crawl request. Closes bazelbuild#14763. PiperOrigin-RevId: 427464876 * fix jdk_http_archives.tmpl
1 parent ffdd633 commit a58ddea

29 files changed

+493
-73
lines changed

.bazelci/build_bazel_binaries.yml

+10
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ platforms:
4949
build_targets:
5050
- "//src:bazel.exe"
5151
- "//src:bazel_nojdk.exe"
52+
windows_arm64:
53+
build_flags:
54+
- "--copt=-w"
55+
- "--host_copt=-w"
56+
- "-c"
57+
- "opt"
58+
- "--cpu=x64_arm64_windows"
59+
build_targets:
60+
- "//src:bazel.exe"
61+
- "//src:bazel_nojdk.exe"

WORKSPACE

+44
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ http_file(
265265
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
266266
)
267267

268+
http_file(
269+
name = "openjdk_win_arm64_vanilla",
270+
downloaded_file_path = "zulu-win-arm64.zip",
271+
sha256 = "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
272+
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip"],
273+
)
274+
268275
http_file(
269276
name = "openjdk_win_minimal",
270277
downloaded_file_path = "zulu-win-minimal.zip",
@@ -558,6 +565,14 @@ http_archive(
558565
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-win_x64.zip"],
559566
)
560567

568+
dist_http_archive(
569+
name = "remotejdk11_win_arm64_for_testing",
570+
build_file = "@local_jdk//:BUILD.bazel",
571+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
572+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
573+
574+
)
575+
561576
# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
562577
http_archive(
563578
name = "remotejdk15_linux_for_testing",
@@ -726,6 +741,14 @@ http_archive(
726741
],
727742
)
728743

744+
dist_http_archive(
745+
name = "remotejdk17_win_arm64_for_testing",
746+
build_file = "@local_jdk//:BUILD.bazel",
747+
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
748+
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
749+
750+
)
751+
729752
# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
730753
dist_http_archive(
731754
name = "remote_java_tools_for_testing",
@@ -833,6 +856,18 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
833856
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-win_x64.zip"],
834857
)
835858

859+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
860+
http_archive(
861+
name = "openjdk11_windows_arm64_archive",
862+
build_file_content = """
863+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
864+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
865+
""",
866+
sha256 = "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
867+
strip_prefix = "jdk-11.0.13+8",
868+
urls = ["https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip"],
869+
)
870+
836871
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
837872
http_archive(
838873
name = "openjdk15_linux_archive",
@@ -1013,6 +1048,15 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
10131048
],
10141049
)
10151050

1051+
# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
1052+
dist_http_archive(
1053+
name = "openjdk17_windows_arm64_archive",
1054+
build_file_content = """
1055+
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
1056+
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
1057+
""",
1058+
)
1059+
10161060
load("@io_bazel_skydoc//:setup.bzl", "stardoc_repositories")
10171061

10181062
stardoc_repositories()

distdir_deps.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ DIST_DEPS = {
130130
"patch_args": ["-p1"],
131131
"patches": [
132132
"//third_party/grpc:grpc_1.41.0.patch",
133+
"//third_party/grpc:grpc_1.41.0.win_arm64.patch",
133134
],
134135
"used_in": [
135136
"additional_distfiles",
@@ -340,6 +341,34 @@ DIST_DEPS = {
340341
"test_WORKSPACE_files",
341342
],
342343
},
344+
"remotejdk17_win_arm64": {
345+
"aliases": [
346+
"remotejdk17_win_arm64_for_testing",
347+
"openjdk17_windows_arm64_archive",
348+
"openjdk_win_arm64_vanilla",
349+
],
350+
"archive": "zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
351+
"sha256": "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
352+
"strip_prefix": "zulu17.30.15-ca-jdk17.0.1-win_aarch64",
353+
"urls": [
354+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
355+
"https://cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
356+
],
357+
"used_in": ["test_WORKSPACE_files"],
358+
},
359+
"remotejdk11_win_arm64": {
360+
"aliases": [
361+
"remotejdk11_win_arm64_for_testing",
362+
"openjdk11_windows_arm64_archive",
363+
],
364+
"archive": "microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
365+
"sha256": "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
366+
"strip_prefix": "jdk-11.0.13+8",
367+
"urls": [
368+
"https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
369+
],
370+
"used_in": ["test_WORKSPACE_files"],
371+
},
343372
}
344373

345374
# Add aliased names

site/docs/windows.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ target architecture, set the `--cpu` build option for your target architecture:
203203
* x64 (default): `--cpu=x64_windows` or no option
204204
* x86: `--cpu=x64_x86_windows`
205205
* ARM: `--cpu=x64_arm_windows`
206-
* ARM64: `--cpu=x64_arm64_windows`
206+
* ARM64: `--cpu=arm64_windows`
207+
208+
Note: `--cpu=x64_arm64_windows` to target ARM64 architecture is deprecated. Please use `--cpu=arm64_windows`
207209
208210
For example, to build targets for ARM architecture, run:
209211

src/BUILD

+12-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ filegroup(
148148
"//src/conditions:darwin_arm64": [
149149
"@openjdk_macos_aarch64//file",
150150
],
151-
"//src/conditions:windows": [
151+
"//src/conditions:windows_arm64": [
152+
"@openjdk_win_arm64_vanilla//file", # TODO(#14339): replace with openjdk_win_arm64 when packages are uploaded to bazel mirror server
153+
],
154+
"//src/conditions:windows_x86_64": [
152155
"@openjdk_win//file",
153156
],
154157
"//src/conditions:linux_aarch64": [
@@ -170,7 +173,10 @@ filegroup(
170173
"//src/conditions:darwin_arm64": [
171174
"@openjdk_macos_aarch64_minimal//file",
172175
],
173-
"//src/conditions:windows": [
176+
"//src/conditions:windows_arm64": [
177+
"@openjdk_win_arm64_vanilla//file", # TODO(#14339): replace with openjdk_win_arm64 when packages are uploaded to bazel mirror server
178+
],
179+
"//src/conditions:windows_x86_64": [
174180
"@openjdk_win_minimal//file",
175181
],
176182
"//src/conditions:linux_aarch64": [
@@ -649,6 +655,7 @@ filegroup(
649655
"@openjdk11_darwin_archive//:WORKSPACE",
650656
"@openjdk11_linux_archive//:WORKSPACE",
651657
"@openjdk11_windows_archive//:WORKSPACE",
658+
"@openjdk11_windows_arm64_archive//:WORKSPACE",
652659
"@openjdk15_darwin_aarch64_archive//:WORKSPACE",
653660
"@openjdk15_darwin_archive//:WORKSPACE",
654661
"@openjdk15_linux_archive//:WORKSPACE",
@@ -661,6 +668,7 @@ filegroup(
661668
"@openjdk17_darwin_archive//:WORKSPACE",
662669
"@openjdk17_linux_archive//:WORKSPACE",
663670
"@openjdk17_windows_archive//:WORKSPACE",
671+
"@openjdk17_windows_arm64_archive//:WORKSPACE",
664672
"@openjdk_linux_aarch64_minimal//file",
665673
"@openjdk_linux_minimal//file",
666674
"@openjdk_macos_aarch64_minimal//file",
@@ -681,6 +689,7 @@ filegroup(
681689
"@remotejdk11_linux_s390x_for_testing//:WORKSPACE",
682690
"@remotejdk11_macos_aarch64_for_testing//:WORKSPACE",
683691
"@remotejdk11_macos_for_testing//:WORKSPACE",
692+
"@remotejdk11_win_arm64_for_testing//:WORKSPACE",
684693
"@remotejdk11_win_for_testing//:WORKSPACE",
685694
"@remotejdk15_linux_for_testing//:WORKSPACE",
686695
"@remotejdk15_macos_aarch64_for_testing//:WORKSPACE",
@@ -693,6 +702,7 @@ filegroup(
693702
"@remotejdk17_linux_for_testing//:WORKSPACE",
694703
"@remotejdk17_macos_aarch64_for_testing//:WORKSPACE",
695704
"@remotejdk17_macos_for_testing//:WORKSPACE",
705+
"@remotejdk17_win_arm64_for_testing//:WORKSPACE",
696706
"@remotejdk17_win_for_testing//:WORKSPACE",
697707
"@rules_cc//:WORKSPACE",
698708
"@rules_java//:WORKSPACE",

src/conditions/BUILD

+39
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,45 @@ config_setting(
167167
visibility = ["//visibility:public"],
168168
)
169169

170+
config_setting(
171+
name = "windows_x86_64",
172+
constraint_values = [
173+
"@platforms//os:windows",
174+
"@platforms//cpu:x86_64",
175+
],
176+
visibility = ["//visibility:public"],
177+
)
178+
179+
config_setting(
180+
name = "windows_arm64_constraint",
181+
constraint_values = [
182+
"@platforms//os:windows",
183+
"@platforms//cpu:arm64",
184+
],
185+
)
186+
187+
# TODO(https://github.com/bazelbuild/bazel/issues/7260)
188+
# Remove the flag after toolchain cc resolution is fixed
189+
config_setting(
190+
name = "windows_x64_arm64_flag",
191+
values = {"cpu": "x64_arm64_windows"},
192+
)
193+
194+
config_setting(
195+
name = "windows_arm64_flag",
196+
values = {"cpu": "arm64_windows"},
197+
)
198+
199+
selects.config_setting_group(
200+
name = "windows_arm64",
201+
match_any = [
202+
":windows_arm64_constraint",
203+
":windows_arm64_flag",
204+
":windows_x64_arm64_flag",
205+
],
206+
visibility = ["//visibility:public"],
207+
)
208+
170209
config_setting(
171210
name = "arm",
172211
constraint_values = ["@platforms//cpu:arm"],

src/conditions/BUILD.tools

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools/windows:windows_config.bzl", "create_windows_host_config")
2+
13
config_setting(
24
name = "freebsd",
35
constraint_values = ["@platforms//os:freebsd"],
@@ -118,20 +120,30 @@ config_setting(
118120
visibility = ["//visibility:public"],
119121
)
120122

121-
# TODO: figure out how to base this selection on constraints
122123
config_setting(
123-
name = "windows_msvc",
124-
values = {"cpu": "x64_windows_msvc"},
125-
visibility = ["//visibility:public"],
124+
name = "windows_x64",
125+
constraint_values = [
126+
"@platforms//os:windows",
127+
"@platforms//cpu:x86_64",
128+
],
129+
)
130+
config_setting(
131+
name = "windows_arm64",
132+
constraint_values = [
133+
"@platforms//os:windows",
134+
"@platforms//cpu:arm64",
135+
],
126136
)
127137

128138
# TODO: figure out how to base this selection on constraints
129139
config_setting(
130-
name = "host_windows",
131-
values = {"host_cpu": "x64_windows"},
140+
name = "windows_msvc",
141+
values = {"cpu": "x64_windows_msvc"},
132142
visibility = ["//visibility:public"],
133143
)
134144

145+
create_windows_host_config()
146+
135147
config_setting(
136148
name = "remote",
137149
values = {"define": "EXECUTOR=remote"},

src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public String convert(String input) throws OptionsParsingException {
5050
switch (CPU.getCurrent()) {
5151
case X86_64:
5252
return "x64_windows";
53+
case AARCH64:
54+
return "arm64_windows";
5355
default:
54-
// We only support x64 Windows for now.
56+
// We only support x64 and arm64 Windows for now.
5557
return "unknown";
5658
}
5759
case LINUX:

src/main/java/com/google/devtools/build/lib/bazel/rules/java/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ gen_workspace_stanza(
8484
"remote_java_tools_linux",
8585
"remote_java_tools_windows",
8686
"remote_java_tools_darwin",
87+
"remotejdk11_win_arm64",
88+
"remotejdk17_win_arm64",
8789
"rules_cc",
8890
"rules_java",
8991
"rules_proto",

src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl

+32
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ maybe(
132132
version = "11",
133133
)
134134

135+
# This must be kept in sync with the top-level WORKSPACE file.
136+
maybe(
137+
remote_java_repository,
138+
name = "remotejdk11_win_arm64",
139+
exec_compatible_with = [
140+
"@platforms//os:windows",
141+
"@platforms//cpu:arm64",
142+
],
143+
sha256 = "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
144+
strip_prefix = "jdk-11.0.13+8",
145+
urls = [
146+
"https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
147+
],
148+
version = "11",
149+
)
150+
135151
# This must be kept in sync with the top-level WORKSPACE file.
136152
maybe(
137153
remote_java_repository,
@@ -336,6 +352,22 @@ maybe(
336352
version = "17",
337353
)
338354

355+
maybe(
356+
remote_java_repository,
357+
name = "remotejdk17_win_arm64",
358+
exec_compatible_with = [
359+
"@platforms//os:windows",
360+
"@platforms//cpu:arm64",
361+
],
362+
sha256 = "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
363+
strip_prefix = "zulu17.30.15-ca-jdk17.0.1-win_aarch64",
364+
urls = [
365+
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
366+
"https://cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
367+
],
368+
version = "17",
369+
)
370+
339371
{remote_java_tools}
340372
{remote_java_tools_linux}
341373
{remote_java_tools_windows}

src/test/py/bazel/bazel_windows_cpp_test.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def testBuildArmCppBinaryWithMsvcCL(self):
920920
self.AssertExitCode(exit_code, 0, stderr)
921921
self.assertIn('arm\\cl.exe', '\n'.join(stderr))
922922

923-
def testBuildArm64CppBinaryWithMsvcCL(self):
923+
def testBuildArm64CppBinaryWithMsvcCLAndCpuX64Arm64Windows(self):
924924
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
925925
self.ScratchFile('BUILD', [
926926
'cc_binary(',
@@ -1033,6 +1033,24 @@ def testBuildCppBinaryWithMsysGCC(self):
10331033
self.AssertFileContentContains(
10341034
os.path.join(bazel_output, paramfile % 'opt'), '-Wl,--gc-sections')
10351035

1036+
def testBuildArm64CppBinaryWithMsvcCLAndCpuArm64Windows(self):
1037+
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
1038+
self.ScratchFile('BUILD', [
1039+
'cc_binary(',
1040+
' name = "main",',
1041+
' srcs = ["main.cc"],',
1042+
')',
1043+
])
1044+
self.ScratchFile('main.cc', [
1045+
'int main() {',
1046+
' return 0;',
1047+
'}',
1048+
])
1049+
exit_code, _, stderr = self.RunBazel(
1050+
['build', '-s', '--cpu=arm64_windows', '//:main'])
1051+
self.AssertExitCode(exit_code, 0, stderr)
1052+
self.assertIn('arm64\\cl.exe', ''.join(stderr))
1053+
10361054

10371055
if __name__ == '__main__':
10381056
unittest.main()

0 commit comments

Comments
 (0)