-
Notifications
You must be signed in to change notification settings - Fork 19
/
Makefile
197 lines (148 loc) · 5.18 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
SLASH := /
ON_FPGA :=y
V := @
#GCCPREFIX:=mips-sde-elf-
GCCPREFIX ?= /home/guest/cpu/build-gcc/mips_gcc/bin/mips-sde-elf-
# eliminate default suffix rules
.SUFFIXES: .c .S .h
# define compiler and flags
HOSTCC := gcc
HOSTCFLAGS := -g -Wall -O2
GDB := $(GCCPREFIX)gdb
THUMIPSCC := ./thumips-cc
CLANG := clang
CC :=$(GCCPREFIX)gcc
CFLAGS := -fno-builtin -nostdlib -nostdinc -g -EL -G0 -fno-delayed-branch -Wa,-O0
CTYPE := c S
LD := $(GCCPREFIX)ld
AS := $(GCCPREFIX)as -EL -g -mips32
AR := $(GCCPREFIX)ar
LDFLAGS += -nostdlib
OBJCOPY := $(GCCPREFIX)objcopy
OBJDUMP := $(GCCPREFIX)objdump
COPY := cp
MKDIR := mkdir -p
MV := mv
RM := rm -f
AWK := awk
SED := sed
SH := sh
TR := tr
TOUCH := touch -c
TAR := tar
ZIP := gzip
OBJDIR := obj
BINDIR := bin
SRCDIR := kern
DEPDIR := dep
MODULES := init libs debug driver trap mm sync process schedule syscall fs fs/vfs fs/sfs fs/devs
SRC_DIR := $(addprefix $(SRCDIR)/,$(MODULES))
BUILD_DIR := $(addprefix $(OBJDIR)/,$(MODULES))
DEP_DIR := $(addprefix $(DEPDIR)/,$(MODULES))
VPATH += $(SRC_DIR)
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
OBJ := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRC))
ASMSRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.S))
OBJ += $(patsubst $(SRCDIR)/%.S, $(OBJDIR)/%.o, $(ASMSRC))
INCLUDES := $(addprefix -I,$(SRC_DIR))
INCLUDES += -I$(SRCDIR)/include
ifeq ($(ON_FPGA), y)
USER_APPLIST:= sh #ls
INITRD_BLOCK_CNT:=300
FPGA_LD_FLAGS += -S
MACH_DEF := -DMACH_FPGA
else
USER_APPLIST:= pwd cat sh ls forktest yield hello faultreadkernel faultread badarg waitkill pgdir exit sleep
# 2M
INITRD_BLOCK_CNT:=4000
MACH_DEF := -DMACH_QEMU
endif
USER_SRCDIR := user
USER_OBJDIR := $(OBJDIR)/$(USER_SRCDIR)
USER_LIB_OBJDIR := $(USER_OBJDIR)/libs
USER_INCLUDE := -I$(USER_SRCDIR)/libs
USER_APP_BINS:= $(addprefix $(USER_OBJDIR)/, $(USER_APPLIST))
USER_LIB_SRCDIR := $(USER_SRCDIR)/libs
USER_LIB_SRC := $(foreach sdir,$(USER_LIB_SRCDIR),$(wildcard $(sdir)/*.c))
USER_LIB_OBJ := $(patsubst $(USER_LIB_SRCDIR)/%.c, $(USER_LIB_OBJDIR)/%.o, $(USER_LIB_SRC))
USER_LIB_OBJ += $(USER_LIB_OBJDIR)/initcode.o
USER_LIB := $(USER_OBJDIR)/libuser.a
BUILD_DIR += $(USER_LIB_OBJDIR)
BUILD_DIR += $(USER_OBJDIR)
DEPENDS := $(patsubst $(SRCDIR)/%.c, $(DEPDIR)/%.d, $(SRC))
MAKEDEPEND = $(CLANG) -M $(CFLAGS) $(INCLUDES) -o $(DEPDIR)/$*.d $<
#vpath %.c $(SRC_DIR)
#vpath %.S $(SRC_DIR)
.PHONY: all checkdirs clean
all: checkdirs boot/loader.bin obj/ucore-kernel-initrd
$(shell mkdir -p $(DEP_DIR))
obj/ucore-kernel: $(OBJ) tools/kernel.ld
@echo LINK $@
$(LD) -nostdlib -n -G 0 -static -T tools/kernel.ld $(OBJ) -o $@
obj/ucore-kernel-piggy: $(BUILD_DIR) $(OBJ) $(USER_APP_BINS) tools/kernel.ld
@echo LINK $@
$(LD) -nostdlib -n -G 0 -static -T tools/kernel.ld $(OBJ) \
$(addsuffix .piggy.o, $(USER_APP_BINS)) -o $@
$(DEPDIR)/%.d: $(SRCDIR)/%.c
@echo DEP $<
@set -e; rm -f $@; \
$(CC) -MM -MT "$(OBJDIR)/$*.o $@" $(CFLAGS) $(INCLUDES) $< > $@;
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) -c -mips1 $(INCLUDES) $(CFLAGS) $(MACH_DEF) $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.S
$(CC) -mips32 -c -D__ASSEMBLY__ $(MACH_DEF) $(INCLUDES) -g -EL -G0 $< -o $@
checkdirs: $(BUILD_DIR) $(DEP_DIR)
$(BUILD_DIR):
@mkdir -p $@
$(DEP_DIR):
@mkdir -p $@
clean:
-rm -rf $(BUILD_DIR)
-rm -rf $(DEPDIR)
-rm -rf boot/loader.o boot/loader boot/loader.bin
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPENDS)
endif
#user lib
$(USER_LIB): $(BUILD_DIR) $(USER_LIB_OBJ)
@echo "Building USERLIB"
$(AR) rcs $@ $(USER_LIB_OBJ)
#user applications
define make-user-app
$1: $(BUILD_DIR) $(addsuffix .o,$1) $(USER_LIB)
@echo LINK $$@
$(LD) $(FPGA_LD_FLAGS) -T $(USER_LIB_SRCDIR)/user.ld $(addsuffix .o,$1) $(USER_LIB) -o $$@
$(SED) 's/$$$$FILE/$(notdir $1)/g' tools/piggy.S.in > $(USER_OBJDIR)/piggy.S
$(AS) $(USER_OBJDIR)/piggy.S -o $$@.piggy.o
endef
$(foreach bdir,$(USER_APP_BINS),$(eval $(call make-user-app,$(bdir))))
$(USER_OBJDIR)/%.o: $(USER_SRCDIR)/%.c
$(CC) -c -mips1 $(USER_INCLUDE) -I$(SRCDIR)/include $(CFLAGS) $< -o $@
$(USER_OBJDIR)/%.o: $(USER_SRCDIR)/%.S
$(CC) -mips32 -c -D__ASSEMBLY__ $(USER_INCLUDE) -I$(SRCDIR)/include -g -EL -G0 $< -o $@
# filesystem
TOOL_MKSFS := tools/mksfs
ROOTFS_DIR:= $(USER_OBJDIR)/rootfs
ROOTFS_IMG:= $(USER_OBJDIR)/initrd.img
$(TOOL_MKSFS): tools/mksfs.c
$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
$(OBJDIR)/ucore-kernel-initrd: $(BUILD_DIR) $(TOOL_MKSFS) $(OBJ) $(USER_APP_BINS) tools/kernel.ld
rm -rf $(ROOTFS_DIR) $(ROOTFS_IMG)
mkdir $(ROOTFS_DIR)
cp $(USER_APP_BINS) $(ROOTFS_DIR)
cp -r $(USER_SRCDIR)/_archive/* $(ROOTFS_DIR)/
dd if=/dev/zero of=$(ROOTFS_IMG) count=$(INITRD_BLOCK_CNT)
#touch $(ROOTFS_IMG)
$(TOOL_MKSFS) $(ROOTFS_IMG) $(ROOTFS_DIR)
$(SED) 's%_FILE_%$(ROOTFS_IMG)%g' tools/initrd_piggy.S.in > $(USER_OBJDIR)/initrd_piggy.S
$(AS) $(USER_OBJDIR)/initrd_piggy.S -o $(USER_OBJDIR)/initrd.img.o
@echo LINK $@
$(LD) $(FPGA_LD_FLAGS) -nostdlib -n -G 0 -static -T tools/kernel.ld $(OBJ) \
$(USER_OBJDIR)/initrd.img.o -o $@
rm -rf $(ROOTFS_DIR)
boot/loader.bin: boot/bootasm.S
$(CC) $(CFLAGS) -g -c -o boot/loader.o $^
$(LD) -EL -n -G0 -Ttext 0xbfc00000 -o boot/loader boot/loader.o
$(OBJCOPY) -O binary -S boot/loader $@