#!/usr/bin/env bash # Compiles modding/hacked/ for Linux and Windows (amd64), entirely offline - # every Go module dependency was already fetched into the build-image at # image-build time (see Dockerfile). Baked into the image at # /usr/local/bin/build-hacked.sh (see Dockerfile's CMD) - run via # ../hacked-build-run-image.sh, which bind-mounts modding/ at /workspace, not # directly. # # Does not follow hacked/build.sh (its own upstream build script): that script # assumes hacked/ is its own git checkout (it derives its version from `git # describe --tags`), which isn't true here - hacked/ is a vendored snapshot, # not a git submodule, so `git describe` there would silently pick up # *questshock's* tags instead. Version is a plain build-date string instead. set -euo pipefail MODDING_ROOT="$(pwd)" HACKED_SRC="$MODDING_ROOT/hacked" OUT_DIR="$MODDING_ROOT/dist" VERSION="hacked-$(date -u +%Y%m%d)" echo "Version: $VERSION" rm -rf "$OUT_DIR" mkdir -p "$OUT_DIR/linux/hacked" "$OUT_DIR/win/hacked" cd "$HACKED_SRC" echo "== Building Linux amd64 ==" go build -gcflags "-d=checkptr=0" -ldflags "-X main.version=$VERSION" \ -trimpath -o "$OUT_DIR/linux/hacked/hacked" . echo "== Preparing the Windows icon/version-info resource ==" # cmd/link automatically links in a *.syso file sitting in the main package # directory during a Windows build - no explicit flag needed, but it does mean # generating it (via windres, from hacked.rc/.ico/.exe.manifest) has to happen # right here, before go build runs, and be cleaned up after regardless of # outcome so it never leaks into a Linux build run from the same checkout. WIN_RES_DIR="$(mktemp -d)" trap 'rm -rf "$WIN_RES_DIR"; rm -f "$HACKED_SRC/main-res.syso"' EXIT cp "$HACKED_SRC"/_resources/build/win/* "$WIN_RES_DIR/" # No git tag to derive a real version from (hacked/ is a vendored snapshot, # not its own checkout) - same date-based fallback hacked/build.sh itself # uses when `git describe` fails. sed -i "s/§MAJOR/$(date -u +%Y)/;s/§MINOR/$(date -u +%m)/;s/§PATCH/$(date -u +%d)/" \ "$WIN_RES_DIR/hacked.exe.manifest" x86_64-w64-mingw32-windres -o "$HACKED_SRC/main-res.syso" "$WIN_RES_DIR/hacked.rc" echo "== Building Windows amd64 (cross-compiled via mingw-w64) ==" # -extldflags -static-libgcc/-static-libstdc++: statically links the mingw # C/C++ runtime into hacked.exe, the same technique the main project's own # Windows cross-build uses (see ../../build-image/Dockerfile) - avoids having # to locate and ship libgcc_s_seh-1.dll/libstdc++-6.dll (their exact path # depends on the mingw-w64 package's gcc version). libwinpthread-1.dll still # needs shipping alongside the .exe; it isn't part of that static link. GOOS=windows GOARCH=amd64 CGO_ENABLED=1 \ CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ \ go build -gcflags "-d=checkptr=0" \ -ldflags "-X main.version=$VERSION -H=windowsgui -extldflags '-static-libgcc -static-libstdc++'" \ -trimpath -o "$OUT_DIR/win/hacked/hacked.exe" . cp /usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll "$OUT_DIR/win/hacked/" echo "== Copying distribution resources ==" for os in linux win; do packageDir="$OUT_DIR/$os/hacked" cp "$HACKED_SRC/LICENSE" "$packageDir/" cp -R "$HACKED_SRC/_resources/dist/." "$packageDir/" done echo "== Packaging ==" # $VERSION (hacked-) is already a complete, self-describing name - no # separate package-name prefix needed on top of it. (cd "$OUT_DIR/linux" && tar -czf "$OUT_DIR/$VERSION.linux64.tgz" hacked) (cd "$OUT_DIR/win" && zip -rq "$OUT_DIR/$VERSION.win64.zip" hacked) echo "== Done ==" echo "Linux: $OUT_DIR/$VERSION.linux64.tgz" echo "Windows: $OUT_DIR/$VERSION.win64.zip"