| 1 |
"""Bazel build rules for the OpenCC Node.js native addon (opencc.node).""" |
| 2 |
|
| 3 |
load("@aspect_rules_js//js:defs.bzl", "js_library") |
| 4 |
load("@rules_node_addon//node_addon:defs.bzl", "node_addon") |
| 5 |
load(":cc_headers_files.bzl", "cc_header_pick", "cc_headers_files") |
| 6 |
|
| 7 |
package(default_visibility = ["//visibility:public"]) |
| 8 |
|
| 9 |
# JavaScript API sources exercised by the Node.js js_test (//:node_test). |
| 10 |
js_library( |
| 11 |
name = "js_srcs", |
| 12 |
srcs = [ |
| 13 |
"cli.js", |
| 14 |
"opencc.js", |
| 15 |
"test.js", |
| 16 |
], |
| 17 |
) |
| 18 |
|
| 19 |
# Raw Node header files for the Windows-Zig cross-compile ONLY (see the genrule |
| 20 |
# below). The main native-addon path (//node:opencc) gets its Node headers |
| 21 |
# through node_addon and does NOT use these targets. rules_nodejs only exposes |
| 22 |
# the toolchain headers as a cc_library (CcInfo), so we unwrap them into raw |
| 23 |
# files that `zig c++` can consume via $(location) / -I. |
| 24 |
cc_headers_files( |
| 25 |
name = "windows_zig_node_header_files", |
| 26 |
lib = "@rules_nodejs//nodejs/headers:current_node_cc_headers", |
| 27 |
) |
| 28 |
|
| 29 |
cc_header_pick( |
| 30 |
name = "windows_zig_node_api_h", |
| 31 |
basename = "node_api.h", |
| 32 |
lib = "@rules_nodejs//nodejs/headers:current_node_cc_headers", |
| 33 |
) |
| 34 |
|
| 35 |
# The native Node addon (opencc.node). node_addon wraps a linkshared cc_binary |
| 36 |
# and renames the output to "opencc.node", and supplies node-addon-api plus the |
| 37 |
# Node C headers itself (the rules_nodejs toolchain on Unix; the windows_node_api |
| 38 |
# import library on Windows). |
| 39 |
# |
| 40 |
# node-gyp and Bazel both compile the same binding source. node-gyp lists the |
| 41 |
# OpenCC and marisa sources directly, while Bazel links against //src:opencc_lib. |
| 42 |
node_addon( |
| 43 |
name = "opencc", |
| 44 |
srcs = ["opencc.cc"], |
| 45 |
copts = select({ |
| 46 |
"@platforms//os:windows": ["/std:c++20"], |
| 47 |
"//conditions:default": ["-std=c++20"], |
| 48 |
}), |
| 49 |
defines = [ |
| 50 |
'OPENCC_VERSION=\\"1.4.1\\"', |
| 51 |
"NAPI_DISABLE_CPP_EXCEPTIONS", |
| 52 |
"Opencc_BUILT_AS_STATIC", |
| 53 |
], |
| 54 |
includes = [".", "src"], |
| 55 |
# node_addon already appends `-undefined dynamic_lookup` on macOS. |
| 56 |
linkopts = select({ |
| 57 |
# LC_UUID must be kept: modern macOS dyld rejects dlopen of a bundle |
| 58 |
# without it (ERR_DLOPEN_FAILED "missing LC_UUID load command"). |
| 59 |
"@platforms//os:macos": ["-Wl,-S"], |
| 60 |
"@platforms//os:linux": ["-Wl,-s", "-Wl,--build-id=sha1"], |
| 61 |
"//conditions:default": ["-Wl,-s"], |
| 62 |
}), |
| 63 |
deps = [ |
| 64 |
"//src:opencc_lib", |
| 65 |
# Explicit sub-library deps for headers not in //src:opencc_lib's hdrs: |
| 66 |
"//src:config_lib", # Config.hpp |
| 67 |
"//src:converter_lib", # Converter.hpp, ConversionInspection.hpp |
| 68 |
"//src:dict_converter_lib", # DictConverter.hpp |
| 69 |
"//src:exception_lib", # Exception.hpp |
| 70 |
], |
| 71 |
) |
| 72 |
|
| 73 |
genrule( |
| 74 |
name = "opencc_node_windows_zig", |
| 75 |
srcs = [ |
| 76 |
"opencc.cc", |
| 77 |
"//deps/darts-clone-0.32h:all_files", |
| 78 |
"//deps/marisa-0.3.1:all_files", |
| 79 |
"//deps/rapidjson-1.1.0:all_files", |
| 80 |
"//src:all_files", |
| 81 |
# node-addon-api headers, exposed as raw files via exports_files in the |
| 82 |
# BCR node_addon_api module's BUILD overlay. |
| 83 |
"@node_addon_api//:napi-inl.deprecated.h", |
| 84 |
"@node_addon_api//:napi-inl.h", |
| 85 |
"@node_addon_api//:napi.h", |
| 86 |
# Node C headers for cross-compiling only; the host Node toolchain's |
| 87 |
# headers (node_api.h is platform-agnostic) feed the zig build. |
| 88 |
":windows_zig_node_header_files", |
| 89 |
], |
| 90 |
outs = ["windows-x64/opencc.node"], |
| 91 |
cmd = ( |
| 92 |
"ZIG=$(ZIG_BIN) " + |
| 93 |
"bash $(location //scripts:build_node_windows_zig) " + |
| 94 |
"$@ " + |
| 95 |
"$(location :windows_zig_node_api_h) " + |
| 96 |
"$(location @node_addon_api//:napi.h) " + |
| 97 |
"$(location @node_win_x64_lib//file)" |
| 98 |
), |
| 99 |
tools = [ |
| 100 |
"//scripts:build_node_windows_zig", |
| 101 |
"@node_addon_api//:napi-inl.deprecated.h", |
| 102 |
"@node_addon_api//:napi-inl.h", |
| 103 |
"@node_addon_api//:napi.h", |
| 104 |
":windows_zig_node_api_h", |
| 105 |
":windows_zig_node_header_files", |
| 106 |
"@node_win_x64_lib//file", |
| 107 |
], |
| 108 |
toolchains = ["@rules_zig//zig:toolchain_type"], |
| 109 |
) |