-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakefile
171 lines (137 loc) · 4.87 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
CC :=$(if $(V), gcc, @gcc)
ASM :=$(if $(V), nasm, @nasm)
LD :=$(if $(V), ld, @ld)
RM :=rm -f
MKDIR :=$(if $(V), mkdir, @mkdir)
PERL :=$(if $(V), perl, @perl)
out :=bin
base :=src
inc :=$(base)/inc
script :=script
script_module_decl :=$(script)/generate_declaration.pl
script_external :=$(script)/generate_external.pl
external :=$(inc)/external.h
CFLAG =-nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Wall -Wextra -Werror -c
LD_SCRIPT =link.ld
LFLAG =-T$(script)/$(LD_SCRIPT) -m elf_i386
ASMFLAG =-felf
src_c :=
src_asm :=
obj_c =$(subst .c,.o, $(src_c))
obj_asm =$(subst .s,.o, $(src_asm))
dep_c =$(subst .c,.d, $(src_c))
dep_asm =$(subst .s,.d, $(src_asm))
obj_partial =$(filter-out %main.o, $(obj_c))
decl =$(subst .o,_declaration.h, $(obj_partial))
obj =$(obj_asm) $(obj_c)
dep =$(dep_c) $(dep_asm)
#
# Make sure boot.o will locate at first 8KB of elf, grub will search multi-boot
# magic number in that range.
#
obj_tmp =$(obj_c) $(obj_asm)
obj_boot =$(filter %/boot.o, $(obj_tmp))
obj =$(obj_boot) $(shell echo $(obj_tmp) | sed -e 's:$(obj_boot)::')
vpath %.h $(inc)
all:
include src/makefile.mk
include src/boot/makefile.mk
include src/boot/descriptor/makefile.mk
include src/boot/multiboot/makefile.mk
include src/bus/makefile.mk
include src/common/makefile.mk
include src/common/print/makefile.mk
include src/data_struct/makefile.mk
include src/block/makefile.mk
include src/block/disk/makefile.mk
include src/block/disk/ata/makefile.mk
include src/interrupt/makefile.mk
include src/interrupt/isr/makefile.mk
include src/interrupt/isr/irq/makefile.mk
include src/memory/makefile.mk
include src/screen/makefile.mk
include src/test/makefile.mk
include src/vfs/makefile.mk
include src/vfs/fs_initrd/makefile.mk
include src/vfs/fs_ext2/makefile.mk
-include $(dep)
CF_DEBUG :=-g
CF_RELEASE :=-O3
CFLAG +=$(addprefix -I,$(inc))
CFLAG +=$(if $(RELEASE),$(CF_RELEASE),$(CF_DEBUG))
ASMFLAG +=$(if $(RELEASE),$(CF_RELEASE),$(CF_DEBUG))
ASMFLAG +=$(addprefix -I,$(dir $@))
.PHONY: all help clean initrd
TARGET :=$(out)/kernel
TARGET_DEP :=$(dep) $(external) $(decl)
all: $(out) $(TARGET_DEP) $(TARGET)
$(out):
@echo " MakeDir $@"
$(MKDIR) $@
$(external):$(decl)
@echo " Generate $(notdir $@)"
$(PERL) $(script_external) $(decl)
$(decl):
@echo " Generate $(notdir $@)"
$(PERL) $(script_module_decl) $@ $^
$(dep_c):%.d:%.c
@echo " Depend $(notdir $@)"
$(CC) -M -MT '$(basename $<).o $(basename $<).d' $(CFLAG) $< \
| sed -e 's: $(external)::' \
| sed -e 's: $(basename $<)_declaration.h::' > $@
$(if $(filter %main.c, $<),,$(append_decl_depend))
$(dep_asm):%.d:%.s
@echo " Depend $(notdir $@)"
$(ASM) -M -MT '$(basename $<).o $(basename $<).d' $(ASMFLAG) $< > $@
$(TARGET):$(obj)
@echo " Link $(notdir $@)"
$(LD) $(LFLAG) $^ -o $@
@ctags -R $(base)
$(obj_c):%.o:%.c
@echo " Compile $(notdir $<)"
$(CC) $(CFLAG) $< -o $@
$(obj_asm):%.o:%.s
@echo " Assembly $(notdir $<)"
$(ASM) $(ASMFLAG) $< -o $@
help:
@echo
@echo " make :Default debug build"
@echo " make RELEASE=1 :Release build"
@echo " make V=1 :Verbose build"
@echo " make initrd :initrd.bin build"
@echo
## define list ##
define append_decl_depend
@echo "$(basename $<)_declaration.h: $(basename $<).d $(addprefix $(dir @),$(shell ls $(dir $@)*.c ))" >> $@
endef
## Initrd ##
INITRD_TARGET :=$(out)/initrd.elf
INITRD_CC_FLAG =-m32 -Wall -Wextra -Werror -c
INITRD_CC_FLAG +=$(if $(RELEASE),-o2,-g) $(addprefix -I,$(inc))
INITRD_LD_FLAG =-m32
INITRD_LD_FLAG +=-m32 $(if $(RELEASE),-o2,-g)
initrd_src =$(base)/initrd/initrd.c
initrd_obj =$(subst .c,.o, $(initrd_src))
initrd_dep =$(subst .c,.d, $(initrd_src))
initrd_decl =$(subst .o,_declaration.h, $(initrd_obj))
-include $(initrd_dep)
initrd: $(initrd_decl) $(INITRD_TARGET)
$(initrd_decl):
@echo " Generate $(notdir $@)"
$(PERL) $(script_module_decl) $@ $^
$(initrd_dep):%.d:%.c
@echo " Depend $(notdir $@)"
$(CC) -M -MT '$(basename $<).o $(basename $<).d' $(INITRD_CC_FLAG) $< \
| sed -e 's: $(basename $<)_declaration.h::' > $@
$(append_decl_depend)
$(INITRD_TARGET):$(initrd_obj)
@echo " Link $(notdir $@)"
$(CC) $(INITRD_LD_FLAG) $< -o $@
$(initrd_obj):$(initrd_src)
@echo " Compile $(notdir $<)"
$(CC) $(INITRD_CC_FLAG) $< -o $@
clean:
@echo " Clean Object"
@$(RM) $(obj) $(initrd_obj)
@echo " Clean Depend"
@$(RM) $(dep) $(initrd_dep)