name: CI

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

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            rust: stable
          - os: ubuntu-20.04
            rust: beta
          - os: ubuntu-20.04
            rust: nightly
          - os: macos-latest
            rust: stable
          - os: macos-latest
            rust: nightly
          # Note that these are on nightly due to rust-lang/rust#63700 not being
          # on stable yet
          - os: windows-latest
            rust: stable-x86_64-msvc
          - os: windows-latest
            rust: stable-i686-msvc
          - os: windows-latest
            rust: stable-x86_64-gnu
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Install Rust (rustup)
      run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
      shell: bash
    - run: echo RUSTFLAGS=-Dwarnings >> $GITHUB_ENV
      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 RUSTFLAGS="-Cforce-frame-pointers $RUSTFLAGS" >> $GITHUB_ENV
      shell: bash
      if: matrix.rust == 'stable-i686-msvc'

    - run: cargo build
    - run: cargo test
    - 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 "std"
    - run: cargo test --manifest-path crates/cpp_smoke_test/Cargo.toml
    # This test is specifically about packed debuginfo with `*.dSYM` files
    - run: cargo test --manifest-path crates/macos_frames_test/Cargo.toml
      env:
        CARGO_PROFILE_DEV_SPLIT_DEBUGINFO: packed
        CARGO_PROFILE_TEST_SPLIT_DEBUGINFO: packed
    - 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 gimli-symbolize

    # Test debuginfo compression still works
    - run: cargo test
      if: contains(matrix.os, 'ubuntu')
      env:
        RUSTFLAGS: "-C link-arg=-Wl,--compress-debug-sections=zlib-gabi"
    - run: cargo test
      if: contains(matrix.os, 'ubuntu')
      env:
        RUSTFLAGS: "-C link-arg=-Wl,--compress-debug-sections=zlib-gnu"

    # Test that, on macOS, packed/unpacked debuginfo both work
    - run: cargo clean && cargo test
    # Test that, on macOS, packed/unpacked debuginfo both work
      if: matrix.os == 'macos-latest'
      env:
        CARGO_PROFILE_DEV_SPLIT_DEBUGINFO: unpacked
        CARGO_PROFILE_TEST_SPLIT_DEBUGINFO: unpacked
    - run: cargo clean && cargo test
      if: matrix.os == 'macos-latest'
      env:
        CARGO_PROFILE_DEV_SPLIT_DEBUGINFO: packed
        CARGO_PROFILE_TEST_SPLIT_DEBUGINFO: packed
    # Test that, on macOS, binaries with no UUID work
    - run: cargo clean && cargo test
      if: matrix.os == 'macos-latest'
      env:
        RUSTFLAGS: "-C link-arg=-Wl,-no_uuid"

    # Test that, on Linux, packed/unpacked debuginfo both work
    - run: cargo clean && cargo test
      if: matrix.rust == 'nightly'
      env:
        RUSTFLAGS: "-C split-debuginfo=unpacked -Zunstable-options"
    - run: cargo clean && cargo test
      if: matrix.rust == 'nightly'
      env:
        RUSTFLAGS: "-C split-debuginfo=packed -Zunstable-options"

    # Test that separate debug info works
    - run: ./ci/debuglink-docker.sh
      if: contains(matrix.os, 'ubuntu')

    # Test that including as a submodule will still work, both with and without
    # the `backtrace` feature enabled.
    - run: cargo build --manifest-path crates/as-if-std/Cargo.toml
    - run: cargo build --manifest-path crates/as-if-std/Cargo.toml --no-default-features

  windows_arm64:
    name: Windows AArch64
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Install Rust
      run: rustup update stable --no-self-update && rustup default stable
      shell: bash
    - run: echo RUSTFLAGS=-Dwarnings >> $GITHUB_ENV
      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:
        include:
        - target: aarch64-apple-ios
          sdk: iphoneos
        - target: x86_64-apple-ios
          sdk: iphonesimulator
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - run: rustup target add ${{ matrix.target }}
    - run: |
        export RUSTFLAGS=-Dwarnings
        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-20.04
    strategy:
      fail-fast: false
      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
        - s390x-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@v3
      with:
        submodules: true
    - name: Install Rust
      run: rustup update stable && rustup default stable
    - run: rustup target add ${{ matrix.target }}
    - run: cargo generate-lockfile
    - run: echo RUSTFLAGS=-Dwarnings >> $GITHUB_ENV
      shell: bash
    - run: ./ci/run-docker.sh ${{ matrix.target }}

  rustfmt:
    name: Rustfmt
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
      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-20.04
    strategy:
      matrix:
        target:
        - wasm32-unknown-unknown
        - wasm32-wasi
        - x86_64-unknown-fuchsia
        - x86_64-fortanix-unknown-sgx
        - x86_64-unknown-illumos
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Install Rust
      run: rustup update nightly && rustup default nightly
    - run: rustup target add ${{ matrix.target }}
    - run: echo RUSTFLAGS=-Dwarnings >> $GITHUB_ENV
      shell: bash
    - run: cargo build --target ${{ matrix.target }}
    - run: cargo build --manifest-path crates/as-if-std/Cargo.toml --target ${{ matrix.target }}

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

  miri:
    name: Miri
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - name: Install Miri
      run: |
        rustup toolchain install nightly --component miri
        rustup override set nightly
        cargo miri setup
    - run: MIRIFLAGS="-Zmiri-disable-isolation" cargo miri test
