|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +#!/usr/bin/env python3 |
| 6 | + |
| 7 | +# Compile all .cpp files in this script's directory into executables with the same stem. |
| 8 | +# Usage: python compile.py [extra g++ flags...] |
| 9 | + |
| 10 | +HERE = Path(__file__).resolve().parent |
| 11 | +CPP_FILES = sorted(HERE.glob("*.cpp")) |
| 12 | + |
| 13 | +if not CPP_FILES: |
| 14 | + print(f"No .cpp files found in {HERE}") |
| 15 | + sys.exit(0) |
| 16 | + |
| 17 | +DEFAULT_FLAGS = [ |
| 18 | + "-std=c++17", |
| 19 | + "-g", |
| 20 | + "-O0", |
| 21 | + "-Wall", |
| 22 | + "-Wextra", |
| 23 | + "-DUSE_IMM", |
| 24 | + "-I/home/zdh/WorkSpace/GenSym/headers", |
| 25 | + "-lz3", |
| 26 | + "-DENABLE_PROFILE_TIME", |
| 27 | + "-DNO_REUSE", |
| 28 | +] |
| 29 | +EXTRA_FLAGS = sys.argv[1:] |
| 30 | +FLAGS = DEFAULT_FLAGS + EXTRA_FLAGS |
| 31 | + |
| 32 | + |
| 33 | +def compile_all(cpp_files=None, flags=None): |
| 34 | + if cpp_files is None: |
| 35 | + cpp_files = CPP_FILES |
| 36 | + if flags is None: |
| 37 | + flags = FLAGS |
| 38 | + |
| 39 | + compiled_total = 0 |
| 40 | + compiled_success = 0 |
| 41 | + compiled_failed = 0 |
| 42 | + |
| 43 | + for cpp in cpp_files: |
| 44 | + out = cpp.with_suffix(".exe") # foo.cpp -> ./foo |
| 45 | + cmd = ["g++", str(cpp), "-o", str(out)] + flags |
| 46 | + print("Compiling:", cpp.name) |
| 47 | + try: |
| 48 | + proc = subprocess.run(cmd, capture_output=True, text=True) |
| 49 | + except FileNotFoundError: |
| 50 | + print("Error: g++ not found. Install a C++ compiler.") |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + compiled_total += 1 |
| 54 | + if proc.returncode == 0: |
| 55 | + compiled_success += 1 |
| 56 | + print(" -> OK:", out.name) |
| 57 | + else: |
| 58 | + compiled_failed += 1 |
| 59 | + print(" -> FAILED:", cpp.name) |
| 60 | + if proc.stdout: |
| 61 | + print(proc.stdout.strip()) |
| 62 | + if proc.stderr: |
| 63 | + print(proc.stderr.strip()) |
| 64 | + |
| 65 | + print() |
| 66 | + print("Overall summary:") |
| 67 | + print(f" Files compiled: {compiled_total}") |
| 68 | + print(f" Succeeded: {compiled_success}") |
| 69 | + print(f" Failed: {compiled_failed}") |
| 70 | + return compiled_total, compiled_success, compiled_failed |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + compile_all() |
0 commit comments