| 1 | #!/usr/bin/envscip-shell filesystem . . `/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 "$(dirnamescip-shell system . . dirname. "${BASH_SOURCE[0]}")/.." && pwdscip-shell system . . pwd.)" |
| 9 | cd "$ROOT_DIR" |
| 10 | |
| 11 | # Git Bash/MSYS rewrites Bazel labels such as //node:opencc as paths on Windows. |
| 12 | case "$(unamescip-shell system . . uname. -s 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.)" in |
| 13 | MINGW*|MSYS*|CYGWIN*) |
| 14 | export MSYS2_ARG_CONV_EXCL='*' |
| 15 | export MSYS_NO_PATHCONV=1 |
| 16 | ;; |
| 17 | esac |
| 18 | |
| 19 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 20 | catscip-shell system . . 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 | usagescip-shell shell shell-project 0.0.0 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 | echoscip-shell system . . echo. "Unsupported target: $target" >&2 |
| 43 | usagescip-shell shell shell-project 0.0.0 usage(). |
| 44 | exit 2 |
| 45 | ;; |
| 46 | esac |
| 47 | |
| 48 | host_target="$(nodescip-shell system . . node. -p "process.platform + '-' + process.arch")" |
| 49 | |
| 50 | if [[ "$target" == "win32-x64" && "$host_target" != "$target" ]]; then |
| 51 | echoscip-shell system . . 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 | echoscip-shell system . . 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 | catscip-shell system . . 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 | echoscip-shell system . . echo. "Preparing shared Node.js prebuild assets..." |
| 69 | nodescip-shell system . . node. scripts/prepare-node-prebuild-artifacts.js |
| 70 | |
| 71 | dest_dir="prebuilds/$target" |
| 72 | dest="$dest_dir/opencc.node" |
| 73 | mkdirscip-shell system . . mkdir. -p "$dest_dir" |
| 74 | cpscip-shell system . . cp. -fL "$addon" "$dest" |
| 75 | |
| 76 | echoscip-shell system . . echo. "Copied Bazel-built addon to $dest" |
| 77 | lsscip-shell system . . ls. -lh "$dest" |