forked from google/mozc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_defs.bzl
619 lines (561 loc) · 19.9 KB
/
build_defs.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# 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.
"""
cc_(library|binary|test) wrappers to add :macro dependency.
:macro defines attributes for each platforms so required macros are defined by
depending on it.
Macro naming guideline: Use mozc_ as a prefix, e.g.:
- mozc_cc_library
- mozc_objc_test
- mozc_macos_application
- mozc_select
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",
"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")
# Tags aliases for build filtering.
MOZC_TAGS = struct(
ANDROID_ONLY = ["nolinux", "nomac", "nowin"],
LINUX_ONLY = ["noandroid", "nomac", "nowin"],
MAC_ONLY = ["noandroid", "nolinux", "nowin"],
WIN_ONLY = ["noandroid", "nolinux", "nomac"],
)
def _copts_unsigned_char():
return select({
"//:compiler_msvc_like": ["/J"],
"//conditions:default": ["-funsigned-char"],
})
def _update_visibility(visibility = None):
"""
Returns updated visibility. This is temporarily used for the code location migration.
"""
if not visibility:
return visibility
if ("//:__subpackages__" in visibility):
return visibility + ["//:__subpackages__"]
return visibility
def mozc_cc_library(deps = [], copts = [], visibility = None, **kwargs):
"""
cc_library wrapper adding //:macro dependecny.
"""
native.cc_library(
deps = deps + ["//:macro"],
copts = copts + _copts_unsigned_char(),
visibility = _update_visibility(visibility),
**kwargs
)
register_extension_info(
extension = mozc_cc_library,
label_regex_for_dep = "{extension_name}",
)
def mozc_cc_binary(deps = [], copts = [], **kwargs):
"""
cc_binary wrapper adding //:macro dependecny.
"""
native.cc_binary(
deps = deps + ["//:macro"],
copts = copts + _copts_unsigned_char(),
**kwargs
)
register_extension_info(
extension = mozc_cc_binary,
label_regex_for_dep = "{extension_name}",
)
def mozc_cc_test(name, tags = [], deps = [], copts = [], **kwargs):
"""cc_test wrapper adding //:macro dependecny.
Args:
name: name for cc_test.
tags: targs for cc_test.
deps: deps for cc_test. //:macro is added.
copts: copts for cc_test.
**kwargs: other args for cc_test.
"""
native.cc_test(
name = name,
tags = tags,
deps = deps + ["//:macro"],
copts = copts + _copts_unsigned_char(),
**kwargs
)
register_extension_info(
extension = mozc_cc_test,
label_regex_for_dep = "{extension_name}",
)
def mozc_cc_win32_library(
name,
srcs = [],
deps = [],
hdrs = [],
win_def_file = None,
tags = MOZC_TAGS.WIN_ONLY,
target_compatible_with = ["@platforms//os:windows"],
visibility = None,
**kwargs):
"""A rule to build an DLL import library for Win32 system DLLs.
Args:
name: name for cc_library.
srcs: stub .cc files to define exported APIs.
deps: deps to build stub .cc files.
hdrs: header files to define exported APIs.
win_def_file: win32 def file to define exported APIs.
tags: optional tags.
target_compatible_with: optional target_compatible_with.
visibility: optional visibility.
**kwargs: other args for cc_library.
"""
# A DLL name, which actually will not be used in production.
# e.g. "input_dll_fake.dll" vs "C:\Windows\System32\input.dll"
# The actual DLL name should be specified in the LIBRARY section of
# win_def_file.
# https://learn.microsoft.com/en-us/cpp/build/reference/library
cc_binary_target_name = name + "_fake.dll"
filegroup_target_name = name + "_lib"
cc_import_taget_name = name + "_import"
mozc_cc_binary(
name = cc_binary_target_name,
srcs = srcs,
deps = deps,
win_def_file = win_def_file,
linkshared = 1,
tags = tags,
target_compatible_with = target_compatible_with,
visibility = ["//visibility:private"],
**kwargs
)
native.filegroup(
name = filegroup_target_name,
srcs = [":" + cc_binary_target_name],
output_group = "interface_library",
tags = tags,
target_compatible_with = target_compatible_with,
visibility = ["//visibility:private"],
)
native.cc_import(
name = cc_import_taget_name,
interface_library = ":" + filegroup_target_name,
shared_library = ":" + cc_binary_target_name,
tags = tags,
target_compatible_with = target_compatible_with,
visibility = ["//visibility:private"],
)
mozc_cc_library(
name = name,
hdrs = hdrs,
deps = [":" + cc_import_taget_name],
tags = tags,
target_compatible_with = target_compatible_with,
visibility = visibility,
)
register_extension_info(
extension = mozc_cc_win32_library,
label_regex_for_dep = "{extension_name}",
)
def mozc_py_library(name, srcs, srcs_version = "PY3", **kwargs):
"""py_library wrapper generating import-modified python scripts for iOS."""
pytype_strict_library(
name = name,
srcs = srcs,
srcs_version = srcs_version,
**kwargs
)
register_extension_info(
extension = mozc_py_library,
label_regex_for_dep = "{extension_name}",
)
def mozc_py_binary(name, srcs, python_version = "PY3", srcs_version = "PY3", test_lib = True, **kwargs):
"""py_binary wrapper generating import-modified python script for iOS.
To use this rule, corresponding mozc_py_library needs to be defined to
generate iOS sources.
"""
pytype_strict_binary(
name = name,
srcs = srcs,
python_version = python_version,
srcs_version = srcs_version,
test_lib = test_lib,
# This main specifier is required because, without it, py_binary expects
# that the file name of source containing main() is name.py.
main = srcs[0],
**kwargs
)
register_extension_info(
extension = mozc_py_binary,
label_regex_for_dep = "{extension_name}",
)
def mozc_infoplist(name, srcs = [], outs = []):
mozc_run_build_tool(
name = name,
srcs = {
"--input": srcs,
"--version_file": ["//base:mozc_version_txt"],
},
args = [
"--branding",
BRANDING,
],
outs = {
"--output": outs[0],
},
tool = "//build_tools:tweak_info_plist",
)
def mozc_infoplist_strings(name, srcs = [], outs = []):
mozc_run_build_tool(
name = name,
srcs = {
"--input": srcs,
},
outs = {
"--output": outs[0],
},
args = [
"--branding",
BRANDING,
],
tool = "//build_tools:tweak_info_plist_strings",
)
def mozc_objc_library(
name,
srcs = [],
hdrs = [],
deps = [],
copts = [],
tags = [],
**kwargs):
native.objc_library(
name = name,
srcs = srcs,
hdrs = hdrs,
deps = deps + [
"//:macro",
],
copts = copts + ["-funsigned-char", "-std=c++17"],
target_compatible_with = select({
"@platforms//os:macos": [],
"@platforms//os:ios": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
tags = tags + ["noandroid", "nolinux", "nowin"],
**kwargs
)
register_extension_info(
extension = mozc_objc_library,
label_regex_for_dep = "{extension_name}",
)
def _snake_case_camel_case(id):
# Don't capitalize if it's just one word.
if id.find("_") < 0:
return id
components = id.split("_")
return "".join([s.capitalize() for s in components])
def mozc_objc_test(
name,
bundle_id = None,
size = None,
visibility = None,
tags = [],
**kwargs):
"""A wrapper for objc_library and macos_unit_test.
This macro internally creates two targets: mozc_objc_library and macos_unit_test because the
macos_unit_test rule doesn't take source files directly.
Args:
name: name for the macos_unit_test target
bundle_id: optional. Test bundle id for macos_unit_test. The default value is
MACOS_BUNDLE_ID_PREFIX.package.name.CamelCasedName.
size: optional. passed to macos_unit_test.
visibility: 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.
"""
lib_name = name + "_lib"
default_bundle_id = ".".join([
MACOS_BUNDLE_ID_PREFIX,
_snake_case_camel_case(native.package_name().replace("/", ".")),
_snake_case_camel_case(name),
])
mozc_objc_library(
name = lib_name,
testonly = True,
alwayslink = True,
visibility = ["//visibility:private"],
tags = ["manual"] + tags,
**kwargs
)
macos_unit_test(
name = name,
minimum_os_version = MACOS_MIN_OS_VER,
bundle_id = bundle_id or default_bundle_id,
deps = [lib_name],
size = size,
visibility = visibility,
target_compatible_with = ["@platforms//os:macos"],
tags = MOZC_TAGS.MAC_ONLY + tags,
)
register_extension_info(
extension = mozc_objc_test,
label_regex_for_dep = "{extension_name}",
)
def _tweak_infoplists(name, infoplists):
tweaked_infoplists = []
for i, plist in enumerate(infoplists):
plist_name = "%s_plist%d" % (name, i)
mozc_infoplist(
name = plist_name,
srcs = [plist],
outs = [plist.replace(".plist", "_tweaked.plist")],
)
tweaked_infoplists.append(plist_name)
return tweaked_infoplists
def _tweak_strings(name, strings):
tweaked_strings = []
for i, string in enumerate(strings):
string_name = "%s_string%d" % (name, i)
mozc_infoplist_strings(
name = string_name,
srcs = [string],
outs = ["tweaked/" + string],
)
tweaked_strings.append(string_name)
return tweaked_strings
def mozc_macos_application(name, bundle_name, infoplists, strings = [], bundle_id = None, tags = [], **kwargs):
"""Rule to create .app for macOS.
Args:
name: name for macos_application.
bundle_name: bundle_name for macos_application.
infoplists: infoplists are tweaked and applied to macos_application.
strings: strings are tweaked and applied to macos_application.
bundle_id: bundle_id for macos_application.
tags: tags for macos_application.
**kwargs: other arguments for macos_application.
"""
macos_application(
name = name,
bundle_id = bundle_id or (MACOS_BUNDLE_ID_PREFIX + "." + bundle_name),
bundle_name = bundle_name,
infoplists = _tweak_infoplists(name, infoplists),
strings = _tweak_strings(name, strings),
minimum_os_version = MACOS_MIN_OS_VER,
version = "//data/version:version_macos",
target_compatible_with = ["@platforms//os:macos"],
tags = tags + MOZC_TAGS.MAC_ONLY,
**kwargs
)
def mozc_macos_bundle(name, bundle_name, infoplists, strings = [], bundle_id = None, tags = [], **kwargs):
"""Rule to create .bundle for macOS.
Args:
name: name for macos_bundle.
bundle_name: bundle_name for macos_bundle.
infoplists: infoplists are tweaked and applied to macos_bundle.
strings: strings are tweaked and applied to macos_bundle.
bundle_id: bundle_id for macos_bundle.
tags: tags for macos_bundle.
**kwargs: other arguments for macos_bundle.
"""
macos_bundle(
name = name,
bundle_id = bundle_id or (MACOS_BUNDLE_ID_PREFIX + "." + bundle_name),
bundle_name = bundle_name,
infoplists = _tweak_infoplists(name, infoplists),
strings = _tweak_strings(name, strings),
minimum_os_version = MACOS_MIN_OS_VER,
version = "//data/version:version_macos",
target_compatible_with = ["@platforms//os:macos"],
tags = tags + MOZC_TAGS.MAC_ONLY,
**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:
return arg
return None
def mozc_select(
default = [],
client = None,
oss = None,
android = None,
ios = None,
chromiumos = None,
linux = None,
macos = None,
oss_android = None,
oss_linux = None,
oss_macos = None,
oss_windows = None,
prod = None,
prod_linux = None,
prod_macos = None,
prod_windows = None,
wasm = None,
windows = None):
"""select wrapper for target os selection.
The priority of value checking:
android: android > client > default
ios,chromiumos,wasm,linux: same with android.
oss_linux: oss_linux > oss > linux > client > default
Args:
default: default fallback value.
client: default value for android, ios, chromiumos, wasm and oss_linux.
If client is not specified, default is used.
oss: default value for OSS build.
If oss or specific platform is not specified, client is used.
android: value for Android build.
ios: value for iOS build.
chromiumos: value for ChromeOS build.
linux: value for Linux build.
macos: value for macOS build.
oss_android: value for OSS Android build.
oss_linux: value for OSS Linux build.
oss_macos: value for OSS macOS build.
oss_windows: value for OSS Windows build.
prod: value for prod build.
prod_linux: value for prod Linux build.
prod_macos: value for prod macOS build.
prod_windows: value for prod Windows build.
wasm: value for wasm build.
windows: value for Windows build. (placeholder)
Returns:
Generated select statement.
"""
return select({
"//bazel/cc_target_os:android": _get_value([android, client, default]),
"//bazel/cc_target_os:apple": _get_value([ios, client, default]),
"//bazel/cc_target_os:chromiumos": _get_value([chromiumos, client, default]),
"//bazel/cc_target_os:darwin": _get_value([macos, ios, client, default]),
"//bazel/cc_target_os:wasm": _get_value([wasm, client, default]),
"//bazel/cc_target_os:windows": _get_value([windows, client, default]),
"//bazel/cc_target_os:linux": _get_value([linux, client, default]),
"//bazel/cc_target_os:oss_android": _get_value([oss_android, oss, android, client, default]),
"//bazel/cc_target_os:oss_linux": _get_value([oss_linux, oss, linux, client, default]),
"//bazel/cc_target_os:oss_macos": _get_value([oss_macos, oss, macos, ios, client, default]),
"//bazel/cc_target_os:oss_windows": _get_value([oss_windows, oss, windows, client, default]),
"//bazel/cc_target_os:prod_linux": _get_value([prod_linux, prod, oss_linux, oss, linux, client, default]),
"//bazel/cc_target_os:prod_macos": _get_value([prod_macos, prod, oss_macos, oss, macos, ios, client, default]),
"//bazel/cc_target_os:prod_windows": _get_value([prod_windows, prod, oss_windows, oss, windows, client, default]),
"//conditions:default": default,
})
# Macros for build config settings.
#
# These macros are syntax sugars for the Bazel select statement.
def mozc_select_enable_session_watchdog(on = [], off = []):
return select({
"//:enable_session_watchdog": on,
"//conditions:default": off,
})
def mozc_select_enable_supplemental_model(on = [], off = []):
return select({
"//:enable_spellchecker": on,
"//conditions:default": off,
})
def mozc_select_enable_usage_rewriter(on = [], off = []):
return select({
"//:enable_usage_rewriter": on,
"//conditions:default": off,
})