"""Build an isolated copy using the existing, working KNP compiler/link settings.""" from concurrent.futures import ThreadPoolExecutor from pathlib import Path import shlex import subprocess source = Path('/mnt/c/KNP/cats-dogs/source') build = Path('/home/int/run/cats-dogs/build') 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) includes = [x.replace('/mnt/c/KNP/examples/examples/mnist-learn', str(source)) for x in flags['CXX_INCLUDES']] link = shlex.split((old / 'CMakeFiles/mnist_learn_example.dir/link.txt').read_text()) files = [x for x in link if x.endswith('.cpp.o')] def compile_file(obj): relative = obj.split('.dir/', 1)[1][:-2] target = build / (relative.replace('/', '_') + '.o') subprocess.run(['/usr/bin/c++', '-std=c++17', *flags['CXX_DEFINES'], *includes, *flags['CXX_FLAGS'], '-c', str(source / relative), '-o', str(target)], check=True) print(relative, flush=True) return str(target) with ThreadPoolExecutor(max_workers=2) as pool: objects = list(pool.map(compile_file, files)) tail = link[link.index('-o') + 2:] tail = [str((old / x).resolve()) if x.startswith('../../') else x for x in tail] subprocess.run(['/usr/bin/c++', *objects, '-o', str(build / 'cats_dogs'), *tail], cwd=old, check=True) print(build / 'cats_dogs', flush=True)