From df80debea46f0d2a469f14d909a3c3ad845d1fdd Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Sun, 29 Dec 2024 02:22:15 +1030 Subject: [PATCH] Rely on CC toolchain resolution on Windows This is a follow up commit to my previous commit [1], which updated the Bazel version from 7.4.1 to 8.0.0. It turns out that --noincompatible_enable_cc_toolchain_resolution is now no-op in Bazel 8.0. Thus there remains no way other than fully migrating to the new CC toolchain resolution as planned in #1112. Otherwise 'mozc_tip32.dll' will be built as a 64-bit executable (#1102). This commit consists of two parts: 1. explicitly register CC toolchains in 'windows.bazelrc'. 2. Switch from '--cpu' commandline option to '--platforms' commandline option in '_win_executable_transition'. With above 'mozc_tip32.dll' will be built as a 32-bit executable again. Closes #1102. Closes #1112. [1]: 6dadef1c3d456649f8fbe48b6ee1f1515367b5e1 --- src/.bazelrc | 17 ++++++++++++----- src/build_defs.bzl | 26 ++++++++++++++++++-------- src/gui/tool/BUILD.bazel | 2 +- src/renderer/win32/BUILD.bazel | 2 +- src/server/BUILD.bazel | 2 +- src/win32/broker/BUILD.bazel | 2 +- src/win32/cache_service/BUILD.bazel | 2 +- src/win32/custom_action/BUILD.bazel | 2 +- src/win32/tip/BUILD.bazel | 4 ++-- 9 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/.bazelrc b/src/.bazelrc index 304ff4d69..065883be5 100644 --- a/src/.bazelrc +++ b/src/.bazelrc @@ -56,11 +56,18 @@ test:macos_env --test_tag_filters=-nomac build:windows_env --build_tag_filters=-nowin test:windows_env --test_tag_filters=-nowin -# A temporary workaround to make "mozc_win_build_rule" work. -# Note that "incompatible_enable_cc_toolchain_resolution" is now enabled by -# default. See https://github.com/bazelbuild/bazel/issues/7260 -# TODO: Re-enable "incompatible_enable_cc_toolchain_resolution" -build:windows_env --noincompatible_enable_cc_toolchain_resolution + +# Bazel does not register cc toolchains for Windows by default. +# We need to explicitly specify them to be compatible with the new behavior +# introduced by --incompatible_enable_cc_toolchain_resolution +# https://github.com/google/mozc/issues/1112 +# https://bazel.build/extending/toolchains#registering-building-toolchains +build:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-arm64_windows +build:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows +build:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_x86_windows +test:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-arm64_windows +test:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows +test:windows_env --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_x86_windows # Android specific options build:android_env --copt "-DOS_ANDROID" diff --git a/src/build_defs.bzl b/src/build_defs.bzl index 59e7affb5..715150cee 100644 --- a/src/build_defs.bzl +++ b/src/build_defs.bzl @@ -212,7 +212,7 @@ def _win_executable_transition_impl( features = ["static_link_msvcrt"] return { "//command_line_option:features": features, - "//command_line_option:cpu": attr.cpu, + "//command_line_option:platforms": [attr.platform], } _win_executable_transition = transition( @@ -220,7 +220,7 @@ _win_executable_transition = transition( inputs = [], outputs = [ "//command_line_option:features", - "//command_line_option:cpu", + "//command_line_option:platforms", ], ) @@ -243,11 +243,10 @@ def _mozc_win_build_rule_impl(ctx): executable = output, )] -# The follwoing CPU values are mentioned in https://bazel.build/configure/windows#build_cpp CPU = struct( - ARM64 = "arm64_windows", # aarch64 (64-bit) environment - X64 = "x64_windows", # x86-64 (64-bit) environment - X86 = "x64_x86_windows", # x86 (32-bit) environment + ARM64 = "@platforms//cpu:arm64", # aarch64 (64-bit) environment + X64 = "@platforms//cpu:x86_64", # x86-64 (64-bit) environment + X86 = "@platforms//cpu:x86_32", # x86 (32-bit) environment ) _mozc_win_build_rule = rule( @@ -263,7 +262,7 @@ _mozc_win_build_rule = rule( mandatory = True, ), "static_crt": attr.bool(), - "cpu": attr.string(), + "platform": attr.label(), }, ) @@ -311,6 +310,7 @@ def mozc_win_build_target( **kwargs: other arguments passed to mozc_objc_library. """ mandatory_target_compatible_with = [ + cpu, "@platforms//os:windows", ] for item in mandatory_target_compatible_with: @@ -322,10 +322,20 @@ def mozc_win_build_target( if item not in tags: tags.append(item) + platform_name = "_" + name + "_platform" + native.platform( + name = platform_name, + constraint_values = [ + cpu, + "@platforms//os:windows", + ], + visibility = ["//visibility:private"], + ) + _mozc_win_build_rule( name = name, target = target, - cpu = cpu, + platform = platform_name, static_crt = static_crt, target_compatible_with = target_compatible_with, tags = tags, diff --git a/src/gui/tool/BUILD.bazel b/src/gui/tool/BUILD.bazel index 0ccf15e50..15c2251b7 100644 --- a/src/gui/tool/BUILD.bazel +++ b/src/gui/tool/BUILD.bazel @@ -135,7 +135,7 @@ mozc_cc_qt_binary( mozc_win32_cc_prod_binary( name = "mozc_tool_win", - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_tool.exe", "GoogleJapaneseInput": "GoogleIMEJaTool.exe", diff --git a/src/renderer/win32/BUILD.bazel b/src/renderer/win32/BUILD.bazel index f27116551..3df8996e0 100644 --- a/src/renderer/win32/BUILD.bazel +++ b/src/renderer/win32/BUILD.bazel @@ -48,7 +48,7 @@ package( mozc_win32_cc_prod_binary( name = "win32_renderer_main", srcs = ["win32_renderer_main.cc"], - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_renderer.exe", "GoogleJapaneseInput": "GoogleIMEJaRenderer.exe", diff --git a/src/server/BUILD.bazel b/src/server/BUILD.bazel index 50cb9acca..0c0fab08b 100644 --- a/src/server/BUILD.bazel +++ b/src/server/BUILD.bazel @@ -59,7 +59,7 @@ mozc_cc_binary( mozc_win32_cc_prod_binary( name = "mozc_server_win", - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_server.exe", "GoogleJapaneseInput": "GoogleIMEJaConverter.exe", diff --git a/src/win32/broker/BUILD.bazel b/src/win32/broker/BUILD.bazel index e8cc03a2a..84897b689 100644 --- a/src/win32/broker/BUILD.bazel +++ b/src/win32/broker/BUILD.bazel @@ -44,7 +44,7 @@ package( mozc_win32_cc_prod_binary( name = "mozc_broker_main", srcs = ["mozc_broker_main.cc"], - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_broker.exe", "GoogleJapaneseInput": "GoogleIMEJaBroker.exe", diff --git a/src/win32/cache_service/BUILD.bazel b/src/win32/cache_service/BUILD.bazel index 1241c0f42..d0d26a601 100644 --- a/src/win32/cache_service/BUILD.bazel +++ b/src/win32/cache_service/BUILD.bazel @@ -45,7 +45,7 @@ package(default_visibility = ["//:__subpackages__"]) mozc_win32_cc_prod_binary( name = "mozc_cache_service", srcs = ["mozc_cache_service.cc"], - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_cache_service.exe", "GoogleJapaneseInput": "GoogleIMEJaCacheService.exe", diff --git a/src/win32/custom_action/BUILD.bazel b/src/win32/custom_action/BUILD.bazel index d812f4deb..2cb2b84ef 100644 --- a/src/win32/custom_action/BUILD.bazel +++ b/src/win32/custom_action/BUILD.bazel @@ -41,7 +41,7 @@ mozc_win32_cc_prod_binary( "custom_action.h", "resource.h", ], - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_installer_helper.dll", "GoogleJapaneseInput": "GoogleIMEJaInstallerHelper.dll", diff --git a/src/win32/tip/BUILD.bazel b/src/win32/tip/BUILD.bazel index 70a9bf2f3..73bcb3d98 100644 --- a/src/win32/tip/BUILD.bazel +++ b/src/win32/tip/BUILD.bazel @@ -68,7 +68,7 @@ mozc_cc_library( mozc_win32_cc_prod_binary( name = "mozc_tip32", srcs = ["mozc_tip_main.cc"], - cpu = "x64_x86_windows", + cpu = "@platforms//cpu:x86_32", executable_name_map = { "Mozc": "mozc_tip32.dll", "GoogleJapaneseInput": "GoogleIMEJaTIP32.dll", @@ -87,7 +87,7 @@ mozc_win32_cc_prod_binary( mozc_win32_cc_prod_binary( name = "mozc_tip64", srcs = ["mozc_tip_main.cc"], - cpu = "x64_windows", + cpu = "@platforms//cpu:x86_64", executable_name_map = { "Mozc": "mozc_tip64.dll", "GoogleJapaneseInput": "GoogleIMEJaTIP64.dll",