our_knp/altai-objects/build.py
2026-10-15 08:11:15 +03:00

36 lines
1.5 KiB
Python

"""Build a standalone KNP program from one .cpp using the working mnist-learn compile and link settings.
usage: python3 build.py <source.cpp> [more.cpp ...] → /home/int/run/altai-objects/<first source stem>
"""
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import shlex
import subprocess
import sys
build = Path('/home/int/run/altai-objects')
build.mkdir(parents=True, exist_ok=True)
old = Path('/home/int/knp-build/examples/mnist-learn')
flags = {}
for line in (old / 'CMakeFiles/mnist_learn_example.dir/flags.make').read_text().splitlines():
if ' = ' in line:
key, value = line.split(' = ', 1)
flags[key] = shlex.split(value)
link = shlex.split((old / 'CMakeFiles/mnist_learn_example.dir/link.txt').read_text())
sources = [Path(s).resolve() for s in sys.argv[1:]]
def compile_file(src):
target = build / (src.stem + '.o')
subprocess.run(['/usr/bin/c++', '-std=c++17', *flags['CXX_DEFINES'], '-I' + str(src.parent), *flags['CXX_INCLUDES'],
*flags['CXX_FLAGS'], '-Wno-error', '-c', str(src), '-o', str(target)], check=True)
return str(target)
with ThreadPoolExecutor(max_workers=4) as pool:
objects = list(pool.map(compile_file, sources))
tail = link[link.index('-o') + 2:]
tail = [str((old / x).resolve()) if x.startswith('../../') else x for x in tail]
out = build / sources[0].stem
subprocess.run(['/usr/bin/c++', *objects, '-o', str(out), *tail], cwd=old, check=True)
print(out, flush=True)