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 18cadd3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 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

0 comments on commit 18cadd3

Please sign in to comment.