Skip to content

Commit

Permalink
feat: Build-script builds 30cc with both gcc-generated and 30cc-gener…
Browse files Browse the repository at this point in the history
…ated compilers
  • Loading branch information
keyvank committed Dec 3, 2024
1 parent dcf1da5 commit 41a8c1e
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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')

0 comments on commit 41a8c1e

Please sign in to comment.