import hashlib
import json
import os
from pathlib import Path
import subprocess
import time
ROOT = Path(__file__).resolve().parents[1]
PACKAGE = ROOT / 'lean-kernel'
LIBRARY = PACKAGE / '.lake/build/lib/libqleisli__kernel_QleisliKernel.a'
BUILD_ENV = 'QLEISLI_NATIVE_BUILD'
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def source_identity():
return {str(path.relative_to(ROOT)): digest(path)
for path in sorted(PACKAGE.rglob('*')) if path.is_file() and '.lake' not in path.parts
and (path.suffix == '.lean' or path.name in {'lakefile.toml', 'lake-manifest.json', 'lean-toolchain'})}
def run(command, directory, log, timeout=600):
started = time.monotonic()
result = subprocess.run(command, cwd=directory, capture_output=True, text=True, timeout=timeout)
log.append(dict(command=[str(x) for x in command], cwd=str(directory), exit=result.returncode,
stdout=result.stdout, stderr=result.stderr, seconds=time.monotonic() - started))
if result.returncode:
raise RuntimeError(json.dumps(log[-1]))
return result.stdout
def prepare(path, log):
if path.exists():
raise ValueError('refuse an existing native build binding')
before = source_identity()
run(['lake', 'build', 'QleisliKernel:static'], PACKAGE, log)
if before != source_identity():
raise ValueError('kernel sources changed during native library build')
products = {str(p.relative_to(ROOT)): digest(p) for p in sorted((PACKAGE / '.lake/build/lib/lean').rglob('*.olean'))}
products[str(LIBRARY.relative_to(ROOT))] = digest(LIBRARY)
data = dict(format=1, root=str(ROOT), sources=before, products=products,
toolchain=run(['lake', 'env', 'lean', '--version'], PACKAGE, log).strip())
path.write_text(json.dumps(data, indent=2) + '\n')
return data
def validate(path):
data = json.loads(path.read_text())
if data.get('format') != 1 or data.get('root') != str(ROOT) or data.get('sources') != source_identity():
raise ValueError('native build source identity changed')
products = data.get('products', {})
expected = {str(p.relative_to(ROOT)) for p in (PACKAGE / '.lake/build/lib/lean').rglob('*.olean')}
expected.add(str(LIBRARY.relative_to(ROOT)))
if set(products) != expected or any(digest(ROOT / name) != value for name, value in products.items()):
raise ValueError('native build products changed')
toolchain = subprocess.check_output(['lake', 'env', 'lean', '--version'], cwd=PACKAGE, text=True).strip()
if data.get('toolchain') != toolchain:
raise ValueError('native build toolchain changed')
return data
def build(project, log, timeout=240):
binding = os.environ.get(BUILD_ENV)
if binding:
data = validate(Path(binding))
log.append(dict(native_build=str(binding), library_sha256=data['products'][str(LIBRARY.relative_to(ROOT))]))
else:
run(['lake', 'build', 'QleisliKernel:static'], PACKAGE, log)
source = project / 'Main.lean'
generated = project / 'Main.c'
binary = project / 'native-test'
run(['lake', 'env', 'lean', '-DwarningAsError=true', '--root=' + str(project), '-c', str(generated), str(source)], PACKAGE, log, timeout)
run(['lake', 'env', 'leanc', '-O3', '-o', str(binary), str(generated), str(LIBRARY)], PACKAGE, log, timeout)
if binding:
validate(Path(binding))
log.append(dict(driver_source_sha256=digest(source), executable_sha256=digest(binary)))
return binary