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
7CHOST := $(shell gcc -dumpmachine)
8HOST_ARCH := $(firstword $(subst -, ,$(CHOST)))
9ifneq (,$(ARCH))
10CBUILD := $(subst -gcc,,$(lastword $(subst /, ,$(firstword $(wildcard $(foreach bindir,$(subst :, ,$(PATH)),$(bindir)/$(ARCH)-*-gcc))))))
11ifeq (,$(CBUILD))
12$(error The toolchain for $(ARCH) is not installed)
13endif
14else
15CBUILD := $(CHOST)
16ARCH := $(firstword $(subst -, ,$(CBUILD)))
17endif
18
19# Set these from the environment to override
20KERNEL_PATH ?= $(PWD)/../../../../..
21BUILD_PATH ?= $(PWD)/build/$(ARCH)
22DISTFILES_PATH ?= $(PWD)/distfiles
23NR_CPUS ?= 4
24
25MIRROR := https://download.wireguard.com/qemu-test/distfiles/
26
27default: qemu
28
29# variable name, tarball project name, version, tarball extension, default URI base
30define tar_download =
31$(1)_VERSION := $(3)
32$(1)_NAME := $(2)-$$($(1)_VERSION)
33$(1)_TAR := $(DISTFILES_PATH)/$$($(1)_NAME)$(4)
34$(1)_PATH := $(BUILD_PATH)/$$($(1)_NAME)
35$(call file_download,$$($(1)_NAME)$(4),$(5),$(6))
36endef
37
38define file_download =
39$(DISTFILES_PATH)/$(1):
40	mkdir -p $(DISTFILES_PATH)
41	flock -x $$@.lock -c '[ -f $$@ ] && exit 0; wget -O $$@.tmp $(MIRROR)$(1) || wget -O $$@.tmp $(2)$(1) || rm -f $$@.tmp'
42	if echo "$(3)  $$@.tmp" | sha256sum -c -; then mv $$@.tmp $$@; else rm -f $$@.tmp; exit 71; fi
43endef
44
45$(eval $(call tar_download,MUSL,musl,1.1.24,.tar.gz,https://www.musl-libc.org/releases/,1370c9a812b2cf2a7d92802510cca0058cc37e66a7bedd70051f0a34015022a3))
46$(eval $(call tar_download,LIBMNL,libmnl,1.0.4,.tar.bz2,https://www.netfilter.org/projects/libmnl/files/,171f89699f286a5854b72b91d06e8f8e3683064c5901fb09d954a9ab6f551f81))
47$(eval $(call tar_download,IPERF,iperf,3.7,.tar.gz,https://downloads.es.net/pub/iperf/,d846040224317caf2f75c843d309a950a7db23f9b44b94688ccbe557d6d1710c))
48$(eval $(call tar_download,BASH,bash,5.0,.tar.gz,https://ftp.gnu.org/gnu/bash/,b4a80f2ac66170b2913efbfb9f2594f1f76c7b1afd11f799e22035d63077fb4d))
49$(eval $(call tar_download,IPROUTE2,iproute2,5.4.0,.tar.xz,https://www.kernel.org/pub/linux/utils/net/iproute2/,fe97aa60a0d4c5ac830be18937e18dc3400ca713a33a89ad896ff1e3d46086ae))
50$(eval $(call tar_download,IPTABLES,iptables,1.8.4,.tar.bz2,https://www.netfilter.org/projects/iptables/files/,993a3a5490a544c2cbf2ef15cf7e7ed21af1845baf228318d5c36ef8827e157c))
51$(eval $(call tar_download,NMAP,nmap,7.80,.tar.bz2,https://nmap.org/dist/,fcfa5a0e42099e12e4bf7a68ebe6fde05553383a682e816a7ec9256ab4773faa))
52$(eval $(call tar_download,IPUTILS,iputils,s20190709,.tar.gz,https://github.com/iputils/iputils/archive/s20190709.tar.gz/#,a15720dd741d7538dd2645f9f516d193636ae4300ff7dbc8bfca757bf166490a))
53$(eval $(call tar_download,WIREGUARD_TOOLS,wireguard-tools,1.0.20191226,.tar.xz,https://git.zx2c4.com/wireguard-tools/snapshot/,aa8af0fdc9872d369d8c890a84dbc2a2466b55795dccd5b47721b2d97644b04f))
54
55KERNEL_BUILD_PATH := $(BUILD_PATH)/kernel$(if $(findstring yes,$(DEBUG_KERNEL)),-debug)
56rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
57WIREGUARD_SOURCES := $(call rwildcard,$(KERNEL_PATH)/drivers/net/wireguard/,*)
58
59export CFLAGS ?= -O3 -pipe
60export LDFLAGS ?=
61export CPPFLAGS := -I$(BUILD_PATH)/include
62
63ifeq ($(HOST_ARCH),$(ARCH))
64CROSS_COMPILE_FLAG := --host=$(CHOST)
65CFLAGS += -march=native
66STRIP := strip
67else
68$(info Cross compilation: building for $(CBUILD) using $(CHOST))
69CROSS_COMPILE_FLAG := --build=$(CBUILD) --host=$(CHOST)
70export CROSS_COMPILE=$(CBUILD)-
71STRIP := $(CBUILD)-strip
72endif
73ifeq ($(ARCH),aarch64)
74QEMU_ARCH := aarch64
75KERNEL_ARCH := arm64
76KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
77ifeq ($(HOST_ARCH),$(ARCH))
78QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
79else
80QEMU_MACHINE := -cpu cortex-a53 -machine virt
81CFLAGS += -march=armv8-a -mtune=cortex-a53
82endif
83else ifeq ($(ARCH),aarch64_be)
84QEMU_ARCH := aarch64
85KERNEL_ARCH := arm64
86KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm64/boot/Image
87ifeq ($(HOST_ARCH),$(ARCH))
88QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
89else
90QEMU_MACHINE := -cpu cortex-a53 -machine virt
91CFLAGS += -march=armv8-a -mtune=cortex-a53
92endif
93else ifeq ($(ARCH),arm)
94QEMU_ARCH := arm
95KERNEL_ARCH := arm
96KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
97ifeq ($(HOST_ARCH),$(ARCH))
98QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
99else
100QEMU_MACHINE := -cpu cortex-a15 -machine virt
101CFLAGS += -march=armv7-a -mtune=cortex-a15 -mabi=aapcs-linux
102endif
103else ifeq ($(ARCH),armeb)
104QEMU_ARCH := arm
105KERNEL_ARCH := arm
106KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/arm/boot/zImage
107ifeq ($(HOST_ARCH),$(ARCH))
108QEMU_MACHINE := -cpu host -machine virt,gic_version=host,accel=kvm
109else
110QEMU_MACHINE := -cpu cortex-a15 -machine virt
111CFLAGS += -march=armv7-a -mabi=aapcs-linux # We don't pass -mtune=cortex-a15 due to a compiler bug on big endian.
112LDFLAGS += -Wl,--be8
113endif
114else ifeq ($(ARCH),x86_64)
115QEMU_ARCH := x86_64
116KERNEL_ARCH := x86_64
117KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
118ifeq ($(HOST_ARCH),$(ARCH))
119QEMU_MACHINE := -cpu host -machine q35,accel=kvm
120else
121QEMU_MACHINE := -cpu Skylake-Server -machine q35
122CFLAGS += -march=skylake-avx512
123endif
124else ifeq ($(ARCH),i686)
125QEMU_ARCH := i386
126KERNEL_ARCH := x86
127KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x86/boot/bzImage
128ifeq ($(subst x86_64,i686,$(HOST_ARCH)),$(ARCH))
129QEMU_MACHINE := -cpu host -machine q35,accel=kvm
130else
131QEMU_MACHINE := -cpu coreduo -machine q35
132CFLAGS += -march=prescott
133endif
134else ifeq ($(ARCH),mips64)
135QEMU_ARCH := mips64
136KERNEL_ARCH := mips
137KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
138ifeq ($(HOST_ARCH),$(ARCH))
139QEMU_MACHINE := -cpu host -machine malta,accel=kvm
140CFLAGS += -EB
141else
142QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
143CFLAGS += -march=mips64r2 -EB
144endif
145else ifeq ($(ARCH),mips64el)
146QEMU_ARCH := mips64el
147KERNEL_ARCH := mips
148KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
149ifeq ($(HOST_ARCH),$(ARCH))
150QEMU_MACHINE := -cpu host -machine malta,accel=kvm
151CFLAGS += -EL
152else
153QEMU_MACHINE := -cpu MIPS64R2-generic -machine malta -smp 1
154CFLAGS += -march=mips64r2 -EL
155endif
156else ifeq ($(ARCH),mips)
157QEMU_ARCH := mips
158KERNEL_ARCH := mips
159KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
160ifeq ($(HOST_ARCH),$(ARCH))
161QEMU_MACHINE := -cpu host -machine malta,accel=kvm
162CFLAGS += -EB
163else
164QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
165CFLAGS += -march=mips32r2 -EB
166endif
167else ifeq ($(ARCH),mipsel)
168QEMU_ARCH := mipsel
169KERNEL_ARCH := mips
170KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
171ifeq ($(HOST_ARCH),$(ARCH))
172QEMU_MACHINE := -cpu host -machine malta,accel=kvm
173CFLAGS += -EL
174else
175QEMU_MACHINE := -cpu 24Kf -machine malta -smp 1
176CFLAGS += -march=mips32r2 -EL
177endif
178else ifeq ($(ARCH),powerpc64le)
179QEMU_ARCH := ppc64
180KERNEL_ARCH := powerpc
181KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
182ifeq ($(HOST_ARCH),$(ARCH))
183QEMU_MACHINE := -cpu host,accel=kvm -machine pseries
184else
185QEMU_MACHINE := -machine pseries
186endif
187CFLAGS += -mcpu=powerpc64le -mlong-double-64
188else ifeq ($(ARCH),powerpc)
189QEMU_ARCH := ppc
190KERNEL_ARCH := powerpc
191KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/powerpc/boot/uImage
192ifeq ($(HOST_ARCH),$(ARCH))
193QEMU_MACHINE := -cpu host,accel=kvm -machine ppce500
194else
195QEMU_MACHINE := -machine ppce500
196endif
197CFLAGS += -mcpu=powerpc -mlong-double-64 -msecure-plt
198else ifeq ($(ARCH),m68k)
199QEMU_ARCH := m68k
200KERNEL_ARCH := m68k
201KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux
202KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMDLINE=\(.*\)/\1/p' arch/m68k.config)
203ifeq ($(HOST_ARCH),$(ARCH))
204QEMU_MACHINE := -cpu host,accel=kvm -machine q800 -smp 1 -append $(KERNEL_CMDLINE)
205else
206QEMU_MACHINE := -machine q800 -smp 1 -append $(KERNEL_CMDLINE)
207endif
208else
209$(error I only build: x86_64, i686, arm, armeb, aarch64, aarch64_be, mips, mipsel, mips64, mips64el, powerpc64le, powerpc, m68k)
210endif
211
212REAL_CC := $(CBUILD)-gcc
213MUSL_CC := $(BUILD_PATH)/musl-gcc
214export CC := $(MUSL_CC)
215USERSPACE_DEPS := $(MUSL_CC) $(BUILD_PATH)/include/.installed $(BUILD_PATH)/include/linux/.installed
216
217build: $(KERNEL_BZIMAGE)
218qemu: $(KERNEL_BZIMAGE)
219	rm -f $(BUILD_PATH)/result
220	timeout --foreground 20m qemu-system-$(QEMU_ARCH) \
221		-nodefaults \
222		-nographic \
223		-smp $(NR_CPUS) \
224		$(QEMU_MACHINE) \
225		-m $$(grep -q CONFIG_DEBUG_KMEMLEAK=y $(KERNEL_BUILD_PATH)/.config && echo 1G || echo 256M) \
226		-serial stdio \
227		-serial file:$(BUILD_PATH)/result \
228		-no-reboot \
229		-monitor none \
230		-kernel $<
231	grep -Fq success $(BUILD_PATH)/result
232
233$(BUILD_PATH)/init-cpio-spec.txt:
234	mkdir -p $(BUILD_PATH)
235	echo "file /init $(BUILD_PATH)/init 755 0 0" > $@
236	echo "file /init.sh $(PWD)/../netns.sh 755 0 0" >> $@
237	echo "dir /dev 755 0 0" >> $@
238	echo "nod /dev/console 644 0 0 c 5 1" >> $@
239	echo "dir /bin 755 0 0" >> $@
240	echo "file /bin/iperf3 $(IPERF_PATH)/src/iperf3 755 0 0" >> $@
241	echo "file /bin/wg $(WIREGUARD_TOOLS_PATH)/src/wg 755 0 0" >> $@
242	echo "file /bin/bash $(BASH_PATH)/bash 755 0 0" >> $@
243	echo "file /bin/ip $(IPROUTE2_PATH)/ip/ip 755 0 0" >> $@
244	echo "file /bin/ss $(IPROUTE2_PATH)/misc/ss 755 0 0" >> $@
245	echo "file /bin/ping $(IPUTILS_PATH)/ping 755 0 0" >> $@
246	echo "file /bin/ncat $(NMAP_PATH)/ncat/ncat 755 0 0" >> $@
247	echo "file /bin/xtables-legacy-multi $(IPTABLES_PATH)/iptables/xtables-legacy-multi 755 0 0" >> $@
248	echo "slink /bin/iptables xtables-legacy-multi 777 0 0" >> $@
249	echo "slink /bin/ping6 ping 777 0 0" >> $@
250	echo "dir /lib 755 0 0" >> $@
251	echo "file /lib/libc.so $(MUSL_PATH)/lib/libc.so 755 0 0" >> $@
252	echo "slink /lib/ld-linux.so.1 libc.so 777 0 0" >> $@
253
254$(KERNEL_BUILD_PATH)/.config: kernel.config arch/$(ARCH).config
255	mkdir -p $(KERNEL_BUILD_PATH)
256	cp kernel.config $(KERNEL_BUILD_PATH)/minimal.config
257	printf 'CONFIG_NR_CPUS=$(NR_CPUS)\nCONFIG_INITRAMFS_SOURCE="$(BUILD_PATH)/init-cpio-spec.txt"\n' >> $(KERNEL_BUILD_PATH)/minimal.config
258	cat arch/$(ARCH).config >> $(KERNEL_BUILD_PATH)/minimal.config
259	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) allnoconfig
260	cd $(KERNEL_BUILD_PATH) && ARCH=$(KERNEL_ARCH) $(KERNEL_PATH)/scripts/kconfig/merge_config.sh -n $(KERNEL_BUILD_PATH)/.config $(KERNEL_BUILD_PATH)/minimal.config
261	$(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,)
262
263$(KERNEL_BZIMAGE): $(KERNEL_BUILD_PATH)/.config $(BUILD_PATH)/init-cpio-spec.txt $(MUSL_PATH)/lib/libc.so $(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 ../netns.sh $(WIREGUARD_SOURCES)
264	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE)
265
266$(BUILD_PATH)/include/linux/.installed: | $(KERNEL_BUILD_PATH)/.config
267	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) INSTALL_HDR_PATH=$(BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) headers_install
268	touch $@
269
270$(MUSL_PATH)/lib/libc.so: $(MUSL_TAR)
271	mkdir -p $(BUILD_PATH)
272	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
273	cd $(MUSL_PATH) && CC=$(REAL_CC) ./configure --prefix=/ --disable-static --build=$(CBUILD)
274	$(MAKE) -C $(MUSL_PATH)
275	$(STRIP) -s $@
276
277$(BUILD_PATH)/include/.installed: $(MUSL_PATH)/lib/libc.so
278	$(MAKE) -C $(MUSL_PATH) DESTDIR=$(BUILD_PATH) install-headers
279	touch $@
280
281$(MUSL_CC): $(MUSL_PATH)/lib/libc.so
282	sh $(MUSL_PATH)/tools/musl-gcc.specs.sh $(BUILD_PATH)/include $(MUSL_PATH)/lib /lib/ld-linux.so.1 > $(BUILD_PATH)/musl-gcc.specs
283	printf '#!/bin/sh\nexec "$(REAL_CC)" --specs="$(BUILD_PATH)/musl-gcc.specs" "$$@"\n' > $(BUILD_PATH)/musl-gcc
284	chmod +x $(BUILD_PATH)/musl-gcc
285
286$(IPERF_PATH)/.installed: $(IPERF_TAR)
287	mkdir -p $(BUILD_PATH)
288	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
289	sed -i '1s/^/#include <stdint.h>/' $(IPERF_PATH)/src/cjson.h $(IPERF_PATH)/src/timer.h
290	sed -i -r 's/-p?g//g' $(IPERF_PATH)/src/Makefile*
291	touch $@
292
293$(IPERF_PATH)/src/iperf3: | $(IPERF_PATH)/.installed $(USERSPACE_DEPS)
294	cd $(IPERF_PATH) && CFLAGS="$(CFLAGS) -D_GNU_SOURCE" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --with-openssl=no
295	$(MAKE) -C $(IPERF_PATH)
296	$(STRIP) -s $@
297
298$(LIBMNL_PATH)/.installed: $(LIBMNL_TAR)
299	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
300	touch $@
301
302$(LIBMNL_PATH)/src/.libs/libmnl.a: | $(LIBMNL_PATH)/.installed $(USERSPACE_DEPS)
303	cd $(LIBMNL_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared
304	$(MAKE) -C $(LIBMNL_PATH)
305	sed -i 's:prefix=.*:prefix=$(LIBMNL_PATH):' $(LIBMNL_PATH)/libmnl.pc
306
307$(WIREGUARD_TOOLS_PATH)/.installed: $(WIREGUARD_TOOLS_TAR)
308	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
309	touch $@
310
311$(WIREGUARD_TOOLS_PATH)/src/wg: | $(WIREGUARD_TOOLS_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
312	LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" $(MAKE) -C $(WIREGUARD_TOOLS_PATH)/src LIBMNL_CFLAGS="-I$(LIBMNL_PATH)/include" LIBMNL_LDLIBS="-lmnl" wg
313	$(STRIP) -s $@
314
315$(BUILD_PATH)/init: init.c | $(USERSPACE_DEPS)
316	mkdir -p $(BUILD_PATH)
317	$(MUSL_CC) -o $@ $(CFLAGS) $(LDFLAGS) -std=gnu11 $<
318	$(STRIP) -s $@
319
320$(IPUTILS_PATH)/.installed: $(IPUTILS_TAR)
321	mkdir -p $(BUILD_PATH)
322	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
323	touch $@
324
325$(IPUTILS_PATH)/ping: | $(IPUTILS_PATH)/.installed $(USERSPACE_DEPS)
326	sed -i /atexit/d $(IPUTILS_PATH)/ping.c
327	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)
328	$(STRIP) -s $@
329
330$(BASH_PATH)/.installed: $(BASH_TAR)
331	mkdir -p $(BUILD_PATH)
332	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
333	touch $@
334
335$(BASH_PATH)/bash: | $(BASH_PATH)/.installed $(USERSPACE_DEPS)
336	cd $(BASH_PATH) && ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --without-bash-malloc --disable-debugger --disable-help-builtin --disable-history --disable-multibyte --disable-progcomp --disable-readline --disable-mem-scramble
337	$(MAKE) -C $(BASH_PATH)
338	$(STRIP) -s $@
339
340$(IPROUTE2_PATH)/.installed: $(IPROUTE2_TAR)
341	mkdir -p $(BUILD_PATH)
342	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
343	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:=y\nHAVE_BERKELEY_DB:=n\nHAVE_LATEX:=n\nHAVE_PDFLATEX:=n\nCFLAGS+=-DHAVE_SETNS -DHAVE_LIBMNL -I$(LIBMNL_PATH)/include\nLDLIBS+=-lmnl' > $(IPROUTE2_PATH)/config.mk
344	printf 'lib: snapshot\n\t$$(MAKE) -C lib\nip/ip: lib\n\t$$(MAKE) -C ip ip\nmisc/ss: lib\n\t$$(MAKE) -C misc ss\n' >> $(IPROUTE2_PATH)/Makefile
345	touch $@
346
347$(IPROUTE2_PATH)/ip/ip: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
348	LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ ip/ip
349	$(STRIP) -s $(IPROUTE2_PATH)/ip/ip
350
351$(IPROUTE2_PATH)/misc/ss: | $(IPROUTE2_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
352	LDFLAGS="$(LDFLAGS) -L$(LIBMNL_PATH)/src/.libs" PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ misc/ss
353	$(STRIP) -s $(IPROUTE2_PATH)/misc/ss
354
355$(IPTABLES_PATH)/.installed: $(IPTABLES_TAR)
356	mkdir -p $(BUILD_PATH)
357	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
358	sed -i -e "/nfnetlink=[01]/s:=[01]:=0:" -e "/nfconntrack=[01]/s:=[01]:=0:" $(IPTABLES_PATH)/configure
359	touch $@
360
361$(IPTABLES_PATH)/iptables/xtables-legacy-multi: | $(IPTABLES_PATH)/.installed $(LIBMNL_PATH)/src/.libs/libmnl.a $(USERSPACE_DEPS)
362	cd $(IPTABLES_PATH) && PKG_CONFIG_LIBDIR="$(LIBMNL_PATH)" ./configure --prefix=/ $(CROSS_COMPILE_FLAG) --enable-static --disable-shared --disable-nftables --disable-bpf-compiler --disable-nfsynproxy --disable-libipq --with-kernel=$(BUILD_PATH)/include
363	$(MAKE) -C $(IPTABLES_PATH)
364	$(STRIP) -s $@
365
366$(NMAP_PATH)/.installed: $(NMAP_TAR)
367	mkdir -p $(BUILD_PATH)
368	flock -s $<.lock tar -C $(BUILD_PATH) -xf $<
369	touch $@
370
371$(NMAP_PATH)/ncat/ncat: | $(NMAP_PATH)/.installed $(USERSPACE_DEPS)
372	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
373	$(MAKE) -C $(NMAP_PATH)/libpcap
374	$(MAKE) -C $(NMAP_PATH)/ncat
375	$(STRIP) -s $@
376
377clean:
378	rm -rf $(BUILD_PATH)
379
380distclean: clean
381	rm -rf $(DISTFILES_PATH)
382
383menuconfig: $(KERNEL_BUILD_PATH)/.config
384	$(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_BUILD_PATH) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) menuconfig
385
386.PHONY: qemu build clean distclean menuconfig
387.DELETE_ON_ERROR:
388