#!/usr/bin/env python3 """Build the standalone 35-track Sound Lab disk using the pinned CMOC bundle.""" # SPDX-License-Identifier: GPL-3.0-or-later import argparse import hashlib import json import os from pathlib import Path import shutil import subprocess PROJECT = Path(__file__).resolve().parent REPO = PROJECT.parents[1] def digest(path): return hashlib.sha256(path.read_bytes()).hexdigest() def run(*args, **kwargs): subprocess.run([str(a) for a in args], check=True, **kwargs) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--toolchain', type=Path, default=REPO/'platforms/macos/LocalResources/ToolchainBundle') parser.add_argument('--output', type=Path, default=REPO/'.build/sound-lab') args = parser.parse_args() output, runtime = args.output.resolve(), args.toolchain.resolve() if output == PROJECT or PROJECT in output.parents: parser.error('Choose a build output outside the source project so generated files cannot become source inputs.') output.mkdir(parents=True, exist_ok=True) env = dict(os.environ, PATH=str(runtime/'bin')+os.pathsep+os.environ.get('PATH', '')) version = subprocess.check_output([runtime/'bin/cmoc', '--version'], text=True).strip() if '0.1.101' not in version: parser.error('This revision is qualified with CMOC 0.1.101; review a different compiler explicitly.') assembler = subprocess.check_output([runtime/'bin/lwasm', '--version'], text=True).strip() if assembler != 'lwasm from lwtools 4.25': parser.error('This revision is qualified with LWTools 4.25.') run('python3', PROJECT/'vectors/generate.py', '--output', output/'generated') vectors = json.loads((output/'generated/vectors.json').read_text()) sources = sorted(p for p in PROJECT.rglob('*') if p.is_file() and '__pycache__' not in p.parts) source_hash = hashlib.sha256() for source in sources: source_hash.update(source.relative_to(PROJECT).as_posix().encode()+b'\0'+source.read_bytes()) build_id = 'SL1-'+source_hash.hexdigest()[:8].upper() (output/'generated/build.h').write_text( '#define SOUNDLAB_BUILD "'+build_id+'"\n#define SOUND_VECTOR_SET_CRC16 0x'+vectors['vector_set_crc16']+'\n') shutil.copy2(PROJECT/'guest/main.c', output/'main.c') run(runtime/'bin/cmoc', '--coco', '-O2', '--intermediate', '--org=1600', '--limit=7A00', '--stack-space=1024', '-Igenerated', '-o', 'SOUNDLAB.BIN', 'main.c', '-lssc', '-ldecbfile', cwd=output, env=env) (output/'SOUNDLAB.BAS').write_bytes(b'10 PCLEAR1:CLEAR 32\n20 LOADM"SOUNDLAB":EXEC\n') # Public-domain DOS hook by Pierre Sarrazin, retained with its attribution. # Keep the hook itself as a normal source, with no Makefile parsing at build time. shutil.copy2(PROJECT/'guest/boot.c', output/'boot.c') run(runtime/'bin/cmoc', '--dos', '-O0', '--limit=2800', '-o', 'BOOT.BIN', 'boot.c', cwd=output, env=env) disk = output/'SOUNDLAB.DSK' disk.write_bytes(b'\xff'*(35*18*256)) run(runtime/'bin/install-coco-boot-loader', disk, output/'BOOT.BIN', env=env) for name in ('SOUNDLAB.BIN', 'SOUNDLAB.BAS'): run(runtime/'bin/writecocofile', *(['--newlines'] if name.endswith('.BAS') else ['--binary']), disk, name, cwd=output, env=env) manifest = dict(vectors, build_id=build_id, compiler=version, assembler=assembler, toolchain_manifest_sha256=digest(runtime/'manifest.json'), libraries=[dict(filename=name,sha256=digest(runtime/'share/cmoc/lib'/name)) for name in ('libssc.a','libdecbfile.a')], sources=[dict(path=p.relative_to(PROJECT).as_posix(),sha256=digest(p)) for p in sources], guest_cpu='MC6809', minimum_ram_kib=64, format='DECB 35x18x256', source_sha256=source_hash.hexdigest(), artifacts=[dict(filename=name, bytes=(output/name).stat().st_size, sha256=digest(output/name)) for name in ('SOUNDLAB.BIN', 'SOUNDLAB.BAS', 'SOUNDLAB.DSK')]) (output/'SOUNDLAB-manifest.json').write_text(json.dumps(manifest, indent=2)+'\n') (output/'SHA256SUMS').write_text(''.join(f"{item['sha256']} {item['filename']}\n" for item in manifest['artifacts'])) print(f'Built {build_id}: {disk}') if __name__ == '__main__': main()