#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Build a relocatable macOS CMOC runtime; host SDK tools are build-time only.""" import argparse import json import os from pathlib import Path import shutil import subprocess import sys ROOT = Path(__file__).resolve().parents[2] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path, default=ROOT / '.build/cmoc-runtime') parser.add_argument('--architectures', nargs='+', choices=['arm64', 'x86_64'], default=['arm64', 'x86_64']) parser.add_argument('--jobs', type=int, default=4) parser.add_argument('--standalone-disk-source', type=Path, help='Optional private DECB-derived disk driver; never downloaded automatically') args = parser.parse_args() output = args.output.resolve() if output.exists(): parser.error('Choose a fresh output directory') output.mkdir(parents=True) architectures = list(dict.fromkeys(args.architectures)) base = dict(os.environ) base.setdefault('DEVELOPER_DIR', '/Applications/Xcode.app/Contents/Developer') def run(*command, cwd=ROOT, env=base): print('+', ' '.join(map(str, command)), flush=True) subprocess.run(list(map(str, command)), cwd=cwd, env=env, check=True) run(sys.executable, ROOT / 'tools/toolchain/verify-sources.py') roots = [] for architecture in architectures: arch_root = output / architecture run(sys.executable, ROOT / 'tools/toolchain/build.py', '--output', arch_root, '--host-arch', architecture, '--jobs', args.jobs) prefix = arch_root / 'install' env = dict(base, CC='clang', CXX='clang++', CFLAGS=f'-O2 -arch {architecture} -mmacosx-version-min=14.0 -Wno-error=implicit-function-declaration', CXXFLAGS=f'-O2 -arch {architecture} -mmacosx-version-min=14.0', LDFLAGS=f'-arch {architecture} -mmacosx-version-min=14.0') for name in ('mcpp', 'make'): source = arch_root / 'work' / name shutil.copytree(ROOT / 'third_party' / name, source) for file in source.rglob('*'): if file.is_file(): os.utime(file, (946684800, 946684800)) options = [f'--host={architecture}-apple-darwin', '--without-guile', '--disable-dependency-tracking'] if name == 'make' else [] run('./configure', f'--prefix={prefix}', *options, cwd=source, env=env) run('/usr/bin/make', f'-j{args.jobs}', cwd=source, env=env) run('/usr/bin/make', 'install', cwd=source, env=env) run('xcrun', 'clang', '-O2', '-arch', architecture, '-mmacosx-version-min=14.0', ROOT / 'tools/toolchain/cpp-mcpp.c', '-o', prefix / 'bin/cpp') roots.append(prefix) bundle = output / 'Toolchain' for name in ('bin', 'libexec', 'share', 'LICENSES'): (bundle / name).mkdir(parents=True) shutil.copytree(roots[0] / 'share/cmoc', bundle / 'share/cmoc') shutil.copyfile(ROOT / 'tools/toolchain/cmoc-runtime.sh', bundle / 'bin/cmoc') (bundle / 'bin/cmoc').chmod(0o755) for tool in ('cmoc', 'cpp', 'mcpp', 'make', 'lwasm', 'lwlink', 'lwar', 'writecocofile'): target = bundle / ('libexec' if tool == 'cmoc' else 'bin') / tool inputs = [prefix / 'bin' / tool for prefix in roots] if len(inputs) > 1: run('xcrun', 'lipo', '-create', *inputs, '-output', target) else: shutil.copyfile(inputs[0], target) target.chmod(0o755) notices = {'CMOC-COPYING.txt': 'cmoc/COPYING', 'CMOC-README.txt': 'cmoc/README', 'LWTools-COPYING.txt': 'lwtools/COPYING', 'LWTools-GPL3.txt': 'lwtools/GPL3', 'MCPP-LICENSE.txt': 'mcpp/LICENSE', 'MAKE-COPYING.txt': 'make/COPYING'} for target, source in notices.items(): shutil.copyfile(ROOT / 'third_party' / source, bundle / 'LICENSES' / target) shutil.copyfile(ROOT / 'tools/toolchain/patches/README.md', bundle / 'LICENSES/CMOC-VCC-EXTENSIONS.txt') record = {'schemaVersion': 1, 'cmoc': json.loads((ROOT / 'third_party/manifests/cmoc.json').read_text())['version'], 'lwtools': '4.25', 'mcpp': '2.7.2', 'make': '4.4.1', 'vccDebugMetadata': 1, 'vccAddZeroFlagsFix': 1, 'vccIndexedByteSubtractionFix': 1, 'vccIndexedArgumentLoadFix': 1, 'vccWriteCoCoFileForward': 1, 'writeCoCoFileAllocation': 'upstream', 'vccLWASMProjectInputs': 1, 'architectures': architectures, 'privateRuntimeInputs': False, 'minimumMacOS': '14.0', 'appleDeveloperToolsRequiredAtRuntime': False} (bundle / 'manifest.json').write_text(json.dumps(record, indent=2) + '\n') perl_output = output / 'perl' run(sys.executable, ROOT / 'tools/toolchain/build-perl.py', '--output', perl_output, '--architectures', *architectures, '--jobs', args.jobs) perl_prefixes = [argument for arch in architectures for argument in ('--prefix', f'{arch}={perl_output / arch / "install"}')] run(sys.executable, ROOT / 'tools/toolchain/stage-perl.py', bundle, *perl_prefixes) run(sys.executable, ROOT / 'tools/toolchain/stage-libraries.py', bundle, '--jobs', args.jobs) run(sys.executable, ROOT / 'tools/toolchain/stage-decbfile.py', bundle, '--jobs', args.jobs) if args.standalone_disk_source: run(sys.executable, ROOT / 'tools/toolchain/stage-standalone-disk.py', bundle, '--source', args.standalone_disk_source) run(sys.executable, ROOT / 'tools/toolchain/verify-runtime.py', bundle) print(f'Runtime ready: {bundle}\nStage at platforms/macos/LocalResources/ToolchainBundle for app builds.') if __name__ == '__main__': main()