#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Build the pinned host toolchain in ignored storage; never download or install globally.""" import argparse import hashlib import json import sys import os from pathlib import Path import shutil import subprocess ROOT = Path(__file__).resolve().parents[2] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path, default=ROOT / '.build/toolchain') parser.add_argument('--mc6839-rom', type=Path, help='Optional private Float09.bin') parser.add_argument('--standalone-disk-source', type=Path, help='Optional private upstream dskcon-standalone.c') parser.add_argument('--jobs', type=int, default=4) parser.add_argument('--host-arch', choices=['native', 'arm64', 'x86_64'], default='native') args = parser.parse_args() out = args.output.resolve() if out == ROOT or ROOT / 'third_party' in out.parents or out == ROOT / 'third_party': parser.error('Choose a separate build output directory') subprocess.run([sys.executable, str(ROOT / 'tools/toolchain/verify-sources.py')], check=True) prefix = out / 'install' work = out / 'work' work.mkdir(parents=True, exist_ok=True) env = dict(os.environ) env['PATH'] = str(prefix / 'bin') + os.pathsep + env['PATH'] env.setdefault('DEVELOPER_DIR', '/Applications/Xcode.app/Contents/Developer') if args.host_arch != 'native': if sys.platform != 'darwin': parser.error('--host-arch is currently a macOS build option') flags = f'-O2 -arch {args.host_arch} -mmacosx-version-min=14.0' env.update(CC='clang', CXX='clang++', CFLAGS=flags, CXXFLAGS=flags, LDFLAGS=f'-arch {args.host_arch} -mmacosx-version-min=14.0') def run(*command, cwd): print('+', ' '.join(map(str, command)), flush=True) subprocess.run(list(map(str, command)), cwd=cwd, env=env, check=True) # Copies protect pristine vendored snapshots from generated makefiles/objects. for name in ('lwtools', 'cmoc'): target = work / name if target.exists(): raise SystemExit(f'{target} already exists; choose a fresh --output for a reproducible build') shutil.copytree(ROOT / 'third_party' / name, target) # Git does not retain mtimes. Equal times keep shipped generated files # from unnecessarily invoking version-specific Autotools generators. for item in target.rglob('*'): if item.is_file(): os.utime(item, (946684800, 946684800)) # Retain pristine upstream snapshots; apply the reviewed patches only to the build copies. run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/cmoc-debug-metadata.patch', cwd=work / 'cmoc') run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/cmoc-add-zero-flags.patch', cwd=work / 'cmoc') run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/cmoc-indexed-byte-subtraction.patch', cwd=work / 'cmoc') run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/cmoc-indexed-argument-load.patch', cwd=work / 'cmoc') run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/writecocofile-before-dir.patch', cwd=work / 'cmoc') run('/usr/bin/patch', '-p1', '-i', ROOT / 'tools/toolchain/patches/lwtools-project-inputs.patch', cwd=work / 'lwtools') run('make', f'-j{args.jobs}', f'PREFIX={prefix}', cwd=work / 'lwtools') run('make', f'PREFIX={prefix}', 'install', cwd=work / 'lwtools') # Optional private inputs are checked against the reviewed upstream release. record = json.loads((ROOT / 'third_party/manifests/cmoc.json').read_text()) for supplied, relative in ((args.mc6839_rom, 'src/float/Float09.bin'), (args.standalone_disk_source, 'src/stdlib/dskcon-standalone.c')): if supplied: if hashlib.sha256(supplied.read_bytes()).hexdigest() != record['excludedFileSHA256'][relative]: raise SystemExit(f'Private input does not match reviewed CMOC release: {supplied}') shutil.copyfile(supplied, work / 'cmoc' / relative) for extension in ('am', 'in'): if args.mc6839_rom: path = work / 'cmoc/src/float' / ('Makefile.' + extension) text = path.read_text().replace('cmoclib_DATA = ', 'cmoclib_DATA = float-ctor.mc6839_o float-ctor.mc6839_os9_o libcmoc-float-mc6839.a libcmoc-float-mc6839_os9.a ') path.write_text(text); os.utime(path, (946684800, 946684800)) if args.standalone_disk_source: path = work / 'cmoc/src/stdlib' / ('Makefile.' + extension) text = path.read_text() # Restore the omitted implementation beside the other C runtime sources. assert 'C_COCO_BASIC =' in text text = text.replace('C_COCO_BASIC =', 'C_COCO_BASIC = dskcon-standalone.c') path.write_text(text); os.utime(path, (946684800, 946684800)) if sys.platform == 'darwin': wrapper = prefix / 'bin/cpp' shutil.copyfile(ROOT / 'tools/toolchain/cpp-macos.sh', wrapper) wrapper.chmod(0o755) run('./configure', f'--prefix={prefix}', '--with-writecocofile', cwd=work / 'cmoc') run('make', f'-j{args.jobs}', cwd=work / 'cmoc') run('make', 'install', cwd=work / 'cmoc') (prefix / 'VCC-toolchain.json').write_text(json.dumps({'mc6839': bool(args.mc6839_rom), 'standaloneDisk': bool(args.standalone_disk_source)}, indent=2) + '\n') for tool in ('cmoc', 'lwasm', 'lwlink', 'lwar'): run(prefix / 'bin' / tool, '--version', cwd=out) print(f'Toolchain ready in {prefix}; no app or global installation changed.') if __name__ == '__main__': main()