on:
  push:
    branches: [master]
  pull_request:

name: CI

jobs:
  rustfmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt
      - run: cargo fmt -- --check

  clippy:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true
          components: clippy
      - run: cargo clippy -- -D warnings

  check:
    name: Check ${{ matrix.toolchain }} / ${{ matrix.triple.target }}
    runs-on: ${{ matrix.triple.os }}
    strategy:
      fail-fast: false
      matrix:
        triple:
          - { os: 'ubuntu-latest',  target: 'x86_64-unknown-linux-gnu', cross: false }
          - { os: 'ubuntu-latest',  target: 'i686-unknown-linux-gnu',   cross: true }
          - { os: 'macos-latest',   target: 'x86_64-apple-darwin',      cross: false }
          - { os: 'windows-latest', target: 'x86_64-pc-windows-msvc',   cross: false }
          ## iOS
          - { os: 'macos-latest', target: 'aarch64-apple-ios', cross: true }
          - { os: 'macos-latest', target: 'x86_64-apple-ios', cross: true }
          ## ARM64
          - { os: 'ubuntu-latest', target: 'aarch64-unknown-linux-gnu',  cross: true }
          - { os: 'ubuntu-latest', target: 'aarch64-unknown-linux-musl', cross: true }
          ## ARMv7
          - { os: 'ubuntu-latest', target: 'armv7-unknown-linux-gnueabihf',  cross: true }
          - { os: 'ubuntu-latest', target: 'armv7-unknown-linux-musleabihf', cross: true }
          ## ARMv6
          - { os: 'ubuntu-latest', target: 'arm-unknown-linux-gnueabihf',  cross: true }
          - { os: 'ubuntu-latest', target: 'arm-unknown-linux-musleabihf', cross: true }
          # Android
          - { os: 'ubuntu-latest', target: 'aarch64-linux-android', cross: true }
          - { os: 'ubuntu-latest', target: 'armv7-linux-androideabi', cross: true }
          - { os: 'ubuntu-latest', target: 'x86_64-linux-android', cross: true }
          - { os: 'ubuntu-latest', target: 'i686-linux-android', cross: true }
        toolchain:
          - "1.45.0"  # minimum supported rust version
          - stable
          - nightly
    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.toolchain }}
          override: true

      - name: Check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --target=${{ matrix.triple.target }} --manifest-path=Cargo.toml
          use-cross: ${{ matrix.triple.cross }}

      - name: Check example
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --target=${{ matrix.triple.target }} --manifest-path=Cargo.toml
          use-cross: ${{ matrix.triple.cross }}
          working-directory: examples

      - name: Check (Apple app store restrictions)
        if: matrix.triple.os == 'macos-latest'
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --features apple-app-store --target=${{ matrix.triple.target }} --manifest-path=Cargo.toml
          use-cross: ${{ matrix.triple.cross }}


  tests:
    needs: [check]
    name: Test ${{ matrix.os }} (rust ${{matrix.toolchain}})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        toolchain:
          - "1.45.0"  # minimum supported rust version
          - stable
          - nightly
    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.toolchain }}
          override: true
      - name: Execute tests (not mac)
        run: cargo test
        if: matrix.os != 'macos-latest'
        env:
          RUST_BACKTRACE: full
      - name: Execute tests (mac)
        run: cargo test -- --test-threads 1
        if: matrix.os == 'macos-latest'
        env:
          RUST_BACKTRACE: full
      - name: Execute tests (not mac, no features)
        run: cargo test --no-default-features
        if: matrix.os != 'macos-latest'
        env:
          RUST_BACKTRACE: full
      - name: Execute tests (mac, no features)
        run: cargo test --no-default-features -- --test-threads 1
        if: matrix.os == 'macos-latest'
        env:
          RUST_BACKTRACE: full

  c_interface:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - run: cargo build --features=c-interface

  test_freebsd:
    runs-on: macos-latest
    name: Check stable / FreeBSD host
    steps:
      - uses: actions/checkout@v2
      - uses: vmactions/freebsd-vm@v0.1.4
        id: test
        with:
          usesh: true
          prepare: pkg install -y curl
          run: |
            curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | RUSTUP_IO_THREADS=1 sh -s -- --profile minimal --default-toolchain stable -y
            $HOME/.cargo/bin/cargo check    
            $HOME/.cargo/bin/cargo test
            cd examples
            $HOME/.cargo/bin/cargo check
