Skip to content

Commit

Permalink
Enable native support for Windows on arm64
Browse files Browse the repository at this point in the history
Following changes are included

- Fix AutoCpuConverter.java to identify win/arm64 CPU
- Add win/arm64 JDK 17
- Add grpc patch to workaround build issues
- Extend build_bazel_binaries.yml to cross-compile for win/arm64
- Fix msvc toolchain to look for tools in HostX86 directory as well

Fixes: #14339
  • Loading branch information
niyas-sait committed Jan 31, 2022
1 parent e1a912c commit 8ca9e35
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 35 deletions.
10 changes: 10 additions & 0 deletions .bazelci/build_bazel_binaries.yml
Original file line number Diff line number Diff line change
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"
build_targets:
- "//src:bazel.exe"
- "//src:bazel_nojdk.exe"
- "--cpu=arm64_windows"
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,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://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
1 change: 1 addition & 0 deletions distdir_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,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
4 changes: 3 additions & 1 deletion site/docs/windows.md
Original file line number Diff line number Diff line change
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
10 changes: 8 additions & 2 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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 @@ -169,7 +172,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
37 changes: 37 additions & 0 deletions src/conditions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ 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",
],
)

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
20 changes: 19 additions & 1 deletion src/test/py/bazel/bazel_windows_cpp_test.py
Original file line number Diff line number Diff line change
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()
1 change: 1 addition & 0 deletions third_party/def_parser/def_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ bool DumpFile(const char* filename, std::set<string>& symbols,
/* Does it look like a COFF OBJ file??? */
else if (((dosHeader->e_magic == IMAGE_FILE_MACHINE_I386) ||
(dosHeader->e_magic == IMAGE_FILE_MACHINE_AMD64) ||
(dosHeader->e_magic == IMAGE_FILE_MACHINE_ARM64) ||
(dosHeader->e_magic == IMAGE_FILE_MACHINE_ARMNT)) &&
(dosHeader->e_sp == 0)) {
/*
Expand Down
1 change: 1 addition & 0 deletions third_party/grpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ licenses(["notice"]) # Apache v2
exports_files([
"grpc_1.31.1.patch",
"grpc_1.41.0.patch",
"grpc_1.41.0.win_arm64.patch"
])

package(default_visibility = ["//visibility:public"])
Expand Down
51 changes: 51 additions & 0 deletions third_party/grpc/grpc_1.41.0.win_arm64.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
From 6ce08c3da545358074eb66dc4202e0474fa5be1b Mon Sep 17 00:00:00 2001
From: Niyas Sait <[email protected]>
Date: Fri, 26 Nov 2021 02:43:37 -0800
Subject: [PATCH] add workarounds to compile for win/arm64

---
src/core/lib/transport/transport.cc | 8 ++++----
third_party/cares/cares.BUILD | 2 +-
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/core/lib/transport/transport.cc b/src/core/lib/transport/transport.cc
index 36060a6bd9..db847d53d2 100644
--- a/src/core/lib/transport/transport.cc
+++ b/src/core/lib/transport/transport.cc
@@ -99,16 +99,16 @@ void grpc_stream_ref_init(grpc_stream_refcount* refcount, int /*initial_refs*/,
refcount, &refcount->slice_refcount);
}

-static void move64(uint64_t* from, uint64_t* to) {
+static void move64bits(uint64_t* from, uint64_t* to) {
*to += *from;
*from = 0;
}

void grpc_transport_move_one_way_stats(grpc_transport_one_way_stats* from,
grpc_transport_one_way_stats* to) {
- move64(&from->framing_bytes, &to->framing_bytes);
- move64(&from->data_bytes, &to->data_bytes);
- move64(&from->header_bytes, &to->header_bytes);
+ move64bits(&from->framing_bytes, &to->framing_bytes);
+ move64bits(&from->data_bytes, &to->data_bytes);
+ move64bits(&from->header_bytes, &to->header_bytes);
}

void grpc_transport_move_stats(grpc_transport_stream_stats* from,
diff --git a/third_party/cares/cares.BUILD b/third_party/cares/cares.BUILD
index 7939021a25..430791aa8d 100644
--- a/third_party/cares/cares.BUILD
+++ b/third_party/cares/cares.BUILD
@@ -22,7 +22,7 @@ config_setting(

config_setting(
name = "windows",
- values = {"cpu": "x64_windows"},
+ constraint_values = ["@platforms//os:windows"],
)

# Android is not officially supported through C++.
--
2.33.0.windows.2

2 changes: 2 additions & 0 deletions tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ filegroup(
"//tools/jdk:srcs",
"//tools/launcher:srcs",
"//tools/def_parser:srcs",
"//tools/windows:srcs",
"//tools/platforms:srcs",
"//tools/objc:srcs",
"//tools/python:embedded_tools",
Expand All @@ -80,6 +81,7 @@ filegroup(
"//tools/cpp:bzl_srcs",
"//tools/jdk:bzl_srcs",
"//tools/osx:bzl_srcs",
"//tools/windows:bzl_srcs",
"//tools/python:bzl_srcs",
"//tools/sh:bzl_srcs",
],
Expand Down
Loading

0 comments on commit 8ca9e35

Please sign in to comment.