From 18cadd33171fb183779323e35b03bc352d5db376 Mon Sep 17 00:00:00 2001 From: Keyvan Kambakhsh Date: Sat, 7 Dec 2024 02:58:30 +0330 Subject: [PATCH] feat: Use Makefile instead of python build script --- .gitignore | 4 +++- Makefile | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 4c441b5..d0196b7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,10 @@ out out.asm output.txt -/target +/target1 +/target2 30cc +30cc_30cc 30cc_gcc # Prerequisites diff --git a/Makefile b/Makefile index 1bbe591..8fdc30d 100644 --- a/Makefile +++ b/Makefile @@ -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