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: bazelbuild#14339
  • Loading branch information
niyas-sait committed Jan 13, 2022
1 parent 02ad3e3 commit d0b11e3
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .bazelci/build_bazel_binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,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=x64_arm64_windows"
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,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
10 changes: 8 additions & 2 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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 @@ -204,7 +207,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
31 changes: 31 additions & 0 deletions src/conditions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ 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_arm64_flag",
values = {"cpu": "x64_arm64_windows"},
)

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

config_setting(
name = "arm",
constraint_values = ["@platforms//cpu:arm"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public String convert(String input) throws OptionsParsingException {
switch (CPU.getCurrent()) {
case X86_64:
return "x64_windows";
case AARCH64:
return "x64_arm64_windows";
default:
// We only support x64 Windows for now.
return "unknown";
Expand Down
2 changes: 1 addition & 1 deletion third_party/grpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load("//tools/distributions:distribution_rules.bzl", "distrib_java_import", "dis

licenses(["notice"]) # Apache v2

exports_files(["grpc_1.33.1.patch"])
exports_files(["grpc_1.33.1.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 <niyas.sait@linaro.org>
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: 1 addition & 1 deletion tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def cc_autoconf_impl(repository_ctx, overriden_tools = dict()):
# container so skipping until we have proper tests for these platforms.
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:bsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.bsd"], "BUILD")
elif cpu_value == "x64_windows":
elif cpu_value in ["x64_windows", "x64_arm64_windows"]:
# TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain.
# We might want to add that to Windows too(at least for msys toolchain).
configure_windows_toolchain(repository_ctx)
Expand Down
7 changes: 6 additions & 1 deletion tools/cpp/lib_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ def get_cpu_value(repository_ctx):
if os_name.find("openbsd") != -1:
return "openbsd"
if os_name.find("windows") != -1:
return "x64_windows"
arch = (get_env_var(repository_ctx, "PROCESSOR_ARCHITECTURE", None, False) or
get_env_var(repository_ctx, "PROCESSOR_ARCHITEW6432", None, False))
if arch == "ARM64":
return "x64_arm64_windows"
else:
return "x64_windows"

# Use uname to figure out whether we are on x86_32 or x86_64
result = repository_ctx.execute(["uname", "-m"])
Expand Down
6 changes: 6 additions & 0 deletions tools/cpp/windows_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ def _get_vc_env_vars(repository_ctx, vc_path, msvc_vars_x64, target_arch):
lib = msvc_vars_x64["%{msvc_env_lib_x64}"]
full_version = _get_vc_full_version(repository_ctx, vc_path)
tools_path = "%s\\Tools\\MSVC\\%s\\bin\\HostX64\\%s" % (vc_path, full_version, target_arch)
# For native windows on arm64 builds host toolchain runs in an emulated x86 environment
if not repository_ctx.path(tools_path).exists:
tools_path = tools_path.replace("HostX64", "HostX86")
else:
lib = msvc_vars_x64["%{msvc_env_lib_x64}"].replace("amd64", _targets_lib_folder[target_arch])
tools_path = vc_path + "\\bin\\" + _targets_archs[target_arch]
Expand Down Expand Up @@ -445,6 +448,9 @@ def find_msvc_tool(repository_ctx, vc_path, tool, target_arch = "x64"):
full_version = _get_vc_full_version(repository_ctx, vc_path)
if full_version:
tool_path = "%s\\Tools\\MSVC\\%s\\bin\\HostX64\\%s\\%s" % (vc_path, full_version, target_arch, tool)
# For native windows on arm64 builds host toolchain runs in an emulated x86 environment
if not repository_ctx.path(tool_path).exists:
tool_path = tool_path.replace("HostX64", "HostX86")
else:
# For VS 2015 and older version, the tools are under:
# C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64
Expand Down

0 comments on commit d0b11e3

Please sign in to comment.