@@ -17,10 +17,8 @@ TEST_BASE_LDFLAGS = -m32 -g -L$(BUILD)
1717SANITIZER_FLAGS := -fsanitize=address,pointer-compare,pointer-subtract,leak,undefined -fno-sanitize=alignment
1818
1919BUILD := build
20- SRCS := stage1.asm stage2_entry.asm stage2.c print.c pci21.c pit.c mem.c serial.c gdbstub.c i386-stub.c idt.c uhci.c
21- OBJS := $(foreach f,$(SRCS ) , $(BUILD ) /$(basename $(notdir $(f ) ) ) .o)
22- DEPS := $(foreach f,$(SRCS ) , $(BUILD ) /$(basename $(notdir $(f ) ) ) .d)
2320TARGET := $(BUILD ) /usbloader.bin
21+ TARGET_IMG := $(BUILD ) /usbloader.img
2422TEST_TARGETS :=
2523
2624ifeq ($(TEST_SAN ) , true)
3331# 1: target name
3432# 2: souce file list
3533define test_target
34+ TEST_BUILD_$(1 ) := $$(BUILD ) /$(1 ) .build
3635TEST_SRCS_$(1 ) := $(2 )
37- TEST_OBJS_$(1 ) := $$(foreach f,$$(TEST_SRCS_$(1 ) ) ,$( BUILD ) /$$(basename $$(notdir $( 1 ) . $$( f ) ) ) .o)
38- TEST_DEPS_$(1 ) := $$(foreach f,$$(TEST_SRCS_$(1 ) ) ,$( BUILD ) /$$(basename $$(notdir $( 1 ) . $$( f ) ) ) .d)
36+ TEST_OBJS_$(1 ) := $$(foreach f,$$(TEST_SRCS_$(1 ) ) ,$$( TEST_BUILD_ $( 1 ) ) /$$(basename $$(f ) ) .o)
37+ TEST_DEPS_$(1 ) := $$(foreach f,$$(TEST_SRCS_$(1 ) ) ,$$( TEST_BUILD_ $( 1 ) ) /$$(basename $$(f ) ) .d)
3938
4039TEST_TARGETS += $(1 )
4140
@@ -44,8 +43,9 @@ $(1): $$(BUILD)/$(1)
4443$$(BUILD ) /$(1 ) : $$(TEST_OBJS_$(1 ) )
4544 $$(TEST_LD ) $$(TEST_BASE_LDFLAGS ) $$(TEST_LDFLAGS ) -o $$@ $$(TEST_OBJS_$(1 ) )
4645
47- $$(BUILD ) /$(1 ) .%.o: %.c $$(BUILD ) /$(1 ) .%.d | $$(BUILD )
48- $$(TEST_CC ) $$(TEST_BASE_CFLAGS ) $$(TEST_CFLAGS ) $$(CDEPFLAGS ) -c $$< -o $$@
46+ $$(TEST_BUILD_$(1 ) ) /%.o: %.c $$(TEST_BUILD_$(1 ) ) /%.d
47+ @mkdir -p $$(@D )
48+ $$(TEST_CC ) $$(TEST_BASE_CFLAGS ) -I. $$(TEST_CFLAGS ) $$(CDEPFLAGS ) -c $$< -o $$@
4949
5050$$(TEST_DEPS_$(1 ) ) :
5151
@@ -56,36 +56,43 @@ all: usbloader usbloader.img tests
5656
5757usbloader : $(TARGET )
5858
59- usbloader.img : $(TARGET )
60- dd if=/dev/zero of=$(BUILD ) /$@ bs=512 count=2880
61- dd if=$(TARGET ) of=$(BUILD ) /$@ bs=512 conv=notrunc
59+ usbloader.img : $(TARGET_IMG )
6260
63- $(TARGET ) : $(OBJS )
64- $( CC ) $( OBJS ) $( BASE_LDFLAGS ) $( LDFLAGS ) -o $( BUILD ) /usbloader.elf
65- $( OBJCOPY ) -O binary $( BUILD ) /usbloader.elf $@
61+ $(TARGET_IMG ) : $(TARGET )
62+ dd if=/dev/zero of= $( TARGET_IMG ) bs=512 count=2880
63+ dd if= $( TARGET ) of= $( TARGET_IMG ) bs=512 conv=notrunc
6664
67- $(BUILD ) /% .o : % .asm $(BUILD ) /% .d | $( BUILD )
65+ $(BUILD ) /% .o : % .asm $(BUILD ) /% .d
6866# NASM produces a dep (.d) file that is newer than the .o
6967# and that causes unnecessary reassembly every time.
7068# `touch`-ing the result after assembly to update the modification time
71- $(NASM) $(BASE_NASMFLAGS) $(NASMFLAGS) $(NASMDEPFLAGS) $< -o $@
69+ @mkdir -p $(@D)
70+ $(NASM) $(BASE_NASMFLAGS) -I$(<D) $(NASMFLAGS) $(NASMDEPFLAGS) $< -o $@
7271 touch $@
7372
74- $(BUILD ) /% .o : % .c $(BUILD ) /% .d | $(BUILD )
75- $(CC ) $(BASE_CFLAGS ) $(CFLAGS ) $(CDEPFLAGS ) -c $< -o $@
76-
77- $(BUILD ) :
78- mkdir -p $@
73+ $(BUILD ) /% .o : % .c $(BUILD ) /% .d
74+ @mkdir -p $(@D )
75+ $(CC ) $(BASE_CFLAGS ) -I. $(CFLAGS ) $(CDEPFLAGS ) -c $< -o $@
7976
8077clean :
8178 rm -rf $(BUILD )
8279
83- $(DEPS ) :
80+ include arch/module.mk
81+ include boot/module.mk
82+ include drivers/module.mk
83+ include mem/module.mk
84+ include utils/module.mk
8485
85- include $(wildcard $(DEPS ) )
86+ OBJS := $(foreach f,$(SRCS ) , $(BUILD ) /$(basename $(f ) ) .o)
87+ DEPS := $(foreach f,$(SRCS ) , $(BUILD ) /$(basename $(f ) ) .d)
8688
87- $(eval $(call test_target,test_mem,unity.c mem_test.c mem.c))
89+ $(TARGET ) : $(OBJS )
90+ $(CC ) $^ $(BASE_LDFLAGS ) $(LDFLAGS ) -o $(BUILD ) /usbloader.elf
91+ $(OBJCOPY ) -O binary $(BUILD ) /usbloader.elf $@
92+
93+ $(DEPS ) :
94+ include $(wildcard $(DEPS ) )
8895
8996tests : $(TEST_TARGETS )
9097
91- .PHONY : all clean
98+ .PHONY : all clean usbloader usbloader.img tests
0 commit comments