Files
totk-pc-recomp/tools/build_hactool.py
T
Wellington Dias 89b5126da0 Declare the clobbers mbedtls's x86-64 multiply asm lacks, so a clang-built hactool verifies signatures.
hactool's bundled mbedtls 2.6.1 adds into memory and changes the flags in its MULADDC asm without saying so. gcc builds
happen to work; clang, the Linux release's compiler, keeps values in registers across it at -O1 and above, so every RSA
check failed (NCA header, NPDM, ACID) and the AppImage's setup stopped at the update merge. Found by installing from the
mounted AppImage; a bisection of the mbedtls objects pointed at bignum.o, and the memory and cc clobbers (as later
mbedtls releases have them) fix it. With the patch the full install from the AppImage completes and the game runs.
2026-10-30 14:28:35 -03:00

90 lines
4.3 KiB
Python

"""Build hactool: toolchain/hactool/hactool.exe (SciresM/hactool at a pinned commit + tools/hactool/*.patch).
Fetches the pinned commit into toolchain/src/hactool, resets it (local changes and build output there are discarded),
applies portable.patch (MacNativeGames' Windows-relevant changes) and unicode.patch (ours), and builds a static exe
with MSYS2 UCRT64 gcc; mbedtls comes with hactool's tree. The exe and hactool's LICENSE go to toolchain/hactool, where
tools/extract.py and tools/make_release.py take them. Run with .venv. On Linux: the system's cc and make build
toolchain/hactool/hactool (unicode.patch only adds a Windows manifest, under EXEEXT .exe).
python tools/build_hactool.py [--src DIR] [--out DIR]
"""
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
URL = 'https://github.com/SciresM/hactool.git'
COMMIT = '1d64a83450e025622f3468c28fc4164dad2c5ef6'
PATCHES = [ROOT / 'tools' / 'hactool' / n for n in ('portable.patch', 'unicode.patch', 'mbedtls-asm-clobbers.patch')]
WIN = os.name == 'nt'
MSYS_BASH = Path(r'C:\msys64\usr\bin\bash.exe')
EXE = 'hactool.exe' if WIN else 'hactool'
CONFIG_MK = ('CC = gcc\nEXEEXT = .exe\nCFLAGS = -O2 -Wall -Wextra -pedantic -std=gnu11\n'
'LDFLAGS = -lmbedtls -lmbedx509 -lmbedcrypto -static\n') if WIN else \
('CC = cc\nEXEEXT =\nCFLAGS = -O2 -Wall -Wextra -pedantic -std=gnu11\n'
'LDFLAGS = -lmbedtls -lmbedx509 -lmbedcrypto\n')
def git(src, *args):
subprocess.run(['git', '-C', str(src), *args], check=True)
def fetch(src):
"""src holds COMMIT with no local changes or untracked files."""
if not (src / '.git').is_dir():
src.mkdir(parents=True, exist_ok=True)
git(src, 'init', '-q')
git(src, 'remote', 'add', 'origin', URL)
head = subprocess.run(['git', '-C', str(src), 'rev-parse', 'HEAD'], capture_output=True, text=True).stdout.strip()
if head != COMMIT:
git(src, 'fetch', '-q', '--depth', '1', 'origin', COMMIT)
git(src, 'checkout', '-q', '-f', COMMIT)
git(src, 'clean', '-q', '-fdx')
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--src', type=Path, default=ROOT / 'toolchain' / 'src' / 'hactool', help='source checkout')
ap.add_argument('--out', type=Path, default=ROOT / 'toolchain' / 'hactool', help='where hactool(.exe) goes')
ap.add_argument('--cc', help='Linux: the C compiler command for hactool and its bundled mbedtls, for example '
'"clang --sysroot=DIR" (tools/make_release_linux.py); default cc')
args = ap.parse_args()
if WIN and not MSYS_BASH.is_file():
sys.exit(f'{MSYS_BASH} not found: install MSYS2 with the UCRT64 gcc and make (docs/building.md)')
if not WIN and not (shutil.which('make') and (args.cc or shutil.which('cc'))):
sys.exit('make and cc not found: install a C compiler and make (docs/building.md)')
src, out = args.src.resolve(), args.out.resolve()
fetch(src)
for p in PATCHES:
git(src, 'apply', '--whitespace=nowarn', str(p))
config = CONFIG_MK.replace('CC = cc\n', f'CC = {args.cc}\n') if args.cc else CONFIG_MK
(src / 'config.mk').write_text(config, encoding='utf-8', newline='\n')
jobs = f'-j{os.cpu_count() or 4}'
if WIN:
env = dict(os.environ, MSYSTEM='UCRT64', CHERE_INVOKING='1')
r = subprocess.run([str(MSYS_BASH), '-lc', f'make {jobs}'], cwd=src, env=env, capture_output=True, text=True)
else:
r = subprocess.run(['make', jobs, *([f'CC={args.cc}'] if args.cc else [])], cwd=src, capture_output=True,
text=True) # CC= on the command line: the bundled mbedtls's own make reads it too
(src / 'build.log').write_text(r.stdout + r.stderr, encoding='utf-8')
if r.returncode:
sys.exit(f'make failed ({r.returncode}); log: {src / "build.log"}')
out.mkdir(parents=True, exist_ok=True)
shutil.copy2(src / EXE, out / EXE)
shutil.copy2(src / 'LICENSE', out / 'LICENSE')
banner = subprocess.run([str(out / EXE)], capture_output=True, text=True)
if 'hactool (c) SciresM' not in banner.stdout + banner.stderr:
sys.exit(f'{out / EXE} does not start')
print(f'{out / EXE}: {COMMIT[:7]} + {", ".join(p.name for p in PATCHES)}')
if __name__ == '__main__':
main()