Files

90 lines
4.7 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
"""Build and package reviewed CMOC libraries with the standalone compiler chain."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
from runtime_inputs import validate_private_inputs
ROOT = Path(__file__).resolve().parents[2]
LIBRARIES = ('astar', 'bcontrol', 'bgraph', 'bsound', 'cardgame', 'ebitor', 'hirestxt', 'wydealloc')
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)
subprocess.run([sys.executable, ROOT / 'tools/toolchain/verify-sources.py'], check=True)
env = dict(os.environ, PATH=str(runtime / 'bin') + ':/usr/bin:/bin', LC_ALL='C',
DEVELOPER_DIR='/VCC-no-developer-tools', SDKROOT='/VCC-no-sdk')
records = {}
with tempfile.TemporaryDirectory(prefix='vcc-cmoc-libraries-', dir='/tmp') as folder:
for name in LIBRARIES:
source = ROOT / 'third_party/cmoc-libraries' / name
work = Path(folder) / name
shutil.copytree(source, work)
subprocess.run([str(runtime / 'bin/make'), f'-j{max(1, args.jobs)}', f'lib{name}.a'],
cwd=work, env=env, check=True)
output = subprocess.check_output([str(runtime / 'bin/make'), '--no-print-directory',
'--eval', 'vcc-list-headers:;@echo VCC_HEADERS $(LIBHEADERS)', 'vcc-list-headers'],
cwd=work, env=env, text=True)
matches = [line for line in output.splitlines() if line.startswith('VCC_HEADERS ')]
if len(matches) != 1: raise SystemExit('Cannot determine library headers: ' + name)
headers = matches[0].split()[1:]
archive = runtime / 'share/cmoc/lib' / f'lib{name}.a'
shutil.copyfile(work / f'lib{name}.a', archive)
files = {archive.relative_to(runtime).as_posix(): sha(archive)}
for header in headers:
if Path(header).name != header or not header.endswith('.h'):
raise SystemExit('Unexpected library header: ' + header)
target = runtime / 'share/cmoc/include' / header
if target.exists() and target.read_bytes() != (source / header).read_bytes():
raise SystemExit('Conflicting library header: ' + header)
shutil.copyfile(source / header, target)
files[target.relative_to(runtime).as_posix()] = sha(target)
record = json.loads((ROOT / 'third_party/manifests' / (name + '.json')).read_text())
notice = runtime / 'LICENSES' / (name.upper() + '-NOTICE.txt')
text = f"{name} {record['version']}\n\n{record['license']}\n\n"
# Keep declarations/acknowledgments from every compiled source and
# public header. Full corresponding source remains in third_party.
for file in sorted(source.glob('*')):
if not file.is_file(): continue
if file.name in ('COPYING', 'README.md'):
text += '\n--- ' + file.name + ' ---\n' + file.read_text()
elif file.suffix in ('.c', '.h'):
content = file.read_text()
if content.startswith('/*'):
notice_end = content.find('*/')
excerpt = content[:notice_end + 2] if notice_end >= 0 else ''
else:
lines = []
for line in content.splitlines():
if line.startswith('//') or not line.strip(): lines.append(line)
else: break
excerpt = '\n'.join(lines).strip()
if excerpt: text += '\n--- ' + file.name + ' ---\n' + excerpt + '\n'
notice.write_text(text)
records[name] = {'version': record['version'], 'license': record['license'],
'profile': 'coco', 'filesSHA256': files}
manifest['libraries'] = records
manifest_path.write_text(json.dumps(manifest, indent=2) + '\n')
subprocess.run([sys.executable, ROOT / 'tools/toolchain/stage-guest-sdk.py', runtime], check=True)
print('PASS: guest SDK and eight optional library archives and headers built with bundled tools')
if __name__ == '__main__': main()