-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
137 lines (113 loc) · 4.98 KB
/
Makefile
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# all the modules that are part of the OS
MODULES := kernel initrd
# Kernel CPP Compiler
KernelCPPCompiler := ./compiler/bin/i686-elf-g++
# Kernal Linker
KernalLinker := ./compiler/bin/i686-elf-ld
# global cpp flags
CPPFLAGS_global := -MMD -I includes
# global asm flags
ASMFLAGS_global := -f elf
# global ld flags
LDFLAGS_global :=
# build the release iso
all: boot.iso
# build the debug iso
debug: debug-boot.iso
# build and run the release iso
run: boot.iso
@toolchain-build/bochs-2.6.6/bochs -f bochsconfig -q
# build and run the debug iso
run-debug: debug-boot.iso
@toolchain-build/bochs-2.6.6/bochs -f bochsconfig -q
# build the release iso
boot.iso: bin/release/kernel.bin bin/release/initrd
cd ./bin/release && ./initrd ../../test.txt test.txt
cp bin/release/kernel.bin isofiles/boot/kernel.bin
cp bin/release/initrd.img isofiles/boot/initrd.img
@echo "Generating ISO..."
@genisoimage -R -quiet -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o boot.iso isofiles
# build the debug iso
debug-boot.iso: bin/debug/kernel.bin bin/debug/initrd
cd ./bin/debug && ./initrd ../../test.txt test.txt
cp bin/debug/kernel.bin isofiles/boot/kernel.bin
cp bin/debug/initrd.img isofiles/boot/initrd.img
@echo "Generating ISO..."
@genisoimage -R -quiet -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o boot.iso isofiles
# template to include all the module build files
define MK_template
include $(1)/build.mk
endef
# template to include all the kernel module build rules
define RULES_KERNEL_template
$(1)/obj/debug/%.o: $(1)/src/%.cpp
@mkdir -p ./$(1)/obj/debug
@echo "g++ " $$<
@$$(KernelCPPCompiler) $$(CPPFLAGS_global) $$(CPPFLAGS_$(1)) $$(CPPFLAGS_DEBUG_$(1)) -c $$< -o $$@
$(1)/obj/release/%.o: $(1)/src/%.cpp
@mkdir -p ./$(1)/obj/release
@echo "g++ " $$<
@$$(KernelCPPCompiler) $$(CPPFLAGS_global) $$(CPPFLAGS_$(1)) $$(CPPFLAGS_RELEASE_$(1)) -c $$< -o $$@
$(1)/obj/debug/%.o: $(1)/src/%.asm
@mkdir -p ./$(1)/obj/debug
@echo "nasm " $$<
@nasm $$(ASMFLAGS_global) $(ASMFLAGS_$(1)) $$(ASMFLAGS_DEBUG_$(1)) $$< -o $$@
$(1)/obj/release/%.o: $(1)/src/%.asm
@mkdir -p ./$(1)/obj/release
@echo "nasm " $$<
@nasm $$(ASMFLAGS_global) $(ASMFLAGS_$(1)) $$(ASMFLAGS_RELEASE_$(1)) $$< -o $$@
endef
# template to include all helper module build rules
define RULES_HELPER_template
$(1)/obj/debug/%.o: $(1)/src/%.cpp
@mkdir -p ./$(1)/obj/debug
@echo "g++ " $$<
@g++ $$(CPPFLAGS_global) $$(CPPFLAGS_$(1)) $$(CPPFLAGS_DEBUG_$(1)) -c $$< -o $$@
$(1)/obj/release/%.o: $(1)/src/%.cpp
@mkdir -p ./$(1)/obj/release
@echo "g++ " $$<
@g++ $$(CPPFLAGS_global) $$(CPPFLAGS_$(1)) $$(CPPFLAGS_RELEASE_$(1)) -c $$< -o $$@
endef
# template to include all the program build steps
define PROGRAM_template
DEPENDENCIES := $(DEPENDENCIES) $(patsubst %,$(2)/obj/%.d,$(basename $($(2)_SOURCES)))
bin/debug/$(1): $(patsubst %,$(2)/obj/debug/%.o,$(basename $($(2)_SOURCES))) $(foreach library,$($(2)_LIBRARIES),lib/$(library))
@mkdir -p ./bin/debug
@$$(KernalLinker) $$(LDFLAGS_global) $$(LDFLAGS_$(2)) $$(LDFLAGS_DEBUG_$(2)) $$^ -o $$@
bin/release/$(1): $(patsubst %,$(2)/obj/release/%.o,$(basename $($(2)_SOURCES))) $(foreach library,$($(2)_LIBRARIES),lib/$(library))
@mkdir -p ./bin/release
@$$(KernalLinker) $$(LDFLAGS_global) $$(LDFLAGS_$(2)) $$(LDFLAGS_RELEASE_$(2)) $$^ -o $$@
endef
# template to include all the helper program build steps
define HELPER_PROGRAM_template
DEPENDENCIES := $(DEPENDENCIES) $(patsubst %,$(2)/obj/%.d,$(basename $($(2)_SOURCES)))
bin/debug/$(1): $(patsubst %,$(2)/obj/debug/%.o,$(basename $($(2)_SOURCES))) $(foreach library,$($(2)_LIBRARIES),lib/$(library))
@mkdir -p ./bin/debug
@g++ $$^ -o $$@
bin/release/$(1): $(patsubst %,$(2)/obj/release/%.o,$(basename $($(2)_SOURCES))) $(foreach library,$($(2)_LIBRARIES),lib/$(library))
@mkdir -p ./bin/release
@g++ $$^ -o $$@
endef
# template to add global links to the include files.
define INCLUDE_template
$(shell mkdir -p ./includes)
$(shell ln -s -f -n ../$(1)/include includes/$(1))
endef
# add the templates for each module
$(foreach module,$(MODULES),$(eval include $(module)/build.mk))
$(foreach module,$(MODULES),$(eval $(foreach binary,$($(module)_PROGRAM),$(call RULES_KERNEL_template,$(module)))))
$(foreach module,$(MODULES),$(eval $(foreach binary,$($(module)_HELPER_PROGRAM),$(call RULES_HELPER_template,$(module)))))
$(foreach module,$(MODULES),$(eval $(foreach binary,$($(module)_PROGRAM),$(call PROGRAM_template,$(binary),$(module)))))
$(foreach module,$(MODULES),$(eval $(foreach binary,$($(module)_HELPER_PROGRAM),$(call HELPER_PROGRAM_template,$(binary),$(module)))))
$(foreach module,$(MODULES),$(eval $(call INCLUDE_template,$(module))))
# include all dependencies
-include $(sort $(DEPENDENCIES))
# cleans up all .o files
clean:
-rm $(foreach mod,$(MODULES),$(mod)/obj/release/*.o)
-rm $(foreach mod,$(MODULES),$(mod)/obj/debug/*.o)
# cleans up all files created by the build process
clean-all:
-rm boot.iso
-rm isofiles/boot/kernel.bin
-rm -r $(foreach mod,$(MODULES),$(mod)/obj) includes bin lib