• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The ChromiumOS Authors
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_BINARY=bzImage
15  DOCKER_ARCH=amd64
16  CROSS_COMPILE=
17  RUSTFLAGS=
18else ifeq ($(ARCH), aarch64)
19  KERNEL_ARCH=arm64
20  KERNEL_BINARY=Image
21  DOCKER_ARCH=arm64v8
22  CROSS_COMPILE=aarch64-linux-gnu-
23  RUSTFLAGS="-Clinker=aarch64-linux-gnu-ld"
24else
25  $(error Only x86_64 or aarch64 are supported)
26endif
27
28# Build against the musl toolchain, which will produce a statically linked,
29# portable binary that we can run on the alpine linux guest without needing
30# libc at runtime
31RUST_TARGET ?= $(ARCH)-unknown-linux-musl
32
33# We are building everything in target/guest_under_test
34CARGO_TARGET ?= $(shell cargo metadata --no-deps --format-version 1 | \
35	jq -r ".target_directory")
36TARGET ?= $(CARGO_TARGET)/guest_under_test/$(ARCH)
37$(shell mkdir -p $(TARGET))
38
39# Parameteters for building the kernel locally
40KERNEL_REPO ?= https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
41KERNEL_BRANCH ?= linux-6.1.y
42
43################################################################################
44# Main targets
45
46all: $(TARGET)/rootfs $(TARGET)/bzImage
47
48# Clean all local build artifacts, but not downloaded sources.
49clean:
50	rm -rf $(TARGET)/kernel-build $(TARGET)/rootfs-build $(TARGET)/rootfs $(TARGET)/bzImage
51
52clean-all:
53	rm -rf $(TARGET)
54
55################################################################################
56# Build rootfs
57
58# Build rootfs from Dockerfile and export into squashfs
59$(TARGET)/rootfs: $(TARGET)/rootfs-build/delegate rootfs/Dockerfile
60	# Build image from Dockerfile
61	DOCKER_BUILDKIT=1 docker build -t crosvm_e2e_test_guest $(TARGET)/rootfs-build \
62		-f rootfs/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_e2e_test_guest); \
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: rootfs/delegate.rs
73	rustup target add $(RUST_TARGET)
74	rustc --edition=2018 rootfs/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		&& make O=$(TARGET)/kernel-build \
83		ARCH=$(KERNEL_ARCH) \
84		CROSS_COMPILE=$(CROSS_COMPILE) \
85		-j$(shell nproc)\
86		olddefconfig \
87		$(KERNEL_BINARY)
88
89	cp $(TARGET)/kernel-build/arch/${KERNEL_ARCH}/boot/$(KERNEL_BINARY) $@
90
91$(TARGET)/kernel-build: $(TARGET)/kernel-source kernel/$(KERNEL_ARCH).config kernel/common.config
92	mkdir -p $@
93	cat kernel/common.config kernel/$(KERNEL_ARCH).config > $@/.config
94	touch $@
95
96$(TARGET)/kernel-source:
97	rm -rf $@
98	git clone --depth 1 --branch $(KERNEL_BRANCH) $(KERNEL_REPO) $@
99
100
101.PHONY: clean all update-prebuilts
102