Cross Compilation - Faster Builds
When building an app with auterion-cli, it calls docker build --platform linux/arm64
under the hood. This makes docker run the build step in an emulated environment using QEMU.
This is convenient, as one can use the normal compilation steps for ones app as you would on the host, without having to worry about cross compilation. However, in larger projects, build times can grow quite long since the entire compilation is done in an emulated environment.
Cross compilation can improve the situation, but is generally fairly complex to set up. We provide tooling to allow for faster cross compilation.
Multi-stage build
We use dockers multi-stage build process for easier cross compilation
- Sysroot stage: Runs emulated in the target arch (arm64), install all dependencies your app needs. This prepares a sysroot we can then later use to link against
- Compile stage: Runs on your native arch (amd64). Compile your app using the supplied cross compiler. Link against the libraries installed in the first stage.
- Run stage: Runs emulated in the target arch, and on the target. Based off the sysroot stage, copy over the compiled binaries from the build stage
Dockerfile setup
```docker
1. -- Sysroot stage. Runs in arm64 emulation (QEMU) --
Install library dependencies
FROM auterion/app-base:v2 as sysroot-stage
RUN apt update && apt install -y \
2. -- Cross-compile build stage. Runs native on x86 --
FROM --platform=$BUILDPLATFORM auterion/app-cross-buildenv:v2 as build-stage
Variable to contain the target arch. Set automatically by docker to either "arm64" or "amd64"
ARG TARGETARCH
This is the magic part - copy the entire sysroot from the first stage into
/opt/sysroot. This path is indicated in the Toolchain file as the place
to look for libraries to link against. This makes sure, we link against the
right libraries (the arm64 versions)
COPY --from=sysroot-stage / /opt/sysroot
Copy and compile the app
COPY app /app WORKDIR /app
Build your app using the right toolchain file. Selected by $TARGETARCH variable
RUN cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/Toolchain_$TARGETARCH.cmake && \ cmake --build build
3. -- Runtime stage. Runs in arm64 emulation and on target --
FROM sysroot-stage as run-stage
COPY --from=build-stage /app/build /app/build CMD /app/my_app
```
app-cross-buildenv contents
This image contains everything from auterion/app-base plus:
- Cross-compilers for arm64 and amd64
aarch64-linux-gnu-*
x86_64-linux-gnu-*
- CMake toolchain files for cross-compiling
/Toolchain_amd64.cmake
/Toolchain_arm64.cmake
Comments
0 comments
Please sign in to leave a comment.