From 41a8c1e53686576e5b28636d8e544a83e6ef4ad3 Mon Sep 17 00:00:00 2001 From: Keyvan Kambakhsh Date: Tue, 3 Dec 2024 23:48:42 +0330 Subject: [PATCH] feat: Build-script builds 30cc with both gcc-generated and 30cc-generated compilers --- build.py | 79 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/build.py b/build.py index 8d2b216..ff1af88 100755 --- a/build.py +++ b/build.py @@ -7,9 +7,10 @@ import pathlib -def compile(path): +def compile(compiler, path): + print("Compiling:", path) out = subprocess.run( - ["./a.out", path, "--asm"], + [compiler, path, "--asm"], capture_output=True, text=True, ) @@ -19,41 +20,47 @@ def compile(path): return out.stdout -has_error = False -for f in glob.glob("**/*.c", recursive=True): - try: - asm = compile(f) - asm_filename = "target/asm/" + f.replace(".c", ".asm") - obj_filename = "target/obj/" + f.replace(".c", ".o") - os.makedirs(pathlib.Path(asm_filename).parent, exist_ok=True) - os.makedirs(pathlib.Path(obj_filename).parent, exist_ok=True) - with io.open(asm_filename, "w") as f: - f.write(asm) - subprocess.run( - ["nasm", "-f", "elf64", asm_filename, "-o", obj_filename], check=True - ) - except Exception as e: - has_error = True - print("Error compiling:", f, e) - +def compile_30cc(compiler, out): + has_error = False + for f in glob.glob("**/*.c", recursive=True): + try: + asm = compile(compiler, f) + asm_filename = "target/asm/" + f.replace(".c", ".asm") + obj_filename = "target/obj/" + f.replace(".c", ".o") + os.makedirs(pathlib.Path(asm_filename).parent, exist_ok=True) + os.makedirs(pathlib.Path(obj_filename).parent, exist_ok=True) + with io.open(asm_filename, "w") as f: + f.write(asm) + subprocess.run( + ["nasm", "-f", "elf64", asm_filename, "-o", obj_filename], check=True + ) + except Exception as e: + has_error = True + print("Error compiling:", f, e) -objs = list( - [ - p - for p in glob.glob("target/obj/**/*.o", recursive=True) - if not p.startswith("target/obj/examples") - ] -) - -if not has_error: - subprocess.run( + objs = list( [ - "ld", - "-dynamic-linker", - "/lib64/ld-linux-x86-64.so.2", - "-lc", - "-o", - "target/30cc", + p + for p in glob.glob("target/obj/**/*.o", recursive=True) + if not p.startswith("target/obj/examples") ] - + objs ) + + if not has_error: + subprocess.run( + [ + "ld", + "-dynamic-linker", + "/lib64/ld-linux-x86-64.so.2", + "-lc", + "-o", + out, + ] + + objs + ) + +print("Compiling 30cc with gcc-generated compiler") +compile_30cc('./a.out', '30cc_gcc') + +print("Compiling 30cc with 30cc-generated compiler") +compile_30cc('./30cc', '30cc') \ No newline at end of file