From 6ed23e90e859e0d3cd93734e8577d173b035d580 Mon Sep 17 00:00:00 2001 From: Scott Lamb Date: Tue, 10 Jan 2023 21:03:01 -0800 Subject: [PATCH] improve docker cross-compilation * support cross-compiling an x86-64 target on an arm64 host. This it turns out is a matter of *removing* an unnecessary dependency. (aarch64-linux-gnu-pkg-config exists but x86_64-linux-gnu-pkg-config doesn't. Turns out neither is necessary.) Added a comment explaining where ${gcc_target}-pkg-config comes from now. * documentation tweaks * improve debug output a bit --- docker/Dockerfile | 2 +- docker/build-server.bash | 18 +++++++++--------- docker/build-ui.bash | 2 +- docker/deploy.bash | 6 ++---- docker/dev-common.bash | 4 ++-- docker/dev.bash | 17 ++++++++++------- guide/build.md | 10 +++++++--- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 0367416..a2ab7d7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,7 +4,7 @@ # SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception. # See documentation here: -# https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md +# https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md # "dev-common" is the portion of "dev" (see below) which isn't specific to the # target arch. It's sufficient for building the non-arch-specific webpack. diff --git a/docker/build-server.bash b/docker/build-server.bash index f4b2932..0a18136 100755 --- a/docker/build-server.bash +++ b/docker/build-server.bash @@ -13,9 +13,8 @@ mkdir /docker-build-debug/build-server exec > >(tee -i /docker-build-debug/build-server/output) 2>&1 date uname -a -ls -laFR /cargo-cache > /docker-build-debug/build-server/cargo-cache-before -ls -laFR /var/lib/moonfire-nvr/target \ - > /docker-build-debug/build-server/target-before +find /cargo-cache -ls > /docker-build-debug/build-server/cargo-cache-before +find ~/target -ls > /docker-build-debug/build-server/target-before source ~/.buildrc @@ -26,12 +25,13 @@ sudo chmod 777 /cargo-cache /var/lib/moonfire-nvr/target mkdir -p /cargo-cache/{git,registry} ln -s /cargo-cache/{git,registry} ~/.cargo +build_profile=release-lto cd src/server time cargo test -time cargo build --profile=release-lto -sudo install -m 755 ~/moonfire-nvr /usr/local/bin/moonfire-nvr - -ls -laFR /cargo-cache > /docker-build-debug/build-server/cargo-cache-after -ls -laFR /var/lib/moonfire-nvr/target \ - > /docker-build-debug/build-server/target-after +time cargo build --profile=$build_profile +find /cargo-cache -ls > /docker-build-debug/build-server/cargo-cache-after +find ~/target -ls > /docker-build-debug/build-server/target-after +sudo install -m 755 \ + ~/platform-target/$build_profile/moonfire-nvr \ + /usr/local/bin/moonfire-nvr date diff --git a/docker/build-ui.bash b/docker/build-ui.bash index 9502e70..a9fe107 100755 --- a/docker/build-ui.bash +++ b/docker/build-ui.bash @@ -19,6 +19,6 @@ npm --version time npm ci time npm run build -ls -laFR /var/lib/moonfire-nvr/src/ui/node_modules \ +find /var/lib/moonfire-nvr/src/ui/node_modules -ls \ > /docker-build-debug/build-ui/node_modules-after date diff --git a/docker/deploy.bash b/docker/deploy.bash index 3e41e87..d897368 100755 --- a/docker/deploy.bash +++ b/docker/deploy.bash @@ -11,8 +11,7 @@ set -o xtrace mkdir -p /docker-build-debug/deploy exec > >(tee -i /docker-build-debug/deploy/output) 2>&1 -ls -laFR /var/cache/apt \ - > /docker-build-debug/deploy/var-cache-apt-before +find /var/cache/apt -ls > /docker-build-debug/deploy/var-cache-apt-before date uname -a @@ -29,6 +28,5 @@ time apt-get install --assume-yes --no-install-recommends \ rm -rf /var/lib/apt/lists/* ln -s moonfire-nvr /usr/local/bin/nvr -ls -laFR /var/cache/apt \ - > /docker-build-debug/deploy/var-cache-apt-after +find /var/cache/apt -ls > /docker-build-debug/deploy/var-cache-apt-after date diff --git a/docker/dev-common.bash b/docker/dev-common.bash index 40063b6..fb26549 100755 --- a/docker/dev-common.bash +++ b/docker/dev-common.bash @@ -15,7 +15,7 @@ exec > >(tee -i /docker-build-debug/dev-common/output) 2>&1 date uname -a -ls -laFR /var/cache/apt > /docker-build-debug/dev-common/var-cache-apt-before +find /var/cache/apt -ls > /docker-build-debug/dev-common/var-cache-apt-before export DEBIAN_FRONTEND=noninteractive @@ -78,5 +78,5 @@ export CARGO_BUILD_TARGET_DIR=/var/lib/moonfire-nvr/target EOF chown moonfire-nvr:moonfire-nvr /var/lib/moonfire-nvr/.buildrc -ls -laFR /var/cache/apt > /docker-build-debug/dev-common/var-cache-apt-after +find /var/cache/apt -ls > /docker-build-debug/dev-common/var-cache-apt-after date diff --git a/docker/dev.bash b/docker/dev.bash index c8735cf..4f7bc68 100755 --- a/docker/dev.bash +++ b/docker/dev.bash @@ -14,7 +14,7 @@ exec > >(tee -i /docker-build-debug/dev/output) 2>&1 date uname -a -ls -laFR /var/cache/apt > /docker-build-debug/dev/var-cache-apt-before +find /var/cache/apt -ls > /docker-build-debug/dev/var-cache-apt-before export DEBIAN_FRONTEND=noninteractive @@ -69,7 +69,6 @@ if [[ "${BUILDARCH}" != "${TARGETARCH}" ]]; then packages+=( g++-${gcc_target/_/-} libc6-dev-${dpkg_arch}-cross - pkg-config-${gcc_target} qemu-user ) fi @@ -85,12 +84,12 @@ time apt-get update time apt-get install --assume-yes --no-install-recommends "${packages[@]}" # Set environment variables for cross-compiling. -# Also set up a symlink that points to the release binary, because the -# release binary's location varies when cross-compiling, as described here: +# Also set up a symlink that points to the output platform's target dir, because +# the target directory layout varies when cross-compiling, as described here: # https://doc.rust-lang.org/cargo/guide/build-cache.html if [[ -n "${rust_target}" ]]; then su moonfire-nvr -lc "rustup target install ${rust_target} && - ln -s target/${rust_target}/release-lto/moonfire-nvr ." + ln -s target/${rust_target} platform-target" underscore_rust_target="${rust_target//-/_}" uppercase_underscore_rust_target="${underscore_rust_target^^}" cat >> /var/lib/moonfire-nvr/.buildrc <_ prefix that the README.md @@ -112,8 +115,8 @@ export CC_${underscore_rust_target}=${gcc_target}-gcc export CXX_${underscore_rust_target}=${gcc_target}-g++ EOF else - su moonfire-nvr -lc "ln -s target/release-lto/moonfire-nvr ." + su moonfire-nvr -lc "ln -s target platform-target" fi -ls -laFR /var/cache/apt > /docker-build-debug/dev/var-cache-apt-after +find /var/cache/apt -ls > /docker-build-debug/dev/var-cache-apt-after date diff --git a/guide/build.md b/guide/build.md index dad4c7c..aac2d4f 100644 --- a/guide/build.md +++ b/guide/build.md @@ -114,8 +114,11 @@ caveats: On Linux hosts (as opposed to when using Docker Desktop on macOS/Windows), you'll likely see errors like the ones below. The solution is to [install emulators](https://github.com/tonistiigi/binfmt#installing-emulators). +You may need to reinstall emulators on each boot of the host. ``` +Exec format error + Error while loading /usr/sbin/dpkg-split: No such file or directory Error while loading /usr/sbin/dpkg-deb: No such file or directory ``` @@ -127,7 +130,7 @@ Moonfire NVR's `Dockerfile` has some built-in debugging tools: info includes: * output (stdout + stderr) from the build script, running long operations through the `time` command. - * `ls -laFR` of cache mounts before and after. + * `find -ls` output on cache mounts before and after. * Each stage accepts a `INVALIDATE_CACHE_` argument. You can use eg `--build-arg=INVALIDATE_CACHE_BUILD_SERVER=$(date +%s)` to force the `build-server` stage to be rebuilt rather than use cached Docker layers. @@ -191,8 +194,9 @@ $ sudo apt-get install \ tzdata ``` -Ubuntu 20.04 (the latest LTS as of this writing) bundles node 10, which has -reached end-of-life (see [node.js: releases](https://nodejs.org/en/about/releases/)). +Ubuntu 20.04 LTS (still popular, supported by Ubuntu until April 2025) bundles +node 10, which has reached end-of-life (see +[node.js: releases](https://nodejs.org/en/about/releases/)). So rather than install the `nodejs` and `npm` packages from the built-in repository, see [Installing Node.js via package manager](https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions).