toolchain refactoring: Only support native out of the box

cross-compilation requires additional configuration that depends
heavily on the host platform. Unfortunately cargo cannot be
configured on a per host platform basis so this has to be done
manually.

Most developers are just using native compilation (and using
the containers for everything else). So we can slim our
install-deps scripts a bit.

To verify the updated install scripts, this change includes
containers to simulate a fresh developer workstation that
can be used to verify out of the box workflows.

BUG=b:262829206
BUG=b:265995780
BUG=b:265842137
TEST=tools/contrib/minimal_container/test-all.sh and CQ

Change-Id: I0e803d0224306e5351728e2c80fcbcd8260d029d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4179290
Reviewed-by: Zihan Chen <zihanchen@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Dennis Kempin 2023-01-18 13:44:10 -08:00 committed by crosvm LUCI
parent d178117a94
commit 108a335d7d
15 changed files with 231 additions and 105 deletions

20
.cargo/config.debian.toml Normal file
View file

@ -0,0 +1,20 @@
# Configure path to linker and emulators to use when running unit tests
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
runner = "qemu-arm-static"
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
runner = "qemu-aarch64-static"
[target.x86_64-pc-windows-gnu]
runner = "wine64-stable"
# Provide path to the pkg-config wrapper for each supported platform
[env]
PKG_CONFIG_x86_64-unknown-linux-gnu = "x86_64-linux-gnu-pkg-config"
PKG_CONFIG_aarch64-unknown-linux-gnu = "aarch64-linux-gnu-pkg-config"
PKG_CONFIG_armv7_unknown_linux_gnueabihf = "arm-linux-gnueabihf-pkg-config"
# libslirp is currently not properly configured via pkg-config and cannot use the wrapper like
# other architextures do.
# TODO(b/266100489): Add a libslirp.pc file and use the pkg-config wrapper
PKG_CONFIG_SYSROOT_DIR_x86_64-pc-windows-gnu = "/usr/x86_64-w64-mingw32"

View file

@ -2,7 +2,7 @@
# This allows ./tools/clippy and IDE integrations to use the same configuration.
# This should be replaced with a proper clippy config once available:
# https://github.com/rust-lang/cargo/issues/5034
[target.'cfg(all())']
[build]
rustflags = [
# TODO(crbug/908640): To be resolved.
"-Aclippy::missing_safety_doc", # 26 errors
@ -33,17 +33,3 @@ rustflags = [
"-Aclippy::useless_let_if_seq",
"-Aclippy::useless_transmute",
]
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
[target.x86_64-pc-windows-gnu]
runner = "wine64-stable"
[env]
PKG_CONFIG_SYSROOT_DIR_armv7-unknown-linux-gnueabihf = "/usr/arm-linux-gnueabihf"
PKG_CONFIG_SYSROOT_DIR_aarch64-unknown-linux-gnu = "/usr/aarch64_linux_gnu"
PKG_CONFIG_SYSROOT_DIR_x86_64-pc-windows-gnu = "/usr/x86_64-w64-mingw32"

View file

@ -27,8 +27,8 @@ git config submodule.recurse true
git config push.recurseSubmodules no
```
Crosvm development best works on Debian derivatives. First install rust via <https://rustup.rs/>.
Then for the rest, we provide a script to install the necessary packages on Debian:
Crosvm development best works on Debian derivatives. We provide a script to install the necessary
packages on Debian, Ubuntu or gLinux:
```sh
./tools/install-deps
@ -37,33 +37,6 @@ Then for the rest, we provide a script to install the necessary packages on Debi
For other systems, please see below for instructions on
[Using the development container](#using-the-development-container).
### Setting up for cross-compilation
Crosvm is built and tested on x86, aarch64 and armhf. Your host needs to be set up to allow
installation of foreign architecture packages.
On Debian this is as easy as:
```sh
sudo dpkg --add-architecture arm64
sudo dpkg --add-architecture armhf
sudo apt update
```
On ubuntu this is a little harder and needs some
[manual modifications](https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library)
of APT sources.
For other systems (**including gLinux**), please see below for instructions on
[Using the development container](#using-the-development-container).
With that enabled, the following scripts will install the needed packages:
```sh
./tools/install-aarch64-deps
./tools/install-armhf-deps
```
### Using the development container
We provide a Debian container with the required packages installed. With
@ -113,6 +86,8 @@ those `cargo build` will build for aarch64 and `cargo test` will run tests insid
The aarch64 VM can be managed with the `./tools/aarch64vm` script.
Note: See [Cross-compilation](#cross-compilation) for notes on cross-compilation.
### Running all tests
Crosvm cannot use `cargo test --workspace` because of various restrictions of cargo. So we have our
@ -170,6 +145,55 @@ The `--quick` variant will skip some slower checks, like building for other plat
./tools/presubmit --quick
```
## Cross-compilation
Crosvm is built and tested on x86, aarch64 and armhf. Your system needs some setup work to be able
to cross-comple for other architectures, hence it is recommended to use the
[development container](#using-the-development-container), which will have everything configured.
Note: Cross-compilation is **not supported on gLinux**. Please use the development container.
### Enable foreign architectures
Your host needs to be set up to allow installation of foreign architecture packages.
On Debian this is as easy as:
```sh
sudo dpkg --add-architecture arm64
sudo dpkg --add-architecture armhf
sudo apt update
```
On ubuntu this is a little harder and needs some
[manual modifications](https://askubuntu.com/questions/430705/how-to-use-apt-get-to-download-multi-arch-library)
of APT sources.
With that enabled, the following scripts will install the needed packages:
```sh
./tools/install-aarch64-deps
./tools/install-armhf-deps
```
### Configuring wine and mingw64
Crosvm is also compiled and tested on windows. Some limited testing can be done with mingw64 and
wine on linux machines. Use the provided setup script to install the needed dependencies.
```sh
./tools/install-mingw64-deps
```
### Configure cargo for cross-compilation
Cargo requries additional configuration to support cross-compilation. You can copy the provided
example config to your cargo configuration:
```sh
cat .cargo/config.debian.toml >> ${CARGO_HOME:-~/.cargo}/config.toml
```
## Known issues
- Devices can't be jailed if `/var/empty` doesn't exist. `sudo mkdir -p /var/empty` to work around

View file

@ -0,0 +1,8 @@
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
FROM docker.io/debian:testing-slim
VOLUME /workspace
WORKDIR /workspace

View file

@ -0,0 +1,26 @@
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
FROM docker.io/ubuntu:kinetic-20221130
# Allow APT to cache packages between docker image builds
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
# Install dependencies (APT and cargo packages are cached between image builds for faster iterative builds).
COPY --chmod=555 tools/install-deps rust-toolchain /tools/
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
--mount=type=cache,target=/var/lib/apt,sharing=private \
--mount=type=cache,target=/scratch/cargo_target,sharing=private \
apt-get update \
# sudo is installed here since it's required by install-deps.
# tzdata is installed here so it won't later prompt for configuration.
&& DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get install --yes sudo tzdata \
# Run crosvm's install-deps
&& cd tools \
&& ./install-deps
ENV PATH=$PATH:~/.cargo/bin
VOLUME /workspace
WORKDIR /workspace

View file

@ -0,0 +1,18 @@
# Minimal Containers
These containers simulate a fresh developer workstation and can be used to test out-of-the box
development workflows for crosvm.
To run one of the containers:
```
./run.sh Dockerfile.ubuntu
```
Or to run `cargo build` in all containers run:
```
./test-all.sh
```
Note: These scripts are experimental and not validated in CI.

View file

@ -0,0 +1,23 @@
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Example usage:
#
# ./run.sh Dockerfile.ubuntu cargo test --lib --bins --workspace
set -e
cd $(dirname $0)
CROSVM_ROOT=$(realpath "../../../")
FILENAME=$1
shift
DOCKER_BUILDKIT=1 docker build -t crosvm_minimal -f $FILENAME $CROSVM_ROOT
if [[ $# -eq 0 ]]; then
docker run --rm -it --volume "${CROSVM_ROOT}:/workspace" crosvm_minimal
else
docker run --rm -it --volume "${CROSVM_ROOT}:/workspace" crosvm_minimal bash -c "${*@Q}"
fi

View file

@ -0,0 +1,12 @@
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Tests if crosvm can be built and unit tests run for all containers defined in this directory.
set -e
cd $(dirname $0)
./run.sh Dockerfile.debian cargo build --workspace --features=all-x86_64
./run.sh Dockerfile.ubuntu cargo build --workspace --features=all-x86_64

View file

@ -9,7 +9,7 @@
#
# Note, if you are using docker, you will probably be using "Dockerfile.user".
FROM docker.io/debian:testing-20220822-slim
FROM docker.io/debian:testing-slim
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
@ -30,27 +30,40 @@ RUN dpkg --add-architecture arm64 \
# Allow APT to cache packages between docker image builds
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
# Setting this will help install-deps skip installing wine deps, which
# will be installed by Dockerfile.user
ENV DOCKER_WINE_SETUP=1
# Install dependencies (APT and cargo packages are cached between image builds for faster iterative builds).
COPY tools/install-deps tools/install-aarch64-deps tools/install-armhf-deps tools/install-docs-deps rust-toolchain /tools/
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=cache,target=/scratch/cargo_target,sharing=locked \
COPY --chmod=555 tools/install-deps tools/install-aarch64-deps tools/install-armhf-deps tools/install-mingw64-deps tools/setup-wine64 rust-toolchain /tools/
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
--mount=type=cache,target=/var/lib/apt,sharing=private \
--mount=type=cache,target=/scratch/cargo_target,sharing=private \
cd /tools \
&& chmod +x * \
&& apt-get update \
&& apt-get install --yes sudo \
&& ./install-deps \
&& ./install-aarch64-deps \
&& ./install-armhf-deps \
&& ./install-docs-deps
&& ./install-mingw64-deps
# Prepare wine64
RUN sudo ln -sf /usr/bin/wine64-stable /usr/bin/wine64 \
&& wine64 wineboot
# Install an older version of binutils-mingw-w64-x86-64. The latest version is crashing when
# linking crosvm. See b/265995780
RUN wget https://snapshot.debian.org/archive/debian/20230101T091029Z/pool/main/b/binutils-mingw-w64/binutils-mingw-w64-x86-64_2.38.90.20220713-2%2B9%2Bb1_amd64.deb \
&& dpkg -i binutils-mingw-w64-x86-64_2.38.90.20220713-2+9+b1_amd64.deb \
&& rm binutils-mingw-w64-x86-64_2.38.90.20220713-2+9+b1_amd64.deb
# Install an older version of wine64. The latest version is crashing during wineboot.
# See b/265995780
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
--mount=type=cache,target=/var/lib/apt,sharing=private \
apt-get install --yes libopenal1 libvkd3d1 \
&& wget https://snapshot.debian.org/archive/debian/20230101T091029Z/pool/main/w/wine/wine64_7.0.1~repack-1_amd64.deb \
&& wget https://snapshot.debian.org/archive/debian/20230101T091029Z/pool/main/w/wine/libwine_7.0.1~repack-1_amd64.deb \
&& dpkg -i libwine_7.0.1~repack-1_amd64.deb wine64_7.0.1~repack-1_amd64.deb \
&& rm wine64_7.0.1~repack-1_amd64.deb libwine_7.0.1~repack-1_amd64.deb
# Setup wine for root user
RUN /tools/setup-wine64
# Install global config.toml for cross-compilation
COPY --chmod=555 .cargo/config.debian.toml /.cargo/config.toml
# Prebuild aarch64 VM image for faster startup.
COPY tools/aarch64vm /tools/

View file

@ -20,18 +20,14 @@ RUN useradd -ms /bin/bash crosvmdev \
# Allow dependencies and build files to be used and overwritten by user
&& chown -R crosvmdev:crosvmdev /scratch /cache
COPY tools/install-wine-deps /tools/
RUN chmod 755 /tools/install-wine-deps
# Following operations will be run as crosvmdev to ensure correct permission.
USER crosvmdev
# Prepare path to rust toolchain for crosvmdev
RUN echo 'export PATH=/cache/cargo_home/bin:/usr/local/cargo/bin:$PATH' >> /home/crosvmdev/.profile
RUN wine64 wineboot
RUN /tools/install-wine-deps
# Re-run wine setup for this user
RUN /tools/setup-wine64
# Switch back to root to avoid usermod crosvmdev as crosvmdev
USER root

View file

@ -1 +1 @@
r0032
r0033

View file

@ -4,7 +4,6 @@
# found in the LICENSE file.
set -ex
sudo apt-get update
sudo apt-get install --yes --no-install-recommends \
ca-certificates \
clang \
@ -18,6 +17,7 @@ sudo apt-get install --yes --no-install-recommends \
libasound2-dev \
libavcodec-dev \
libavutil-dev \
libcap-dev \
libclang-dev \
libdbus-1-dev \
libdrm-dev \
@ -36,6 +36,7 @@ sudo apt-get install --yes --no-install-recommends \
ninja-build \
openssh-client \
pkg-config \
protobuf-compiler \
python3 \
python3-pip \
python3-setuptools \
@ -43,26 +44,26 @@ sudo apt-get install --yes --no-install-recommends \
rsync \
screen \
tmux \
wget \
wine \
wine64 \
gcc-mingw-w64-x86-64-win32 \
wayland-protocols
wayland-protocols \
wget
pip3 install \
meson \
mdformat \
mdformat-gfm \
mdformat-footnote \
argh \
black \
mdformat \
mdformat-footnote \
mdformat-gfm \
meson \
mypy \
black
rich
# Install rustup if not available yet
if ! command -v rustup &>/dev/null; then
wget "https://static.rust-lang.org/rustup/archive/1.25.1/x86_64-unknown-linux-gnu/rustup-init"
echo "5cc9ffd1026e82e7fb2eec2121ad71f4b0f044e88bca39207b3f6b769aaa799c *rustup-init" | sha256sum -c -
chmod +x rustup-init
./rustup-init -y --no-modify-path --profile minimal --default-toolchain none
source ${CARGO_HOME:-~/.cargo}/env
rm rustup-init
fi
@ -76,16 +77,19 @@ rustup component add llvm-tools-preview
# Allow cross-compilation via mingw64
rustup target add x86_64-pc-windows-gnu
# Cargo extension to install binary packages from github
curl -L https://github.com/cargo-bins/cargo-binstall/releases/download/v0.19.3/cargo-binstall-x86_64-unknown-linux-gnu.tgz | tar -xzvvf - -C ${CARGO_HOME:-~/.cargo}/bin
# The bindgen tool is required to build a crosvm dependency.
cargo install -f bindgen --version=0.60.1
cargo binstall --no-confirm bindgen --version "0.60.1"
# binutils are wrappers to call the rustup bundled versions of llvm tools.
cargo install -f cargo-binutils
cargo binstall --no-confirm cargo-binutils
# Install dependencies used to generate mdbook documentation.
$(dirname "$0")/install-docs-deps
# The mdbook and mdbook-mermaid tools are used to build the crosvm book.
cargo binstall --no-confirm mdbook --version "0.4.25"
cargo binstall --no-confirm mdbook-mermaid --version "0.12.6"
cargo binstall --no-confirm mdbook-linkcheck --version "0.7.7"
# Install wine related dependencies if this is a local install and not a docker build
if [[ -z "${DOCKER_WINE_SETUP}" ]]; then
$(dirname "$0")/install-wine-deps
fi
# Nextest is an improved test runner for cargo
cargo binstall --no-confirm cargo-nextest --version "0.9.49"

View file

@ -1,21 +0,0 @@
#!/usr/bin/env bash
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Install dependencies to generate mdbook documentation.
# Before running this script, `apt update` needs to be executed.
set -ex
# Install packages to run build.rs in some crate:
# * libcap-dev: Used by minijail-sys/build.rs
# * protobuf-compiler: Generates Rust files in protos
sudo apt install --yes --no-install-recommends \
libcap-dev \
protobuf-compiler
# The mdbook and mdbook-mermaid tools are used to build the crosvm book.
cargo install mdbook --no-default-features --features search --version "^0.4.10"
cargo install mdbook-mermaid --version "^0.8.3"
cargo install mdbook-linkcheck --version "^0.7.6"

15
tools/install-mingw64-deps Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -ex
WINETRICKS_COMMIT_HASH="b666e05a0bf28fd940e5c258ccdfc0a2799d1948"
WINETRICKS_URL="https://raw.githubusercontent.com/Winetricks/winetricks/"$WINETRICKS_COMMIT_HASH"/src/winetricks"
WINESTRICKS_SCRIPT_SHA="40fdf1f89f3636187201858e3591e86752172814"
# Install mingw64 toolchain and wine
sudo apt-get install --yes --no-install-recommends \
gcc-mingw-w64-x86-64-win32 \
wine \
wine64

View file

@ -4,6 +4,8 @@
# found in the LICENSE file.
set -ex
wine64-stable wineboot
WINETRICKS_COMMIT_HASH="b666e05a0bf28fd940e5c258ccdfc0a2799d1948"
WINETRICKS_URL="https://raw.githubusercontent.com/Winetricks/winetricks/"$WINETRICKS_COMMIT_HASH"/src/winetricks"
WINESTRICKS_SCRIPT_SHA="40fdf1f89f3636187201858e3591e86752172814"