| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Build opencc.node with Bazel and copy it to the npm prebuilds directory. |
| 4 | # Usage: ./scripts/build-node-prebuild-bazel.sh <target> |
| 5 | |
| 6 | set -euo pipefail |
| 7 | |
| 8 | ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 9 | cd "$ROOT_DIR" |
| 10 | |
| 11 | # Git Bash/MSYS rewrites Bazel labels such as //node:opencc as paths on Windows. |
| 12 | case "$(uname -s 2>/dev/null || true)" in |
| 13 | MINGW*|MSYS*|CYGWIN*) |
| 14 | export MSYS2_ARG_CONV_EXCL='*' |
| 15 | export MSYS_NO_PATHCONV=1 |
| 16 | ;; |
| 17 | esac |
| 18 | |
| 19 | usage() { |
| 20 | cat >&2 <<'EOF' |
| 21 | Usage: ./scripts/build-node-prebuild-bazel.sh <target> |
| 22 | |
| 23 | Supported targets: |
| 24 | darwin-arm64 |
| 25 | darwin-x64 |
| 26 | linux-arm64 |
| 27 | linux-x64 |
| 28 | win32-x64 |
| 29 | EOF |
| 30 | } |
| 31 | |
| 32 | if [[ $# -ne 1 ]]; then |
| 33 | usage |
| 34 | exit 2 |
| 35 | fi |
| 36 | |
| 37 | target="$1" |
| 38 | case "$target" in |
| 39 | darwin-arm64|darwin-x64|linux-arm64|linux-x64|win32-x64) |
| 40 | ;; |
| 41 | *) |
| 42 | echo "Unsupported target: $target" >&2 |
| 43 | usage |
| 44 | exit 2 |
| 45 | ;; |
| 46 | esac |
| 47 | |
| 48 | host_target="$(node -p "process.platform + '-' + process.arch")" |
| 49 | |
| 50 | if [[ "$target" == "win32-x64" && "$host_target" != "$target" ]]; then |
| 51 | echo "Building $target with Bazel + Zig cross-compile..." |
| 52 | bazel build -c opt //data/dictionary:binary_dictionaries |
| 53 | bazel build -c opt --config=node-windows-x64-zig //node:opencc_node_windows_zig |
| 54 | addon="bazel-bin/node/windows-x64/opencc.node" |
| 55 | elif [[ "$target" == "$host_target" ]]; then |
| 56 | echo "Building $target with native Bazel toolchain..." |
| 57 | bazel build -c opt //data/dictionary:binary_dictionaries //node:opencc |
| 58 | addon="bazel-bin/node/opencc.node" |
| 59 | else |
| 60 | cat >&2 <<EOF |
| 61 | Target $target does not match host $host_target. |
| 62 | Run this script on a matching runner, or use win32-x64 from macOS/Linux via the |
| 63 | existing Bazel + Zig cross-compile path. |
| 64 | EOF |
| 65 | exit 1 |
| 66 | fi |
| 67 | |
| 68 | echo "Preparing shared Node.js prebuild assets..." |
| 69 | node scripts/prepare-node-prebuild-artifacts.js |
| 70 | |
| 71 | dest_dir="prebuilds/$target" |
| 72 | dest="$dest_dir/opencc.node" |
| 73 | mkdir -p "$dest_dir" |
| 74 | cp -fL "$addon" "$dest" |
| 75 | |
| 76 | echo "Copied Bazel-built addon to $dest" |
| 77 | ls -lh "$dest" |