docs(pipeline): reproducible end-to-end build+verify guide

This commit is contained in:
ulw-execute committed 2026-11-28 06:14:09 -05:00
1 parent 9c4e9bc507
commit 74902f528b
1 file changed
+242
+242
View File
@@ -0,0 +1,242 @@
# Build and verification pipeline
Zero-to-built, zero-to-verified guide for `gamekdm.dll` on a standard Linux
machine. It takes a clean checkout to a rebuilt 32-bit Windows DLL plus a full
pass of the verification harness, with no `sudo` and no system package install.
Everything here was dry-run on a clean clone before it was written; the
recorded transcript is in `.omo/evidence/gamekdm-decomp-linux/task-18.log`.
## Who this is for
Someone who is not the author, on their own Linux box, who wants to reproduce
the build and confirm the reconstruction matches the shipping binary. You do
not need root. You do need the few host prerequisites below.
## 0. Host prerequisites
These are assumed already present on the machine (they are not part of the
no-sudo toolchain and are not installed by this guide):
- `git`, `bash`, `cmake` (>= 3.16), and a C/C++ host compiler.
- User-space Nix with flakes, reachable at `~/.nix-profile`. If Nix is not yet
installed, use the single-user (rootless) installer; it needs no root after
setup. See <https://nixos.org/download> (single-user install).
- `uv` (the Python environment manager) on `PATH`. Install no-sudo with
`curl -LsSf https://astral.sh/uv/install.sh | sh`, which drops it in
`~/.local/bin`. `uv` is used instead of `python3 -m venv` because it bundles
its own pip and its own pinned CPython, so it works even when the system
Python has no `ensurepip`.
- The shipping original `gamekdm.dll`, readable at `/home/athapa/ghidra/gamekdm.dll`.
The Unicorn differential (step 5) emulates this binary. If your copy lives
elsewhere, edit the `DLL = Path(...)` line near the top of
`tools/unicorn_diff/prove_t11.py`.
- The legally-possessed game content folder, for the companion DLLs that step 2
reads. The default is
`/mnt/f/Users/Aaron Thapa/Downloads/KDM-2016021900/contents`. Pass your own
path as the first argument to `tools/gen_importlibs.sh` if it differs.
All toolchain versions are pinned in `toolchain.lock` (nixpkgs rev
`619c39f6eac5d1ed6b8f073a9414baf7034b2356`, GCC 16.2.0 / mingw-w64 14.0.0,
wine 11.0).
## 1. Get a clean checkout
```bash
git clone <this-repo> kdm-re
cd kdm-re
```
## 2. Install the pinned no-sudo toolchain (T1)
Source the user-space Nix profile, then install the exact pinned set. All of it
lands in `~/.nix-profile`; nothing touches the system.
```bash
source ~/.nix-profile/etc/profile.d/nix.sh
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.buildPackages.binutils
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install --priority 11 \
github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.buildPackages.gcc
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#gendef
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.windows.mcfgthreads
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#wine
```
`gcc` installs last with `--priority 11` on purpose: it and binutils both ship a
conflicting `i686-w64-mingw32-nm` symlink, so binutils' copy (default priority
10) must win while gcc's unique files still link in.
Now put the cross toolchain on `PATH`. Source this once per shell:
```bash
source ./activate.sh
```
`activate.sh` sources the Nix profile, prepends `~/.nix-profile/bin` and a
generated worktree-local `bin/` to `PATH`, and creates the bare `dlltool`,
`gendef`, and `objdump` aliases that point at their target-prefixed binaries.
On success it is silent and returns 0. If a tool is missing it prints exact
recovery steps and returns non-zero, so a broken toolchain never looks like a
clean activation. Verify:
```bash
command -v i686-w64-mingw32-g++ gendef dlltool wine
```
## 3. Generate companion import libraries (T3)
The reconstructed slice links against MinGW import libs derived from the game's
own companion DLLs. Regenerate them (the committed tree ships the `.def` files
but not the `.dll.a` archives, which are gitignored):
```bash
tools/gen_importlibs.sh
# or, if your game content lives elsewhere:
# tools/gen_importlibs.sh "/path/to/KDM-.../contents"
```
This runs `gendef` then `i686-w64-mingw32-dlltool` for each in-scope companion
and writes `re/importlibs/lib<name>.dll.a`. The stdcall companions (`arkkdm`,
`Kinect10`) get `--kill-at`; see `docs/companion-conventions.md` for why.
## 4. Build the proxy and reconstructed slice (T4 + T11)
Configure against the pinned cross toolchain file, then build:
```bash
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw-i686.cmake
cmake --build build
```
Output is `build/gamekdm.dll`, a PE32 (i386) DLL. The toolchain file links
`-static -static-libgcc -static-libstdc++` so the rebuild imports zero MinGW
runtime DLLs, matching the original's import shape. Confirm the export table is
exactly `gameMain` at ordinal 1:
```bash
i686-w64-mingw32-objdump -p build/gamekdm.dll | grep -A6 'Export Address Table'
```
## 5. Run the verification harness
### 5a. Unicorn differential (T8 + T11)
This is the universal gate: for each reconstructed function it emulates the real
routine inside the shipping `gamekdm.dll` (Unicorn) and runs the compiled C++
reconstruction under wine on the same inputs, then asserts behavioural equality.
It also injects a known bug and confirms the differential catches it
(non-vacuity).
Create the Python environment with `uv` and install the pinned deps:
```bash
uv venv tools/unicorn_diff/.venv
VIRTUAL_ENV=tools/unicorn_diff/.venv uv pip install -r tools/unicorn_diff/requirements.txt
```
Build the reconstruction harness into a 32-bit wine executable. (Keep these
flags exact: `-mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math` reproduce
the original's float semantics.)
```bash
i686-w64-mingw32-g++ -std=c++20 -O2 -mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math \
-Iinclude -Isrc -Isrc/game \
tools/unicorn_diff/recon_harness.cpp \
src/game/box_distance.cpp src/game/validity.cpp src/game/nui_elevation.cpp \
-L/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib \
-o tools/unicorn_diff/recon.exe -static -static-libgcc -static-libstdc++
```
Run the proof (expect `ALL DIFFERENTIALS PASS`, exit 0):
```bash
tools/unicorn_diff/.venv/bin/python tools/unicorn_diff/prove_t11.py
```
### 5b. Golden I/O harness (T9)
A standalone, stdlib-only supplement that replays committed input/output tuples
against an independent reconstruction and proves the assertions can fail
(perturbation guard). No game binary, no network, no wine.
```bash
tests/golden/run.sh
```
Expect `GOLDEN HARNESS OK: clean run passed and every perturbation failed.`,
exit 0.
### 5c. Wine load-smoke
Confirm the rebuilt DLL actually loads under wine and still exports `gameMain`.
Build a tiny loader, drop it beside the rebuilt DLL, and run it:
```bash
MCFG=/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib
SM=$(mktemp -d)
cat > "$SM/loadsmoke.c" <<'EOF'
#include <windows.h>
#include <stdio.h>
int main(void){
HMODULE h = LoadLibraryA("gamekdm.dll");
if(!h){ printf("LOAD_SMOKE: LoadLibraryA failed err=%lu\n",(unsigned long)GetLastError()); return 1; }
FARPROC p = GetProcAddress(h, "gameMain");
if(!p) p = GetProcAddress(h, (LPCSTR)1);
printf("LOAD_SMOKE: loaded gamekdm.dll at %p, gameMain=%p\n",(void*)h,(void*)p);
return p ? 0 : 2;
}
EOF
i686-w64-mingw32-gcc "$SM/loadsmoke.c" -L"$MCFG" -o "$SM/loadsmoke.exe"
cp build/gamekdm.dll "$SM/gamekdm.dll"
( cd "$SM" && WINEDEBUG=-all wine loadsmoke.exe )
rm -rf "$SM"
```
Expect a `LOAD_SMOKE: loaded gamekdm.dll at ...` line with a non-null
`gameMain`, exit 0.
The Phase-0 `gameMain` is a forwarding proxy: it only resolves the original
`gamekdm_orig.dll` when actually called, so loading the rebuilt DLL on its own
is a valid smoke test and needs no original present.
## 6. Deploy to the Windows / spice2x rig (optional, deferred)
In-game QA on the real spice2x rig is a later integration milestone and is not
part of the reproducible Linux build above. When it lands: rename
`build/gamekdm.dll` to the launcher's expected name, keep the original beside it
as `gamekdm_orig.dll` for the proxy to forward into, and launch through the
game's normal `gamestart.bat` / `launcher.exe` path. Treat this as deferred; the
steps 2 through 5 chain is the complete, self-contained verification.
## One-shot reference
With the toolchain already installed (step 2), a full rebuild-and-verify from a
clean checkout is:
```bash
source ~/.nix-profile/etc/profile.d/nix.sh
source ./activate.sh
tools/gen_importlibs.sh
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw-i686.cmake
cmake --build build
uv venv tools/unicorn_diff/.venv
VIRTUAL_ENV=tools/unicorn_diff/.venv uv pip install -r tools/unicorn_diff/requirements.txt
i686-w64-mingw32-g++ -std=c++20 -O2 -mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math \
-Iinclude -Isrc -Isrc/game \
tools/unicorn_diff/recon_harness.cpp \
src/game/box_distance.cpp src/game/validity.cpp src/game/nui_elevation.cpp \
-L/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib \
-o tools/unicorn_diff/recon.exe -static -static-libgcc -static-libstdc++
tools/unicorn_diff/.venv/bin/python tools/unicorn_diff/prove_t11.py
tests/golden/run.sh
```