Skip to content

Commit

Permalink
Add a rule to build Windows executables w/ custom build configs
Browse files Browse the repository at this point in the history
This CL adds a custom rule `mozc_win_build_rule` to build Windows
executables with the given build configurations. This rule is necessary
to build Windows executables with the correct build configurations,
such as the CPU architecture and the CRT library.

For instance, the following build rule tells Bazel to have
"custom_action" target with "x64_windows" CPU architecture and with
statically linking to MSVC C++ runtime.

  mozc_win_build_rule(
      name = "custom_action",
      target = "//win32/custom_action:custom_action",
      cpu = "x64_windows",
      static_crt = True,
  )

This CL is a part of the effort to migrate the Windows build system
from GYP to Bazel (google#948).

#codehealth

PiperOrigin-RevId: 646324022
  • Loading branch information
yukawa authored and hiroyuki-komatsu committed Jun 25, 2024
1 parent 703a7f8 commit 5efa371
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ build:windows --define TARGET=oss_windows --build_tag_filters=-nowin
build:oss_windows --define TARGET=oss_windows --build_tag_filters=-nowin
build:prod_windows --define TARGET=prod_windows --build_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 --noincompatible_enable_cc_toolchain_resolution
build:oss_macos --noincompatible_enable_cc_toolchain_resolution
build:prod_macos --noincompatible_enable_cc_toolchain_resolution

# Android / OSS Android (same configurations)
build:android --define TARGET=oss_android --copt "-DOS_ANDROID"
build:android --android_crosstool_top=@androidndk//:toolchain
Expand Down
82 changes: 81 additions & 1 deletion src/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ See also: https://bazel.build/rules/bzl-style#rules
"""

load("@build_bazel_rules_apple//apple:macos.bzl", "macos_application", "macos_bundle", "macos_unit_test")
load("//:config.bzl", "BRANDING", "MACOS_BUNDLE_ID_PREFIX", "MACOS_MIN_OS_VER")
load(
"//:config.bzl",
"BAZEL_TOOLS_PREFIX",
"BRANDING",
"MACOS_BUNDLE_ID_PREFIX",
"MACOS_MIN_OS_VER",
)
load("//bazel:run_build_tool.bzl", "mozc_run_build_tool")
load("//bazel:stubs.bzl", "pytype_strict_binary", "pytype_strict_library", "register_extension_info")

Expand Down Expand Up @@ -434,6 +440,80 @@ 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,
)]

# 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 = "x64_windows",
# 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

0 comments on commit 5efa371

Please sign in to comment.