Skip to content

Commit 25571f7

Browse files
committed
Modularize the build system
This patch utilizes GNU make facilities to modularize the whole build system, and it is more convenient for maintenance. No standalone bash scripts are required.
1 parent 3bf1d49 commit 25571f7

File tree

6 files changed

+107
-139
lines changed

6 files changed

+107
-139
lines changed

Makefile

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include mk/common.mk
2+
13
CC ?= gcc
24
CFLAGS = -O2
35
CFLAGS += -Wall -std=gnu99
@@ -9,16 +11,15 @@ BIN = $(OUT)/kvm-host
911

1012
all: $(BIN)
1113

12-
# Control the build verbosity
13-
ifeq ("$(VERBOSE)","1")
14-
Q :=
15-
VECHO = @true
16-
else
17-
Q := @
18-
VECHO = @printf
19-
endif
20-
21-
OBJS := vm.o serial.o main.o pci.o virtio-pci.o virtq.o virtio-blk.o diskimg.o
14+
OBJS := \
15+
vm.o \
16+
serial.o \
17+
pci.o \
18+
virtio-pci.o \
19+
virtq.o \
20+
virtio-blk.o \
21+
diskimg.o \
22+
main.o
2223
OBJS := $(addprefix $(OUT)/,$(OBJS))
2324
deps := $(OBJS:%.o=%.o.d)
2425

@@ -31,17 +32,12 @@ $(OUT)/%.o: src/%.c
3132
$(VECHO) " CC\t$@\n"
3233
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF $@.d $<
3334

34-
$(OUT)/bzImage:
35-
$(Q)scripts/build-linux.sh
36-
37-
$(OUT)/rootfs.cpio:
38-
$(Q)scripts/build-rootfs.sh
39-
40-
rootfs: $(OUT)/rootfs.cpio
35+
# Rules for downloading and building the minimal Linux system
36+
include mk/external.mk
4137

42-
check: $(BIN) $(OUT)/bzImage $(OUT)/rootfs.cpio
43-
$(VECHO) "\nOnce the message 'Kernel panic' appears, press ctrl-c to exit\n"
44-
$(Q)sudo $(BIN) -k $(OUT)/bzImage -i $(OUT)/rootfs.cpio
38+
check: $(BIN) $(LINUX_IMG) $(ROOTFS_IMG)
39+
$(VECHO) "\nOnce the message 'Kernel panic' appears, press Ctrl-C to exit\n\n"
40+
$(Q)sudo $(BIN) -k $(LINUX_IMG) -i $(ROOTFS_IMG)
4541

4642
clean:
4743
$(VECHO) "Cleaning...\n"

mk/common.mk

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
UNAME_S := $(shell uname -s)
2+
ifeq ($(UNAME_S),Darwin)
3+
PRINTF = printf
4+
else
5+
PRINTF = env printf
6+
endif
7+
8+
# Control the build verbosity
9+
ifeq ("$(VERBOSE)","1")
10+
Q :=
11+
VECHO = @true
12+
REDIR =
13+
else
14+
Q := @
15+
VECHO = @$(PRINTF)
16+
REDIR = >/dev/null
17+
endif
18+
19+
# Test suite
20+
PASS_COLOR = \e[32;01m
21+
NO_COLOR = \e[0m
22+
23+
notice = $(PRINTF) "$(PASS_COLOR)$(strip $1)$(NO_COLOR)\n"
24+
25+
PARALLEL = -j $(shell nproc)

mk/external.mk

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# For each external target, the following must be defined in advance:
2+
# _SRC_URL : the hyperlink which points to archive.
3+
# _SRC : the file to be read by specific executable.
4+
# _SRC_SHA1 : the checksum of the content in _SRC
5+
6+
TOP=$(shell pwd)
7+
CONF=$(TOP)/configs
8+
FILE=$(TOP)/target
9+
10+
# Linux kernel
11+
LINUX_VER = 5.18.8
12+
LINUX_SRC_URL = https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-${LINUX_VER}.tar.xz
13+
LINUX_SRC = $(OUT)/linux-${LINUX_VER}
14+
LINUX_SRC_SHA1 = 8db5e3c3bc63a66fba5cdac53c125252dfbf3b82
15+
16+
# BusyBox
17+
BUSYBOX_VER=1.35.0
18+
BUSYBOX_SRC_URL = https://busybox.net/downloads/busybox-${BUSYBOX_VER}.tar.bz2
19+
BUSYBOX_SRC = $(OUT)/busybox-${BUSYBOX_VER}
20+
BUSYBOX_SRC_SHA1 = 36a1766206c8148bc06aca4e1f134016d40912d0
21+
22+
define download-n-extract
23+
$(eval $(T)_SRC_ARCHIVE = $(OUT)/$(shell basename $($(T)_SRC_URL)))
24+
$($(T)_SRC_ARCHIVE):
25+
$(VECHO) " GET\t$$@\n"
26+
$(Q)curl --progress-bar -o $$@ -L -C - "$(strip $($(T)_SRC_URL))"
27+
$(Q)echo "$(strip $$($(T)_SRC_SHA1)) $$@" | shasum -c
28+
$($(T)_SRC): $($(T)_SRC_ARCHIVE)
29+
$(VECHO) "Unpacking $$@ ... "
30+
$(Q)tar -xf $$< -C ${OUT} && $(call notice, [OK])
31+
endef
32+
33+
EXTERNAL_SRC = LINUX BUSYBOX
34+
$(foreach T,$(EXTERNAL_SRC),$(eval $(download-n-extract)))
35+
36+
# Build Linux kernel image
37+
LINUX_IMG = $(OUT)/bzImage
38+
$(LINUX_IMG): $(LINUX_SRC)
39+
$(VECHO) "Configuring Linux kernel... "
40+
$(Q)cp -f ${CONF}/linux.config $</.config
41+
$(Q)(cd $< ; make ARCH=x86 oldconfig $(REDIR)) && $(call notice, [OK])
42+
$(VECHO) "Building Linux kernel image... "
43+
$(Q)(cd $< ; make ARCH=x86 bzImage $(PARALLEL) $(REDIR))
44+
$(Q)(cd $< ; cp -f arch/x86/boot/bzImage $(TOP)/$(OUT)) && $(call notice, [OK])
45+
46+
# Build busybox single binary
47+
BUSYBOX_BIN = $(OUT)/rootfs/bin/busybox
48+
$(BUSYBOX_BIN): $(BUSYBOX_SRC)
49+
$(VECHO) "Configuring BusyBox... "
50+
$(Q)mkdir $(OUT)/rootfs || (rm -rf $(OUT)/rootfs ; mkdir -p $(OUT)/rootfs)
51+
$(Q)cp -f $(CONF)/busybox.config $</.config
52+
$(Q)(cd $< ; $(MAKE) oldconfig $(REDIR)) && $(call notice, [OK])
53+
$(VECHO) "Building BusyBox single binary... "
54+
$(Q)(cd $< ; $(MAKE) $(PARALLEL) 2>/dev/null $(REDIR))
55+
$(Q)(cd $< ; $(MAKE) CONFIG_PREFIX='../rootfs' install $(REDIR)) && $(call notice, [OK])
56+
57+
# Generate root file system
58+
ROOTFS_IMG = $(OUT)/rootfs.cpio
59+
$(ROOTFS_IMG): $(BUSYBOX_BIN)
60+
$(VECHO) "Generating root file system... "
61+
$(Q)(cd $(OUT)/rootfs ; \
62+
mv linuxrc init ; \
63+
mkdir -p etc/init.d ; \
64+
cp -f $(FILE)/rc-startup etc/init.d/rcS ; \
65+
chmod 755 etc/init.d/rcS ; \
66+
find . | cpio -o --format=newc > $(TOP)/$(OUT)/rootfs.cpio 2>/dev/null) && $(call notice, [OK])

scripts/build-linux.sh

Lines changed: 0 additions & 26 deletions
This file was deleted.

scripts/build-rootfs.sh

Lines changed: 0 additions & 38 deletions
This file was deleted.

scripts/common.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)