1# SPDX-License-Identifier: GPL-2.0
2#
3# Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4
5PWD := $(shell pwd)
6
7# Set these from the environment to override
8KERNEL_PATH ?= $(PWD)/../../../../..
9BUILD_PATH ?= $(PWD)/build/$(ARCH)
10DISTFILES_PATH ?= $(PWD)/distfiles
11NR_CPUS ?= 4
12ARCH ?=
13CBUILD := $(shell gcc -dumpmachine)
14HOST_ARCH := $(firstword $(subst -, ,$(CBUILD)))
15ifeq ($(ARCH),)
16ARCH := $(HOST_ARCH)
17endif
18
19MIRROR := https://download.wireguard.com/qemu-test/distfiles/
20
21KERNEL_BUILD_PATH := $(BUILD_PATH)/kernel$(if $(findstring yes,$(DEBUG_KERNEL)),-debug)
22
23default: qemu
24
25# variable name, tarball project name, version, tarball extension, default URI base
26define tar_download =
27$(1)_VERSION := $(3)
28$(1)_NAME := $(2)-$$($(1)_VERSION)
29$(1)_TAR := $(DISTFILES_PATH)/$$($(1)_NAME)$(4)
30$(1)_PATH := $(BUILD_PATH)/$$($(1)_NAME)
31$(call file_download,$$($(1)_NAME)$(4),$(5),$(6))
32endef
33
34define file_download =
35$(DISTFILES_PATH)/$(1): | $(4)
36	mkdir -p $(DISTFILES_PATH)
37	flock -x $$@.lock -c '[ -f $$@ ] && exit 0; wget -O $$@.tmp $(MIRROR)$(1) || wget -O $$@.tmp $(2)$(1) || rm -f $$@.tmp; [ -f $$@.tmp ] || exit 1; if ([ -n "$(4)" ] && sed -n "s#^\([a-f0-9]\{64\}\)  \($(1)\)\$$$$#\1  $(DISTFILES_PATH)/\2.tmp#p" "$(4)" || echo "$(3)  $$@.tmp") | sha256sum -c -; then mv $$@.tmp $$@; else rm -f $$@.tmp; exit 71; fi'
38endef
39
40$(eval $(call tar_download,IPERF,iperf,3.11,.tar.gz,https://downloads.es.net/pub/iperf/,de8cb409fad61a0574f4cb07eb19ce1159707403ac2dc01b5d175e91240b7e5f))
41$(eval $(call tar_download,BASH,bash,5.1.16,.tar.gz,https://ftp.gnu.org/gnu/bash/,5bac17218d3911834520dad13cd1f85ab944e1c09ae1aba55906be1f8192f558))
42$(eval $(call tar_download,IPROUTE2,iproute2,5.17.0,.tar.gz,https://www.kernel.org/pub/linux/utils/net/iproute2/,bda331d5c4606138892f23a565d78fca18919b4d508a0b7ca8391c2da2db68b9))
43$(eval $(call tar_download,IPTABLES,iptables,1.8.7,.tar.bz2,https://www.netfilter.org/projects/iptables/files/,c109c96bb04998cd44156622d36f8e04b140701ec60531a10668cfdff5e8d8f0))
44$(eval $(call tar_download,NMAP,nmap,7.92,.tgz,https://nmap.org/dist/,064183ea642dc4c12b1ab3b5358ce1cef7d2e7e11ffa2849f16d339f5b717117))
45$(eval $(call tar_download,IPUTILS,iputils,s20190709,.tar.gz,https://github.com/iputils/iputils/archive/s20190709.tar.gz/#,a15720dd741d7538dd2645f9f516d193636ae4300ff7dbc8bfca757bf166490a))
46$(eval $(call tar_download,WIREGUARD_TOOLS,wireguard-tools,1.0.20210914,.tar.xz,https://git.zx2c4.com/wireguard-tools/snapshot/,97ff31489217bb265b7ae850d3d0f335ab07d2652ba1feec88b734bc96bd05ac))
47
48export CFLAGS := -O3 -pipe
49ifeq ($(HOST_ARCH),$(ARCH))
50CFLAGS += -march=native
51endif
52export LDFLAGS :=
53export CPPFLAGS :=
54
55QEMU_VPORT_RESULT :=
56ifeq ($(ARCH),aarch64)
57CHOST := aarch64-linux-musl
58QEMU_ARCH := aarch64
59KERNEL_ARCH := arm64
60KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
61QEMU_VPORT_RESULT := virtio-serial-device
62ifeq ($(HOST_ARCH),$(ARCH))
63QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
64else
65QEMU_MACHINE := -cpu max -machine virt
66CFLAGS += -march=armv8-a
67endif
68else ifeq ($(ARCH),aarch64_be)
69CHOST := aarch64_be-linux-musl
70QEMU_ARCH := aarch64
71KERNEL_ARCH := arm64
72KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
73QEMU_VPORT_RESULT := virtio-serial-device
74ifeq ($(HOST_ARCH),$(ARCH))
75QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
76else
77QEMU_MACHINE := -cpu max -machine virt
78CFLAGS += -march=armv8-a
79endif
80else ifeq ($(ARCH),arm)
81CHOST := arm-linux-musleabi
82QEMU_ARCH := arm
83KERNEL_ARCH := arm
84KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
85QEMU_VPORT_RESULT := virtio-serial-device
86ifeq ($(HOST_ARCH),$(ARCH))
87QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
88else
89QEMU_MACHINE := -cpu max -machine virt
90CFLAGS += -march=armv7-a -mabi=aapcs-linux
91endif
92else ifeq ($(ARCH),armeb)
93CHOST := armeb-linux-musleabi
94QEMU_ARCH := arm
95KERNEL_ARCH := arm
96KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
97QEMU_VPORT_RESULT := virtio-serial-device
98ifeq ($(HOST_ARCH),$(ARCH))
99QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
100else
101QEMU_MACHINE := -cpu max -machine virt
102CFLAGS += -march=armv7-a -mabi=aapcs-linux
103LDFLAGS += -Wl,--be8
104endif
105else ifeq ($(ARCH),x86_64)
106CHOST := x86_64-linux-musl
107QEMU_ARCH := x86_64
108KERNEL_ARCH := x86_64
109KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
110QEMU_VPORT_RESULT := virtio-serial-device
111ifeq ($(HOST_ARCH),$(ARCH))
112QEMU_MACHINE := -cpu host -machine microvm,accel=kvm,pit=off,pic=off,rtc=off,acpi=off
113else
114QEMU_MACHINE := -cpu max -machine microvm,acpi=off
115endif
116else ifeq ($(ARCH),i686)
117CHOST := i686-linux-musl
118QEMU_ARCH := i386
119KERNEL_ARCH := x86
120KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
121QEMU_VPORT_RESULT := virtio-serial-device
122ifeq ($(subst x86_64,i686,$(HOST_ARCH)),$(ARCH))
123QEMU_MACHINE := -cpu host -machine microvm,accel=kvm,pit=off,pic=off,rtc=off,acpi=off
124else
125QEMU_MACHINE := -cpu coreduo -machine microvm,acpi=off
126endif
127else ifeq ($(ARCH),mips64)
128CHOST := mips64-linux-musl
129QEMU_ARCH := mips64
130KERNEL_ARCH := mips
131KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
132ifeq ($(HOST_ARCH),$(ARCH))
133QEMU_MACHINE := -cpu host -machine malta,accel=kvm
134CFLAGS += -EB
135else
136QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
137CFLAGS += -march=mips64r2 -EB
138endif
139else ifeq ($(ARCH),mips64el)
140CHOST := mips64el-linux-musl
141QEMU_ARCH := mips64el
142KERNEL_ARCH := mips
143KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
144ifeq ($(HOST_ARCH),$(ARCH))
145QEMU_MACHINE := -cpu host -machine malta,accel=kvm
146CFLAGS += -EL
147else
148QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
149CFLAGS += -march=mips64r2 -EL
150endif
151else ifeq ($(ARCH),mips)
152CHOST := mips-linux-musl
153QEMU_ARCH := mips
154KERNEL_ARCH := mips
155KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
156ifeq ($(HOST_ARCH),$(ARCH))
157QEMU_MACHINE := -cpu host -machine malta,accel=kvm
158CFLAGS += -EB
159else
160QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
161CFLAGS += -march=mips32r2 -EB
162endif
163else ifeq ($(ARCH),mipsel)
164CHOST := mipsel-linux-musl
165QEMU_ARCH := mipsel
166KERNEL_ARCH := mips
167KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
168ifeq ($(HOST_ARCH),$(ARCH))
169QEMU_MACHINE := -cpu host -machine malta,accel=kvm
170CFLAGS += -EL
171else
172QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
173CFLAGS += -march=mips32r2 -EL
174endif
175else ifeq ($(ARCH),powerpc64)
176CHOST := powerpc64-linux-musl
177QEMU_ARCH := ppc64
178KERNEL_ARCH := powerpc
179KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
180ifeq ($(HOST_ARCH),$(ARCH))
181QEMU_MACHINE := -cpu host,accel=kvm -machine pseries
182else
183QEMU_MACHINE := -machine pseries -device spapr-rng,rng=rng -object rng-random,id=rng
184endif
185else ifeq ($(ARCH),powerpc64le)
186CHOST := powerpc64le-linux-musl
187QEMU_ARCH := ppc64
188KERNEL_ARCH := powerpc
189KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
190ifeq ($(HOST_ARCH),$(ARCH))
191QEMU_MACHINE := -cpu host,accel=kvm -machine pseries
192else
193QEMU_MACHINE := -machine pseries -device spapr-rng,rng=rng -object rng-random,id=rng
194endif
195else ifeq ($(ARCH),powerpc)
196CHOST := powerpc-linux-musl
197QEMU_ARCH := ppc
198KERNEL_ARCH := powerpc
199KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/powerpc/boot/uImage
200ifeq ($(HOST_ARCH),$(ARCH))
201QEMU_MACHINE := -cpu host,accel=kvm -machine ppce500
202else
203QEMU_MACHINE := -machine ppce500
204endif
205else ifeq ($(ARCH),m68k)
206CHOST := m68k-linux-musl
207QEMU_ARCH := m68k
208KERNEL_ARCH := m68k
209KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
210KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMDLINE=\(.*\)/\1/p' arch/m68k.config)
211QEMU_VPORT_RESULT := virtio-serial-device
212ifeq ($(HOST_ARCH),$(ARCH))
213QEMU_MACHINE := -cpu host,accel=kvm -machine virt -append $(KERNEL_CMDLINE)
214else
215QEMU_MACHINE := -machine virt -smp 1 -append $(KERNEL_CMDLINE)
216endif
217else ifeq ($(ARCH),riscv64)
218CHOST := riscv64-linux-musl
219QEMU_ARCH := riscv64
220KERNEL_ARCH := riscv
221KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/riscv/boot/Image
222QEMU_VPORT_RESULT := virtio-serial-device
223ifeq ($(HOST_ARCH),$(ARCH))
224QEMU_MACHINE := -cpu host,accel=kvm -machine virt
225else
226QEMU_MACHINE := -cpu rv64 -machine virt
227endif
228else ifeq ($(ARCH),riscv32)
229CHOST := riscv32-linux-musl
230QEMU_ARCH := riscv32
231KERNEL_ARCH := riscv
232KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/riscv/boot/Image
233QEMU_VPORT_RESULT := virtio-serial-device
234ifeq ($(HOST_ARCH),$(ARCH))
235QEMU_MACHINE := -cpu host,accel=kvm -machine virt
236else
237QEMU_MACHINE := -cpu rv32 -machine virt
238endif
239else ifeq ($(ARCH),s390x)
240CHOST := s390x-linux-musl
241QEMU_ARCH := s390x
242KERNEL_ARCH := s390
243KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/s390/boot/bzImage
244KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMDLINE=\(.*\)/\1/p' arch/s390x.config)
245QEMU_VPORT_RESULT := virtio-serial-ccw
246ifeq ($(HOST_ARCH),$(ARCH))
247QEMU_MACHINE := -cpu host,accel=kvm -machine s390-ccw-virtio -append $(KERNEL_CMDLINE)
248else
249QEMU_MACHINE := -cpu max -machine s390-ccw-virtio -append $(KERNEL_CMDLINE)
250endif
251else ifeq ($(ARCH),um)
252CHOST := $(HOST_ARCH)-linux-musl
253KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
254KERNEL_ARCH := um
255KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMDLINE=\(.*\)/\1/p' arch/um.config)
256else
257$(error I only build: x86_64, i686, arm, armeb, aarch64, aarch64_be, mips, mipsel, mips64, mips64el, powerpc64, powerpc64le, powerpc, m68k, riscv64, riscv32, s390x, um)
258endif
259
260TOOLCHAIN_FILENAME := $(CHOST)-cross.tgz
261TOOLCHAIN_TAR := $(DISTFILES_PATH)/$(TOOLCHAIN_FILENAME)
262TOOLCHAIN_PATH := $(BUILD_PATH)/$(CHOST)-cross
263TOOLCHAIN_DIR := https://download.wireguard.com/qemu-test/toolchains/20211123/
264$(eval $(call file_download,toolchain-sha256sums-20211123,$(TOOLCHAIN_DIR)SHA256SUMS#,83da033fd8c798df476c21d9612da2dfb896ec62fbed4ceec5eefc0e56b3f0c8))
265$(eval $(call file_download,$(TOOLCHAIN_FILENAME),$(TOOLCHAIN_DIR),,$(DISTFILES_PATH)/toolchain-sha256sums-20211123))
266
267STRIP := $(CHOST)-strip
268CROSS_COMPILE_FLAG := --build=$(CBUILD) --host=$(CHOST)
269$(info Building for $(CHOST) using $(CBUILD))
270ifneq ($(ARCH),um)
271export CROSS_COMPILE := $(CHOST)-
272endif
273export PATH := $(TOOLCHAIN_PATH)/bin:$(PATH)
274export CC := $(CHOST)-gcc
275CCACHE_PATH := $(shell which ccache 2>/dev/null)
276ifneq ($(CCACHE_PATH),)
277export KBUILD_BUILD_TIMESTAMP := Fri Jun  5 15:58:00 CEST 2015
278export PATH := $(TOOLCHAIN_PATH)/bin/ccache:$(PATH)
279export CCACHE_SLOPPINESS := file_macro,time_macros
280export CCACHE_DIR ?= $(PWD)/ccache
281endif
282
283USERSPACE_DEPS := $(TOOLCHAIN_PATH)/.installed $(TOOLCHAIN_PATH)/$(CHOST)/include/linux/.installed
284
285comma := ,
286build: $(KERNEL_BZIMAGE)
287qemu: $(KERNEL_BZIMAGE)
288	rm -f $(BUILD_PATH)/result
289ifneq ($(ARCH),um)
290	timeout --foreground 20m qemu-system-$(QEMU_ARCH) \
291		-nodefaults \
292		-nographic \
293		-smp $(NR_CPUS) \
294		$(QEMU_MACHINE) \
295		-m $$(grep -q CONFIG_DEBUG_KMEMLEAK=y $(KERNEL_BUILD_PATH)/.config && echo 1G || echo 256M) \
296		-serial stdio \
297		-chardev file,path=$(BUILD_PATH)/result,id=result \
298		$(if $(QEMU_VPORT_RESULT),-device $(QEMU_VPORT_RESULT) -device virtserialport$(comma)chardev=result,-serial chardev:result) \
299		-no-reboot \
300		-monitor none \
301		-kernel $<
302else
303	timeout --foreground 20m $< \
304		$(KERNEL_CMDLINE) \
305		mem=$$(grep -q CONFIG_DEBUG_KMEMLEAK=y $(KERNEL_BUILD_PATH)/.config && echo 1G || echo 256M) \
306		noreboot \
307		con1=fd:51 51>$(BUILD_PATH)/result </dev/null 2>&1 | cat
308endif
309	grep -Fq success $(BUILD_PATH)/result
310
311$(BUILD_PATH)/init-cpio-spec.txt: $(TOOLCHAIN_PATH)/.installed $(BUILD_PATH)/init
312	mkdir -p $(BUILD_PATH)
313	echo "file /init $(BUILD_PATH)/init 755 0 0" > $@
314	echo "file /init.sh $(PWD)/../netns.sh 755 0 0" >> $@
315	echo "dir /dev 755 0 0" >> $@
316	echo "nod /dev/console 644 0 0 c 5 1" >> $@
317	echo "dir /bin 755 0 0" >> $@
318	echo "file /bin/iperf3 $(IPERF_PATH)/src/iperf3 755 0 0" >> $@
319	echo "file /bin/wg $(WIREGUARD_TOOLS_PATH)/src/wg 755 0 0" >> $@
320	echo "file /bin/bash $(BASH_PATH)/bash 755 0 0" >> $@
321	echo "file /bin/ip $(IPROUTE2_PATH)/ip/ip 755 0 0" >> $@
322	echo "file /bin/ss $(IPROUTE2_PATH)/misc/ss 755 0 0" >> $@
323	echo "file /bin/ping $(IPUTILS_PATH)/ping 755 0 0" >> $@
324	echo "file /bin/ncat $(NMAP_PATH)/ncat/ncat 755 0 0" >> $@
325	echo "file /bin/xtables-legacy-multi $(IPTABLES_PATH)/iptables/xtables-legacy-multi 755 0 0" >> $@
326	echo "slink /bin/iptables xtables-legacy-multi 777 0 0" >> $@
327	echo "slink /bin/ping6 ping 777 0 0" >> $@
328	echo "dir /lib 755 0 0" >> $@
329	echo "file /lib/libc.so $(TOOLCHAIN_PATH)/$(CHOST)/lib/libc.so 755 0 0" >> $@
330	echo "slink $$($(CHOST)-readelf -p .interp '$(BUILD_PATH)/init'| grep -o '/lib/.*') libc.so 777 0 0" >> $@
331
332$(KERNEL_BUILD_PATH)/.config: $(TOOLCHAIN_PATH)/.installed kernel.config arch/$(ARCH).config
333	mkdir -p $(KERNEL_BUILD_PATH)
334	cp kernel.config $(KERNEL_BUILD_PATH)/minimal.config
335	printf 'CONFIG_NR_CPUS=$(NR_CPUS)\nCONFIG_INITRAMFS_SOURCE="$(BUILD_PATH)/init-cpio-spec.txt"\n' >> $(KERNEL_BUILD_PATH)/minimal.config
336	cat arch/$(ARCH).config >> $(KERNEL_BUILD_PATH)/minimal.config
337	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) allnoconfig
338	cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config $(KERNEL_BUILD_PATH)/minimal.config
339	$(if $(findstring yes,$(DEBUG_KERNEL)),cp debug.config $(KERNEL_BUILD_PATH) && cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config debug.config,)
340
341$(KERNEL_BZIMAGE): $(TOOLCHAIN_PATH)/.installed $(KERNEL_BUILD_PATH)/.config $(BUILD_PATH)/init-cpio-spec.txt $(IPERF_PATH)/src/iperf3 $(IPUTILS_PATH)/ping $(BASH_PATH)/bash $(IPROUTE2_PATH)/misc/ss $(IPROUTE2_PATH)/ip/ip $(IPTABLES_PATH)/iptables/xtables-legacy-multi $(NMAP_PATH)/ncat/ncat $(WIREGUARD_TOOLS_PATH)/src/wg $(BUILD_PATH)/init
342	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE)
343.PHONY: $(KERNEL_BZIMAGE)
344
345$(TOOLCHAIN_PATH)/$(CHOST)/include/linux/.installed: | $(KERNEL_BUILD_PATH)/.config $(TOOLCHAIN_PATH)/.installed
346ifneq ($(ARCH),um)
347	rm -rf $(TOOLCHAIN_PATH)/$(CHOST)/include/linux
348	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) INSTALL_HDR_PATH=$(TOOLCHAIN_PATH)/$(CHOST) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) headers_install
349endif
350	touch $@
351
352$(TOOLCHAIN_PATH)/.installed: $(TOOLCHAIN_TAR)
353	mkdir -p $(BUILD_PATH)
354	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
355	$(STRIP) -s $(TOOLCHAIN_PATH)/$(CHOST)/lib/libc.so
356ifneq ($(CCACHE_PATH),)
357	mkdir -p $(TOOLCHAIN_PATH)/bin/ccache
358	ln -s $(CCACHE_PATH) $(TOOLCHAIN_PATH)/bin/ccache/$(CC)
359endif
360	touch $@
361
362$(IPERF_PATH)/.installed: $(IPERF_TAR)
363	mkdir -p $(BUILD_PATH)
364	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
365	sed -i '1s/^/#include <stdint.h>/' $(IPERF_PATH)/src/cjson.h $(IPERF_PATH)/src/timer.h
366	sed -i -r 's/-p?g//g' $(IPERF_PATH)/src/Makefile*
367	touch $@
368
369$(IPERF_PATH)/src/iperf3: | $(IPERF_PATH)/.installed $(USERSPACE_DEPS)
370	cd $(IPERF_PATH) && autoreconf -fi
371	cd $(IPERF_PATH) && CFLAGS="$(CFLAGS) -D_GNU_SOURCE" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --with-openssl=no
372	$(MAKE) -C $(IPERF_PATH)
373	$(STRIP) -s $@
374
375$(WIREGUARD_TOOLS_PATH)/.installed: $(WIREGUARD_TOOLS_TAR)
376	mkdir -p $(BUILD_PATH)
377	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
378	touch $@
379
380$(WIREGUARD_TOOLS_PATH)/src/wg: | $(WIREGUARD_TOOLS_PATH)/.installed $(USERSPACE_DEPS)
381	$(MAKE) -C $(WIREGUARD_TOOLS_PATH)/src wg
382	$(STRIP) -s $@
383
384$(BUILD_PATH)/init: init.c | $(USERSPACE_DEPS)
385	mkdir -p $(BUILD_PATH)
386	$(CC) -o $@ $(CFLAGS) $(LDFLAGS) -std=gnu11 $<
387	$(STRIP) -s $@
388
389$(IPUTILS_PATH)/.installed: $(IPUTILS_TAR)
390	mkdir -p $(BUILD_PATH)
391	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
392	touch $@
393
394$(IPUTILS_PATH)/ping: | $(IPUTILS_PATH)/.installed $(USERSPACE_DEPS)
395	sed -i /atexit/d $(IPUTILS_PATH)/ping.c
396	cd $(IPUTILS_PATH) && $(CC) $(CFLAGS) -std=c99 -o $@ ping.c ping_common.c ping6_common.c iputils_common.c -D_GNU_SOURCE -D'IPUTILS_VERSION(f)=f' -lresolv $(LDFLAGS)
397	$(STRIP) -s $@
398
399$(BASH_PATH)/.installed: $(BASH_TAR)
400	mkdir -p $(BUILD_PATH)
401	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
402	touch $@
403
404$(BASH_PATH)/bash: | $(BASH_PATH)/.installed $(USERSPACE_DEPS)
405	cd $(BASH_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --without-bash-malloc --disable-debugger --disable-help-builtin --disable-history --disable-progcomp --disable-readline --disable-mem-scramble
406	$(MAKE) -C $(BASH_PATH)
407	$(STRIP) -s $@
408
409$(IPROUTE2_PATH)/.installed: $(IPROUTE2_TAR)
410	mkdir -p $(BUILD_PATH)
411	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
412	printf 'CC:=$(CC)\nPKG_CONFIG:=pkg-config\nTC_CONFIG_XT:=n\nTC_CONFIG_ATM:=n\nTC_CONFIG_IPSET:=n\nIP_CONFIG_SETNS:=y\nHAVE_ELF:=n\nHAVE_MNL:=n\nHAVE_BERKELEY_DB:=n\nHAVE_LATEX:=n\nHAVE_PDFLATEX:=n\nCFLAGS+=-DHAVE_SETNS -DHAVE_HANDLE_AT\n' > $(IPROUTE2_PATH)/config.mk
413	printf 'libutil.a.done:\n\tflock -x $$@.lock $$(MAKE) -C lib\n\ttouch $$@\nip/ip: libutil.a.done\n\t$$(MAKE) -C ip ip\nmisc/ss: libutil.a.done\n\t$$(MAKE) -C misc ss\n' >> $(IPROUTE2_PATH)/Makefile
414	touch $@
415
416$(IPROUTE2_PATH)/ip/ip: | $(IPROUTE2_PATH)/.installed $(USERSPACE_DEPS)
417	$(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ ip/ip
418	$(STRIP) -s $@
419
420$(IPROUTE2_PATH)/misc/ss: | $(IPROUTE2_PATH)/.installed $(USERSPACE_DEPS)
421	$(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ misc/ss
422	$(STRIP) -s $@
423
424$(IPTABLES_PATH)/.installed: $(IPTABLES_TAR)
425	mkdir -p $(BUILD_PATH)
426	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
427	sed -i -e "/nfnetlink=[01]/s:=[01]:=0:" -e "/nfconntrack=[01]/s:=[01]:=0:" $(IPTABLES_PATH)/configure
428	touch $@
429
430$(IPTABLES_PATH)/iptables/xtables-legacy-multi: | $(IPTABLES_PATH)/.installed $(USERSPACE_DEPS)
431	cd $(IPTABLES_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --disable-nftables --disable-bpf-compiler --disable-nfsynproxy --disable-libipq --disable-connlabel --with-kernel=$(BUILD_PATH)/include
432	$(MAKE) -C $(IPTABLES_PATH)
433	$(STRIP) -s $@
434
435$(NMAP_PATH)/.installed: $(NMAP_TAR)
436	mkdir -p $(BUILD_PATH)
437	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
438	touch $@
439
440$(NMAP_PATH)/ncat/ncat: | $(NMAP_PATH)/.installed $(USERSPACE_DEPS)
441	cd $(NMAP_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --without-ndiff --without-zenmap --without-nping --with-libpcap=included --with-libpcre=included --with-libdnet=included --without-liblua --with-liblinear=included --without-nmap-update --without-openssl --with-pcap=linux --without-libssh
442	$(MAKE) -C $(NMAP_PATH)/libpcap
443	$(MAKE) -C $(NMAP_PATH)/ncat
444	$(STRIP) -s $@
445
446clean:
447	rm -rf $(BUILD_PATH)
448
449distclean: clean
450	rm -rf $(DISTFILES_PATH)
451
452cacheclean: clean
453ifneq ($(CCACHE_DIR),)
454	rm -rf $(CCACHE_DIR)
455endif
456
457menuconfig: $(KERNEL_BUILD_PATH)/.config
458	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) menuconfig
459
460.PHONY: qemu build clean distclean cacheclean menuconfig
461.DELETE_ON_ERROR:
462