Files

101 lines
5.1 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Prebuild the public-domain, ROM-backed DECB file API into an existing runtime.
The same bundled compiler/preprocessor builds libraries and end-user programs;
Apple developer tools are not required for this staging operation.
"""
import argparse
from concurrent.futures import ThreadPoolExecutor
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tempfile
from runtime_inputs import validate_private_inputs
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / 'third_party/cmoc-libraries/decbfile'
VERSION = '0.1.13'
EXCLUDED = {
'drivewire.c', 'dwReadBeckerPort.c', 'dwWriteBeckerPort.c',
'dwReadBitBangerSerialPort.c', 'dwWriteBitBangerSerialPort.c',
'FileChoiceDialog.c', 'runDOSLoader.c', 'binfile.c', 'writeMemoryToFile.c',
'readFileToMemory.c', 'readFileToDynamicMemory.c', 'decbutil.c', 'crc16.c',
}
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('runtime', type=Path)
parser.add_argument('--jobs', type=int, default=4)
args = parser.parse_args()
runtime = args.runtime.resolve()
manifest_path = runtime / 'manifest.json'
manifest = json.loads(manifest_path.read_text())
validate_private_inputs(runtime, manifest)
source_manifest = ROOT / 'third_party/manifests/decbfile.json'
source_record = json.loads(source_manifest.read_text())
if source_record['version'] != VERSION:
parser.error('Review the ROM-backed library selection before changing upstream version')
subprocess.run([sys.executable, ROOT / 'tools/toolchain/verify-sources.py'], check=True)
source_list = re.search(r'^LIBSRC = \\\n(.*?)\n\nLIBHEADERS', (SOURCE / 'Makefile').read_text(), re.M | re.S)
if not source_list:
parser.error('Unexpected upstream source list')
sources = source_list.group(1).replace('\\', '').split()
if not EXCLUDED.issubset(sources):
parser.error('Unexpected upstream optional components')
selected = [name for name in sources if name not in EXCLUDED]
env = dict(os.environ, PATH=str(runtime / 'bin') + ':/usr/bin:/bin')
env['DEVELOPER_DIR'] = '/VCC-no-developer-tools'
env['SDKROOT'] = '/VCC-no-sdk'
# A simple temporary path also avoids upstream CMOC's shell-quoting limits.
with tempfile.TemporaryDirectory(prefix='vcc-decbfile-', dir='/tmp') as folder:
work = Path(folder)
for name in selected + ['decbfile.h', 'decbfile_private.h']:
shutil.copyfile(SOURCE / name, work / name)
def compile_source(name):
subprocess.run([str(runtime / 'bin/cmoc'), '--coco', '-O2',
'-DNDEBUG', '-Wfor-condition-sizes', '-Werror', '-I.', '-c',
'-o', name[:-2] + '.o', name], cwd=work, env=env, check=True)
with ThreadPoolExecutor(max_workers=max(1, args.jobs)) as pool:
list(pool.map(compile_source, selected))
archive = work / 'libdecbfile.a'
subprocess.run([str(runtime / 'bin/lwar'), '--create', str(archive),
*[name[:-2] + '.o' for name in selected]], cwd=work, env=env, check=True)
archive_listing = subprocess.check_output([str(runtime / 'bin/lwar'), '--list', str(archive)], text=True)
for name in EXCLUDED:
if name[:-2] + '.o' in archive_listing:
raise SystemExit('Unexpected excluded archive member: ' + name)
target_archive = runtime / 'share/cmoc/lib/libdecbfile.a'
target_header = runtime / 'share/cmoc/include/decbfile.h'
shutil.copyfile(archive, target_archive)
shutil.copyfile(SOURCE / 'decbfile.h', target_header)
# Retain the original declaration verbatim instead of assigning a GPL label
# to a component whose author explicitly released it into the public domain.
header_notice = (SOURCE / 'decbfile.h').read_text().split('*/', 1)[0] + '*/\n'
notice = ('DECB File Library ' + VERSION + ' — ROM-backed core\n\n' + header_notice +
'\nUses the Disk BASIC ROM already mapped in the emulated machine.\n'
'No Disk BASIC ROM bytes or standalone Disk BASIC-derived driver are included in this archive.\n'
'Optional standalone, DriveWire, UI and binary-loader extensions are not included.\n\n'
'Included upstream source files:\n' + ''.join(' ' + name + '\n' for name in selected))
(runtime / 'LICENSES/DECBFILE-NOTICE.txt').write_text(notice)
manifest['decbfile'] = {'version': VERSION, 'profile': 'rom-backed-core',
'license': 'public-domain', 'requiresDiskBASICROM': True,
'standaloneDriverIncluded': False, 'sourceFiles': selected,
'sourceManifestSHA256': sha(source_manifest),
'archiveSHA256': sha(target_archive), 'headerSHA256': sha(target_header)}
manifest_path.write_text(json.dumps(manifest, indent=2) + '\n')
print(f'PASS: staged {len(selected)} public-domain DECB core objects; link with -ldecbfile')
if __name__ == '__main__': main()