Documentation
name: Build

on:
  push:
    branches: [ master ]

jobs:
  test-and-build:
    strategy:
      fail-fast: false
      matrix:
        target:
          - { name: Linux-x64, os: ubuntu-latest, tool: x86_64-unknown-linux-musl }
          - { name: Linux-x64-GNU, os: ubuntu-20.04, tool: x86_64-unknown-linux-gnu }
          - { name: macOS-x64, os: macos-latest, tool: x86_64-apple-darwin }
          - { name: Windows-x64, os: windows-latest, tool: x86_64-pc-windows-msvc }

    name: ${{ matrix.target.name }}
    runs-on: ${{ matrix.target.os }}

    env:
      CI: 1
      CARGO_INCREMENTAL: 0
      windows: ${{ startsWith(matrix.target.name, 'Windows') }}
      linux: ${{ startsWith(matrix.target.name, 'Linux') }}

    steps:
      - uses: actions/checkout@v3

      - if: ${{ endsWith(matrix.target.tool, 'musl') }}
        run: sudo apt install -y musl-tools musl-dev

      - name: Install rust toolchain
        run: |
          rustup default nightly
          rustup target add ${{ matrix.target.tool }}

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Test
        run: cargo test --target ${{ matrix.target.tool }}

      - name: Build
        run: cargo build --target ${{ matrix.target.tool }}

      - name: Upload
        if: env.windows != 'true'
        uses: actions/upload-artifact@v3
        with:
          name: atri_bot-${{ matrix.target.tool }}
          path: target/${{ matrix.target.tool }}/debug/atri_bot

      - name: Upload exe
        if: env.windows == 'true'
        uses: actions/upload-artifact@v3
        with:
          name: atri_bot-${{ matrix.target.tool }}.exe
          path: target/${{ matrix.target.tool }}/debug/atri_bot.exe


  build:
    strategy:
      fail-fast: false
      matrix:
        target:
          - { name: macOS-aarch64, os: macos-latest, tool: aarch64-apple-darwin }
          - { name: Windows-aarch64, os: windows-latest, tool: aarch64-pc-windows-msvc }
          - { name: Linux-aarch64, os: ubuntu-latest, tool: aarch64-unknown-linux-musl }
          - { name: Linux-aarch64-GNU, os: ubuntu-18.04, tool: aarch64-unknown-linux-gnu }
          - { name: Linux-armv7, os: ubuntu-latest, tool: armv7-unknown-linux-musleabihf }
          - { name: Linux-armv7-GNU, os: ubuntu-18.04, tool: armv7-unknown-linux-gnueabihf }
          - { name: Linux-arm, os: ubuntu-latest, tool: arm-unknown-linux-musleabihf }
          - { name: Linux-arm-GNU, os: ubuntu-18.04, tool: arm-unknown-linux-gnueabihf }
          - { name: Linux-mips64, os: ubuntu-latest, tool: mips64-unknown-linux-muslabi64 }
          - { name: Linux-mips64-GNU, os: ubuntu-18.04, tool: mips64-unknown-linux-gnuabi64 }
          - { name: Linux-powerpc64, os: ubuntu-latest, tool: powerpc64-unknown-linux-gnu }
          - { name: Linux-thumbv7, os: ubuntu-latest, tool: thumbv7neon-unknown-linux-gnueabihf }
          - { name: Linux-riscv64, os: ubuntu-latest, tool: riscv64gc-unknown-linux-gnu }
          - { name: Linux-s390x, os: ubuntu-latest, tool: s390x-unknown-linux-gnu }
          - { name: Linux-sparc64, os: ubuntu-latest, tool: sparc64-unknown-linux-gnu }
          - { name: iOS-aarch64, os: macos-latest, tool: aarch64-apple-ios }
          - { name: FreeBSD-x64, os: ubuntu-latest, tool: x86_64-unknown-freebsd }
          - { name: NetBSD-x64, os: ubuntu-latest, tool: x86_64-unknown-netbsd }
          - { name: Illumos-x64, os: ubuntu-latest, tool: x86_64-unknown-illumos }

    name: ${{ matrix.target.name }}
    needs: [ test-and-build ]
    runs-on: ${{ matrix.target.os }}

    env:
      CI: 1
      CARGO_INCREMENTAL: 0
      windows: ${{ startsWith(matrix.target.name, 'Windows') }}
      linux: ${{ startsWith(matrix.target.name, 'Linux') }}

    steps:
      - uses: actions/checkout@v3

      - name: Apt update
        if: ${{ startsWith(matrix.target.os, 'ubuntu') }}
        run: sudo apt update

      - if: ${{ endsWith(matrix.target.tool, 'musl') }}
        run: sudo apt install -y musl-tools musl-dev

      - name: Install rust toolchain
        run: |
          rustup default nightly
          rustup target add ${{ matrix.target.tool }}

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --target ${{ matrix.target.tool }}
          use-cross: true

      - name: Upload
        if: env.windows != 'true'
        uses: actions/upload-artifact@v3
        with:
          name: atri_bot-${{ matrix.target.tool }}
          path: target/${{ matrix.target.tool }}/debug/atri_bot

      - name: Upload exe
        if: env.windows == 'true'
        uses: actions/upload-artifact@v3
        with:
          name: atri_bot-${{ matrix.target.tool }}.exe
          path: target/${{ matrix.target.tool }}/debug/atri_bot.exe


  build-android:
    strategy:
      fail-fast: false
      matrix:
        arch:
          - aarch64
          - x86_64

    name: Android-${{ matrix.arch }}
    needs: [ test-and-build ]
    runs-on: ubuntu-latest

    env:
      CI: 1
      CARGO_INCREMENTAL: 0

    steps:
      - uses: actions/checkout@v3

      - name: Install rust toolchain
        run: |
          rustup default nightly
          rustup target add ${{ matrix.arch }}-linux-android

      - name: Install NDK
        id: setup-ndk
        uses: nttld/setup-ndk@v1
        with:
          ndk-version: r25

      - name: Set Android NDK ToolChains Path
        run: |
          echo "ANDROID_NDK_HOME=${{ steps.setup-ndk.outputs.ndk-path }}" >> $GITHUB_ENV
          echo ${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin >> $GITHUB_PATH

      - name: Set libgcc
        run: |
          cat << EOF > ${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.6/lib/linux/${{ matrix.arch }}/libgcc.a
          INPUT(-lunwind)
          EOF

      - name: Build Android ${{ matrix.arch }}
        run: cargo build --target ${{ matrix.arch }}-linux-android
        env:
          CC_AARCH64_LINUX_ANDROID: aarch64-linux-android26-clang
          CXX_AARCH64_LINUX_ANDROID: aarch64-linux-android26-clang++
          CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: aarch64-linux-android26-clang
          CC_X86_64_LINUX_ANDROID: x86_64-linux-android26-clang
          CXX_X86_64_LINUX_ANDROID: x86_64-linux-android26-clang++
          CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER: x86_64-linux-android26-clang

      - name: Upload
        uses: actions/upload-artifact@v3
        with:
          name: atri_bot-${{ matrix.arch }}-linux-android
          path: target/${{ matrix.arch }}-linux-android/debug/atri_bot