Skip to content

Commit

Permalink
Merge branch 'google:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shitamo authored Dec 9, 2024
2 parents 65491bf + bc546b2 commit 4c7a407
Show file tree
Hide file tree
Showing 21 changed files with 662 additions and 324 deletions.
175 changes: 175 additions & 0 deletions src/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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 @@ -203,6 +204,180 @@ register_extension_info(
label_regex_for_dep = "{extension_name}",
)

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 = target_compatible_with,
tags = tags,
**kwargs
)

def mozc_win32_cc_prod_binary(
name,
executable_name_map = {}, # @unused
srcs = [], # @unused
deps = [], # @unused
linkopts = [], # @unused
cpu = CPU.X64,
static_crt = False,
tags = MOZC_TAGS.WIN_ONLY,
win_def_file = None, # @unused
target_compatible_with = ["@platforms//os:windows"],
visibility = ["//visibility:public"], # @unused
**kwargs):
"""A rule to build production binaries for Windows.
This wraps mozc_cc_binary so that you can specify the target CPU
architecture and CRT linkage type in a declarative manner with also building
a debug symbol file (*.pdb).
Implicit output targets:
name.pdb: A debug symbol file.
Args:
name: name of the target.
executable_name_map: a map from the branding name to the executable name.
srcs: .cc files to build the executable.
deps: deps to build the executable.
linkopts: linker options to build the executable.
cpu: optional. The target CPU architecture.
static_crt: optional. True if the target should be built with static CRT.
tags: optional. Tags for both the library and unit test targets.
win_def_file: optional. win32 def file to define exported functions.
target_compatible_with: optional. Defines target platforms.
visibility: optional. The visibility of the target.
**kwargs: other arguments passed to mozc_cc_binary.
"""
mozc_win_build_target(
name = name,
target = "//data/prod:prod_binary",
cpu = cpu,
static_crt = static_crt,
target_compatible_with = target_compatible_with,
tags = tags,
**kwargs
)

def mozc_cc_win32_library(
name,
srcs = [],
Expand Down
1 change: 1 addition & 0 deletions src/converter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ mozc_cc_test(
"//dictionary:user_dictionary_stub",
"//dictionary:user_pos",
"//engine",
"//engine:engine_interface",
"//engine:mock_data_engine_factory",
"//engine:modules",
"//prediction:dictionary_predictor",
Expand Down
33 changes: 20 additions & 13 deletions src/converter/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <numeric>
#include <string>
#include <utility>
#include <vector>

#include "absl/base/optimization.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/util.h"
Expand Down Expand Up @@ -523,28 +525,33 @@ bool Converter::ResizeSegment(Segments *segments,
return true;
}

bool Converter::ResizeSegment(Segments *segments,
const ConversionRequest &request,
size_t start_segment_index, size_t segments_size,
absl::Span<const uint8_t> new_size_array) const {
bool Converter::ResizeSegments(Segments *segments,
const ConversionRequest &request,
size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const {
if (request.request_type() != ConversionRequest::CONVERSION) {
return false;
}

constexpr size_t kMaxArraySize = 256;
start_segment_index = GetSegmentIndex(segments, start_segment_index);
const size_t end_segment_index = start_segment_index + segments_size;
if (start_segment_index == kErrorIndex ||
end_segment_index <= start_segment_index ||
end_segment_index > segments->segments_size() ||
new_size_array.size() > kMaxArraySize) {
if (start_segment_index == kErrorIndex) {
return false;
}

const size_t total_size =
std::accumulate(new_size_array.begin(), new_size_array.end(), 0);
if (total_size == 0) {
return false;
}

std::string key;
for (const Segment &segment :
segments->all().subrange(start_segment_index, segments_size)) {
key += segment.key();
size_t segments_size = 0;
for (const Segment &segment : segments->all().drop(start_segment_index)) {
absl::StrAppend(&key, segment.key());
++segments_size;
if (Util::CharsLen(key) >= total_size) {
break;
}
}

if (key.empty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class Converter final : public ConverterInterface {
const ConversionRequest &request,
size_t segment_index,
int offset_length) const override;
ABSL_MUST_USE_RESULT bool ResizeSegment(
ABSL_MUST_USE_RESULT bool ResizeSegments(
Segments *segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size,
size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const override;

// Execute ImmutableConverter, Rewriters, SuppressionDictionary.
Expand Down
4 changes: 2 additions & 2 deletions src/converter/converter_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ class ConverterInterface {

// Resize [start_segment_index, start_segment_index + segment_size]
// segments with the new size in new_size_array.
ABSL_MUST_USE_RESULT virtual bool ResizeSegment(
ABSL_MUST_USE_RESULT virtual bool ResizeSegments(
Segments *segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size,
size_t start_segment_index,
absl::Span<const uint8_t> new_size_array) const = 0;

protected:
Expand Down
13 changes: 9 additions & 4 deletions src/converter/converter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,20 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
return converter.ResizeSegment(segments, conversion_request,
NumberUtil::SimpleAtoi(fields[1]),
NumberUtil::SimpleAtoi(fields[2]));
} else if (fields.size() > 3) {
}
} else if (func == "resizesegments" || func == "resizes") {
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
if (fields.size() > 3) {
std::vector<uint8_t> new_arrays;
for (size_t i = 3; i < fields.size(); ++i) {
for (size_t i = 2; i < fields.size(); ++i) {
new_arrays.push_back(
static_cast<uint8_t>(NumberUtil::SimpleAtoi(fields[i])));
}
return converter.ResizeSegment(
return converter.ResizeSegments(
segments, conversion_request, NumberUtil::SimpleAtoi(fields[1]),
NumberUtil::SimpleAtoi(fields[2]), new_arrays);
new_arrays);
}
} else if (func == "disableuserhistory") {
config->set_history_learning_level(config::Config::NO_HISTORY);
Expand Down
4 changes: 2 additions & 2 deletions src/converter/converter_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class StrictMockConverter : public ConverterInterface {
(Segments * segments, const ConversionRequest &request,
size_t segment_index, int offset_length),
(const, override));
MOCK_METHOD(bool, ResizeSegment,
MOCK_METHOD(bool, ResizeSegments,
(Segments * segments, const ConversionRequest &request,
size_t start_segment_index, size_t segments_size,
size_t start_segment_index,
absl::Span<const uint8_t> new_size_array),
(const, override));
};
Expand Down
Loading

0 comments on commit 4c7a407

Please sign in to comment.