blob: 4e3bd663fc739dd64ac58bcd6ce97cbebd3d3e84 [file] [log] [blame]
David Brazdile8ebf272020-07-24 10:00:31 +00001# Copyright 2020 The Android KVM Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
David Brazdilef8506d2020-07-27 19:56:16 +000015SELF := $(abspath $(lastword $(MAKEFILE_LIST)))
16
Elena Petrova467cfd92021-01-15 14:56:04 +000017CPUS ?= $(shell nproc)
18MAKEFLAGS += --jobs=$(CPUS)
David Brazdil94acb6f2021-08-23 08:35:03 +000019SHELL ?= $(shell which bash)
Elena Petrova467cfd92021-01-15 14:56:04 +000020
Fuad Tabba42a3b292020-05-13 15:04:03 +010021##
22## Folders
23##
David Brazdilacc8ea42020-04-28 16:01:08 +010024ROOT_DIR := $(CURDIR)
Fuad Tabbad99cc752020-08-17 11:31:56 +000025
David Brazdilacc8ea42020-04-28 16:01:08 +010026OUT_DIR := $(ROOT_DIR)/out
Fuad Tabbacd908402021-02-09 17:18:15 +000027
28# DIST_DIR: target directory for artifacts and logs.
29# The CI build (pKVM Busytown) uses it to separate artifacts from other
30# temporary build files, which are placed in in OUT_DIR.
31# For other users, OUT_DIR and DIST_DIR are typically the same.
32DIST_DIR ?= $(OUT_DIR)
33
34# Copy specified file or directory only if OUT_DIR and DIST_DIR aren't the same.
35#
36# Parameters:
37# $(1): Source files or directories (to be copied recursively)
38# $(2): Destination directory (is created if it doesn't exist)
39ifneq ($(abspath $(OUT_DIR)), $(abspath $(DIST_DIR)))
40define copy_to_dist
41 mkdir -p $(2) && cp -r $(1) $(2)
42endef
Fuad Tabbad99cc752020-08-17 11:31:56 +000043endif
44
Fuad Tabbaa74ba082020-05-29 11:56:18 +010045LINUX_OUT := $(OUT_DIR)/linux
Fuad Tabbacd908402021-02-09 17:18:15 +000046LINUX_DIST := $(DIST_DIR)/linux
David Brazdilef8506d2020-07-27 19:56:16 +000047SCRIPTS_DIR := $(ROOT_DIR)/build
David Brazdilacc8ea42020-04-28 16:01:08 +010048
Fuad Tabba42a3b292020-05-13 15:04:03 +010049##
50## Common options
51##
52VERBOSE ?= 0
Fuad Tabba7f167f72020-09-17 15:26:45 +010053ARCH = arm64
Fuad Tabbaa74ba082020-05-29 11:56:18 +010054
Pierre-Clément Tosi5133e852021-08-03 12:25:08 +000055DEFAULT_KERNEL_IMAGE := $(LINUX_OUT)/arch/arm64/boot/Image
Fuad Tabbaa74ba082020-05-29 11:56:18 +010056# KERNEL_IMAGE can be set to use own custom kernel image.
Pierre-Clément Tosi5133e852021-08-03 12:25:08 +000057KERNEL_IMAGE ?= $(DEFAULT_KERNEL_IMAGE)
Fuad Tabbaa74ba082020-05-29 11:56:18 +010058
David Brazdilacc8ea42020-04-28 16:01:08 +010059##
Fuad Tabba7f167f72020-09-17 15:26:45 +010060## Toolchain options and variables
61##
62
63# Specifies which toolchain of the prebuilt toolchains to use.
David Brazdil53a70302021-08-20 13:32:11 +000064# Can be either clang, gcc-5.1, or gcc-9.2.
Fuad Tabba67618472021-04-27 11:56:35 +010065# GCC will always use the latest version for building everything else other
66# than the kernel.
Fuad Tabba7f167f72020-09-17 15:26:45 +010067TOOLCHAIN ?= clang
68
69# Find the toolchain path for the current OS.
70UNNAME_S := $(shell uname -s | tr '[:upper:]' '[:lower:]')
Fuad Tabba3aeba792021-04-27 10:30:08 +010071TOOLCHAIN_CLANG := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/clang/clang-r416183b/
Fuad Tabba7f167f72020-09-17 15:26:45 +010072TOOLCHAIN_GCC_49 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-4.9/
Fuad Tabbaaa07f1e2021-02-04 14:14:19 +000073TOOLCHAIN_GCC_51 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-5.1/
Fuad Tabba7f167f72020-09-17 15:26:45 +010074TOOLCHAIN_GCC_92 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-9.2/
75
76# Set the toolchain build binary paths and prefixes.
Elena Petrova9cbf9b92021-02-01 12:53:31 +000077TARGET = aarch64-linux-gnu
78TARGET_GCC_92 = aarch64-none-linux-gnu
79
David Brazdil53a70302021-08-20 13:32:11 +000080ifeq ($(TOOLCHAIN), gcc-5.1)
Fuad Tabbaaa07f1e2021-02-04 14:14:19 +000081TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_51)/bin
Fuad Tabba7f167f72020-09-17 15:26:45 +010082else ifeq ($(TOOLCHAIN), gcc-9.2)
83TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_92)/bin
Elena Petrova9cbf9b92021-02-01 12:53:31 +000084TARGET = $(TARGET_GCC_92)
Fuad Tabba7f167f72020-09-17 15:26:45 +010085else ifeq ($(TOOLCHAIN), clang)
86TOOLCHAIN_BIN := $(TOOLCHAIN_CLANG)/bin
87CC := $(TOOLCHAIN_BIN)/clang
88LD := $(TOOLCHAIN_BIN)/ld.lld
89OBJCOPY := $(TOOLCHAIN_BIN)/llvm-objcopy
90OBJDUMP := $(TOOLCHAIN_BIN)/llvm-objdump
Fuad Tabba67618472021-04-27 11:56:35 +010091# The GCC toolchain is needed when building kvm-unit-tests and Linux with Clang.
Fuad Tabbaeac7ff62021-02-10 17:30:19 +000092GCC_TOOLCHAIN_DIR = $(TOOLCHAIN_GCC_49)
Fuad Tabba72cc5fd2020-11-03 13:01:51 +000093GCC_TOOLCHAIN_BIN = $(GCC_TOOLCHAIN_DIR)/bin/
Fuad Tabba67618472021-04-27 11:56:35 +010094LINUX_EXTRA = LLVM=1 LLVM_IAS=1 GCC_TOOLCHAIN_DIR="$(GCC_TOOLCHAIN_BIN)"
Fuad Tabba72cc5fd2020-11-03 13:01:51 +000095# Some targets require additional flags to build with Clang.
96COMMON_CFLAGS := -Qunused-arguments --target=$(TARGET) -fno-integrated-as -Wno-asm-operand-widths -fpic --gcc-toolchain=$(GCC_TOOLCHAIN_DIR) --sysroot=$(GCC_TOOLCHAIN_DIR)/$(TARGET)/libc --prefix=$(GCC_TOOLCHAIN_BIN)/$(TARGET)-
Fuad Tabba7f167f72020-09-17 15:26:45 +010097else
98$(error Unrecognized toolchain: TOOLCHAIN=$(TOOLCHAIN))
99endif
100
Fuad Tabba67618472021-04-27 11:56:35 +0100101LINUX_TOOLCHAIN_BIN := $(TOOLCHAIN_BIN)
102LINUX_TARGET := $(TARGET)
103
104# Common to all GCC versions:
105# Use the latest GCC toolchain to build everything other than the kernel
106# because kvm-unit-tests as well as u-boot require recent versions.
Fuad Tabba7f167f72020-09-17 15:26:45 +0100107ifneq ($(TOOLCHAIN), clang)
Fuad Tabba67618472021-04-27 11:56:35 +0100108TARGET = $(TARGET_GCC_92)
109TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_92)/bin
110GCC_TOOLCHAIN_BIN := $(TOOLCHAIN_BIN)
Fuad Tabba7f167f72020-09-17 15:26:45 +0100111CC := $(TOOLCHAIN_BIN)/$(TARGET)-gcc
112LD := $(TOOLCHAIN_BIN)/$(TARGET)-ld
113OBJCOPY := $(TOOLCHAIN_BIN)/$(TARGET)-objcopy
114OBJDUMP := $(TOOLCHAIN_BIN)/$(TARGET)-objdump
Fuad Tabba7f167f72020-09-17 15:26:45 +0100115endif
116
117##
David Brazdilacc8ea42020-04-28 16:01:08 +0100118## Common targets
119##
120
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100121.DEFAULT_GOAL := default
122
123.PHONY: default
David Brazdil54fb7442020-07-27 09:54:56 +0000124default: kvm-unit-tests linux
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100125
David Brazdilacc8ea42020-04-28 16:01:08 +0100126.PHONY: clean
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000127clean: buildroot_clean kvm-unit-tests_clean linux_clean u-boot_clean
David Brazdilacc8ea42020-04-28 16:01:08 +0100128
Fuad Tabba42a3b292020-05-13 15:04:03 +0100129.PHONY: distclean
David Brazdilf770f602020-07-27 09:53:28 +0000130distclean:
Fuad Tabbacd908402021-02-09 17:18:15 +0000131 - rm -rf $(OUT_DIR) $(DIST_DIR)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100132
133.PHONY: all
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000134all: buildroot kvm-unit-tests linux u-boot
Fuad Tabba42a3b292020-05-13 15:04:03 +0100135
136.PHONY: help
137help:
138 @echo 'Generic Targets:'
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100139 @echo ' default - builds all generic targets except for buildroot'
140 @echo ' all - builds all generic targets'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100141 @echo ' buildroot - the buildroot rootfs image as well as the host qemu'
David Brazdil54fb7442020-07-27 09:54:56 +0000142 @echo ' kvm-unit-tests - the kvm-unit-tests'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100143 @echo ' linux - the linux kernel'
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000144 @echo ' u-boot - the u-boot'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100145 @echo ''
146 @echo 'Clean Targets:'
147 @echo ' clean - cleans all, but keeps the prebuilts'
148 @echo ' distclean - distclean for all targets'
149 @echo ' buildroot_clean - the buildroot rootfs image as well as the host qemu'
David Brazdil54fb7442020-07-27 09:54:56 +0000150 @echo ' kvm-unit-tests_clean - the kvm-unit-tests'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100151 @echo ' linux_clean - the linux kernel'
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000152 @echo ' u-boot_clean - the u-boot'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100153 @echo ''
David Brazdilef8506d2020-07-27 19:56:16 +0000154 @echo 'Test Targets:'
155 @echo ' test - runs all tests in qemu-aarch64 simulated environment'
156 @echo ' test-kut - runs all kvm-unit-tests in qemu-aarch64 simulated environment'
157 @echo ' test-list - lists all test targets'
158 @echo ''
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200159 @echo 'Test Parameters:'
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200160 @echo ' CROSVM_TAR_GZ - path to a custom cvd-host_package.tar.gz'
161 @echo ' (default: uses a pre-built version)'
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200162 @echo ' VM_IMAGE - path to an image to be used by the VMM to boot the VM'
163 @echo ' (default: uses the host image)'
164 @echo ''
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000165 @echo 'Test Flags:'
166 @echo ' VERBOSE=[0|1] - prints the verbose output of the scripts running the test'
167 @echo ' (default: 0)'
168 @echo ' QUIET=[0|1] - redirects the output to a file instead of stdout'
169 @echo ' (default: 1)'
170 @echo ' GDB=[0|1] - makes the started test wait for a GDB connection'
171 @echo ' (default: 0)'
172 @echo ''
Fuad Tabba42a3b292020-05-13 15:04:03 +0100173 @echo 'Misc Targets:'
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100174 @echo ' update-prebuilts - generates/update the prebuilt rootfs image (aarch64) and the qemu host (aarch64)'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100175
David Brazdilacc8ea42020-04-28 16:01:08 +0100176
177##
178## Buildroot
179##
180
Fuad Tabba714a6922020-10-29 15:12:23 +0000181BR_DEFCONFIG ?= qemu_aarch64_virt_kvmunittests_defconfig
Fuad Tabba42a3b292020-05-13 15:04:03 +0100182BR_VERBOSE ?= $(VERBOSE)
David Brazdilacc8ea42020-04-28 16:01:08 +0100183
184BR_SRC := $(ROOT_DIR)/buildroot
185BR_OUT := $(OUT_DIR)/buildroot
186BR_MAKE := $(MAKE) -C $(BR_SRC) V=$(BR_VERBOSE) O=$(BR_OUT)
187
188.PHONY: buildroot
189buildroot:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100190 + $(BR_MAKE) $(BR_DEFCONFIG)
191 + $(BR_MAKE) all
David Brazdilacc8ea42020-04-28 16:01:08 +0100192
193.PHONY: buildroot_clean
194buildroot_clean:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100195 + $(BR_MAKE) clean
196
197
198##
David Brazdil54fb7442020-07-27 09:54:56 +0000199## kvm-unit-tests
Fuad Tabba42a3b292020-05-13 15:04:03 +0100200##
Fuad Tabba42a3b292020-05-13 15:04:03 +0100201KUT_SRC := $(ROOT_DIR)/kvm-unit-tests
202KUT_OUT := $(OUT_DIR)/kvm-unit-tests
Fuad Tabbacd908402021-02-09 17:18:15 +0000203KUT_DIST := $(DIST_DIR)/kvm-unit-tests
David Brazdilef8506d2020-07-27 19:56:16 +0000204KUT_STAMP := $(KUT_OUT)/kvm-unit-tests.stamp
Fuad Tabba62b21192020-05-13 11:57:26 +0100205
Fuad Tabba42a3b292020-05-13 15:04:03 +0100206
David Brazdil54fb7442020-07-27 09:54:56 +0000207.PHONY: kvm-unit-tests
David Brazdilef8506d2020-07-27 19:56:16 +0000208kvm-unit-tests $(KUT_STAMP):
David Brazdil8f61ea52020-07-27 09:50:56 +0000209 mkdir -p $(KUT_OUT)
210 cd $(KUT_OUT) && \
211 $(KUT_SRC)/configure \
212 --erratatxt=$(KUT_SRC)/errata.txt \
Fuad Tabba7f167f72020-09-17 15:26:45 +0100213 --prefix=$(KUT_OUT) --arch=$(ARCH) \
214 --cc=$(CC) --ld=$(LD) \
215 --objcopy=$(OBJCOPY) --objdump=$(OBJDUMP)
Fuad Tabba72cc5fd2020-11-03 13:01:51 +0000216 COMMON_CFLAGS="$(COMMON_CFLAGS)" \
David Brazdile6c26432020-07-27 16:17:44 +0000217 $(MAKE) -C $(KUT_OUT) standalone
Fuad Tabbacd908402021-02-09 17:18:15 +0000218 $(call copy_to_dist, $(KUT_OUT)/tests, $(KUT_DIST))
David Brazdilef8506d2020-07-27 19:56:16 +0000219 touch $(KUT_STAMP)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100220
David Brazdil54fb7442020-07-27 09:54:56 +0000221.PHONY: kvm-unit-tests_clean
222kvm-unit-tests_clean:
David Brazdilef8506d2020-07-27 19:56:16 +0000223 - rm -f $(KUT_STAMP)
David Brazdil8f61ea52020-07-27 09:50:56 +0000224 - $(MAKE) -C $(KUT_OUT) clean
Fuad Tabba42a3b292020-05-13 15:04:03 +0100225
226
227##
228## Linux kernel
229##
230LINUX_DEFCONFIG ?= defconfig
231LINUX_VERBOSE ?= $(VERBOSE)
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100232LINUX_SRC ?= $(ROOT_DIR)/linux
Fuad Tabbacd908402021-02-09 17:18:15 +0000233LINUX_VMLINUX = $(LINUX_OUT)/vmlinux
234LINUX_DIST_FILES = $(LINUX_CONFIG) $(LINUX_VMLINUX) $(KERNEL_IMAGE)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100235
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100236LINUX_MAKE := \
Fuad Tabba67618472021-04-27 11:56:35 +0100237 PATH=$(LINUX_TOOLCHAIN_BIN):$(PATH) \
Fuad Tabba7f167f72020-09-17 15:26:45 +0100238 ARCH=$(ARCH) \
Fuad Tabba67618472021-04-27 11:56:35 +0100239 CROSS_COMPILE="$(LINUX_TARGET)-" \
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100240 $(MAKE) \
Fuad Tabba67618472021-04-27 11:56:35 +0100241 $(LINUX_EXTRA) \
Fuad Tabba62b21192020-05-13 11:57:26 +0100242 -C $(LINUX_SRC) \
243 V=$(LINUX_VERBOSE) \
244 O=$(LINUX_OUT)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100245
David Brazdilef8506d2020-07-27 19:56:16 +0000246LINUX_CONFIG := $(LINUX_OUT)/.config
247
248.PHONY: linux_defconfig
249linux_defconfig $(LINUX_CONFIG):
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100250 + $(LINUX_MAKE) $(LINUX_DEFCONFIG)
David Brazdilef8506d2020-07-27 19:56:16 +0000251
David Brazdila7e35662020-12-03 09:40:38 +0000252.PHONY: linux_menuconfig
253linux_menuconfig: $(LINUX_CONFIG)
254 + $(LINUX_MAKE) menuconfig
255
David Brazdilef8506d2020-07-27 19:56:16 +0000256.PHONY: linux
257linux: $(LINUX_CONFIG)
Fuad Tabba990105b2020-05-12 15:39:52 +0100258 + $(LINUX_MAKE)
Fuad Tabbacd908402021-02-09 17:18:15 +0000259 $(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
Fuad Tabba42a3b292020-05-13 15:04:03 +0100260
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100261.PHONY: linux_image
Pierre-Clément Tosi5133e852021-08-03 12:25:08 +0000262linux_image $(DEFAULT_KERNEL_IMAGE): $(LINUX_CONFIG)
David Brazdilef8506d2020-07-27 19:56:16 +0000263 + $(LINUX_MAKE) Image.gz
Fuad Tabbacd908402021-02-09 17:18:15 +0000264 $(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
David Brazdilef8506d2020-07-27 19:56:16 +0000265
Fuad Tabba42a3b292020-05-13 15:04:03 +0100266.PHONY: linux_clean
267linux_clean:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100268 + $(LINUX_MAKE) mrproper
269
270
271##
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000272## u-boot
273##
274UBOOT_DEFCONFIG ?= pvmfw_arm64_defconfig
275UBOOT_VERBOSE ?= $(VERBOSE)
276UBOOT_SRC ?= $(ROOT_DIR)/u-boot
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000277UBOOT_OUT := $(OUT_DIR)/u-boot
278UBOOT_MAKE := \
Fuad Tabba67618472021-04-27 11:56:35 +0100279 PATH=$(TOOLCHAIN_BIN):$(GCC_TOOLCHAIN_BIN):$(PATH) \
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000280 ARCH=$(ARCH) \
Fuad Tabba67618472021-04-27 11:56:35 +0100281 CROSS_COMPILE="$(TARGET)-" \
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000282 $(MAKE) \
Fuad Tabba67618472021-04-27 11:56:35 +0100283 CC=$(CC) \
284 LD=$(LD) \
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000285 -C $(UBOOT_SRC) \
286 V=$(UBOOT_VERBOSE) \
287 O=$(UBOOT_OUT)
288
289UBOOT_CONFIG := $(UBOOT_OUT)/.config
Elena Petrova044fe522021-02-25 13:10:46 +0000290UBOOT_BIN := $(UBOOT_OUT)/u-boot.bin
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000291
292.PHONY: u-boot_defconfig
293u-boot_defconfig $(UBOOT_CONFIG):
294 + $(UBOOT_MAKE) $(UBOOT_DEFCONFIG)
295
296.PHONY: u-boot_menuconfig
297u-boot_menuconfig: $(UBOOT_CONFIG)
298 + $(UBOOT_MAKE) menuconfig
299
300.PHONY: u-boot
Elena Petrova044fe522021-02-25 13:10:46 +0000301u-boot $(UBOOT_BIN): $(UBOOT_CONFIG)
Elena Petrova9cbf9b92021-02-01 12:53:31 +0000302 + $(UBOOT_MAKE)
303
304.PHONY: u-boot_clean
305u-boot_clean:
306 + $(UBOOT_MAKE) mrproper
307
308
309##
Fuad Tabba62b21192020-05-13 11:57:26 +0100310## Generating/Updating prebuilts
311##
312
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200313PREBUILTS_CROSVM_EXT4 := prebuilts/linux-aarch64/images/crosvm.ext4
David Brazdile3446db2021-02-02 20:58:49 +0000314PREBUILTS_ROOTFS_EXT4 := prebuilts/linux-aarch64/images/rootfs.ext4
315PREBUILTS_QEMU_ROM := prebuilts/linux-aarch64/images/efi-virtio.rom
316PREBUILTS_QEMU_BIN := prebuilts/linux-x86/qemu
317
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200318CUSTOM_CROSVM_EXT4 := $(OUT_DIR)/images/crosvm.ext4
319
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200320ifeq ($(CROSVM_TAR_GZ),)
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200321CROSVM_EXT4 := $(PREBUILTS_CROSVM_EXT4)
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200322else
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200323CROSVM_EXT4 := $(CUSTOM_CROSVM_EXT4)
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200324endif
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200325
Fuad Tabba7f167f72020-09-17 15:26:45 +0100326.PHONY:update-prebuilts
Fuad Tabba62b21192020-05-13 11:57:26 +0100327update-prebuilts: \
David Brazdile3446db2021-02-02 20:58:49 +0000328 $(PREBUILTS_ROOTFS_EXT4) \
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200329 update-prebuilts-crosvm \
David Brazdile3446db2021-02-02 20:58:49 +0000330 $(PREBUILTS_QEMU_ROM) \
331 $(PREBUILTS_QEMU_BIN)
Fuad Tabba62b21192020-05-13 11:57:26 +0100332
David Brazdile3446db2021-02-02 20:58:49 +0000333$(PREBUILTS_ROOTFS_EXT4): buildroot
David Brazdil4f0603f2021-02-02 16:18:18 +0000334 mkdir -p $(shell dirname $@)
Fuad Tabba62b21192020-05-13 11:57:26 +0100335 cp $(BR_OUT)/images/rootfs.ext4 $@
David Brazdil4f0603f2021-02-02 16:18:18 +0000336
David Brazdile3446db2021-02-02 20:58:49 +0000337$(PREBUILTS_QEMU_ROM): buildroot
David Brazdil4f0603f2021-02-02 16:18:18 +0000338 mkdir -p $(shell dirname $@)
Fuad Tabba62b21192020-05-13 11:57:26 +0100339 cp $(BR_OUT)/per-package/host-qemu/host/share/qemu/efi-virtio.rom $@
340
David Brazdil4f0603f2021-02-02 16:18:18 +0000341CF_DOWNLOAD_AOSP_SH := $(ROOT_DIR)/build/cuttlefish/download-aosp.sh
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200342
343# A PHONY target is necessary for allowing the user to update the archive
344# because, as we are downloading it from a remote server, make dependencies are
345# unable to express the local-->remote dependency:
346.PHONY: update-prebuilts-crosvm
347update-prebuilts-crosvm $(PREBUILTS_CROSVM_TAR_GZ):
348 @rm -f $(PREBUILTS_CROSVM_TAR_GZ) # else, wget will create "$@.1"
349 cd $(dir $(PREBUILTS_CROSVM_TAR_GZ)) && $(CF_DOWNLOAD_AOSP_SH) -XCa arm64
350 @touch -c $(PREBUILTS_CROSVM_TAR_GZ) # wget doesn't set timestamp to now
351
David Brazdil4f0603f2021-02-02 16:18:18 +0000352CROSVM_IMG_SIZE_MB := 1024
353
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200354$(CUSTOM_CROSVM_EXT4): TMP_DIR := $(shell mktemp -d)
355$(CUSTOM_CROSVM_EXT4): $(CROSVM_TAR_GZ)
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200356 mkdir -p $(@D)
357 cd $(TMP_DIR) && tar xzvf $(abspath $<)
David Brazdil4f0603f2021-02-02 16:18:18 +0000358 dd if=/dev/zero of=$@.tmp bs=1M count=$(CROSVM_IMG_SIZE_MB)
359 mkfs.ext4 -d $(TMP_DIR) $@.tmp
360 rm -rf $(TMP_DIR)
361 mv $@.tmp $@
362
Fuad Tabbace135522020-08-26 10:15:47 +0100363# The qemu binary
David Brazdile3446db2021-02-02 20:58:49 +0000364.PHONY:$(PREBUILTS_QEMU_BIN)
365$(PREBUILTS_QEMU_BIN):
Fuad Tabba62b21192020-05-13 11:57:26 +0100366 - rm -rf $@
Fuad Tabba7f167f72020-09-17 15:26:45 +0100367 ${SCRIPTS_DIR}/aarch64/build_qemu.sh $(OUT_DIR)/qemu ${TOOLCHAIN_BIN}
Fuad Tabba62b21192020-05-13 11:57:26 +0100368 mkdir -p $@/bin
Fuad Tabbace135522020-08-26 10:15:47 +0100369 cp $(OUT_DIR)/qemu/qemu-system-aarch64 $@/bin
Fuad Tabba62b21192020-05-13 11:57:26 +0100370
Fuad Tabba62b21192020-05-13 11:57:26 +0100371##
David Brazdilef8506d2020-07-27 19:56:16 +0000372## Run tests
Fuad Tabba62b21192020-05-13 11:57:26 +0100373##
374
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000375KVM_MODES := vhe nvhe pkvm
376hyphensuffix = $(lastword $(subst -, ,$(1)))
377
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000378RUN_TEST_SH := $(SCRIPTS_DIR)/kvm-unit-tests/run_test.sh
379RUN_TEST = $(RUN_TEST_SH) \
380 -d $@ \
381 -M $(call hyphensuffix,$@) \
382 -k "$(KERNEL_IMAGE)" \
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000383 $(RUN_TEST_ARGS) "$(RUN_TEST_HOST_SH)" \
384 $(if $(RUN_TEST_QEMU_ARGS),-- ,)$(RUN_TEST_QEMU_ARGS)
385
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000386ifeq ($(VERBOSE),1)
387RUN_TEST_ARGS += -v
388endif
389
390ifneq ($(QUIET),0)
391RUN_TEST_ARGS += -o "$(RUN_TEST_LOG_DIR)/$@.log"
392endif
393
394ifeq ($(GDB),1)
395# RUN_TEST_SH defaults to no-timeout when -G is passed but -t forces one:
396RUN_TEST_ARGS += -G
397else
398# Without -G, RUN_TEST_SH defaults to a non-zero timeout duration.
399# RUN_TEST_TIMEOUT should be use if a specific value is wanted (see 'man timeout').
400# In particular, RUN_TEST_TIMEOUT=0 prevents the test from timing out.
401RUN_TEST_ARGS += $(if $(RUN_TEST_TIMEOUT),-t ,)$(RUN_TEST_TIMEOUT)
402endif
403
David Brazdilef8506d2020-07-27 19:56:16 +0000404# Root test targets. Dynamically generated per-test targets
405# will add themselves as dependencies of these.
406.PHONY: test test-list
407test:
408test-list:
Fuad Tabba62b21192020-05-13 11:57:26 +0100409
David Brazdilef8506d2020-07-27 19:56:16 +0000410#
411# kvm-unit-tests
412#
413
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000414KUT_RUN_TEST = $(RUN_TEST_SH)
David Brazdilef8506d2020-07-27 19:56:16 +0000415KUT_GEN_MAKEFILE := $(SCRIPTS_DIR)/kvm-unit-tests/gen_makefile.sh
416KUT_MAKEFILE := $(OUT_DIR)/Makefile.kvm-unit-tests
417KUT_TEST_DIR := $(KUT_OUT)/tests
Fuad Tabbacd908402021-02-09 17:18:15 +0000418KUT_LOG_DIR := $(DIST_DIR)/logs/kvm-unit-tests/
Fuad Tabba62b21192020-05-13 11:57:26 +0100419
Fuad Tabba42a3b292020-05-13 15:04:03 +0100420# Exclude tests that require user interaction or are known to fail.
David Brazdilef8506d2020-07-27 19:56:16 +0000421KUT_EXCLUDE := (.+migrat.+)|(gicv2-.+)|(pmu-event-introspection)|(micro-bench)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100422
David Brazdilef8506d2020-07-27 19:56:16 +0000423# Generate a Makefile with targets per test and configuration.
424# Pass in variable/target names from this Makefile that the
425# generated Makefile will refer to.
426$(KUT_MAKEFILE): $(SELF) $(KUT_GEN_MAKEFILE) $(KUT_STAMP)
427 @ mkdir -p $(shell dirname $@)
428 @ $(KUT_GEN_MAKEFILE) \
429 $(KUT_TEST_DIR) "$(KUT_EXCLUDE)" \
430 KERNEL_IMAGE KUT_LOG_DIR test test-list \
431 > $@.tmp
432 @ mv $@.tmp $@
433include $(KUT_MAKEFILE)
David Brazdil19d0b7a2021-02-02 18:15:44 +0000434
435#
David Brazdil490370a2021-03-10 22:55:02 +0000436# Basic boot tests in the various arm64 KVM modes
437#
438
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000439BOOT_TESTS := $(addprefix test-boot-,$(KVM_MODES))
David Brazdil490370a2021-03-10 22:55:02 +0000440
David Brazdil490370a2021-03-10 22:55:02 +0000441.PHONY: test-boot
442test-boot: $(BOOT_TESTS)
443
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000444TEST_LISTS += test-list-boot
445test-list-boot: LISTED_TESTS = $(BOOT_TESTS)
David Brazdil490370a2021-03-10 22:55:02 +0000446
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000447TESTS += $(BOOT_TESTS)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000448$(BOOT_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/boot
449$(BOOT_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/aarch64/test_boot.sh
450$(BOOT_TESTS): RUN_TEST_QEMU_ARGS = -a "boot_test_mode=$(call hyphensuffix,$@)"
David Brazdil490370a2021-03-10 22:55:02 +0000451
452#
David Brazdil19d0b7a2021-02-02 18:15:44 +0000453# CrosVM Hello World test
454#
455
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000456CROSVM_HELLOWORLD_TESTS := $(addprefix test-crosvm-helloworld-,$(KVM_MODES))
David Brazdil19d0b7a2021-02-02 18:15:44 +0000457
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200458VM_IMAGE ?= $(KERNEL_IMAGE)
459
David Brazdil19d0b7a2021-02-02 18:15:44 +0000460.PHONY: test-crosvm-helloworld
461test-crosvm-helloworld: $(CROSVM_HELLOWORLD_TESTS)
462
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000463TEST_LISTS += test-list-crosvm-helloworld
464test-list-crosvm-helloworld: LISTED_TESTS = $(CROSVM_HELLOWORLD_TESTS)
David Brazdil19d0b7a2021-02-02 18:15:44 +0000465
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000466TESTS += $(CROSVM_HELLOWORLD_TESTS)
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200467$(CROSVM_HELLOWORLD_TESTS): $(CROSVM_EXT4)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000468$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/crosvm/helloworld
469$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/crosvm/helloworld/host.sh
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000470$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_QEMU_ARGS = \
471 -s 4 \
472 -R $(SCRIPTS_DIR)/crosvm/helloworld/guest.sh \
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200473 -R $(VM_IMAGE) \
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000474 -R $(PREBUILTS_ROOTFS_EXT4) \
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200475 -R $(CROSVM_EXT4)
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000476$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_TIMEOUT := 300s
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000477
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000478.PHONY test: $(TESTS)
479$(TESTS): $(KERNEL_IMAGE)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000480 @ $(RUN_TEST)
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000481.PHONY test-list: $(TEST_LISTS)
482$(TEST_LISTS):
483 @ for x in $(LISTED_TESTS); do echo $$x; done