cargo-ndk 4.1.2

Makes building Rust libraries for Android simpler
Documentation
on: [push, pull_request]

name: CI

jobs:
  build:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # MSRV builds
          - name: Windows - MSRV
            os: windows-latest
            rust: 1.86.0
            target: x86_64-pc-windows-msvc
          - name: Linux - MSRV
            os: ubuntu-latest
            rust: 1.86.0
            target: x86_64-unknown-linux-gnu
          - name: macOS - MSRV
            os: macos-latest
            rust: 1.86.0
            target: aarch64-apple-darwin
          # Beta builds
          - name: Windows - Beta
            os: windows-latest
            rust: beta
            target: x86_64-pc-windows-msvc
          - name: Linux - Beta
            os: ubuntu-latest
            rust: beta
            target: x86_64-unknown-linux-gnu
          - name: macOS - Beta
            os: macos-latest
            rust: beta
            target: aarch64-apple-darwin
          # Nightly builds
          - name: Windows - Nightly
            os: windows-latest
            rust: nightly
            target: x86_64-pc-windows-msvc
          - name: Linux - Nightly
            os: ubuntu-latest
            rust: nightly
            target: x86_64-unknown-linux-gnu
          - name: macOS - Nightly
            os: macos-latest
            rust: nightly
            target: aarch64-apple-darwin
          # Stable builds with artifact creation
          - name: Windows - Stable (x86_64)
            os: windows-latest
            rust: stable
            target: x86_64-pc-windows-msvc
          - name: Linux - Stable (x86_64)
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-gnu
          - name: macOS - Stable (aarch64)
            os: macos-latest
            rust: stable
            target: aarch64-apple-darwin
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
          targets: ${{ matrix.target && format('{0},', matrix.target) || '' }}aarch64-linux-android,armv7-linux-androideabi
          components: rustfmt, clippy
      - name: Check code formatting
        run: cargo fmt --all -- --check
      - name: Run clippy
        run: cargo clippy -- -D warnings
      - name: Install MSYS (Windows only)
        if: runner.os == 'Windows'
        uses: msys2/setup-msys2@v2
        with:
          path-type: inherit
          msystem: UCRT64
      - name: Run build
        run: cargo install --locked --target ${{ matrix.target }} --path .
      - name: Smoke test `ndk-env`
        run: cargo ndk-env --target armeabi-v7a
      - name: Run basic example
        working-directory: example/basic
        run: cargo ndk -t armeabi-v7a -o jniLibs -- build
      - name: Run openssl example (Windows)
        if: runner.os == 'Windows'
        shell: 'msys2 {0}'
        working-directory: example/openssl
        run: cargo ndk -t arm64-v8a --platform 28 build
      - name: Run openssl example (Unix)
        if: runner.os != 'Windows'
        working-directory: example/openssl
        run: cargo ndk -t arm64-v8a --platform 28 build
      - name: Run ndk-sys example
        working-directory: example/ndk-sys
        run: cargo ndk --target arm64-v8a --platform 26 -- clippy --all --all-features --tests -- -D warnings
      - name: Build release binary
        if: ${{ contains(matrix.name, 'Stable') }}
        run: cargo build --locked --release --target ${{ matrix.target }}
      - name: Get version
        if: ${{ contains(matrix.name, 'Stable') }}
        id: version
        shell: bash
        run: |
          if [[ "$GITHUB_REF" == refs/tags/* ]]; then
            VERSION="${GITHUB_REF#refs/tags/}"
          else
            VERSION=dev
          fi
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "ARTIFACT_NAME=${{ format('cargo-ndk-{0}', matrix.target) }}-$VERSION" >> $GITHUB_OUTPUT
      - name: Copy binary (Unix)
        if: ${{ runner.os != 'Windows' && contains(matrix.name, 'Stable') }}
        run: |
          mkdir ${{ steps.version.outputs.ARTIFACT_NAME }}
          for file in cargo-ndk cargo-ndk-env cargo-ndk-test cargo-ndk-runner; do
            cp target/${{ matrix.target }}/release/$file ${{ steps.version.outputs.ARTIFACT_NAME }}/$file
          done
      - name: Copy binary (Windows)
        if: ${{ runner.os == 'Windows' && contains(matrix.name, 'Stable') }}
        run: |
          mkdir ${{ steps.version.outputs.ARTIFACT_NAME }}
          foreach ($file in @('cargo-ndk.exe', 'cargo-ndk-env.exe', 'cargo-ndk-test.exe', 'cargo-ndk-runner.exe')) {
            Copy-Item "target/${{ matrix.target }}/release/$file" "${{ steps.version.outputs.ARTIFACT_NAME }}/$file"
          }
      - name: Create tar.gz archive (Unix)
        if: ${{ contains(matrix.name, 'Stable') && runner.os != 'Windows' }}
        run: |
          tar -cf ${{ steps.version.outputs.ARTIFACT_NAME }}.tar ${{ steps.version.outputs.ARTIFACT_NAME }}
          gzip -9 ${{ steps.version.outputs.ARTIFACT_NAME }}.tar
          mv ${{ steps.version.outputs.ARTIFACT_NAME }}{.tar.gz,.tgz}
      - name: Create zip archive (Windows)
        if: ${{ contains(matrix.name, 'Stable') && runner.os == 'Windows' }}
        run: |
          7z a -mx=9 ${{ steps.version.outputs.ARTIFACT_NAME }}.zip ${{ steps.version.outputs.ARTIFACT_NAME }}
      - name: Upload artifacts (Unix)
        uses: actions/upload-artifact@v4
        if: ${{ contains(matrix.name, 'Stable') && runner.os != 'Windows' }}
        with:
          compression-level: 0
          name: ${{ steps.version.outputs.ARTIFACT_NAME }}
          path: ${{ steps.version.outputs.ARTIFACT_NAME }}.tgz
      - name: Upload artifacts (Windows)
        uses: actions/upload-artifact@v4
        if: ${{ contains(matrix.name, 'Stable') && runner.os == 'Windows' }}
        with:
          compression-level: 0
          name: ${{ steps.version.outputs.ARTIFACT_NAME }}
          path: ${{ steps.version.outputs.ARTIFACT_NAME }}.zip
  build-non-host:
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Windows - Stable (aarch64, non-host)
            os: windows-latest
            rust: stable
            target: aarch64-pc-windows-msvc
          - name: Linux - Stable (x86_64-musl, non-host)
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-musl
            cross: true
          - name: Linux - Stable (aarch64, non-host)
            os: ubuntu-latest
            rust: stable
            target: aarch64-unknown-linux-gnu
            cross: true
          - name: Linux - Stable (aarch64-musl, non-host)
            os: ubuntu-latest
            rust: stable
            target: aarch64-unknown-linux-musl
            cross: true
          - name: macOS - Stable (x86_64, non-host)
            os: macos-latest
            rust: stable
            target: x86_64-apple-darwin
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
          targets: ${{ matrix.target }}
      - name: Set up cross compilation (Linux)
        if: matrix.cross
        run: |
          cargo install cross --git https://github.com/cross-rs/cross
      - name: Build release binary (native)
        if: ${{ !matrix.cross }}
        run: cargo build --locked --release --target ${{ matrix.target }}
      - name: Build release binary (cross)
        if: matrix.cross
        run: cross build --locked --release --target ${{ matrix.target }}
      - name: Get version
        id: version
        shell: bash
        run: |
          if [[ "$GITHUB_REF" == refs/tags/* ]]; then
            VERSION="${GITHUB_REF#refs/tags/}"
          else
            VERSION=dev
          fi
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "ARTIFACT_NAME=${{ format('cargo-ndk-{0}', matrix.target) }}-$VERSION" >> $GITHUB_OUTPUT
      - name: Copy binary (Unix)
        if: runner.os != 'Windows'
        run: |
          mkdir ${{ steps.version.outputs.ARTIFACT_NAME }}
          for file in cargo-ndk cargo-ndk-env cargo-ndk-test cargo-ndk-runner; do
            cp target/${{ matrix.target }}/release/$file ${{ steps.version.outputs.ARTIFACT_NAME }}/$file
          done
      - name: Copy binary (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir ${{ steps.version.outputs.ARTIFACT_NAME }}
          foreach ($file in @('cargo-ndk.exe', 'cargo-ndk-env.exe', 'cargo-ndk-test.exe', 'cargo-ndk-runner.exe')) {
            Copy-Item "target/${{ matrix.target }}/release/$file" "${{ steps.version.outputs.ARTIFACT_NAME }}/$file"
          }
      - name: Create tar.gz archive (Unix)
        if: runner.os != 'Windows'
        run: |
          tar -cf ${{ steps.version.outputs.ARTIFACT_NAME }}.tar ${{ steps.version.outputs.ARTIFACT_NAME }}
          gzip -9 ${{ steps.version.outputs.ARTIFACT_NAME }}.tar
          mv ${{ steps.version.outputs.ARTIFACT_NAME }}{.tar.gz,.tgz}
      - name: Create zip archive (Windows)
        if: runner.os == 'Windows'
        run: |
          7z a -mx=9 ${{ steps.version.outputs.ARTIFACT_NAME }}.zip ${{ steps.version.outputs.ARTIFACT_NAME }}
      - name: Upload artifacts (Unix)
        uses: actions/upload-artifact@v4
        if: runner.os != 'Windows'
        with:
          compression-level: 0
          name: ${{ steps.version.outputs.ARTIFACT_NAME }}
          path: ${{ steps.version.outputs.ARTIFACT_NAME }}.tgz
      - name: Upload artifacts (Windows)
        uses: actions/upload-artifact@v4
        if: runner.os == 'Windows'
        with:
          compression-level: 0
          name: ${{ steps.version.outputs.ARTIFACT_NAME }}
          path: ${{ steps.version.outputs.ARTIFACT_NAME }}.zip
  release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: [build, build-non-host]
    if: startsWith(github.ref, 'refs/tags/v') && contains(github.ref, '.')
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
      - name: Unzip all the things
        run: |
          ls -l artifacts/*/*
          mkdir upload/
          mv artifacts/*/* upload/
          ls -l upload
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          files: upload/*
          generate_release_notes: true
          prerelease: ${{ contains(github.ref, '-') }}