From 1e2d66416368514cf178fc13a9c8cf88a21e513d Mon Sep 17 00:00:00 2001 From: Mike Rostecki Date: Wed, 4 Oct 2023 12:15:40 +0100 Subject: [PATCH] chore: Build bpf-linker and LLVM for multiple targets Apart from the x86_64 Linux build, cross compile also to Linux arm64, macOS x86_64 and macoOS arm64. --- .github/workflows/ci.yml | 55 ++++++++++++++++++++++++++--------- .github/workflows/llvm.yml | 35 ++++++++++++++++++---- .github/workflows/release.yml | 25 ++++++++++++++-- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86c83cb0..43fade20 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,7 +48,6 @@ jobs: run: cargo fmt --all -- --check build: - runs-on: ubuntu-22.04 strategy: fail-fast: false matrix: @@ -59,7 +58,21 @@ jobs: llvm: - 17 - source - name: rustc=${{ matrix.rust }} llvm=${{ matrix.llvm }} + include: + - target: aarch64-unknown-linux-gnu + llvm-target: aarch64-linux-gnu + os: ubuntu-latest + - target: aarch64-unknown-linux-musl + llvm-target: aarch64-linux-gnu + os: ubuntu-latest + - target: aarch64-apple-darwin + os: macos-latest + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + - target: x86_64-apple-darwin + os: macos-latest + name: rustc=${{ matrix.rust }} llvm=${{ matrix.llvm }} target=${{ matrix.target }} + runs-on: ${{ matrix.os }} needs: llvm env: @@ -91,8 +104,8 @@ jobs: - name: Build (default features, no system LLVM) run: cargo build - - name: Install dependencies - if: matrix.rust == 'nightly' + - name: Install dependencies (Linux) + if: matrix.rust == 'nightly' && startsWith(matrix.os, 'ubuntu') # ubuntu-22.04 comes with clang 14[0] which doesn't include support for signed and 64bit # enum values which was added in clang 15[1]. # @@ -110,8 +123,8 @@ jobs: sudo apt update sudo apt -y install clang gcc-multilib - - name: Install LLVM - if: matrix.llvm != 'source' + - name: Install LLVM (Linux) + if: matrix.llvm != 'source' && startsWith(matrix.os, 'ubuntu') run: | set -euxo pipefail wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc @@ -121,6 +134,16 @@ jobs: sudo apt -y install llvm-${{ matrix.llvm }}-dev echo /usr/lib/llvm-${{ matrix.llvm }}/bin >> $GITHUB_PATH + - name: Install LLVM (macOS) + if: matrix.llvm != 'source' && startsWith(matrix.os, 'macos') + run: | + set -euxo pipefail + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + echo "/usr/local/bin/brew" >> $GITHUB_PATH + brew install llvm + echo "$(brew --prefix llvm)/bin" >> $GITHUB_PATH + echo "LLVM_SYS_170_PREFIX=$(brew --prefix llvm)" >> $GITHUB_ENV + - name: Restore LLVM if: matrix.llvm == 'source' uses: actions/cache/restore@v3 @@ -133,10 +156,10 @@ jobs: if: matrix.llvm == 'source' run: | set -euxo pipefail - echo "${{ github.workspace }}/llvm-install/bin" >> $GITHUB_PATH + echo "${{ github.workspace }}/llvm-install/${{ matrix.llvm-target }}/bin" >> $GITHUB_PATH # LD_LIBRARY_PATH is needed because we're going to link everything dynamically below. This # doesn't affect behavior, but greatly reduces disk usage. - echo "LD_LIBRARY_PATH=${{ github.workspace }}/llvm-install/lib" >> $GITHUB_ENV + echo "LD_LIBRARY_PATH=${{ github.workspace }}/llvm-install/${{ matrix.llvm_target }}/lib" >> $GITHUB_ENV # llvm-sys discovers link flags at build script time; these are cached by cargo. The cached # flags may be incorrect when the cache is reused across LLVM versions. @@ -151,6 +174,10 @@ jobs: - name: Check run: cargo hack check --feature-powerset --features llvm-sys/force-dynamic + - name: Setup tmate session + if: ${{ failure() }} + uses: mxschmitt/action-tmate@v3 + - name: Build run: cargo hack build --feature-powerset --features llvm-sys/force-dynamic @@ -159,19 +186,19 @@ jobs: run: cargo hack test --feature-powerset --features llvm-sys/force-dynamic - uses: actions/checkout@v4 - if: matrix.rust == 'nightly' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' with: repository: aya-rs/aya path: aya submodules: recursive - name: Install - if: matrix.rust == 'nightly' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' run: cargo install --path . --no-default-features --features llvm-sys/force-dynamic # TODO: Remove this and run the integration tests on the local machine when they pass on 5.15. - name: Download debian kernels - if: matrix.rust == 'nightly' && runner.arch == 'ARM64' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' && runner.arch == 'ARM64' working-directory: aya run: | set -euxo pipefail @@ -182,7 +209,7 @@ jobs: # TODO: Remove this and run the integration tests on the local machine when they pass on 5.15. - name: Download debian kernels - if: matrix.rust == 'nightly' && runner.arch == 'X64' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' && runner.arch == 'X64' working-directory: aya run: | set -euxo pipefail @@ -193,7 +220,7 @@ jobs: # TODO: Remove this and run the integration tests on the local machine when they pass on 5.15. - name: Extract debian kernels - if: matrix.rust == 'nightly' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' working-directory: aya run: | set -euxo pipefail @@ -201,7 +228,7 @@ jobs: sh -c "dpkg --fsys-tarfile {} | tar -C test/.tmp --wildcards --extract '*vmlinuz*' --file -" - name: Run aya integration tests - if: matrix.rust == 'nightly' + if: startsWith(matrix.os, 'ubuntu') && matrix.rust == 'nightly' working-directory: aya run: | set -euxo pipefail diff --git a/.github/workflows/llvm.yml b/.github/workflows/llvm.yml index 0fd2819a..ad3a70d3 100644 --- a/.github/workflows/llvm.yml +++ b/.github/workflows/llvm.yml @@ -8,7 +8,18 @@ on: jobs: llvm: - runs-on: ubuntu-22.04 + strategy: + matrix: + include: + - target: aarch64-linux-gnu + os: ubuntu-latest + - target: aarch64-apple-darwin + os: macos-latest + - target: x86_64-linux-gnu + os: ubuntu-latest + - target: x86_64-apple-darwin + os: macos-latest + runs-on: ${{ matrix.os }} name: llvm outputs: cache-key: ${{ steps.cache-key.outputs.cache-key }} @@ -30,8 +41,8 @@ jobs: key: ${{ steps.cache-key.outputs.cache-key }} lookup-only: true - - name: Install Tools - if: steps.cache-llvm.outputs.cache-hit != 'true' + - name: Install Tools (Linux) + if: steps.cache-llvm.outputs.cache-hit != 'true' && startsWith(matrix.os, 'ubuntu') run: | set -euxo pipefail wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | \ @@ -43,6 +54,14 @@ jobs: sudo apt update sudo apt -y install cmake ninja-build clang lld + - name: Install Tools (macOS) + if: steps.cache-llvm.outputs.cache-hit != 'true' && startsWith(matrix.os, 'macos-latest') + run: | + set -euxo pipefail + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" + echo "/usr/local/bin/brew" >> $GITHUB_PATH + brew install cmake ninja + - name: Checkout LLVM Source if: steps.cache-llvm.outputs.cache-hit != 'true' uses: actions/checkout@v4 @@ -55,6 +74,10 @@ jobs: if: steps.cache-llvm.outputs.cache-hit != 'true' run: | set -euxo pipefail + LLVM_USE_LINKER_OPTION="" + if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then + LLVM_USE_LINKER_OPTION="-DLLVM_USE_LINKER=lld" + fi cmake \ -S llvm-project/llvm \ -B llvm-build \ @@ -62,15 +85,17 @@ jobs: -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DCMAKE_C_COMPILER=clang \ -DCMAKE_CXX_COMPILER=clang++ \ - -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/llvm-install" \ + -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/llvm-install/${{ matrix.target }}" \ -DLLVM_BUILD_LLVM_DYLIB=ON \ + -DLLVM_DEFAULT_TARGET_TRIPLE=${{ matrix.target }} \ -DLLVM_ENABLE_ASSERTIONS=ON \ -DLLVM_ENABLE_PROJECTS= \ -DLLVM_ENABLE_RUNTIMES= \ + -DLLVM_HOST_TRIPLE=${{ matrix.target }} \ -DLLVM_INSTALL_UTILS=ON \ -DLLVM_LINK_LLVM_DYLIB=ON \ -DLLVM_TARGETS_TO_BUILD=BPF \ - -DLLVM_USE_LINKER=lld + $LLVM_USE_LINKER_OPTION - name: Install LLVM if: steps.cache-llvm.outputs.cache-hit != 'true' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 180be024..0ec3fa8e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,8 +9,22 @@ jobs: uses: ./.github/workflows/llvm.yml upload-bins: - # TODO: Build for macos someday. - runs-on: ubuntu-22.04 + strategy: + matrix: + include: + - target: aarch64-unknown-linux-gnu + llvm-target: aarch64-linux-gnu + os: ubuntu-latest + - target: aarch64-unknown-linux-musl + llvm-target: aarch64-linux-gnu + os: ubuntu-latest + - target: aarch64-apple-darwin + os: macos-latest + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + - target: x86_64-apple-darwin + os: macos-latest + runs-on: ${{ matrix.os }} needs: llvm steps: - name: Restore LLVM @@ -22,15 +36,20 @@ jobs: - name: Add LLVM to PATH run: | - echo "${{ github.workspace }}/llvm-install/bin" >> $GITHUB_PATH + echo "${{ github.workspace }}/llvm-install/${{ matrix.llvm-target }}/bin" >> $GITHUB_PATH echo "$PATH" - uses: actions/checkout@v4 - uses: Swatinem/rust-cache@v2 + - uses: taiki-e/setup-cross-toolchain-action@v1 + if: startsWith(matrix.os, 'ubuntu') + with: + target: ${{ matrix.target }} - uses: taiki-e/upload-rust-binary-action@v1 with: bin: bpf-linker + target: ${{ matrix.target }} features: llvm-sys/force-static env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}