From d0b11e3c131dd6ab951fe26fd4a2191d27f0e7f9 Mon Sep 17 00:00:00 2001 From: Niyas Sait Date: Fri, 26 Nov 2021 02:01:35 -0800 Subject: [PATCH] Enable native support for Windows on arm64 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 --- .bazelci/build_bazel_binaries.yml | 10 ++++ WORKSPACE | 7 +++ src/BUILD | 10 +++- src/conditions/BUILD | 31 +++++++++++ .../lib/analysis/config/AutoCpuConverter.java | 2 + third_party/grpc/BUILD | 2 +- third_party/grpc/grpc_1.41.0.win_arm64.patch | 51 +++++++++++++++++++ tools/cpp/cc_configure.bzl | 2 +- tools/cpp/lib_cc_configure.bzl | 7 ++- tools/cpp/windows_cc_configure.bzl | 6 +++ 10 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 third_party/grpc/grpc_1.41.0.win_arm64.patch diff --git a/.bazelci/build_bazel_binaries.yml b/.bazelci/build_bazel_binaries.yml index 5478050004e442..26c8b06d7935c9 100644 --- a/.bazelci/build_bazel_binaries.yml +++ b/.bazelci/build_bazel_binaries.yml @@ -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" \ No newline at end of file diff --git a/WORKSPACE b/WORKSPACE index 2d995f095ed4f7..520cf3ee3d6430 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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", diff --git a/src/BUILD b/src/BUILD index 936adcd513ed52..cbc363fd1cfec8 100644 --- a/src/BUILD +++ b/src/BUILD @@ -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": [ @@ -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": [ diff --git a/src/conditions/BUILD b/src/conditions/BUILD index 5f8f6a7767ffc4..35faefd06f8977 100644 --- a/src/conditions/BUILD +++ b/src/conditions/BUILD @@ -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"], diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java b/src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java index 9f5fbca65fd003..69c40e381581a5 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java @@ -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"; diff --git a/third_party/grpc/BUILD b/third_party/grpc/BUILD index 5b33e42ca210b5..4c333c5da623c0 100644 --- a/third_party/grpc/BUILD +++ b/third_party/grpc/BUILD @@ -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"]) diff --git a/third_party/grpc/grpc_1.41.0.win_arm64.patch b/third_party/grpc/grpc_1.41.0.win_arm64.patch new file mode 100644 index 00000000000000..355c9dd4bd57e8 --- /dev/null +++ b/third_party/grpc/grpc_1.41.0.win_arm64.patch @@ -0,0 +1,51 @@ +From 6ce08c3da545358074eb66dc4202e0474fa5be1b Mon Sep 17 00:00:00 2001 +From: Niyas Sait +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 + diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 8e5acc2ee319b6..ecc565ff8b74c1 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -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) diff --git a/tools/cpp/lib_cc_configure.bzl b/tools/cpp/lib_cc_configure.bzl index 1464d8ca6688e5..e48db4d19bc793 100644 --- a/tools/cpp/lib_cc_configure.bzl +++ b/tools/cpp/lib_cc_configure.bzl @@ -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"]) diff --git a/tools/cpp/windows_cc_configure.bzl b/tools/cpp/windows_cc_configure.bzl index fa7b3399abd4a7..1eb4f082730900 100644 --- a/tools/cpp/windows_cc_configure.bzl +++ b/tools/cpp/windows_cc_configure.bzl @@ -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] @@ -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