name: CI

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        thing: [stable, beta, nightly, macos, windows-msvc64, windows-msvc32, windows-gnu64, windows-gnu32]
        include:
          - thing: stable
            os: ubuntu-latest
            rust: stable
          - thing: beta
            os: ubuntu-latest
            rust: beta
          - thing: nightly
            os: ubuntu-latest
            rust: nightly
          - thing: macos
            os: macos-latest
            rust: stable
          # Note that these are on nightly due to rust-lang/rust#63700 not being
          # on stable yet
          - thing: windows-msvc64
            os: windows-latest
            rust: stable-x86_64-msvc
          - thing: windows-msvc32
            os: windows-latest
            rust: stable-i686-msvc
          - thing: windows-gnu64
            os: windows-latest
            rust: stable-x86_64-gnu
          - thing: windows-gnu32
            os: windows-latest
            rust: stable-i686-gnu
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust (rustup)
      run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
      shell: bash
    # full fidelity of backtraces on 32-bit msvc requires frame pointers, so
    # enable that for our tests
    - name: Force frame pointers
      run: echo "##[set-env name=RUSTFLAGS]-Cforce-frame-pointers"
      shell: bash
      if: matrix.thing == 'windows-msvc32'
    - run: cargo build --manifest-path crates/backtrace-sys/Cargo.toml
    - run: cargo build
    - run: cargo test
    - run: cargo test --features "gimli-symbolize"
    - run: cargo test --features "libbacktrace"
    - run: cargo test --features "libbacktrace gimli-symbolize"
    - run: cargo test --features "serialize-rustc"
    - run: cargo test --features "serialize-serde"
    - run: cargo test --features "verify-winapi"
    - run: cargo test --features "cpp_demangle"
    - run: cargo test --no-default-features
    - run: cargo test --no-default-features --features "libbacktrace"
    - run: cargo test --no-default-features --features "gimli-symbolize"
    - run: cargo test --no-default-features --features "gimli-symbolize libbacktrace"
    - run: cargo test --no-default-features --features "libbacktrace std"
    - run: cargo test --no-default-features --features "gimli-symbolize std"
    - run: cargo test --no-default-features --features "std"
    - run: cargo test --manifest-path crates/cpp_smoke_test/Cargo.toml
    - run: cargo test --manifest-path crates/macos_frames_test/Cargo.toml
    - run: cargo test --features libbacktrace --manifest-path crates/without_debuginfo/Cargo.toml
    - run: cargo test --features gimli-symbolize --manifest-path crates/without_debuginfo/Cargo.toml
    - run: cargo test --manifest-path crates/line-tables-only/Cargo.toml --features libbacktrace
    - run: cargo test --manifest-path crates/line-tables-only/Cargo.toml --features gimli-symbolize
    - run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gabi" cargo test --features gimli-symbolize
      if: contains(matrix.os, 'ubuntu')
    - run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gnu" cargo test --features gimli-symbolize
      if: contains(matrix.os, 'ubuntu')
    - run: cargo build --manifest-path crates/as-if-std/Cargo.toml

  windows_arm64:
    name: Windows AArch64
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust
      run: rustup update stable --no-self-update && rustup default stable
      shell: bash
    - run: rustup target add aarch64-pc-windows-msvc
    - run: cargo test --no-run --target aarch64-pc-windows-msvc
    - run: cargo test --no-run --target aarch64-pc-windows-msvc --features verify-winapi

  ios:
    name: iOS
    runs-on: macos-latest
    strategy:
      matrix:
        target:
        - aarch64-apple-ios
        - x86_64-apple-ios
        include:
        - target: aarch64-apple-ios
          sdk: iphoneos
        - target: x86_64-apple-ios
          sdk: iphonesimulator
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust (
      run: |
        curl https://sh.rustup.rs | sh -s -- -y
        echo "##[add-path]$HOME/.cargo/bin"
    - run: rustup target add ${{ matrix.target }}
    - run: |
        export SDK_PATH=`xcrun --show-sdk-path --sdk ${{ matrix.sdk }}`
        export RUSTFLAGS="-C link-arg=-isysroot -C link-arg=$SDK_PATH"
        cargo test --no-run --target ${{ matrix.target }}
      name: Build tests

  docker:
    name: Docker
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
        - aarch64-unknown-linux-gnu
        - arm-unknown-linux-gnueabihf
        - armv7-unknown-linux-gnueabihf
        - i586-unknown-linux-gnu
        - i686-unknown-linux-gnu
        - powerpc64-unknown-linux-gnu
        - x86_64-pc-windows-gnu
        - x86_64-unknown-linux-gnu
        - x86_64-unknown-linux-musl
        - arm-linux-androideabi
        - armv7-linux-androideabi
        - aarch64-linux-android
        - i686-linux-android
        - x86_64-linux-android
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust
      run: rustup update stable && rustup default stable
    - run: rustup target add ${{ matrix.target }}
    - run: cargo generate-lockfile
    - run: ./ci/run-docker.sh ${{ matrix.target }}

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

  build:
    name: Build Targets
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [wasm32-unknown-unknown, wasm32-wasi, x86_64-fuchsia, x86_64-fortanix-unknown-sgx]
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust
      run: rustup update nightly && rustup default nightly
    - run: rustup target add ${{ matrix.target }}
    - run: cargo build --target ${{ matrix.target }}

  msrv:
    name: MSRV
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust
      run: rustup update 1.40.0 && rustup default 1.40.0
    - run: cargo build

  miri:
    name: Miri
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: true
    - name: Install Rust
      run: ./ci/miri-rustup.sh
    - run: MIRIFLAGS="-Zmiri-disable-isolation" cargo miri test
