upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/linux.md
blob: 6542e07c607197e153f3b065c91aea12783303f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Linux

What a Linux user (x86-64, a Vulkan GPU) can do today, from one page: the build of the shaders and assets runs
natively, the Vulkan test tools build natively with GCC or Clang, and the loader (`gta6rt`) is a Windows program that
runs under Wine or Proton. Nothing is playable on any platform yet (README, *Project status*).

The package names below were checked on Ubuntu 26.04 LTS. Other distributions have the same tools under their own
names; the build lists anything it can't find before it starts.

## What runs where

| Piece | On Linux | How it is checked |
|---|---|---|
| `make_game.py`: check, shaders, assets, pipelines | Native | CI (`linux-python-and-viewer`, `semantic-tests`) |
| `Build.sh`, `Mods.sh` | Native (`/bin/sh`) | `tests/test_shell_launchers.py`, in the standard-library loop of `linux-python-and-viewer` |
| Shader translator, its tests and the GPU test tools (`vk_harness`, `vk_pipeline_check`, ...) | Native, GCC or Clang | CI (`linux-gpu-tools`: GCC, tests on llvmpipe); Clang builds them too, but no CI job uses it yet |
| `rpf9dump` (asset export) | Native, .NET 8 or newer | CI builds it on Windows only (the `check` job) |
| `gta6rt`, its shims and their tests | Windows program, under Wine or Proton | Cross-built with clang-cl and xwin; CI runs the Windows build under Wine on pushes to `main` |
| `scripts/run_game_wine.sh`: one retail run under Wine, headless or visible, counts only ([linux_proton.md](linux_proton.md#running-the-game-under-wine-with-one-command)) | Native (`bash`) around the Windows `gta6rt` | `tests/test_run_game_wine.py` and `tests/test_game_run_summary.py`, without the game |

## Build the port

`scripts/install_deps_linux.sh` installs all of the prerequisites below with apt (Debian, Ubuntu and derivatives
such as Parrot or Kali), dnf (Fedora) or pacman (Arch). It shows the commands and asks before running them
(`--dry-run` only shows them, `--minimal` leaves out .NET, ffmpeg, zenity and Tk), then checks what the build needs.
On apt it takes the newest `llvm-21` or later the system's own sources have: `llvm-22` on Debian 12 and 13 and their
derivatives, `llvm-21` on Ubuntu 22.04 and 24.04, so apt.llvm.org isn't needed there. The check after the install
reports a Python older than 3.12 (Debian 12 has 3.11, Ubuntu 22.04 3.10).

Or install them by hand (Ubuntu 26.04; the comment says what each is for):

```sh
sudo apt install git python3 \
  llvm-21 spirv-tools \
  dotnet-sdk-10.0 \
  cmake ninja-build g++ libvulkan-dev \
  ffmpeg zenity python3-tk
#   git, python3          the clone and make_game.py (Python 3.12 or newer)
#   llvm-21, spirv-tools  the shaders stage: llvm-mc with the AMDGPU target, spirv-as and spirv-val
#   dotnet-sdk-10.0       the assets stage builds rpf9dump; Ubuntu 26.04 packages no .NET 8 SDK, and 10 builds the
#                         net8.0 project through global.json's roll-forward
#   cmake ... libvulkan-dev  the pipelines stage builds vk_pipeline_check
#   ffmpeg                optional: XMA2 audio and --wav
#   zenity, python3-tk    optional: the --pick folder dialog and the mod manager window
```

Then, with your own copy of the game already readable on disk:

```sh
git clone --recursive https://github.com/ProjectLeonida/mandy
cd mandy
./Build.sh --find        # list the copies found: the home folder, Downloads, Documents, /mnt, /media/<user>, ...
./Build.sh --check       # check your copy, build nothing
./Build.sh               # build into ~/Documents/Leonida
```

`./Build.sh` is `python3 -I make_game.py` with the newest Python 3.12+ it finds, so every `make_game.py` option in
the README works with it (`--game`, `--out`, `--work`, `--stages`, ...). Without the cross-build tools below, the
default stages leave `runtime` out (`check,shaders,assets,pipelines`).

Notes:

- **llvm-mc**: the `shaders` stage needs LLVM 23 or newer, so install `llvm-23` where the distribution has it. Debian and Ubuntu's `llvm-21` installs `llvm-mc` only as `llvm-mc-21` and `/usr/lib/llvm-21/bin/llvm-mc`.
  With `LLVM_MC` unset the tools use the plain `llvm-mc` when it is on `PATH` and otherwise the newest of those
  versioned names (`tools/mc_command.py`). Set `LLVM_MC` to choose another one. The `shaders` stage needs LLVM 23+
  for its pipeline library: 21.1.8's listings of 840 of its 2,684 compute programs carry 79,490 undecodable words and
  the translator refuses them, while 23.1.5 leaves none in those 840 and the stage translates 2,683 of 2,684
  ([design.md](design.md#disassembling-shaders)).
- **File names are case-sensitive.** The build opens the package's files by their exact names
  (`MicrosoftGame.Config`, `Catalog000.bin`, ...). A copy whose names changed case on the way is reported with the
  name it found and the one it needs; nothing is renamed for you.
- **Mods**: `./Mods.sh` opens the mod manager window ([modding.md](modding.md)); `python3 -I tools/leonida_mods.py`
  is the same manager on the command line.

## The runtime, under Wine or Proton

`gta6rt` is a Windows program. To build it on Linux, add the LLVM cross tools and an MSVC CRT and Windows SDK laid
out by [xwin](https://github.com/Jake-Shadle/xwin) (running xwin downloads them from Microsoft and means accepting
Microsoft's licence):

```sh
sudo apt install clang-21 lld-21 llvm-21 wine
xwin --accept-license --sdk-version 10.0.26100 splat --use-winsysroot-style --preserve-ms-arch-notation \
     --output ~/.local/share/xwin-sdk
./Build.sh --stages runtime
```

On Ubuntu 24.04, `clang-cl-<N>` is in `clang-tools-<N>` instead ([testing.md](testing.md#known-problems)).
`make_game.py` finds the five tools under one version suffix (`clang-cl-21`, `lld-link-21`, `llvm-lib-21`,
`llvm-rc-21`, `llvm-mt-21`) or their plain names. Without the SDK it lists what is missing before any work starts.
`--runtime-build <folder>` takes a Windows build made elsewhere instead, such as the `runtime-windows` artifact of the
Runtime workflow. The stage writes `<out>/gta6rt.sh`, which starts `gta6rt.exe` with `wine` or the Wine named by `WINE`
(for Proton, its `files/bin/wine`); README, *Running the loader on Linux*, has the commands.
[testing.md](testing.md#the-windows-runtime-on-linux-under-wine-or-proton) has the test runs under Wine and Proton
and what fails there.

On Apple Silicon, set `LEONIDA_CX_BOTTLE` to the name of an existing CrossOver bottle to use the same launcher:

```sh
LEONIDA_CX_BOTTLE=Leonida ./gta6rt.sh --dry-run
```

Run this from the packaged output directory. The launcher uses CrossOver in `/Applications` by default. Set `WINE`
to the CrossOver `bin/wine` path if the app is installed elsewhere. It enables Rosetta's AVX advertisement by default
for this mode and honours an explicit `ROSETTA_ADVERTISE_AVX` value.

For XMA2 audio, `runtime/CMakeLists.txt` builds `runtime/xma_decoder` for `x86_64-pc-windows-msvc` when `cargo` is on
`PATH`. Distribution Rust packages typically omit the Windows MSVC standard library, which leaves XMA voices silent
unless you add the `x86_64-pc-windows-msvc` Rust target or pass a prebuilt static library with
`-DLEONIDA_XMA2_DECODER_LIB=<path>`, built yourself from this checkout's `runtime/xma_decoder` with the command in
[runtime/xma_decoder/README.md](../runtime/xma_decoder/README.md).

### Which Wine

Both runtimes keep the title's graphics memory write-watched (`MEM_WRITE_WATCH`;
`d3d12/crates/leonida-hle/src/modules/xmem.rs`) and ask Windows each frame which pages the title wrote. Wine has two
ways to answer:

- **In the kernel**: userfaultfd's asynchronous write protection and the `PAGEMAP_SCAN` ioctl, from Linux 6.7. The
  kernel marks a page written without leaving it. Upstream Wine has it from the 10.7 development release
  (`dlls/ntdll/unix/virtual.c`; not in 10.6 or the stable 10.0), and Proton's Wine has it too. When the kernel
  lacks it, Wine falls back to signals by itself.
  Linux 6.7 is needed but not always enough: a kernel built without userfaultfd write protection, or a sandbox that
  blocks `userfaultfd` (such as Docker's default seccomp profile), still gets signals.
- **With signals**: Wine 9.0 and 10.0 (up to 10.6), or any Wine on a kernel older than 6.7. Every watched page is
  made read-only at each reset, and the first write to it after that is a `SIGSEGV` that Wine handles before the
  title's code goes on.

Measured with `scripts/write_watch_bench.c` (it builds a small Windows program; the command is in its header) and a
native Linux program that does the same through userfaultfd (not kept in the repository; it was not measured under
Wine 10.7+ or Proton itself). Both ran on a 4-core cloud VM, Linux 6.18, over 256 MiB and 1 GiB of our own memory,
in three runs:

| Per page | Wine 9.0 (Ubuntu 24.04), signals | userfaultfd and `PAGEMAP_SCAN` natively (Wine 10.7+, Proton) |
|---|---|---|
| first write after a reset | 2.3-3.8 us | 0.7-0.85 us |
| a later write | 0.02-0.03 us | 0.02-0.04 us |
| a read-only page opened by an exception handler (the runtime's guards) | 5-10 us | - |

A frame that first writes some thousands of pages pays the difference on the title's threads. How many it writes
was not measured under Wine. Bare metal is expected to handle faults faster than this VM (not measured), which would
lower both columns.

`scripts/play-linux.sh` says so in one line when the `ntdll.so` beside its `wine` has no `PAGEMAP_SCAN` (or, with no
`ntdll.so` found, when `wine --version` is older than 10.7), or when the kernel is older than 6.7. It still starts
the game. `LEONIDA_WINE_CHECK=0` leaves the line out. Ubuntu 24.04 ships Wine 9.0 and Debian 13 ships 10.0. There,
put Proton's `files/bin` or a Wine 11 build first on `PATH`.

### Why there is no native Linux `gta6rt`

A native (ELF) loader is not a matter of build flags:

- The loader shims only the console's own modules and a few Win32 entry points it has to own (GDK, XMem,
  `d3d12_xs`, the library loader, the process and file-system shims). Every other import binds to the host's Windows
  DLLs unchanged (`runtime/src/pe_loader.cpp`; [runtime_imports.md](runtime_imports.md): `XboxBvhBuilder.dll`
  alone binds 4 imports to shims and 53 to the host). On Linux, those host DLLs are what Wine provides.
- The mapped code uses the Windows x64 calling convention and the Windows thread and exception model: the loader
  registers unwind tables with `RtlAddFunctionTable`, keeps a module TLS slot through `gta6rt_tls_host.dll`, and
  `main.cpp` installs a vectored exception handler. The shims are written against `windows.h`.

A Linux-native host would have to reimplement each of those Win32 APIs, which is the work Wine already does, so the
supported way is the Windows build under Wine or Proton with the D3D12 layer on native Vulkan. What does build
natively is everything that doesn't touch Win32: the Vulkan tools and the portable runtime pieces with their tests
(`ctest` on a Linux build runs them, including the root-signature model and the host CPU check).

## Build and test without the game

```sh
cmake -S runtime -B runtime/build/linux -G Ninja                 # CC=clang CXX=clang++ for Clang
cmake --build runtime/build/linux --parallel
ctest --test-dir runtime/build/linux --output-on-failure         # Vulkan tests run on Mesa's lavapipe without a GPU
python3 -I scripts/run_semantic_tests.py tests/semantic          # translator suite
python3 -I tests/test_<name>.py                                  # one standard-library test
```

[testing.md](testing.md) covers the GPU tests (`scripts/run_gpu_tests.py`), running them on llvmpipe, and choosing
the GPU.

### Without root (micromamba)

If you cannot install distribution packages (no `sudo`), you can install `llvm-tools` and `spirv-tools` into a
user-owned folder with [micromamba](https://mamba.readthedocs.io/en/latest/installation/micromamba-installation.html)
(x86-64 glibc; for ARM64 use `linux-aarch64` from the micromamba guide; musl/Alpine is not natively supported) and let
CMake fetch its pinned, hash-checked Vulkan headers if the system does not provide them:

```bash
LEO=$HOME/leonida-tools
mkdir -p "$LEO/bin" "$LEO/mamba"
# Note: `latest` is unpinned and downloaded without signature verification; inspect or pin per the micromamba guide.
curl -fsSLo "$LEO/micromamba.tar.bz2" https://micro.mamba.pm/api/micromamba/linux-64/latest
tar -xjf "$LEO/micromamba.tar.bz2" -C "$LEO/bin" --strip-components=1 bin/micromamba

export MAMBA_ROOT_PREFIX="$LEO/mamba"
"$LEO/bin/micromamba" create -y -n leonida -c conda-forge --override-channels "llvm-tools>=21" spirv-tools
export PATH="$LEO/mamba/envs/leonida/bin:$PATH"

cmake -S runtime -B runtime/build/linux -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_VULKAN_HEADERS_FETCH=ON
cmake --build runtime/build/linux --parallel "$(nproc)"
LLVM_MC=llvm-mc LEONIDA_VK_DEVICE="<part of your GPU name>" \
  python3 -I scripts/run_gpu_tests.py --build runtime/build/linux --work "$LEO/run" --require-pass | tee "$LEO/run.log"
echo "runner exit: ${PIPESTATUS[0]}"
```

Notes:
- `LEONIDA_VK_DEVICE` selects the physical GPU by substring so the runner does not fall back to Mesa's `llvmpipe`.
- Keep `$LEO` on a filesystem without `noexec` (`findmnt -no OPTIONS "$LEO"`).
- System prerequisites still needed: a working Vulkan driver/loader (`vulkaninfo --summary`), `cmake`, `ninja`, `g++`
  (or `clang++`), `python3`, `curl`, `tar`, and `bzip2`.
- Cleanup: delete `$LEO` and `runtime/build/linux`. micromamba also writes `~/.conda/environments.txt` and
  `~/.mamba/pkgs/urls.txt` (delete only those files if you already use conda).