| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Cross-compile the OpenCC command-line executable for Windows x64 using Zig. |
| 4 | # |
| 5 | # This script is intentionally a direct compiler invocation instead of a full |
| 6 | # release build. It is useful as a fast smoke test for Windows-facing C++ code |
| 7 | # paths when developing on Unix-like hosts: |
| 8 | # |
| 9 | # - _WIN32 preprocessor branches are compiled. |
| 10 | # - MinGW-style Windows headers are parsed. |
| 11 | # - Windows import libraries such as shell32 are linked. |
| 12 | # - The CLI entry wrapper, UTF-8 argv handling, and UTF-8 file I/O boundary |
| 13 | # are checked without requiring a Windows machine. |
| 14 | # |
| 15 | # It does not run the generated executable. Use real Windows CI or a Windows |
| 16 | # VM for runtime validation of command-line parsing, pipes, and Unicode paths. |
| 17 | # |
| 18 | # Usage: |
| 19 | # ./scripts/build-cli-windows-zig.sh [output.exe] |
| 20 | # |
| 21 | # Environment: |
| 22 | # ZIG Zig executable to use. Defaults to "zig". |
| 23 | # ZIG_TARGET Zig target triple. Defaults to "x86_64-windows-gnu". |
| 24 | # ZIG_OPTIMIZE Optimization flag. Defaults to "-O2". |
| 25 | # OPENCC_VERSION OPENCC_VERSION macro value. Defaults to MODULE.bazel version. |
| 26 | # ZIG_LOCAL_CACHE_DIR Zig local cache. Defaults to "$ROOT/.zig-local-cache". |
| 27 | # ZIG_GLOBAL_CACHE_DIR Zig global cache. Defaults to "$ROOT/.zig-global-cache". |
| 28 | |
| 29 | set -euo pipefail |
| 30 | |
| 31 | ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 32 | cd "$ROOT_DIR" |
| 33 | |
| 34 | zig="${ZIG:-zig}" |
| 35 | target="${ZIG_TARGET:-x86_64-windows-gnu}" |
| 36 | optimize="${ZIG_OPTIMIZE:--O2}" |
| 37 | output="${1:-build/zig-windows/opencc.exe}" |
| 38 | |
| 39 | if [[ $# -gt 1 ]]; then |
| 40 | echo "Usage: $0 [output.exe]" >&2 |
| 41 | exit 2 |
| 42 | fi |
| 43 | |
| 44 | version="${OPENCC_VERSION:-}" |
| 45 | if [[ -z "$version" ]]; then |
| 46 | version="$( |
| 47 | sed -n 's/^[[:space:]]*version[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' \ |
| 48 | MODULE.bazel | head -n 1 |
| 49 | )" |
| 50 | fi |
| 51 | version="${version:-1.4.1}" |
| 52 | |
| 53 | export ZIG_LOCAL_CACHE_DIR="${ZIG_LOCAL_CACHE_DIR:-$ROOT_DIR/.zig-local-cache}" |
| 54 | export ZIG_GLOBAL_CACHE_DIR="${ZIG_GLOBAL_CACHE_DIR:-$ROOT_DIR/.zig-global-cache}" |
| 55 | mkdir -p "$ZIG_LOCAL_CACHE_DIR" "$ZIG_GLOBAL_CACHE_DIR" "$(dirname "$output")" |
| 56 | |
| 57 | zig_log="${output}.zig.log" |
| 58 | |
| 59 | # Keep this source list explicit. The goal is to catch Windows-specific |
| 60 | # compile/link problems in the CLI and core library, not to hide missing source |
| 61 | # additions behind a separate build generator. |
| 62 | sources=( |
| 63 | src/tools/CommandLine.cpp |
| 64 | src/tools/CommandLineMain.cpp |
| 65 | src/tools/PlatformIO.cpp |
| 66 | src/BinaryDict.cpp |
| 67 | src/Config.cpp |
| 68 | src/Conversion.cpp |
| 69 | src/ConversionChain.cpp |
| 70 | src/Converter.cpp |
| 71 | src/DartsDict.cpp |
| 72 | src/Dict.cpp |
| 73 | src/DictConverter.cpp |
| 74 | src/DictEntry.cpp |
| 75 | src/DictGroup.cpp |
| 76 | src/Lexicon.cpp |
| 77 | src/MarisaDict.cpp |
| 78 | src/MaxMatchSegmentation.cpp |
| 79 | src/PhraseExtract.cpp |
| 80 | src/PipelineConverter.cpp |
| 81 | src/PluginSegmentation.cpp |
| 82 | src/SingleStageConverter.cpp |
| 83 | src/PrefixMatch.cpp |
| 84 | src/ResourceProvider.cpp |
| 85 | src/Segmentation.cpp |
| 86 | src/SerializableDict.cpp |
| 87 | src/SerializedValues.cpp |
| 88 | src/SimpleConverter.cpp |
| 89 | src/TextDict.cpp |
| 90 | src/UTF8StringSlice.cpp |
| 91 | src/UTF8Util.cpp |
| 92 | deps/marisa-0.3.1/lib/marisa/agent.cc |
| 93 | deps/marisa-0.3.1/lib/marisa/grimoire/io/mapper.cc |
| 94 | deps/marisa-0.3.1/lib/marisa/grimoire/io/reader.cc |
| 95 | deps/marisa-0.3.1/lib/marisa/grimoire/io/writer.cc |
| 96 | deps/marisa-0.3.1/lib/marisa/grimoire/trie/louds-trie.cc |
| 97 | deps/marisa-0.3.1/lib/marisa/grimoire/trie/tail.cc |
| 98 | deps/marisa-0.3.1/lib/marisa/grimoire/vector/bit-vector.cc |
| 99 | deps/marisa-0.3.1/lib/marisa/keyset.cc |
| 100 | deps/marisa-0.3.1/lib/marisa/trie.cc |
| 101 | ) |
| 102 | |
| 103 | set +e |
| 104 | "$zig" c++ \ |
| 105 | -target "$target" \ |
| 106 | "$optimize" \ |
| 107 | -std=c++17 \ |
| 108 | -w \ |
| 109 | -Wno-nullability-completeness \ |
| 110 | -DNDEBUG \ |
| 111 | "-DOPENCC_VERSION=\"$version\"" \ |
| 112 | -DOpencc_BUILT_AS_STATIC \ |
| 113 | -I. \ |
| 114 | -Isrc \ |
| 115 | -Ideps/rapidjson-1.1.0 \ |
| 116 | -Ideps/marisa-0.3.1/include \ |
| 117 | -Ideps/marisa-0.3.1/lib \ |
| 118 | -Ideps/tclap-1.2.5 \ |
| 119 | -Ideps/darts-clone-0.32h/include \ |
| 120 | "${sources[@]}" \ |
| 121 | -lshell32 \ |
| 122 | -Wl,--strip-all \ |
| 123 | -Wl,--build-id=sha1 \ |
| 124 | -o "$output" \ |
| 125 | 2>"$zig_log" |
| 126 | zig_status=$? |
| 127 | set -e |
| 128 | |
| 129 | if [[ $zig_status -ne 0 ]]; then |
| 130 | cat "$zig_log" >&2 |
| 131 | exit "$zig_status" |
| 132 | fi |
| 133 | |
| 134 | if [[ -s "$zig_log" ]]; then |
| 135 | echo "Zig completed with warnings; see $zig_log" >&2 |
| 136 | else |
| 137 | rm -f "$zig_log" |
| 138 | fi |
| 139 | |
| 140 | ls -lh "$output" |