name: build

on:
  push:
    branches-ignore: [main]
  pull_request:
    branches: [main]

jobs:
  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          components: clippy
          override: true
      - uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-features

  test-doc:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true
      - uses: Swatinem/rust-cache@v1
      - run: cargo install cargo-deadlinks
      - name: doc
        env:
          RUSTDOCFLAGS: --cfg doc_cfg
        run: cargo deadlinks --ignore-fragments -- --all

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            toolchain: stable
          - os: macos-latest
            target: x86_64-apple-darwin
            toolchain: stable
            # TODO: also aarch64 / M1
          - os: windows-latest
            target: x86_64-pc-windows-gnu
            toolchain: stable
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            toolchain: beta
            # Test both windows-gnu and windows-msvc; use beta rust on one
          - os: ubuntu-latest
            deps: sudo apt-get update ; sudo apt install gcc-multilib
            target: i686-unknown-linux-gnu
            toolchain: nightly
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            toolchain: nightly
            variant: minimal_versions

    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          target: ${{ matrix.target }}
          toolchain: ${{ matrix.toolchain }}
          override: true
      - uses: Swatinem/rust-cache@v1
      - run: ${{ matrix.deps }}
      - name: Maybe minimal versions
        if: ${{ matrix.variant == 'minimal_versions' }}
        run: cargo generate-lockfile -Z minimal-versions

      - name: Test
        run: |
          cargo test --lib --target ${{ matrix.target }} ${{ matrix.toolchain == 'nightly' && '--features=nightly' || '' }}

        env:
          # Enables address sanitization if supported target and nightly.
          RUSTFLAGS: |
            ${{ 
              (
                matrix.toolchain == 'nightly' && (
                  matrix.target == 'aarch64-apple-darwin' ||
                  matrix.target == 'aarch64-fuchsia' ||
                  matrix.target == 'aarch64-unknown-linux-gnu' ||
                  matrix.target == 'x86_64-apple-darwin' ||
                  matrix.target == 'x86_64-fuchsia' ||
                  matrix.target == 'x86_64-unknown-freebsd' ||
                  matrix.target == 'x86_64-unknown-linux-gnu'
                )
              )
                && '-Z sanitizer=address'
                || ''
            }}

  test-cross:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: mips-unknown-linux-gnu
            toolchain: stable
          - os: ubuntu-latest
            target: arm-unknown-linux-gnueabi
            toolchain: stable
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            toolchain: stable
          - os: ubuntu-latest
            target: arm-unknown-linux-gnueabi
            toolchain: nightly
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            toolchain: nightly

    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          target: ${{ matrix.target }}
          toolchain: ${{ matrix.toolchain }}
          override: true

      - uses: Swatinem/rust-cache@v1
      - name: Install cross
        run: cargo install cross || true
      - name: Test
        run: |
          cross test --no-fail-fast --target ${{ matrix.target }} ${{ matrix.toolchain == 'nightly' && '--features=nightly' || '' }}

  test-msrv:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          target: x86_64-unknown-linux-gnu
          toolchain: 1.36.0 # MSRV
      - uses: Swatinem/rust-cache@v1
      - name: Test
        run: cargo test --target=x86_64-unknown-linux-gnu --no-default-features --features=std

  test-no-std:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          target: thumbv6m-none-eabi
          override: true
      - uses: Swatinem/rust-cache@v1
      - name: Build top-level only
        run: cargo build --target=thumbv6m-none-eabi --no-default-features
