name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

defaults:
  run:
    shell: bash

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
        - target: x86_64-unknown-linux-gnu
          rust: stable
          os: ubuntu-latest
        - target: x86_64-unknown-linux-gnu
          rust: beta
          os: ubuntu-latest
        - target: x86_64-unknown-linux-gnu
          rust: nightly
          os: ubuntu-latest
        - target: x86_64-apple-darwin
          rust: stable
          os: macos-latest
        - target: aarch64-apple-darwin
          rust: nightly
          os: macos-latest
        #- target: arm-linux-androideabi
        #  rust: stable
        #  os: ubuntu-latest
        #- target: aarch64-linux-android
        #  rust: stable
        #  os: ubuntu-latest
        #- target: i686-linux-android
        #  rust: stable
        #  os: ubuntu-latest
        #- target: x86_64-linux-android
        #  rust: stable
        #  os: ubuntu-latest
        - target: i686-unknown-linux-gnu
          rust: stable
          os: ubuntu-latest
        - target: arm-unknown-linux-gnueabi
          rust: stable
          os: ubuntu-latest
        - target: aarch64-unknown-linux-gnu
          rust: stable
          os: ubuntu-latest
        - target: riscv64gc-unknown-linux-gnu
          rust: stable
          os: ubuntu-latest
        - target: loongarch64-unknown-linux-gnu
          rust: stable
          os: ubuntu-latest
        - target: x86_64-unknown-linux-musl
          rust: stable
          os: ubuntu-latest
        - target: x86_64-pc-windows-gnu
          rust: stable
          os: ubuntu-latest
        # - target: wasm32-wasi
        #   rust: stable
        #   os: ubuntu-latest
        - target: i686-pc-windows-msvc
          rust: stable-i686-msvc
          os: windows-2022
        - target: i686-pc-windows-msvc
          rust: stable-i686-msvc
          os: windows-2022
          crt_static: yes
        - target: x86_64-pc-windows-msvc
          rust: stable-x86_64-msvc
          os: windows-latest
        - target: x86_64-pc-windows-msvc
          rust: stable-x86_64-msvc
          os: windows-latest
          crt_static: yes
        - target: x86_64-pc-windows-msvc
          rust: stable-x86_64-msvc
          os: windows-latest
          nasm_exe: installed

    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust (rustup)
      run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
    - run: rustup target add ${{ matrix.target }}
    - name: Set crt-static
      if: matrix.crt_static == 'yes'
      run: echo RUSTFLAGS=-Ctarget-feature=+crt-static >> $GITHUB_ENV
    - name: Use strawberry perl
      if: startsWith(matrix.os, 'windows')
      run: echo OPENSSL_SRC_PERL=C:/Strawberry/perl/bin/perl >> $GITHUB_ENV
    - name: Run tests (not Windows)
      if: "!startsWith(matrix.os, 'windows')"
      run: |
        set -e
        cargo generate-lockfile
        ./ci/run-docker.sh ${{ matrix.target }}
    - name: Download nasm.exe (Windows)
      if: matrix.nasm_exe == 'installed'
      run: |
        WINNASMVERSION='2.15.05'
        curl -O https://www.nasm.us/pub/nasm/releasebuilds/${WINNASMVERSION}/win64/nasm-${WINNASMVERSION}-win64.zip
        unzip nasm-${WINNASMVERSION}-win64.zip
        echo "$GITHUB_WORKSPACE\\nasm-${WINNASMVERSION}" >> $GITHUB_PATH
        echo "OPENSSL_RUST_USE_NASM=1" >> $GITHUB_ENV
    - name: Run tests (Windows)
      if: startsWith(matrix.os, 'windows')
      run: |
        cargo test --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} -vv
        cargo test --manifest-path testcrate/Cargo.toml --target ${{ matrix.target }} --release -vv
        cargo run --release --target ${{ matrix.target }} --manifest-path testcrate/Cargo.toml --features package -vv

  rustfmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Install Rust
      run: rustup update stable && rustup default stable && rustup component add rustfmt
    - run: cargo fmt -- --check
