Skip to content

Commit f884148

Browse files
committed
Reorganize project structure
- "3rdparty/": location for submodules - "include/": location for headers - "build/": location for intermediate files and binaries - "src/": location for source codes
1 parent 43eef97 commit f884148

32 files changed

+158
-89
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
*.o
2-
*.o.d
3-
*.sw*
4-
semu
1+
build/
52

63
# Linux-specific
7-
*.dtb
84
Image
95
ext4.img
106
rootfs.cpio

.gitmodules

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
[submodule "mini-gdbstub"]
2-
path = mini-gdbstub
3-
url = https://github.com/RinHizakura/mini-gdbstub
4-
shallow = true
5-
[submodule "minislirp"]
6-
path = minislirp
1+
[submodule "3rdparty/mini-gdbstub"]
2+
path = 3rdparty/mini-gdbstub
3+
url = https://github.com/RinHizakura/mini-gdbstub
4+
shallow = true
5+
[submodule "3rdparty/minislirp"]
6+
path = 3rdparty/minislirp
77
url = https://github.com/edubart/minislirp
8-
shallow = true
9-
[submodule "portaudio"]
10-
path = portaudio
11-
url = https://github.com/PortAudio/portaudio
12-
shallow = true
8+
shallow = true
9+
[submodule "3rdparty/portaudio"]
10+
path = 3rdparty/portaudio
11+
url = https://github.com/PortAudio/portaudio
12+
shallow = true

3rdparty/mini-gdbstub

Submodule mini-gdbstub added at 39020f2

3rdparty/portaudio

Submodule portaudio added at c3f922f

Makefile

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
include mk/common.mk
22
include mk/check-libs.mk
33

4+
THIRDPARTY_DIR := 3rdparty
5+
INCLUDE_DIR := include
6+
BUILD_DIR := build
7+
SRC_DIR := src
8+
49
CC ?= gcc
510
CFLAGS := -O2 -g -Wall -Wextra
6-
CFLAGS += -include common.h
11+
CFLAGS += -I$(INCLUDE_DIR) -I$(THIRDPARTY_DIR) -include $(INCLUDE_DIR)/common.h
712

813
# clock frequency
914
CLOCK_FREQ ?= 65000000
1015
DT_CFLAGS := -D CLOCK_FREQ=$(CLOCK_FREQ)
1116
CFLAGS += $(DT_CFLAGS)
1217

1318
OBJS_EXTRA :=
19+
LIBS_EXTRA :=
20+
1421
# command line option
1522
OPTS :=
1623

1724
LDFLAGS :=
1825

26+
SHELL_HACK := $(shell mkdir -p $(BUILD_DIR))
27+
1928
# virtio-blk
2029
ENABLE_VIRTIOBLK ?= 1
2130
$(call set-feature, VIRTIOBLK)
@@ -24,7 +33,7 @@ MKFS_EXT4 ?= mkfs.ext4
2433
ifeq ($(call has, VIRTIOBLK), 1)
2534
OBJS_EXTRA += virtio-blk.o
2635
DISKIMG_FILE := ext4.img
27-
OPTS += -d $(DISKIMG_FILE)
36+
OPTS += -d $(BUILD_DIR)/$(DISKIMG_FILE)
2837
MKFS_EXT4 := $(shell which $(MKFS_EXT4))
2938
ifndef MKFS_EXT4
3039
MKFS_EXT4 := $(shell which $$(brew --prefix e2fsprogs)/sbin/mkfs.ext4)
@@ -77,8 +86,9 @@ $(call set-feature, VIRTIOSND)
7786
ifeq ($(call has, VIRTIOSND), 1)
7887
OBJS_EXTRA += virtio-snd.o
7988

80-
PA_LIB := portaudio/lib/.libs/libportaudio.a
81-
PA_CFLAGS := -Iportaudio/include
89+
PORTAUDIO_DIR := $(THIRDPARTY_DIR)/portaudio
90+
PA_LIB := $(PORTAUDIO_DIR)/lib/.libs/libportaudio.a
91+
PA_CFLAGS := -I$(PORTAUDIO_DIR)/include
8292
PA_CONFIG_PARAMS :=
8393
LDFLAGS += $(PA_LIB)
8494
CFLAGS += $(PA_CFLAGS)
@@ -105,9 +115,10 @@ ifeq ($(call has, VIRTIOSND), 1)
105115
# -lm separately.
106116
LDFLAGS += -lpthread
107117

108-
portaudio/Makefile:
109-
git submodule update --init portaudio
110-
$(PA_LIB): portaudio/Makefile
118+
$(PORTAUDIO_DIR)/Makefile:
119+
git submodule update --init $(PORTAUDIO_DIR)
120+
121+
$(PA_LIB): $(PORTAUDIO_DIR)/Makefile
111122
cd $(dir $<) && git clean -fdx && git reset --hard HEAD
112123
cd $(dir $<) && ./configure \
113124
--enable-static \
@@ -119,10 +130,10 @@ $(PA_LIB): portaudio/Makefile
119130
--disable-dependency-tracking \
120131
$(PA_CONFIG_PARAMS)
121132
$(MAKE) -C $(dir $<)
122-
main.o: $(PA_LIB)
123-
virtio-snd.o: $(PA_LIB)
133+
$(BUILD_DIR)/main.o: $(PA_LIB)
134+
$(BUILD_DIR)/virtio-snd.o: $(PA_LIB)
124135
# suppress warning when compiling PortAudio
125-
virtio-snd.o: CFLAGS += -Wno-unused-parameter
136+
$(BUILD_DIR)/virtio-snd.o: CFLAGS += -Wno-unused-parameter
126137
endif
127138

128139
# Set libm as the last dependency so that no need to set -lm seperately.
@@ -132,8 +143,9 @@ LDFLAGS += -lm
132143
# after git submodule.
133144
.DEFAULT_GOAL := all
134145

135-
BIN = semu
136-
all: $(BIN) minimal.dtb
146+
BIN = $(BUILD_DIR)/semu
147+
148+
all: $(BIN) $(BUILD_DIR)/minimal.dtb
137149

138150
OBJS := \
139151
riscv.o \
@@ -145,34 +157,36 @@ OBJS := \
145157
aclint.o \
146158
$(OBJS_EXTRA)
147159

148-
deps := $(OBJS:%.o=.%.o.d)
160+
objs := $(foreach obj,$(OBJS),$(BUILD_DIR)/$(obj))
161+
deps := $(patsubst %.o,$(BUILD_DIR)/%.o.d,$(OBJS))
149162

150-
GDBSTUB_LIB := mini-gdbstub/build/libgdbstub.a
151-
LDFLAGS += $(GDBSTUB_LIB)
152-
mini-gdbstub/Makefile:
163+
MINI_GDBSTUB_DIR := $(THIRDPARTY_DIR)/mini-gdbstub
164+
MINI_GDBSTUB_LIB := $(MINI_GDBSTUB_DIR)/build/libgdbstub.a
165+
LDFLAGS += $(MINI_GDBSTUB_LIB)
166+
$(THIRDPARTY_DIR)/mini-gdbstub/Makefile:
153167
git submodule update --init $(dir $@)
154-
$(GDBSTUB_LIB): mini-gdbstub/Makefile
168+
$(MINI_GDBSTUB_LIB): $(MINI_GDBSTUB_DIR)/Makefile
155169
$(MAKE) -C $(dir $<)
156-
$(OBJS): $(GDBSTUB_LIB)
170+
LIBS_EXTRA += $(MINI_GDBSTUB_LIB)
157171

158172
ifeq ($(call has, VIRTIONET), 1)
159-
MINISLIRP_DIR := minislirp
160-
MINISLIRP_LIB := minislirp/src/libslirp.a
173+
MINISLIRP_DIR := $(THIRDPARTY_DIR)/minislirp
174+
MINISLIRP_LIB := $(MINISLIRP_DIR)/src/libslirp.a
161175
LDFLAGS += $(MINISLIRP_LIB)
162176
$(MINISLIRP_DIR)/src/Makefile:
163177
git submodule update --init $(MINISLIRP_DIR)
164178
$(MINISLIRP_LIB): $(MINISLIRP_DIR)/src/Makefile
165179
$(MAKE) -C $(dir $<)
166-
$(OBJS): $(MINISLIRP_LIB)
180+
LIBS_EXTRA += $(MINISLIRP_LIB)
167181
endif
168182

169-
$(BIN): $(OBJS)
183+
$(BIN): $(objs)
170184
$(VECHO) " LD\t$@\n"
171185
$(Q)$(CC) -o $@ $^ $(LDFLAGS)
172186

173-
%.o: %.c
187+
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(LIBS_EXTRA)
174188
$(VECHO) " CC\t$@\n"
175-
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF .$@.d $<
189+
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF $@.d $<
176190

177191
DTC ?= dtc
178192

@@ -196,40 +210,42 @@ S := $E $E
196210
CFLAGS += -D SEMU_BOOT_TARGET_TIME=10
197211

198212
SMP ?= 1
199-
.PHONY: riscv-harts.dtsi
200-
riscv-harts.dtsi:
213+
.PHONY: $(INCLUDE_DIR)/riscv-harts.dtsi
214+
$(INCLUDE_DIR)/riscv-harts.dtsi:
201215
$(Q)python3 scripts/gen-hart-dts.py $@ $(SMP) $(CLOCK_FREQ)
202216

203-
minimal.dtb: minimal.dts riscv-harts.dtsi
217+
$(BUILD_DIR)/minimal.dtb: $(SRC_DIR)/minimal.dts $(INCLUDE_DIR)/riscv-harts.dtsi
204218
$(VECHO) " DTC\t$@\n"
205219
$(Q)$(CC) -nostdinc -E -P -x assembler-with-cpp -undef \
220+
-I$(INCLUDE_DIR) \
206221
$(DT_CFLAGS) \
207222
$(subst ^,$S,$(filter -D^SEMU_FEATURE_%, $(subst -D$(S)SEMU_FEATURE,-D^SEMU_FEATURE,$(CFLAGS)))) $< \
208223
| $(DTC) - > $@
209224

210-
# Rules for downloading prebuilt Linux kernel image
211-
include mk/external.mk
212-
213-
ext4.img:
225+
$(BUILD_DIR)/$(DISKIMG_FILE):
214226
$(Q)dd if=/dev/zero of=$@ bs=4k count=600
215227
$(Q)$(MKFS_EXT4) -F $@
216228

217-
check: $(BIN) minimal.dtb $(KERNEL_DATA) $(INITRD_DATA) $(DISKIMG_FILE)
229+
.PHONY: download-artifacts
230+
download-artifacts:
231+
$(Q)scripts/download-artifacts.sh Image $(BUILD_DIR)
232+
$(Q)scripts/download-artifacts.sh rootfs $(BUILD_DIR)
233+
234+
check: $(BIN) $(BUILD_DIR)/minimal.dtb $(BUILD_DIR)/$(DISKIMG_FILE) download-artifacts
218235
@$(call notice, Ready to launch Linux kernel. Please be patient.)
219-
$(Q)./$(BIN) -k $(KERNEL_DATA) -c $(SMP) -b minimal.dtb -i $(INITRD_DATA) -n $(NETDEV) $(OPTS)
236+
$(Q)$(BIN) -k $(BUILD_DIR)/Image -c $(SMP) -b $(BUILD_DIR)/minimal.dtb -i $(BUILD_DIR)/rootfs.cpio -n $(NETDEV) $(OPTS)
220237

221238
build-image:
222239
scripts/build-image.sh
223240

224241
clean:
225-
$(Q)$(RM) $(BIN) $(OBJS) $(deps)
226-
$(Q)$(MAKE) -C mini-gdbstub clean
227-
$(Q)$(MAKE) -C minislirp/src clean
242+
$(Q)$(RM) $(BIN) $(objs) $(deps)
243+
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/mini-gdbstub clean
244+
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/minislirp/src clean
245+
$(Q)$(MAKE) -C $(THIRDPARTY_DIR)/portaudio clean
228246

229247
distclean: clean
230-
$(Q)$(RM) riscv-harts.dtsi
231-
$(Q)$(RM) minimal.dtb
232-
$(Q)$(RM) Image rootfs.cpio
233-
$(Q)$(RM) ext4.img
248+
$(Q)$(RM) $(INCLUDE_DIR)/riscv-harts.dtsi
249+
$(Q)$(RM) $(BUILD_DIR)/*
234250

235251
-include $(deps)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)