-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
98 lines (71 loc) · 2.72 KB
/
Copy pathMakefile
File metadata and controls
98 lines (71 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
NASM = nasm
OBJCOPY = objcopy
TEST_CC = $(CC)
TEST_LD = $(CC)
BASE_NASMFLAGS = -f elf
NASMDEPFLAGS = -MT $@ -MD -MP -MF $(@:.o=.d)
BASE_CFLAGS = -m32 -march=i486 -mtune=i686 -ffreestanding -fno-pic -fno-stack-protector -fno-asynchronous-unwind-tables -Wall -Wextra -Werror -Wconversion
TEST_BASE_CFLAGS = -m32 -g -Wall -Wextra -Werror -Wconversion
CDEPFLAGS = -MT $@ -MMD -MP -MF $(@:.o=.d)
BASE_LDFLAGS = -m32 -nostdlib -lgcc -no-pie -T linker.ld -L$(BUILD) -Wl,-Map=$(BUILD)/usbloader.map,--no-warn-rwx-segments,--build-id=none
TEST_BASE_LDFLAGS = -m32 -g -L$(BUILD)
SANITIZER_FLAGS := -fsanitize=address,pointer-compare,pointer-subtract,leak,undefined -fno-sanitize=alignment
BUILD := build
TARGET := $(BUILD)/usbloader.bin
TARGET_IMG := $(BUILD)/usbloader.img
TEST_TARGETS :=
ifeq ($(TEST_SAN), true)
TEST_BASE_CFLAGS += $(SANITIZER_FLAGS)
TEST_BASE_LDFLAGS += $(SANITIZER_FLAGS)
endif
# Create new test target
# Parameters
# 1: target name
# 2: souce file list
define test_target
TEST_BUILD_$(1) := $$(BUILD)/$(1).build
TEST_SRCS_$(1) := $(2)
TEST_OBJS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$$(TEST_BUILD_$(1))/$$(basename $$(f)).o)
TEST_DEPS_$(1) := $$(foreach f,$$(TEST_SRCS_$(1)),$$(TEST_BUILD_$(1))/$$(basename $$(f)).d)
TEST_TARGETS += $(1)
$(1): $$(BUILD)/$(1)
$$(BUILD)/$(1): $$(TEST_OBJS_$(1))
$$(TEST_LD) $$(TEST_BASE_LDFLAGS) $$(TEST_LDFLAGS) -o $$@ $$(TEST_OBJS_$(1))
$$(TEST_BUILD_$(1))/%.o: %.c $$(TEST_BUILD_$(1))/%.d
@mkdir -p $$(@D)
$$(TEST_CC) $$(TEST_BASE_CFLAGS) -I. $$(TEST_CFLAGS) $$(CDEPFLAGS) -c $$< -o $$@
$$(TEST_DEPS_$(1)):
include $$(wildcard $$(TEST_DEPS_$(1)))
endef
all: usbloader usbloader.img tests
usbloader: $(TARGET)
usbloader.img: $(TARGET_IMG)
$(TARGET_IMG): $(TARGET)
dd if=/dev/zero of=$(TARGET_IMG) bs=512 count=2880
dd if=$(TARGET) of=$(TARGET_IMG) bs=512 conv=notrunc
$(BUILD)/%.o: %.asm $(BUILD)/%.d
# NASM produces a dep (.d) file that is newer than the .o
# and that causes unnecessary reassembly every time.
# `touch`-ing the result after assembly to update the modification time
@mkdir -p $(@D)
$(NASM) $(BASE_NASMFLAGS) -I$(<D) $(NASMFLAGS) $(NASMDEPFLAGS) $< -o $@
touch $@
$(BUILD)/%.o: %.c $(BUILD)/%.d
@mkdir -p $(@D)
$(CC) $(BASE_CFLAGS) -I. $(CFLAGS) $(CDEPFLAGS) -c $< -o $@
clean:
rm -rf $(BUILD)
include arch/module.mk
include boot/module.mk
include drivers/module.mk
include mem/module.mk
include utils/module.mk
OBJS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(f)).o)
DEPS := $(foreach f,$(SRCS), $(BUILD)/$(basename $(f)).d)
$(TARGET): $(OBJS)
$(CC) $^ $(BASE_LDFLAGS) $(LDFLAGS) -o $(BUILD)/usbloader.elf
$(OBJCOPY) -O binary $(BUILD)/usbloader.elf $@
$(DEPS):
include $(wildcard $(DEPS))
tests: $(TEST_TARGETS)
.PHONY: all clean usbloader usbloader.img tests