made multithreadded

This commit is contained in:
OusmBlueNinja 2025-04-18 22:12:29 -05:00
parent 04e3b5cada
commit 13a22ff182

View File

@ -7,6 +7,12 @@ import time
import json import json
from remake_config import * from remake_config import *
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
print_lock = threading.Lock()
# ========== COLOR UTILS ========== # ========== COLOR UTILS ==========
class Colors: class Colors:
HEADER = '\033[95m' HEADER = '\033[95m'
@ -148,16 +154,20 @@ def compile_source(source, obj, includes):
cmd = [compiler, *flags, *[f"-I{inc}" for inc in includes], "-MMD", "-MP", "-c", str(source), "-o", str(obj)] cmd = [compiler, *flags, *[f"-I{inc}" for inc in includes], "-MMD", "-MP", "-c", str(source), "-o", str(obj)]
try: try:
print(f"\r{color('🔨 Compiling:', Colors.OKCYAN)} {source}{' ' * 40}", end="", flush=True) with print_lock:
print(f"\r{color('🔨 Compiling:', Colors.OKCYAN)} {source}{' ' * 40}", end="", flush=True)
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
log(f"[COMPILE] {' '.join(cmd)}") log(f"[COMPILE] {' '.join(cmd)}")
return True
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print() # move to new line before showing error with print_lock:
error(f"Failed to compile {source}") print() # move to new line before showing error
print("🔧 Command:", " ".join(cmd)) error(f"Failed to compile {source}")
print(e.stderr.decode()) print("🔧 Command:", " ".join(cmd))
print(e.stderr.decode())
log(f"[ERROR] {' '.join(cmd)}\n{e.stderr.decode()}") log(f"[ERROR] {' '.join(cmd)}\n{e.stderr.decode()}")
sys.exit(1) return False
@ -184,34 +194,42 @@ def build():
link_dirs, libs, extra_includes = resolve_packages() link_dirs, libs, extra_includes = resolve_packages()
all_includes = INCLUDE_DIRS + extra_includes all_includes = INCLUDE_DIRS + extra_includes
for source in cpp_files: compile_tasks = []
obj = obj_path(source) with ThreadPoolExecutor() as executor:
dep = dep_path(obj) for source in cpp_files:
obj_mtime = obj.stat().st_mtime if obj.exists() else 0 obj = obj_path(source)
needs_build = not obj.exists() or source.stat().st_mtime > obj_mtime dep = dep_path(obj)
obj_mtime = obj.stat().st_mtime if obj.exists() else 0
needs_build = not obj.exists() or source.stat().st_mtime > obj_mtime
if not needs_build and dep.exists(): if not needs_build and dep.exists():
for dep_file in parse_dep_file(dep): for dep_file in parse_dep_file(dep):
try: try:
if Path(dep_file).exists() and Path(dep_file).stat().st_mtime > obj_mtime: if Path(dep_file).exists() and Path(dep_file).stat().st_mtime > obj_mtime:
needs_build = True
break
except Exception:
needs_build = True needs_build = True
break break
except Exception:
needs_build = True
break
if needs_build: if needs_build:
compile_source(source, obj, all_includes) compile_tasks.append(executor.submit(compile_source, source, obj, all_includes))
else: else:
print(f"\r{color('👌 Up-to-date:', Colors.GRAY)} {source}{' ' * 20}", end="", flush=True) with print_lock:
print(f"\r{color('👌 Up-to-date:', Colors.GRAY)} {source}{' ' * 20}", end="", flush=True)
obj_files.append(obj)
for task in as_completed(compile_tasks):
if not task.result():
sys.exit(1)
obj_files.append(obj)
print() print()
link_objects(obj_files, link_dirs, libs) link_objects(obj_files, link_dirs, libs)
banner("✅ Build Complete") banner("✅ Build Complete")
print(color(f"⏱ Build time: {time.time() - build_start:.2f}s", Colors.OKCYAN)) print(color(f"⏱ Build time: {time.time() - build_start:.2f}s", Colors.OKCYAN))
def run(): def run():
build() build()
if TARGET.exists(): if TARGET.exists():