Skip to content

Commit

Permalink
Cherry pick win arm64 (bazelbuild#14794)
Browse files Browse the repository at this point in the history
* 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 <pcloudy@google.com>

* 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
  • Loading branch information
niyas-sait committed Feb 14, 2022
1 parent ffdd633 commit a58ddea
Show file tree
Hide file tree
Showing 29 changed files with 493 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .bazelci/build_bazel_binaries.yml
Expand Up @@ -49,3 +49,13 @@ platforms:
build_targets:
- "//src:bazel.exe"
- "//src:bazel_nojdk.exe"
windows_arm64:
build_flags:
- "--copt=-w"
- "--host_copt=-w"
- "-c"
- "opt"
- "--cpu=x64_arm64_windows"
build_targets:
- "//src:bazel.exe"
- "//src:bazel_nojdk.exe"
44 changes: 44 additions & 0 deletions WORKSPACE
Expand Up @@ -265,6 +265,13 @@ http_file(
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip"],
)

http_file(
name = "openjdk_win_arm64_vanilla",
downloaded_file_path = "zulu-win-arm64.zip",
sha256 = "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip"],
)

http_file(
name = "openjdk_win_minimal",
downloaded_file_path = "zulu-win-minimal.zip",
Expand Down Expand Up @@ -558,6 +565,14 @@ http_archive(
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-win_x64.zip"],
)

dist_http_archive(
name = "remotejdk11_win_arm64_for_testing",
build_file = "@local_jdk//:BUILD.bazel",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,

)

# This must be kept in sync with src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
http_archive(
name = "remotejdk15_linux_for_testing",
Expand Down Expand Up @@ -726,6 +741,14 @@ http_archive(
],
)

dist_http_archive(
name = "remotejdk17_win_arm64_for_testing",
build_file = "@local_jdk//:BUILD.bazel",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,

)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_for_testing",
Expand Down Expand Up @@ -833,6 +856,18 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
urls = ["https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-win_x64.zip"],
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
http_archive(
name = "openjdk11_windows_arm64_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
sha256 = "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
strip_prefix = "jdk-11.0.13+8",
urls = ["https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip"],
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
http_archive(
name = "openjdk15_linux_archive",
Expand Down Expand Up @@ -1013,6 +1048,15 @@ exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
],
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk17_windows_arm64_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

load("@io_bazel_skydoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()
Expand Down
29 changes: 29 additions & 0 deletions distdir_deps.bzl
Expand Up @@ -130,6 +130,7 @@ DIST_DEPS = {
"patch_args": ["-p1"],
"patches": [
"//third_party/grpc:grpc_1.41.0.patch",
"//third_party/grpc:grpc_1.41.0.win_arm64.patch",
],
"used_in": [
"additional_distfiles",
Expand Down Expand Up @@ -340,6 +341,34 @@ DIST_DEPS = {
"test_WORKSPACE_files",
],
},
"remotejdk17_win_arm64": {
"aliases": [
"remotejdk17_win_arm64_for_testing",
"openjdk17_windows_arm64_archive",
"openjdk_win_arm64_vanilla",
],
"archive": "zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
"sha256": "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
"strip_prefix": "zulu17.30.15-ca-jdk17.0.1-win_aarch64",
"urls": [
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
"https://cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
],
"used_in": ["test_WORKSPACE_files"],
},
"remotejdk11_win_arm64": {
"aliases": [
"remotejdk11_win_arm64_for_testing",
"openjdk11_windows_arm64_archive",
],
"archive": "microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
"sha256": "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
"strip_prefix": "jdk-11.0.13+8",
"urls": [
"https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
],
"used_in": ["test_WORKSPACE_files"],
},
}

# Add aliased names
Expand Down
4 changes: 3 additions & 1 deletion site/docs/windows.md
Expand Up @@ -203,7 +203,9 @@ target architecture, set the `--cpu` build option for your target architecture:
* x64 (default): `--cpu=x64_windows` or no option
* x86: `--cpu=x64_x86_windows`
* ARM: `--cpu=x64_arm_windows`
* ARM64: `--cpu=x64_arm64_windows`
* ARM64: `--cpu=arm64_windows`

Note: `--cpu=x64_arm64_windows` to target ARM64 architecture is deprecated. Please use `--cpu=arm64_windows`

For example, to build targets for ARM architecture, run:

Expand Down
14 changes: 12 additions & 2 deletions src/BUILD
Expand Up @@ -148,7 +148,10 @@ filegroup(
"//src/conditions:darwin_arm64": [
"@openjdk_macos_aarch64//file",
],
"//src/conditions:windows": [
"//src/conditions:windows_arm64": [
"@openjdk_win_arm64_vanilla//file", # TODO(#14339): replace with openjdk_win_arm64 when packages are uploaded to bazel mirror server
],
"//src/conditions:windows_x86_64": [
"@openjdk_win//file",
],
"//src/conditions:linux_aarch64": [
Expand All @@ -170,7 +173,10 @@ filegroup(
"//src/conditions:darwin_arm64": [
"@openjdk_macos_aarch64_minimal//file",
],
"//src/conditions:windows": [
"//src/conditions:windows_arm64": [
"@openjdk_win_arm64_vanilla//file", # TODO(#14339): replace with openjdk_win_arm64 when packages are uploaded to bazel mirror server
],
"//src/conditions:windows_x86_64": [
"@openjdk_win_minimal//file",
],
"//src/conditions:linux_aarch64": [
Expand Down Expand Up @@ -649,6 +655,7 @@ filegroup(
"@openjdk11_darwin_archive//:WORKSPACE",
"@openjdk11_linux_archive//:WORKSPACE",
"@openjdk11_windows_archive//:WORKSPACE",
"@openjdk11_windows_arm64_archive//:WORKSPACE",
"@openjdk15_darwin_aarch64_archive//:WORKSPACE",
"@openjdk15_darwin_archive//:WORKSPACE",
"@openjdk15_linux_archive//:WORKSPACE",
Expand All @@ -661,6 +668,7 @@ filegroup(
"@openjdk17_darwin_archive//:WORKSPACE",
"@openjdk17_linux_archive//:WORKSPACE",
"@openjdk17_windows_archive//:WORKSPACE",
"@openjdk17_windows_arm64_archive//:WORKSPACE",
"@openjdk_linux_aarch64_minimal//file",
"@openjdk_linux_minimal//file",
"@openjdk_macos_aarch64_minimal//file",
Expand All @@ -681,6 +689,7 @@ filegroup(
"@remotejdk11_linux_s390x_for_testing//:WORKSPACE",
"@remotejdk11_macos_aarch64_for_testing//:WORKSPACE",
"@remotejdk11_macos_for_testing//:WORKSPACE",
"@remotejdk11_win_arm64_for_testing//:WORKSPACE",
"@remotejdk11_win_for_testing//:WORKSPACE",
"@remotejdk15_linux_for_testing//:WORKSPACE",
"@remotejdk15_macos_aarch64_for_testing//:WORKSPACE",
Expand All @@ -693,6 +702,7 @@ filegroup(
"@remotejdk17_linux_for_testing//:WORKSPACE",
"@remotejdk17_macos_aarch64_for_testing//:WORKSPACE",
"@remotejdk17_macos_for_testing//:WORKSPACE",
"@remotejdk17_win_arm64_for_testing//:WORKSPACE",
"@remotejdk17_win_for_testing//:WORKSPACE",
"@rules_cc//:WORKSPACE",
"@rules_java//:WORKSPACE",
Expand Down
39 changes: 39 additions & 0 deletions src/conditions/BUILD
Expand Up @@ -167,6 +167,45 @@ config_setting(
visibility = ["//visibility:public"],
)

config_setting(
name = "windows_x86_64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
visibility = ["//visibility:public"],
)

config_setting(
name = "windows_arm64_constraint",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:arm64",
],
)

# TODO(https://github.com/bazelbuild/bazel/issues/7260)
# Remove the flag after toolchain cc resolution is fixed
config_setting(
name = "windows_x64_arm64_flag",
values = {"cpu": "x64_arm64_windows"},
)

config_setting(
name = "windows_arm64_flag",
values = {"cpu": "arm64_windows"},
)

selects.config_setting_group(
name = "windows_arm64",
match_any = [
":windows_arm64_constraint",
":windows_arm64_flag",
":windows_x64_arm64_flag",
],
visibility = ["//visibility:public"],
)

config_setting(
name = "arm",
constraint_values = ["@platforms//cpu:arm"],
Expand Down
24 changes: 18 additions & 6 deletions src/conditions/BUILD.tools
@@ -1,3 +1,5 @@
load("//tools/windows:windows_config.bzl", "create_windows_host_config")

config_setting(
name = "freebsd",
constraint_values = ["@platforms//os:freebsd"],
Expand Down Expand Up @@ -118,20 +120,30 @@ config_setting(
visibility = ["//visibility:public"],
)

# TODO: figure out how to base this selection on constraints
config_setting(
name = "windows_msvc",
values = {"cpu": "x64_windows_msvc"},
visibility = ["//visibility:public"],
name = "windows_x64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
)
config_setting(
name = "windows_arm64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:arm64",
],
)

# TODO: figure out how to base this selection on constraints
config_setting(
name = "host_windows",
values = {"host_cpu": "x64_windows"},
name = "windows_msvc",
values = {"cpu": "x64_windows_msvc"},
visibility = ["//visibility:public"],
)

create_windows_host_config()

config_setting(
name = "remote",
values = {"define": "EXECUTOR=remote"},
Expand Down
Expand Up @@ -50,8 +50,10 @@ public String convert(String input) throws OptionsParsingException {
switch (CPU.getCurrent()) {
case X86_64:
return "x64_windows";
case AARCH64:
return "arm64_windows";
default:
// We only support x64 Windows for now.
// We only support x64 and arm64 Windows for now.
return "unknown";
}
case LINUX:
Expand Down
Expand Up @@ -84,6 +84,8 @@ gen_workspace_stanza(
"remote_java_tools_linux",
"remote_java_tools_windows",
"remote_java_tools_darwin",
"remotejdk11_win_arm64",
"remotejdk17_win_arm64",
"rules_cc",
"rules_java",
"rules_proto",
Expand Down
Expand Up @@ -132,6 +132,22 @@ maybe(
version = "11",
)

# This must be kept in sync with the top-level WORKSPACE file.
maybe(
remote_java_repository,
name = "remotejdk11_win_arm64",
exec_compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:arm64",
],
sha256 = "b8a28e6e767d90acf793ea6f5bed0bb595ba0ba5ebdf8b99f395266161e53ec2",
strip_prefix = "jdk-11.0.13+8",
urls = [
"https://mirror.bazel.build/aka.ms/download-jdk/microsoft-jdk-11.0.13.8.1-windows-aarch64.zip",
],
version = "11",
)

# This must be kept in sync with the top-level WORKSPACE file.
maybe(
remote_java_repository,
Expand Down Expand Up @@ -336,6 +352,22 @@ maybe(
version = "17",
)

maybe(
remote_java_repository,
name = "remotejdk17_win_arm64",
exec_compatible_with = [
"@platforms//os:windows",
"@platforms//cpu:arm64",
],
sha256 = "811d7e7591bac4f081dfb00ba6bd15b6fc5969e1f89f0f327ef75147027c3877",
strip_prefix = "zulu17.30.15-ca-jdk17.0.1-win_aarch64",
urls = [
"https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
"https://cdn.azul.com/zulu/bin/zulu17.30.15-ca-jdk17.0.1-win_aarch64.zip",
],
version = "17",
)

{remote_java_tools}
{remote_java_tools_linux}
{remote_java_tools_windows}
Expand Down
20 changes: 19 additions & 1 deletion src/test/py/bazel/bazel_windows_cpp_test.py
Expand Up @@ -920,7 +920,7 @@ def testBuildArmCppBinaryWithMsvcCL(self):
self.AssertExitCode(exit_code, 0, stderr)
self.assertIn('arm\\cl.exe', '\n'.join(stderr))

def testBuildArm64CppBinaryWithMsvcCL(self):
def testBuildArm64CppBinaryWithMsvcCLAndCpuX64Arm64Windows(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile('BUILD', [
'cc_binary(',
Expand Down Expand Up @@ -1033,6 +1033,24 @@ def testBuildCppBinaryWithMsysGCC(self):
self.AssertFileContentContains(
os.path.join(bazel_output, paramfile % 'opt'), '-Wl,--gc-sections')

def testBuildArm64CppBinaryWithMsvcCLAndCpuArm64Windows(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile('BUILD', [
'cc_binary(',
' name = "main",',
' srcs = ["main.cc"],',
')',
])
self.ScratchFile('main.cc', [
'int main() {',
' return 0;',
'}',
])
exit_code, _, stderr = self.RunBazel(
['build', '-s', '--cpu=arm64_windows', '//:main'])
self.AssertExitCode(exit_code, 0, stderr)
self.assertIn('arm64\\cl.exe', ''.join(stderr))


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

0 comments on commit a58ddea

Please sign in to comment.