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 25, 2022
1 parent 02ad3e3 commit bcfdf9a
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 19 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
2 changes: 1 addition & 1 deletion src/conditions/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ config_setting(
# TODO: figure out how to base this selection on constraints
config_setting(
name = "host_windows",
values = {"host_cpu": "x64_windows"},
values = {"host_cpu": "x64_arm64_windows"},
visibility = ["//visibility:public"],
)

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
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public UnloadedToolchainContext compute(SkyKey skyKey, Environment env)
Preconditions.checkNotNull(configuration.getFragment(PlatformConfiguration.class));

// Check if debug output should be generated.
boolean debug =
boolean debug =
configuration.getOptions().get(PlatformOptions.class).toolchainResolutionDebug;

// Load the configured target for the toolchain types to ensure that they are valid and
Expand Down
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
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

66 changes: 66 additions & 0 deletions tools/cpp/BUILD.windows.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ cc_toolchain_suite(
"x64_windows": ":cc-compiler-x64_windows",
"x64_x86_windows": ":cc-compiler-x64_x86_windows",
"x64_arm_windows": ":cc-compiler-x64_arm_windows",
"x64_arm64_windows|clang-cl": ":cc-compiler-x64_arm64_windows-clang-cl",
"x64_arm64_windows": ":cc-compiler-x64_arm64_windows",
"armeabi-v7a": ":cc-compiler-armeabi-v7a",
},
Expand Down Expand Up @@ -488,6 +489,71 @@ toolchain(
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
name = "cc-compiler-x64_arm64_windows-clang-cl",
toolchain_identifier = "clang_cl_arm64",
toolchain_config = ":clang_cl_arm64",
all_files = ":empty",
ar_files = ":empty",
as_files = ":clangcl_compiler_files",
compiler_files = ":clangcl_compiler_files",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 1,
)

cc_toolchain_config(
name = "clang_cl_arm64",
cpu = "x64_arm64_windows",
compiler = "clang-cl",
host_system_name = "local",
target_system_name = "aarch64-pc-windows-msvc",
target_libc = "msvcrt",
abi_version = "local",
abi_libc_version = "local",
toolchain_identifier = "clang_arm64_x64",
msvc_env_tmp = "%{clang_cl_env_tmp}",
msvc_env_path = "%{clang_cl_env_path}",
msvc_env_include = "%{clang_cl_env_include}",
msvc_env_lib = "%{clang_cl_env_lib}",
msvc_cl_path = "%{clang_cl_cl_path}",
msvc_ml_path = "%{clang_cl_ml_path}",
msvc_link_path = "%{clang_cl_link_path}",
msvc_lib_path = "%{clang_cl_lib_path}",
cxx_builtin_include_directories = [%{clang_cl_cxx_builtin_include_directories}],
tool_paths = {
"ar": "%{clang_cl_lib_path}",
"ml": "%{clang_cl_ml_path}",
"cpp": "%{clang_cl_cl_path}",
"gcc": "%{clang_cl_cl_path}",
"gcov": "wrapper/bin/msvc_nop.bat",
"ld": "%{clang_cl_link_path}",
"nm": "wrapper/bin/msvc_nop.bat",
"objcopy": "wrapper/bin/msvc_nop.bat",
"objdump": "wrapper/bin/msvc_nop.bat",
"strip": "wrapper/bin/msvc_nop.bat",
},
archiver_flags = ["/MACHINE:ARM64"],
default_link_flags = ["/MACHINE:ARM64"],
dbg_mode_debug_flag = "%{clang_cl_dbg_mode_debug_flag}",
fastbuild_mode_debug_flag = "",
)

toolchain(
name = "cc-toolchain-x64_arm64_windows-clang-cl",
exec_compatible_with = [
"@platforms//os:windows",
"@bazel_tools//tools/cpp:clang-cl",
],
target_compatible_with = [
"@platforms//os:windows",
],
toolchain = ":cc-compiler-x64_arm64_windows-clang-cl",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

cc_toolchain(
name = "cc-compiler-armeabi-v7a",
toolchain_identifier = "stub_armeabi-v7a",
Expand Down
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
25 changes: 16 additions & 9 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 Expand Up @@ -686,7 +692,7 @@ def _get_clang_cl_vars(repository_ctx, paths, msvc_vars):
"""Get the variables we need to populate the clang-cl toolchains."""
llvm_path = find_llvm_path(repository_ctx)
error_script = None
if msvc_vars["%{msvc_cl_path_x64}"] == "vc_installation_error_x64.bat":
if msvc_vars["%{msvc_cl_path_arm64}"] == "vc_installation_error_x64.bat":
error_script = "vc_installation_error_x64.bat"
elif not llvm_path:
repository_ctx.template(
Expand Down Expand Up @@ -738,18 +744,18 @@ def _get_clang_cl_vars(repository_ctx, paths, msvc_vars):
clang_include_path = (clang_dir + "\\include").replace("\\", "\\\\")
clang_lib_path = (clang_dir + "\\lib\\windows").replace("\\", "\\\\")

clang_cl_include_directories = msvc_vars["%{msvc_cxx_builtin_include_directories_x64}"] + (",\n \"%s\"" % clang_include_path)
clang_cl_include_directories = msvc_vars["%{msvc_cxx_builtin_include_directories_arm64}"] + (",\n \"%s\"" % clang_include_path)
write_builtin_include_directory_paths(repository_ctx, "clang-cl", [clang_cl_include_directories], file_suffix = "_clangcl")
clang_cl_vars = {
"%{clang_cl_env_tmp}": msvc_vars["%{msvc_env_tmp_x64}"],
"%{clang_cl_env_path}": msvc_vars["%{msvc_env_path_x64}"],
"%{clang_cl_env_include}": msvc_vars["%{msvc_env_include_x64}"] + ";" + clang_include_path,
"%{clang_cl_env_lib}": msvc_vars["%{msvc_env_lib_x64}"] + ";" + clang_lib_path,
"%{clang_cl_env_tmp}": msvc_vars["%{msvc_env_tmp_arm64}"],
"%{clang_cl_env_path}": msvc_vars["%{msvc_env_path_arm64}"],
"%{clang_cl_env_include}": msvc_vars["%{msvc_env_include_arm64}"] + ";" + clang_include_path,
"%{clang_cl_env_lib}": msvc_vars["%{msvc_env_lib_arm64}"] + ";" + clang_lib_path,
"%{clang_cl_cxx_builtin_include_directories}": clang_cl_include_directories,
"%{clang_cl_cl_path}": clang_cl_path,
"%{clang_cl_link_path}": lld_link_path,
"%{clang_cl_lib_path}": llvm_lib_path,
"%{clang_cl_ml_path}": msvc_vars["%{msvc_ml_path_x64}"],
"%{clang_cl_ml_path}": clang_cl_path,
# LLVM's lld-link.exe doesn't support /DEBUG:FASTLINK.
"%{clang_cl_dbg_mode_debug_flag}": "/DEBUG",
"%{clang_cl_fastbuild_mode_debug_flag}": "/DEBUG",
Expand Down Expand Up @@ -783,11 +789,12 @@ def configure_windows_toolchain(repository_ctx):
template_vars = dict()
msvc_vars_x64 = _get_msvc_vars(repository_ctx, paths, "x64")
template_vars.update(msvc_vars_x64)
template_vars.update(_get_clang_cl_vars(repository_ctx, paths, msvc_vars_x64))
template_vars.update(_get_msys_mingw_vars(repository_ctx))
template_vars.update(_get_msvc_vars(repository_ctx, paths, "x86", msvc_vars_x64))
template_vars.update(_get_msvc_vars(repository_ctx, paths, "arm", msvc_vars_x64))
template_vars.update(_get_msvc_vars(repository_ctx, paths, "arm64", msvc_vars_x64))
msvc_vars_arm64 = _get_msvc_vars(repository_ctx, paths, "arm64", msvc_vars_x64)
template_vars.update(msvc_vars_arm64)
template_vars.update(_get_clang_cl_vars(repository_ctx, paths, msvc_vars_arm64))

repository_ctx.template(
"BUILD",
Expand Down
Loading

0 comments on commit bcfdf9a

Please sign in to comment.