1 # SPDX-License-Identifier: GPL-2.0 2 # 3 # Copyright (C) 2015-2019 Jason A. Donenfeld <J 4 5 PWD := $(shell pwd) 6 7 # Set these from the environment to override 8 KERNEL_PATH ?= $(PWD)/../../../../.. 9 BUILD_PATH ?= $(PWD)/build/$(ARCH) 10 DISTFILES_PATH ?= $(PWD)/distfiles 11 NR_CPUS ?= 4 12 ARCH ?= 13 CBUILD := $(shell gcc -dumpmachine) 14 HOST_ARCH := $(firstword $(subst -, ,$(CBUILD) 15 ifeq ($(ARCH),) 16 ARCH := $(HOST_ARCH) 17 endif 18 19 MIRROR := https://download.wireguard.com/qemu- 20 21 KERNEL_BUILD_PATH := $(BUILD_PATH)/kernel$(if 22 23 default: qemu 24 25 # variable name, tarball project name, version 26 define 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),$( 32 endef 33 34 define file_download = 35 $(DISTFILES_PATH)/$(1): | $(4) 36 mkdir -p $(DISTFILES_PATH) 37 flock -x $$@.lock -c '[ -f $$@ ] && ex 38 endef 39 40 $(eval $(call tar_download,IPERF,iperf,3.11,.t 41 $(eval $(call tar_download,BASH,bash,5.1.16,.t 42 $(eval $(call tar_download,IPROUTE2,iproute2,5 43 $(eval $(call tar_download,IPTABLES,iptables,1 44 $(eval $(call tar_download,NMAP,nmap,7.92,.tgz 45 $(eval $(call tar_download,IPUTILS,iputils,s20 46 $(eval $(call tar_download,WIREGUARD_TOOLS,wir 47 48 export CFLAGS := -O3 -pipe 49 ifeq ($(HOST_ARCH),$(ARCH)) 50 CFLAGS += -march=native 51 endif 52 export LDFLAGS := 53 export CPPFLAGS := 54 55 QEMU_VPORT_RESULT := 56 ifeq ($(ARCH),aarch64) 57 CHOST := aarch64-linux-musl 58 QEMU_ARCH := aarch64 59 KERNEL_ARCH := arm64 60 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ar 61 QEMU_VPORT_RESULT := virtio-serial-device 62 ifeq ($(HOST_ARCH),$(ARCH)) 63 QEMU_MACHINE := -cpu host -machine virt,gic_ve 64 else 65 QEMU_MACHINE := -cpu max -machine virt 66 CFLAGS += -march=armv8-a 67 endif 68 else ifeq ($(ARCH),aarch64_be) 69 CHOST := aarch64_be-linux-musl 70 QEMU_ARCH := aarch64 71 KERNEL_ARCH := arm64 72 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ar 73 QEMU_VPORT_RESULT := virtio-serial-device 74 ifeq ($(HOST_ARCH),$(ARCH)) 75 QEMU_MACHINE := -cpu host -machine virt,gic_ve 76 else 77 QEMU_MACHINE := -cpu max -machine virt 78 CFLAGS += -march=armv8-a 79 endif 80 else ifeq ($(ARCH),arm) 81 CHOST := arm-linux-musleabi 82 QEMU_ARCH := arm 83 KERNEL_ARCH := arm 84 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ar 85 QEMU_VPORT_RESULT := virtio-serial-device 86 ifeq ($(HOST_ARCH),$(ARCH)) 87 QEMU_MACHINE := -cpu host -machine virt,gic_ve 88 else 89 QEMU_MACHINE := -cpu max -machine virt 90 CFLAGS += -march=armv7-a -mabi=aapcs-linux 91 endif 92 else ifeq ($(ARCH),armeb) 93 CHOST := armeb-linux-musleabi 94 QEMU_ARCH := arm 95 KERNEL_ARCH := arm 96 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ar 97 QEMU_VPORT_RESULT := virtio-serial-device 98 ifeq ($(HOST_ARCH),$(ARCH)) 99 QEMU_MACHINE := -cpu host -machine virt,gic_ve 100 else 101 QEMU_MACHINE := -cpu max -machine virt 102 CFLAGS += -march=armv7-a -mabi=aapcs-linux 103 LDFLAGS += -Wl,--be8 104 endif 105 else ifeq ($(ARCH),x86_64) 106 CHOST := x86_64-linux-musl 107 QEMU_ARCH := x86_64 108 KERNEL_ARCH := x86_64 109 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x8 110 QEMU_VPORT_RESULT := virtio-serial-device 111 ifeq ($(HOST_ARCH),$(ARCH)) 112 QEMU_MACHINE := -cpu host -machine microvm,acc 113 else 114 QEMU_MACHINE := -cpu max -machine microvm,acpi 115 endif 116 else ifeq ($(ARCH),i686) 117 CHOST := i686-linux-musl 118 QEMU_ARCH := i386 119 KERNEL_ARCH := x86 120 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/x8 121 QEMU_VPORT_RESULT := virtio-serial-device 122 ifeq ($(subst x86_64,i686,$(HOST_ARCH)),$(ARCH 123 QEMU_MACHINE := -cpu host -machine microvm,acc 124 else 125 QEMU_MACHINE := -cpu coreduo -machine microvm, 126 endif 127 else ifeq ($(ARCH),mips64) 128 CHOST := mips64-linux-musl 129 QEMU_ARCH := mips64 130 KERNEL_ARCH := mips 131 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 132 ifeq ($(HOST_ARCH),$(ARCH)) 133 QEMU_MACHINE := -cpu host -machine malta,accel 134 CFLAGS += -EB 135 else 136 QEMU_MACHINE := -cpu MIPS64R2-generic -machine 137 CFLAGS += -march=mips64r2 -EB 138 endif 139 else ifeq ($(ARCH),mips64el) 140 CHOST := mips64el-linux-musl 141 QEMU_ARCH := mips64el 142 KERNEL_ARCH := mips 143 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 144 ifeq ($(HOST_ARCH),$(ARCH)) 145 QEMU_MACHINE := -cpu host -machine malta,accel 146 CFLAGS += -EL 147 else 148 QEMU_MACHINE := -cpu MIPS64R2-generic -machine 149 CFLAGS += -march=mips64r2 -EL 150 endif 151 else ifeq ($(ARCH),mips) 152 CHOST := mips-linux-musl 153 QEMU_ARCH := mips 154 KERNEL_ARCH := mips 155 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 156 ifeq ($(HOST_ARCH),$(ARCH)) 157 QEMU_MACHINE := -cpu host -machine malta,accel 158 CFLAGS += -EB 159 else 160 QEMU_MACHINE := -cpu 24Kf -machine malta -smp 161 CFLAGS += -march=mips32r2 -EB 162 endif 163 else ifeq ($(ARCH),mipsel) 164 CHOST := mipsel-linux-musl 165 QEMU_ARCH := mipsel 166 KERNEL_ARCH := mips 167 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 168 ifeq ($(HOST_ARCH),$(ARCH)) 169 QEMU_MACHINE := -cpu host -machine malta,accel 170 CFLAGS += -EL 171 else 172 QEMU_MACHINE := -cpu 24Kf -machine malta -smp 173 CFLAGS += -march=mips32r2 -EL 174 endif 175 else ifeq ($(ARCH),powerpc64) 176 CHOST := powerpc64-linux-musl 177 QEMU_ARCH := ppc64 178 KERNEL_ARCH := powerpc 179 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 180 ifeq ($(HOST_ARCH),$(ARCH)) 181 QEMU_MACHINE := -cpu host,accel=kvm -machine p 182 else 183 QEMU_MACHINE := -machine pseries -device spapr 184 endif 185 else ifeq ($(ARCH),powerpc64le) 186 CHOST := powerpc64le-linux-musl 187 QEMU_ARCH := ppc64 188 KERNEL_ARCH := powerpc 189 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 190 ifeq ($(HOST_ARCH),$(ARCH)) 191 QEMU_MACHINE := -cpu host,accel=kvm -machine p 192 else 193 QEMU_MACHINE := -machine pseries -device spapr 194 endif 195 else ifeq ($(ARCH),powerpc) 196 CHOST := powerpc-linux-musl 197 QEMU_ARCH := ppc 198 KERNEL_ARCH := powerpc 199 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/po 200 ifeq ($(HOST_ARCH),$(ARCH)) 201 QEMU_MACHINE := -cpu host,accel=kvm -machine p 202 else 203 QEMU_MACHINE := -machine ppce500 204 endif 205 else ifeq ($(ARCH),m68k) 206 CHOST := m68k-linux-musl 207 QEMU_ARCH := m68k 208 KERNEL_ARCH := m68k 209 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 210 KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMD 211 QEMU_VPORT_RESULT := virtio-serial-device 212 ifeq ($(HOST_ARCH),$(ARCH)) 213 QEMU_MACHINE := -cpu host,accel=kvm -machine v 214 else 215 QEMU_MACHINE := -machine virt -smp 1 -append $ 216 endif 217 else ifeq ($(ARCH),riscv64) 218 CHOST := riscv64-linux-musl 219 QEMU_ARCH := riscv64 220 KERNEL_ARCH := riscv 221 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ri 222 QEMU_VPORT_RESULT := virtio-serial-device 223 ifeq ($(HOST_ARCH),$(ARCH)) 224 QEMU_MACHINE := -cpu host,accel=kvm -machine v 225 else 226 QEMU_MACHINE := -cpu rv64 -machine virt 227 endif 228 else ifeq ($(ARCH),riscv32) 229 CHOST := riscv32-linux-musl 230 QEMU_ARCH := riscv32 231 KERNEL_ARCH := riscv 232 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/ri 233 QEMU_VPORT_RESULT := virtio-serial-device 234 ifeq ($(HOST_ARCH),$(ARCH)) 235 QEMU_MACHINE := -cpu host,accel=kvm -machine v 236 else 237 QEMU_MACHINE := -cpu rv32 -machine virt 238 endif 239 else ifeq ($(ARCH),s390x) 240 CHOST := s390x-linux-musl 241 QEMU_ARCH := s390x 242 KERNEL_ARCH := s390 243 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/arch/s3 244 KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMD 245 QEMU_VPORT_RESULT := virtio-serial-ccw 246 ifeq ($(HOST_ARCH),$(ARCH)) 247 QEMU_MACHINE := -cpu host,accel=kvm -machine s 248 else 249 QEMU_MACHINE := -cpu max -machine s390-ccw-vir 250 endif 251 else ifeq ($(ARCH),um) 252 CHOST := $(HOST_ARCH)-linux-musl 253 KERNEL_BZIMAGE := $(KERNEL_BUILD_PATH)/vmlinux 254 KERNEL_ARCH := um 255 KERNEL_CMDLINE := $(shell sed -n 's/CONFIG_CMD 256 else 257 $(error I only build: x86_64, i686, arm, armeb 258 endif 259 260 TOOLCHAIN_FILENAME := $(CHOST)-cross.tgz 261 TOOLCHAIN_TAR := $(DISTFILES_PATH)/$(TOOLCHAIN 262 TOOLCHAIN_PATH := $(BUILD_PATH)/$(CHOST)-cross 263 TOOLCHAIN_DIR := https://download.wireguard.co 264 $(eval $(call file_download,toolchain-sha256su 265 $(eval $(call file_download,$(TOOLCHAIN_FILENA 266 267 STRIP := $(CHOST)-strip 268 CROSS_COMPILE_FLAG := --build=$(CBUILD) --host 269 $(info Building for $(CHOST) using $(CBUILD)) 270 ifneq ($(ARCH),um) 271 export CROSS_COMPILE := $(CHOST)- 272 endif 273 export PATH := $(TOOLCHAIN_PATH)/bin:$(PATH) 274 export CC := $(CHOST)-gcc 275 CCACHE_PATH := $(shell which ccache 2>/dev/nul 276 ifneq ($(CCACHE_PATH),) 277 export KBUILD_BUILD_TIMESTAMP := Fri Jun 5 15 278 export PATH := $(TOOLCHAIN_PATH)/bin/ccache:$( 279 export CCACHE_SLOPPINESS := file_macro,time_ma 280 export CCACHE_DIR ?= $(PWD)/ccache 281 endif 282 283 USERSPACE_DEPS := $(TOOLCHAIN_PATH)/.installed 284 285 comma := , 286 build: $(KERNEL_BZIMAGE) 287 qemu: $(KERNEL_BZIMAGE) 288 rm -f $(BUILD_PATH)/result 289 ifneq ($(ARCH),um) 290 timeout --foreground 20m qemu-system-$ 291 -nodefaults \ 292 -nographic \ 293 -smp $(NR_CPUS) \ 294 $(QEMU_MACHINE) \ 295 -m $$(grep -q CONFIG_DEBUG_KME 296 -serial stdio \ 297 -chardev file,path=$(BUILD_PAT 298 $(if $(QEMU_VPORT_RESULT),-dev 299 -no-reboot \ 300 -monitor none \ 301 -kernel $< 302 else 303 timeout --foreground 20m $< \ 304 $(KERNEL_CMDLINE) \ 305 mem=$$(grep -q CONFIG_DEBUG_KM 306 noreboot \ 307 con1=fd:51 51>$(BUILD_PATH)/re 308 endif 309 grep -Fq success $(BUILD_PATH)/result 310 311 $(BUILD_PATH)/init-cpio-spec.txt: $(TOOLCHAIN_ 312 mkdir -p $(BUILD_PATH) 313 echo "file /init $(BUILD_PATH)/init 75 314 echo "file /init.sh $(PWD)/../netns.sh 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)/s 319 echo "file /bin/wg $(WIREGUARD_TOOLS_P 320 echo "file /bin/bash $(BASH_PATH)/bash 321 echo "file /bin/ip $(IPROUTE2_PATH)/ip 322 echo "file /bin/ss $(IPROUTE2_PATH)/mi 323 echo "file /bin/ping $(IPUTILS_PATH)/p 324 echo "file /bin/ncat $(NMAP_PATH)/ncat 325 echo "file /bin/xtables-legacy-multi $ 326 echo "slink /bin/iptables xtables-lega 327 echo "slink /bin/ping6 ping 777 0 0" > 328 echo "dir /lib 755 0 0" >> $@ 329 echo "file /lib/libc.so $(TOOLCHAIN_PA 330 echo "slink $$($(CHOST)-readelf -p .in 331 332 $(KERNEL_BUILD_PATH)/.config: $(TOOLCHAIN_PATH 333 mkdir -p $(KERNEL_BUILD_PATH) 334 cp kernel.config $(KERNEL_BUILD_PATH)/ 335 printf 'CONFIG_NR_CPUS=$(NR_CPUS)\nCON 336 cat arch/$(ARCH).config >> $(KERNEL_BU 337 $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_B 338 cd $(KERNEL_BUILD_PATH) && ARCH=$(KERN 339 $(if $(findstring yes,$(DEBUG_KERNEL)) 340 341 $(KERNEL_BZIMAGE): $(TOOLCHAIN_PATH)/.installe 342 $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_B 343 .PHONY: $(KERNEL_BZIMAGE) 344 345 $(TOOLCHAIN_PATH)/$(CHOST)/include/linux/.inst 346 ifneq ($(ARCH),um) 347 rm -rf $(TOOLCHAIN_PATH)/$(CHOST)/incl 348 $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_B 349 endif 350 touch $@ 351 352 $(TOOLCHAIN_PATH)/.installed: $(TOOLCHAIN_TAR) 353 mkdir -p $(BUILD_PATH) 354 flock -s $<.lock tar -C $(BUILD_PATH) 355 $(STRIP) -s $(TOOLCHAIN_PATH)/$(CHOST) 356 ifneq ($(CCACHE_PATH),) 357 mkdir -p $(TOOLCHAIN_PATH)/bin/ccache 358 ln -s $(CCACHE_PATH) $(TOOLCHAIN_PATH) 359 endif 360 touch $@ 361 362 $(IPERF_PATH)/.installed: $(IPERF_TAR) 363 mkdir -p $(BUILD_PATH) 364 flock -s $<.lock tar -C $(BUILD_PATH) 365 sed -i '1s/^/#include <stdint.h>/' $(I 366 sed -i -r 's/-p?g//g' $(IPERF_PATH)/sr 367 touch $@ 368 369 $(IPERF_PATH)/src/iperf3: | $(IPERF_PATH)/.ins 370 cd $(IPERF_PATH) && autoreconf -fi 371 cd $(IPERF_PATH) && CFLAGS="$(CFLAGS) 372 $(MAKE) -C $(IPERF_PATH) 373 $(STRIP) -s $@ 374 375 $(WIREGUARD_TOOLS_PATH)/.installed: $(WIREGUAR 376 mkdir -p $(BUILD_PATH) 377 flock -s $<.lock tar -C $(BUILD_PATH) 378 touch $@ 379 380 $(WIREGUARD_TOOLS_PATH)/src/wg: | $(WIREGUARD_ 381 $(MAKE) -C $(WIREGUARD_TOOLS_PATH)/src 382 $(STRIP) -s $@ 383 384 $(BUILD_PATH)/init: init.c | $(USERSPACE_DEPS) 385 mkdir -p $(BUILD_PATH) 386 $(CC) -o $@ $(CFLAGS) $(LDFLAGS) -std= 387 $(STRIP) -s $@ 388 389 $(IPUTILS_PATH)/.installed: $(IPUTILS_TAR) 390 mkdir -p $(BUILD_PATH) 391 flock -s $<.lock tar -C $(BUILD_PATH) 392 touch $@ 393 394 $(IPUTILS_PATH)/ping: | $(IPUTILS_PATH)/.insta 395 sed -i /atexit/d $(IPUTILS_PATH)/ping. 396 cd $(IPUTILS_PATH) && $(CC) $(CFLAGS) 397 $(STRIP) -s $@ 398 399 $(BASH_PATH)/.installed: $(BASH_TAR) 400 mkdir -p $(BUILD_PATH) 401 flock -s $<.lock tar -C $(BUILD_PATH) 402 touch $@ 403 404 $(BASH_PATH)/bash: | $(BASH_PATH)/.installed $ 405 cd $(BASH_PATH) && ./configure --prefi 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) 412 printf 'CC:=$(CC)\nPKG_CONFIG:=pkg-con 413 printf 'libutil.a.done:\n\tflock -x $$ 414 touch $@ 415 416 $(IPROUTE2_PATH)/ip/ip: | $(IPROUTE2_PATH)/.in 417 $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ i 418 $(STRIP) -s $@ 419 420 $(IPROUTE2_PATH)/misc/ss: | $(IPROUTE2_PATH)/. 421 $(MAKE) -C $(IPROUTE2_PATH) PREFIX=/ m 422 $(STRIP) -s $@ 423 424 $(IPTABLES_PATH)/.installed: $(IPTABLES_TAR) 425 mkdir -p $(BUILD_PATH) 426 flock -s $<.lock tar -C $(BUILD_PATH) 427 sed -i -e "/nfnetlink=[01]/s:=[01]:=0: 428 touch $@ 429 430 $(IPTABLES_PATH)/iptables/xtables-legacy-multi 431 cd $(IPTABLES_PATH) && ./configure --p 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) 438 touch $@ 439 440 $(NMAP_PATH)/ncat/ncat: | $(NMAP_PATH)/.instal 441 cd $(NMAP_PATH) && ./configure --prefi 442 $(MAKE) -C $(NMAP_PATH)/libpcap 443 $(MAKE) -C $(NMAP_PATH)/ncat 444 $(STRIP) -s $@ 445 446 clean: 447 rm -rf $(BUILD_PATH) 448 449 distclean: clean 450 rm -rf $(DISTFILES_PATH) 451 452 cacheclean: clean 453 ifneq ($(CCACHE_DIR),) 454 rm -rf $(CCACHE_DIR) 455 endif 456 457 menuconfig: $(KERNEL_BUILD_PATH)/.config 458 $(MAKE) -C $(KERNEL_PATH) O=$(KERNEL_B 459 460 .PHONY: qemu build clean distclean cacheclean 461 .DELETE_ON_ERROR:
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.