1# Copyright 2020 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Builds the kernel and rootfs for the guest used in integration testing.
6#
7# The main artifacts are:
8# target/guest_under_test/bzImage
9# target/guest_under_test/rootfs
10
11ARCH ?= $(shell arch)
12ifeq ($(ARCH), x86_64)
13  KERNEL_ARCH=x86
14  KERNEL_CONFIG=arch/x86/configs/chromiumos-container-vm-x86_64_defconfig
15  KERNEL_BINARY=bzImage
16  DOCKER_ARCH=amd64
17  CROSS_COMPILE=
18  RUSTFLAGS=
19else ifeq ($(ARCH), aarch64)
20  KERNEL_ARCH=arm64
21  KERNEL_CONFIG=arch/arm64/configs/chromiumos-container-vm-arm64_defconfig
22  KERNEL_BINARY=Image
23  DOCKER_ARCH=arm64v8
24  CROSS_COMPILE=aarch64-linux-gnu-
25  RUSTFLAGS="-Clinker=aarch64-linux-gnu-ld"
26else
27  $(error Only x86_64 or aarch64 are supported)
28endif
29
30# Build against the musl toolchain, which will produce a statically linked,
31# portable binary that we can run on the alpine linux guest without needing
32# libc at runtime
33RUST_TARGET ?= $(ARCH)-unknown-linux-musl
34
35# We are building everything in target/guest_under_test
36CARGO_TARGET ?= $(shell cargo metadata --no-deps --format-version 1 | \
37	jq -r ".target_directory")
38TARGET ?= $(CARGO_TARGET)/guest_under_test/$(ARCH)
39$(shell mkdir -p $(TARGET))
40
41# Parameteters for building the kernel locally
42KERNEL_REPO ?= https://chromium.googlesource.com/chromiumos/third_party/kernel
43KERNEL_BRANCH ?= chromeos-4.19
44
45################################################################################
46# Main targets
47
48all: $(TARGET)/rootfs $(TARGET)/bzImage
49
50clean:
51	rm -rf $(TARGET)
52
53################################################################################
54# Build rootfs
55
56dockerfile := $(shell pwd)/Dockerfile
57
58# Build rootfs from Dockerfile and export into squashfs
59$(TARGET)/rootfs: $(TARGET)/rootfs-build/delegate
60	# Build image from Dockerfile
61	docker build -t crosvm_integration_test $(TARGET)/rootfs-build \
62		-f $(dockerfile) --build-arg ARCH=$(DOCKER_ARCH)
63
64	# Create container and export into squashfs, and don't forget to clean up
65	# the container afterwards.
66	set -x; \
67		CONTAINER=$$(docker create crosvm_integration_test); \
68		docker export $$CONTAINER | tar2sqfs -c gzip -f $@; \
69		docker rm $$CONTAINER
70
71# Build and copy delegate binary into rootfs build directory
72$(TARGET)/rootfs-build/delegate: delegate.rs
73	rustup target add $(RUST_TARGET)
74	rustc --edition=2018 delegate.rs --out-dir $(@D) \
75		 $(RUSTFLAGS) --target $(RUST_TARGET)
76
77################################################################################
78# Build kernel
79
80$(TARGET)/bzImage: $(TARGET)/kernel-source $(TARGET)/kernel-build
81	cd $(TARGET)/kernel-source && \
82		yes "" | make \
83		O=$(TARGET)/kernel-build \
84		ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) \
85		-j$(shell nproc) $(KERNEL_BINARY)
86	cp $(TARGET)/kernel-build/arch/${KERNEL_ARCH}/boot/$(KERNEL_BINARY) $@
87
88$(TARGET)/kernel-build: $(TARGET)/kernel-source
89	mkdir -p $@
90	cp -f $(TARGET)/kernel-source/$(KERNEL_CONFIG) $@/.config
91
92$(TARGET)/kernel-source:
93	rm -rf $@
94	git clone --depth 1 --branch $(KERNEL_BRANCH) $(KERNEL_REPO) $@
95
96
97.PHONY: clean all update-prebuilts
98