Skip to content

Commit

Permalink
feat: Use Makefile instead of python build script
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Dec 6, 2024
1 parent 4176484 commit 9a8903d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 74 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
out
out.asm
output.txt
/target
/target1
/target2
30cc
30cc_30cc
30cc_gcc

# Prerequisites
Expand Down
41 changes: 36 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
.PHONY=run

CC ?= cc
CFLAGS := -std=gnu99 -Og -ggdb
C_FILES := $(filter-out $(wildcard examples/*), $(shell find . -type f -name '*.c' -not -path './examples/*'))
H_FILES := $(filter-out $(wildcard examples/*), $(shell find . -type f -name '*.h' -not -path './examples/*'))

BIN := a.out
# Compile 30cc with gcc
STAGE1_CC ?= cc
STAGE1_CFLAGS := -std=gnu99 -Og -ggdb
STAGE1_BIN := ./30cc_gcc

$(BIN): *.h *.c parser/*.c parser/*.h parser/expr/*.c parser/expr/*.h codegen/*.c codegen/*.h preprocess/*.c preprocess/*.h
$(CC) $(CFLAGS) *.c parser/*.c codegen/*.c parser/expr/*.c preprocess/*.c
# Compile 30cc with gcc-generated 30cc
STAGE2_CC := ./30cc_gcc
STAGE2_BIN := ./30cc_30cc

# Compile 30cc with 30cc-generated 30cc
STAGE3_CC := ./30cc_30cc
STAGE3_BIN := ./30cc

$(STAGE1_BIN): $(H_FILES) $(C_FILES)
$(STAGE1_CC) $(STAGE1_CFLAGS) $(C_FILES) -o $(STAGE1_BIN)

$(STAGE2_BIN): $(STAGE1_BIN) **/*.h **/*.c
rm -rf target1
for file in $(C_FILES) ; do \
mkdir -p target1/$$(dirname $${file}); \
$(STAGE2_CC) $${file} --asm > target1/$${file}.asm; \
nasm -f elf64 target1/$${file}.asm -o target1/$${file}.o; \
done
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o $(STAGE2_BIN) $$(find target1 -type f -name '*.o')
rm -rf target1

$(STAGE3_BIN): $(STAGE2_BIN) **/*.h **/*.c
rm -rf target2
for file in $(C_FILES) ; do \
mkdir -p target2/$$(dirname $${file}); \
$(STAGE2_CC) $${file} --asm > target2/$${file}.asm; \
nasm -f elf64 target2/$${file}.asm -o target2/$${file}.o; \
done
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o $(STAGE3_BIN) $$(find target2 -type f -name '*.o')
rm -rf target2

run: $(BIN)
@./a.out $(program) --asm > out.asm
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
- You'll first need to bootstrap the compiler by running `make`. This will compile the 30cc compiler and store its binary in `a.out`.
- Then run `./build.py`. This will use the bootstrapped 30cc-compiler to compile 30cc itself. It then again uses the 30cc-compiled compiler to compile 30cc once again. The final compiler is then stored as `./30cc`.
- In the end, you will have 3 binary files which should all behave the same:
1. `a.out` which is the bootstrapped gcc-compiled version of 30cc
2. `30cc_gcc` which is the output of gcc-compiled 30cc compiler, compiling the 30cc compiler
1. `30cc_gcc` which is the bootstrapped gcc-compiled version of 30cc
2. `30cc_30cc` which is the output of gcc-compiled 30cc compiler, compiling the 30cc compiler
3. `30cc` which is the output of 30cc-compiled 30cc compiler, compiling the 30cc compiler

Running independent source-files through `make`:
Expand Down
66 changes: 0 additions & 66 deletions build.py

This file was deleted.

0 comments on commit 9a8903d

Please sign in to comment.