-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
96 lines (77 loc) · 2.29 KB
/
Makefile
File metadata and controls
96 lines (77 loc) · 2.29 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
UNAME := $(shell uname)# doesn't work on Windows, but obviously we don't care
BUILD_DIR := build
OBJ_DIR := $(BUILD_DIR)/obj
BIN_DIR := $(BUILD_DIR)/bin
SCRIPTS_DIR := scripts
INC_DIRS := kernel/include game/include
AS := nasm
# TODO: cross-compiled ld
ifeq ($(UNAME),Darwin)
# GNU ld doesn't exist on macoS
LD := x86_64-elf-ld
else
LD := ld
endif
OBJCOPY := objcopy
DD := dd
ASFLAGS := -felf32 $(addprefix -I,$(INC_DIRS))
LDFLAGS := -nostdlib -melf_i386 -static -n
BSRC_DIRS := \
boot/src
KSRC_DIRS := \
arch/i386 \
game/src \
game/src/renderer \
kernel/src \
kernel/src/drivers/vga
BSRCS := $(shell find $(BSRC_DIRS) -name "*.asm")
BOBJS := $(BSRCS:%=$(OBJ_DIR)/%.o)
KSRCS := $(shell find $(KSRC_DIRS) -name "*.asm")
KOBJS := $(KSRCS:%=$(OBJ_DIR)/%.o)
OBJ_SUBDIRS := \
$(addsuffix /,$(addprefix $(OBJ_DIR)/,$(BSRC_DIRS))) \
$(addsuffix /,$(addprefix $(OBJ_DIR)/,$(KSRC_DIRS)))
# run with DEBUG=1 to use debug configuration
ifeq "$(DEBUG)" "1"
ASFLAGS += -g -Fdwarf -D_DEBUG
else
LDFLAGS += -s
endif
.PHONY: all
all: $(BUILD_DIR)/doom.iso $(OBJ_SUBDIRS)
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)
rm -rf kernel/include/isr_definitions.inc
.PHONY: rebuild
rebuild:
$(MAKE) clean
$(MAKE) all
# assemble unlinked machine code objects
$(OBJ_DIR)/%.asm.o: %.asm kernel/include/isr_definitions.inc | $(OBJ_SUBDIRS)
$(AS) $(ASFLAGS) -o $@ $<
# link bootloader ELF32 binary
$(BIN_DIR)/boot.elf: $(BOBJS) | $(BIN_DIR)
$(LD) $(LDFLAGS) -Tboot/linker.ld -o $@ $^
# translate bootloader from ELF32 to raw/flat binary
$(BIN_DIR)/boot.bin: $(BIN_DIR)/boot.elf
$(OBJCOPY) --gap-fill=0 -Obinary $< $@
# link kernel ELF32 binary
$(BIN_DIR)/kernel.elf: $(KOBJS) | $(BIN_DIR)
$(LD) $(LDFLAGS) -Tkernel/linker.ld -o $@ $^
# translate kernel from ELF32 to raw/flat binary
$(BIN_DIR)/kernel.bin: $(BIN_DIR)/kernel.elf
$(OBJCOPY) --gap-fill=0 -Obinary $< $@
# generate bootable disk image with bootloader and kernel
$(BUILD_DIR)/doom.iso: $(BIN_DIR)/boot.bin $(BIN_DIR)/kernel.bin
$(DD) if=/dev/zero of=$@ bs=512 count=16
$(DD) if=$(BIN_DIR)/boot.bin of=$@ seek=0 count=1 conv=notrunc
$(DD) if=$(BIN_DIR)/kernel.bin of=$@ seek=1 count=15 conv=notrunc
# generate ISR stub definitions file
kernel/include/isr_definitions.inc: $(SCRIPTS_DIR)/isr_gen.sh
./$< &> $@
# director(y/ies)
$(BIN_DIR):
mkdir -p $@
$(OBJ_DIR)/%/: %
mkdir -p $@