Skip to content

Commit

Permalink
Build Win32 executables with required build config
Browse files Browse the repository at this point in the history
As a preparation to use Bazel to build Mozc for Windows (#948), this
commit reworks my previous attempts [1][2], which aimed to make it
configurable on which executable should be built with what build
configurations (e.g. CRT link type, target CPU architecture).

For instance, the following command will build mozc_tool with dynamic
CRT for 64-bit, and mozc_tip32 with static CRT for 32-bit.

  bazel --bazelrc=windows.bazelrc build  \
      //win32/installer:mozc_tool        \
      //win32/installer:mozc_tip32       \
      --config oss_windows

There must be no observable behavior change in GYP build.

 [1]: 5efa371
 [2]: ff64988

PiperOrigin-RevId: 680165643
  • Loading branch information
yukawa authored and hiroyuki-komatsu committed Sep 29, 2024
1 parent 4e7b736 commit ea55af0
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 84 deletions.
82 changes: 0 additions & 82 deletions src/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application", "macos_bu
load("@windows_sdk//:windows_sdk_rules.bzl", "windows_resource")
load(
"//:config.bzl",
"BAZEL_TOOLS_PREFIX",
"BRANDING",
"MACOS_BUNDLE_ID_PREFIX",
"MACOS_MIN_OS_VER",
Expand Down Expand Up @@ -513,87 +512,6 @@ def mozc_macos_bundle(name, bundle_name, infoplists, strings = [], bundle_id = N
**kwargs
)

def _win_executable_transition_impl(
settings, # @unused
attr):
features = []
if attr.static_crt:
features = ["static_link_msvcrt"]
return {
"//command_line_option:features": features,
"//command_line_option:cpu": attr.cpu,
}

_win_executable_transition = transition(
implementation = _win_executable_transition_impl,
inputs = [],
outputs = [
"//command_line_option:features",
"//command_line_option:cpu",
],
)

def _mozc_win_build_rule_impl(ctx):
input_file = ctx.file.target
output = ctx.actions.declare_file(
ctx.label.name + "." + input_file.extension,
)
if input_file.path == output.path:
fail("input=%d and output=%d are the same." % (input_file.path, output.path))

# Create a symlink as we do not need to create an actual copy.
ctx.actions.symlink(
output = output,
target_file = input_file,
is_executable = True,
)
return [DefaultInfo(
files = depset([output]),
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
)

# A custom rule to reference the given build target with the given build configurations.
#
# For instance, the following rule creates a target "my_target" with setting "cpu" as "x64_windows"
# and setting "static_link_msvcrt" feature.
#
# mozc_win_build_rule(
# name = "my_target",
# cpu = CPU.X64,
# static_crt = True,
# target = "//bath/to/target:my_target",
# )
#
# See the following page for the details on transition.
# https://bazel.build/rules/lib/builtins/transition
mozc_win_build_rule = rule(
implementation = _mozc_win_build_rule_impl,
cfg = _win_executable_transition,
attrs = {
"_allowlist_function_transition": attr.label(
default = BAZEL_TOOLS_PREFIX + "//tools/allowlists/function_transition_allowlist",
),
"target": attr.label(
allow_single_file = [".dll", ".exe"],
doc = "the actual Bazel target to be built.",
mandatory = True,
),
"static_crt": attr.bool(
default = False,
),
"cpu": attr.string(
default = "x64_windows",
),
},
)

def _get_value(args):
for arg in args:
if arg != None:
Expand Down
77 changes: 75 additions & 2 deletions src/win32/installer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,78 @@

# Build rules for the Windows installer.

# Note, omaha_channel_type can be controlled by //:dev_channel
# See BUILD_TYPE_SELECT in mac/BUILD as an example.
load("//:build_defs.bzl", "MOZC_TAGS")
load(":transition.bzl", "mozc_win_build_target")

package(default_visibility = ["//visibility:private"])

_TARGET_COMPATIBLE_WITH = [
"@platforms//os:windows",
]

mozc_win_build_target(
name = "mozc_tool",
cpu = "x64_windows",
tags = MOZC_TAGS.WIN_ONLY,
target = "//gui/tool:mozc_tool",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_renderer",
cpu = "x64_windows",
tags = MOZC_TAGS.WIN_ONLY,
target = "//renderer/win32:win32_renderer_main",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_server",
cpu = "x64_windows",
tags = MOZC_TAGS.WIN_ONLY,
target = "//server:mozc_server",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_broker",
cpu = "x64_windows",
tags = MOZC_TAGS.WIN_ONLY,
target = "//win32/broker:mozc_broker_main",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_cache_service",
cpu = "x64_windows",
tags = MOZC_TAGS.WIN_ONLY,
target = "//win32/cache_service:mozc_cache_service",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_tip32",
cpu = "x64_x86_windows",
static_crt = True,
tags = MOZC_TAGS.WIN_ONLY,
target = "//win32/tip:mozc_tip",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "mozc_tip64",
cpu = "x64_windows",
static_crt = True,
tags = MOZC_TAGS.WIN_ONLY,
target = "//win32/tip:mozc_tip",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)

mozc_win_build_target(
name = "custom_action",
cpu = "x64_windows",
static_crt = True,
tags = MOZC_TAGS.WIN_ONLY,
target = "//win32/custom_action",
target_compatible_with = _TARGET_COMPATIBLE_WITH,
)
161 changes: 161 additions & 0 deletions src/win32/installer/transition.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Define a transition rules for Windows build."""

load("//:build_defs.bzl", "MOZC_TAGS")
load("//:config.bzl", "BAZEL_TOOLS_PREFIX")

def _win_executable_transition_impl(
settings, # @unused
attr):
features = []
if attr.static_crt:
features = ["static_link_msvcrt"]
return {
"//command_line_option:features": features,
"//command_line_option:cpu": attr.cpu,
}

_win_executable_transition = transition(
implementation = _win_executable_transition_impl,
inputs = [],
outputs = [
"//command_line_option:features",
"//command_line_option:cpu",
],
)

def _mozc_win_build_rule_impl(ctx):
input_file = ctx.file.target
output = ctx.actions.declare_file(
ctx.label.name + "." + input_file.extension,
)
if input_file.path == output.path:
fail("input=%d and output=%d are the same." % (input_file.path, output.path))

# Create a symlink as we do not need to create an actual copy.
ctx.actions.symlink(
output = output,
target_file = input_file,
is_executable = True,
)
return [DefaultInfo(
files = depset([output]),
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
)

_mozc_win_build_rule = rule(
implementation = _mozc_win_build_rule_impl,
cfg = _win_executable_transition,
attrs = {
"_allowlist_function_transition": attr.label(
default = BAZEL_TOOLS_PREFIX + "//tools/allowlists/function_transition_allowlist",
),
"target": attr.label(
allow_single_file = [".dll", ".exe"],
doc = "the actual Bazel target to be built.",
mandatory = True,
),
"static_crt": attr.bool(),
"cpu": attr.string(),
},
)

# Define a transition target with the given build target with the given build configurations.
#
# For instance, the following code creates a target "my_target" with setting "cpu" as "x64_windows"
# and setting "static_link_msvcrt" feature.
#
# mozc_win_build_rule(
# name = "my_target",
# cpu = CPU.X64,
# static_crt = True,
# target = "//bath/to/target:my_target",
# )
#
# See the following page for the details on transition.
# https://bazel.build/rules/lib/builtins/transition
def mozc_win_build_target(
name,
target,
cpu = CPU.X64,
static_crt = False,
target_compatible_with = [],
tags = [],
**kwargs):
"""Define a transition target with the given build target with the given build configurations.
The following code creates a target "my_target" with setting "cpu" as "x64_windows" and setting
"static_link_msvcrt" feature.
mozc_win_build_target(
name = "my_target",
cpu = CPU.X64,
static_crt = True,
target = "//bath/to/target:my_target",
)
Args:
name: name of the target.
target: the actual Bazel target to be built with the specified configurations.
cpu: CPU type of the target.
static_crt: True if the target should be built with static CRT.
target_compatible_with: optional. Visibility for the unit test target.
tags: optional. Tags for both the library and unit test targets.
**kwargs: other arguments passed to mozc_objc_library.
"""
mandatory_target_compatible_with = [
"@platforms//os:windows",
]
for item in mandatory_target_compatible_with:
if item not in target_compatible_with:
target_compatible_with.append(item)

mandatory_tags = MOZC_TAGS.WIN_ONLY
for item in mandatory_tags:
if item not in tags:
tags.append(item)

_mozc_win_build_rule(
name = name,
target = target,
cpu = cpu,
static_crt = static_crt,
target_compatible_with = mandatory_target_compatible_with,
tags = tags,
**kwargs
)

0 comments on commit ea55af0

Please sign in to comment.