-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
234 lines (196 loc) · 7.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
include config.mk
# All target architectures
TARGETS := $(patsubst targets/%/,%,$(dir $(wildcard targets/*/config)))
# Default to native build
ARCH ?= $(shell uname -m)
# Directories
BINDIR := $(PWD)/bin
SRCDIR := $(PWD)/src
BUILDDIR := $(PWD)/build/$(ARCH)
TARGETDIR := $(PWD)/targets/$(ARCH)
SCRIPTDIR := $(PWD)/scripts
# Kernel version
ifneq ($(KERNEL_SRC),)
KERNEL_DIR := $(notdir $(patsubst %/,%,$(KERNEL_SRC)))
else
ifneq ($(KERNEL_VER),)
KERNEL_DIR := linux-$(KERNEL_VER)
KERNEL_PKG := $(KERNEL_DIR).tar.xz
KERNEL_URL := https://cdn.kernel.org/pub/linux/kernel/v5.x/$(KERNEL_PKG)
endif
endif
# Busybox version
BUSYBOX_DIR := busybox-$(BUSYBOX_VER)
BUSYBOX_PKG := $(BUSYBOX_DIR).tar.bz2
BUSYBOX_URL := https://busybox.net/downloads/$(BUSYBOX_PKG)
# Toolchain cross-make version
CROSSMAKE_DIR := musl-cross-make
CROSSMAKE_GIT := https://github.com/richfelker/musl-cross-make.git
# Arch-specific configuration
-include $(TARGETDIR)/config
TOOLCHAIN ?= $(ARCH)-linux-musl
KERNEL_ARCH ?= $(ARCH)
QEMU_ARCH ?= $(ARCH)
QEMU_TTY ?= ttyS0
ifneq ($(QEMU_DTB),)
QEMU_FLAGS += -dtb $(BUILDDIR)/$(KERNEL_DIR)/$(QEMU_DTB)
endif
# Arch-specific kernel config dir
KERNEL_CONFIGDIR := $(SRCDIR)/$(KERNEL_DIR)/arch/$(KERNEL_ARCH)/configs
# Add built toolchains to the search path
PATH := $(PATH):$(BINDIR)/$(TOOLCHAIN)/bin
# ================================================================
# Top-level targets
# ================================================================
run: $(BUILDDIR)/$(KERNEL_DIR)/vmlinux $(BUILDDIR)/initramfs.cpio.gz
@echo "Starting system (Ctrl-a x to exit)"
qemu-system-$(QEMU_ARCH) $(QEMU_FLAGS) \
-kernel $(BUILDDIR)/$(KERNEL_DIR)/$(KERNEL_IMAGE) \
-initrd $(BUILDDIR)/initramfs.cpio.gz \
-nographic -append "console=$(QEMU_TTY) $(QEMU_APPEND)"
clean:
rm -rf $(BUILDDIR)
distclean: clean
rm -rf build src dl
prerequisities:
sudo -k apt-get -y install openbios-sparc openbios-ppc qemu-system
.PHONY: run clean distclean prerequisities
# ================================================================
# Target architectures
# ================================================================
define Target
$(1):
$$(MAKE) ARCH=$(1) run
.PHONY: $(1)
endef
$(foreach target,$(TARGETS),$(eval $(call Target,$(target))))
# ================================================================
# Root file system
# ================================================================
$(BUILDDIR)/initramfs.cpio.gz: $(SCRIPTDIR)/init \
$(BUILDDIR)/$(KERNEL_DIR)/vmlinux \
$(BUILDDIR)/$(BUSYBOX_DIR)/_install
mkdir -p $(BUILDDIR)/initramfs
( \
set -e ; \
cd $(BUILDDIR)/initramfs ; \
mkdir -p bin sbin etc proc sys usr/bin usr/sbin ; \
cp -a $(BUILDDIR)/$(BUSYBOX_DIR)/_install/* . ; \
cp -a $< . ; \
chmod +x init ; \
find . -print0 | cpio --null -ov --format=newc | gzip > $@ ; \
)
# ================================================================
# Kernel build
# ================================================================
# Build the kernel
$(BUILDDIR)/$(KERNEL_DIR)/vmlinux: $(BUILDDIR)/$(KERNEL_DIR)/.config
rm -rf $(BUILDDIR)/initramfs/lib/modules
$(MAKE) -C $(BUILDDIR)/$(KERNEL_DIR) ARCH=$(KERNEL_ARCH) \
CROSS_COMPILE=$(TOOLCHAIN)- all
$(MAKE) -C $(BUILDDIR)/$(KERNEL_DIR) ARCH=$(KERNEL_ARCH) \
INSTALL_MOD_PATH=$(BUILDDIR)/initramfs modules_install
.PHONY: $(BUILDDIR)/$(KERNEL_DIR)/vmlinux
# Generate kernel config
$(BUILDDIR)/$(KERNEL_DIR)/.config: $(KERNEL_CONFIGDIR)/$(ARCH)_qemu_defconfig
mkdir -p $(BUILDDIR)/$(KERNEL_DIR)
$(MAKE) -C $(SRCDIR)/$(KERNEL_DIR) ARCH=$(KERNEL_ARCH) \
O=$(BUILDDIR)/$(KERNEL_DIR) $(ARCH)_qemu_defconfig
(cat defaults/kernel && echo && cat $(TARGETDIR)/kernel && echo) | \
while read -r LINE; do \
CONFIG=$$(echo "$$LINE" | grep -o 'CONFIG_[A-Za-z0-9_-]\+'); \
[ -z "$$CONFIG" ] || grep -q "$$CONFIG" $@ || echo "$$LINE" >>$@; \
done
# Generate kernel defconfig
$(KERNEL_CONFIGDIR)/$(ARCH)_qemu_defconfig: \
$(KERNEL_CONFIGDIR)/$(KERNEL_DEFCONFIG) \
$(TARGETDIR)/kernel $(TARGETDIR)/config \
defaults/kernel Makefile
echo | cat $< - > $@
(cat defaults/kernel && echo && cat $(TARGETDIR)/kernel && echo) | \
while read -r LINE; do \
CONFIG=$$(echo "$$LINE" | grep -o 'CONFIG_[A-Za-z0-9_-]\+'); \
[ -z "$$CONFIG" ] || sed -i "/$$CONFIG/d" $@; \
done
echo | cat defaults/kernel - >> $@
echo | cat $(TARGETDIR)/kernel - >> $@
# Generate base defconfig
ifeq ($(wildcard $(TARGETDIR)/$(KERNEL_DEFCONFIG)),)
$(KERNEL_CONFIGDIR)/$(KERNEL_DEFCONFIG):
mkdir -p $(BUILDDIR)/$(KERNEL_DIR)
$(MAKE) -C $(SRCDIR)/$(KERNEL_DIR) ARCH=$(KERNEL_ARCH) \
O=$(BUILDDIR)/$(KERNEL_DIR) $(KERNEL_DEFCONFIG)
$(MAKE) -C $(BUILDDIR)/$(KERNEL_DIR) ARCH=$(KERNEL_ARCH) savedefconfig
mv $(BUILDDIR)/$(KERNEL_DIR)/defconfig $@
else
$(KERNEL_CONFIGDIR)/$(KERNEL_DEFCONFIG): $(TARGETDIR)/$(KERNEL_DEFCONFIG)
cp $< $@
endif
$(KERNEL_CONFIGDIR)/$(KERNEL_DEFCONFIG): $(SRCDIR)/$(KERNEL_DIR)/Makefile
# Prepare kernel source
ifneq ($(KERNEL_SRC),)
$(SRCDIR)/$(KERNEL_DIR)/Makefile:
mkdir -p $(SRCDIR)
if echo $(KERNEL_SRC) | grep -q '^/'; then \
ln -nfs $(KERNEL_SRC) $(SRCDIR)/$(KERNEL_DIR) || exit 1; \
else \
ln -nfs ../$(KERNEL_SRC) $(SRCDIR)/$(KERNEL_DIR) || exit 1; \
fi
else
ifneq ($(KERNEL_VER),)
$(SRCDIR)/$(KERNEL_DIR)/Makefile: dl/$(KERNEL_PKG)
mkdir -p $(SRCDIR)
tar xf $< -C $(SRCDIR)
touch $@
dl/$(KERNEL_PKG):
mkdir -p dl
wget $(KERNEL_URL) -O $@
else
$(SRCDIR)/$(KERNEL_DIR)/Makefile:
@echo "Either KERNEL_SRC or KERNEL_VER must be set"
endif
endif
# ================================================================
# Busybox build
# ================================================================
$(BUILDDIR)/$(BUSYBOX_DIR)/_install: $(BUILDDIR)/$(BUSYBOX_DIR)/.config
$(MAKE) $(MAKEOPTS) -C $(BUILDDIR)/$(BUSYBOX_DIR) install
$(BUILDDIR)/$(BUSYBOX_DIR)/.config: defaults/busybox defaults/kernel \
$(TARGETDIR)/config \
$(SRCDIR)/$(BUSYBOX_DIR)/Makefile
mkdir -p $(BUILDDIR)/$(BUSYBOX_DIR)
cp defaults/busybox $(BUILDDIR)/$(BUSYBOX_DIR)/defconfig
echo "CONFIG_CROSS_COMPILER_PREFIX=\"$(TOOLCHAIN)-\"" >> \
$(BUILDDIR)/$(BUSYBOX_DIR)/defconfig
$(MAKE) -C $(SRCDIR)/$(BUSYBOX_DIR) \
O=$(BUILDDIR)/$(BUSYBOX_DIR) \
KBUILD_DEFCONFIG=$(BUILDDIR)/$(BUSYBOX_DIR)/defconfig \
defconfig
$(SRCDIR)/$(BUSYBOX_DIR)/Makefile: dl/$(BUSYBOX_PKG)
mkdir -p $(SRCDIR)
tar xf $< -C $(SRCDIR)
touch $@
dl/$(BUSYBOX_PKG):
mkdir -p dl
wget $(BUSYBOX_URL) -O $@
# ================================================================
# Toolchain
# ================================================================
ifeq ($(shell PATH=$(PATH) which $(TOOLCHAIN)-gcc),)
$(BUILDDIR)/$(KERNEL_DIR)/vmlinux $(BUILDDIR)/$(BUSYBOX_DIR)/_install: \
$(TOOLCHAIN)
.PHONY: $(TOOLCHAIN)
$(patsubst %,$(ARCH)-linux-%,musl musleabi muslabi64): \
$(BINDIR)/$(TOOLCHAIN)/bin/$(TOOLCHAIN)-gcc
$(patsubst %,$(ARCH)-linux-%,gnu gnueabi gnuabi64):
@echo "Installing package gcc-$(TOOLCHAIN)"
sudo -k apt-get -y install gcc-$(TOOLCHAIN)
endif
$(BINDIR)/$(TOOLCHAIN)/bin/$(TOOLCHAIN)-gcc: MAKEOVERRIDES =
$(BINDIR)/$(TOOLCHAIN)/bin/$(TOOLCHAIN)-gcc: $(SRCDIR)/$(CROSSMAKE_DIR)
cd $< && $(MAKE) TARGET=$(TOOLCHAIN)
cd $< && $(MAKE) TARGET=$(TOOLCHAIN) \
OUTPUT=$(BINDIR)/$(TOOLCHAIN) install
$(SRCDIR)/$(CROSSMAKE_DIR):
mkdir -p $(SRCDIR)
cd $(SRCDIR) && git clone $(CROSSMAKE_GIT) $(CROSSMAKE_DIR)