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-24.04
            rust: stable
          - os: ubuntu-24.04
            rust: beta
          - os: ubuntu-24.04
            rust: nightly
          - os: macos-latest
            rust: stable
          - os: macos-latest
            rust: nightly
          - os: windows-latest
            rust: stable-x86_64-msvc
          - os: windows-latest
            rust: stable-i686-msvc
          - os: windows-latest
            rust: stable-x86_64-gnu
          - os: windows-latest
            rust: nightly-x86_64-msvc
          - os: windows-latest
            rust: nightly-i686-msvc
          - os: windows-latest
            rust: nightly-x86_64-gnu
    steps:
      - uses: actions/checkout@v4
        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: contains(matrix.rust, 'i686')

      # Starting with Ubuntu 22.04 libc6-dbg is needed.
      - name: Install libc debug info
        run: sudo apt-get install -y libc6-dbg
        shell: bash
        if: contains(matrix.os, 'ubuntu-24.04')

      - name: Enable collapse_debuginfo based on version
        run: echo RUSTFLAGS="--cfg dbginfo=\"collapsible\" $RUSTFLAGS" >> $GITHUB_ENV
        shell: bash
        if: contains(matrix.rust, 'nightly') || contains(matrix.rust, 'beta')

      - run: cargo build
      - run: cargo test
      - run: cargo test --features "serialize-serde"
      - 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 --manifest-path crates/without_debuginfo/Cargo.toml
      - run: cargo test --manifest-path crates/line-tables-only/Cargo.toml

      # Test debuginfo compression still works
      - run: cargo test
        if: contains(matrix.os, 'ubuntu')
        env:
          RUSTFLAGS: "-C link-arg=-Wl,--compress-debug-sections=zlib"
      - run: cargo test --features "ruzstd"
        if: contains(matrix.os, 'ubuntu-24.04') ||
            (contains(matrix.os, 'ubuntu') && contains(matrix.rust, 'nightly'))
        env:
          RUSTFLAGS: "-C link-arg=-Wl,--compress-debug-sections=zstd"

      # 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 backtraces are still symbolicated if we don't embed an absolute
      # path to the PDB file in the binary.
      # Add -Cforce-frame-pointers for stability. The test otherwise fails
      # non-deterministically on i686-pc-windows-msvc because the stack cannot be
      # unwound reliably. This failure is not related to the feature being tested.
      - run: cargo clean && cargo test
        if: contains(matrix.rust, 'msvc')
        name: "Test that backtraces are symbolicated without absolute PDB path"
        env:
          RUSTFLAGS: "-Clink-arg=/PDBALTPATH:%_PDB% -Cforce-frame-pointers"

      # Test that including as a submodule will still work, both with and without
      # the `backtrace` feature enabled.
      # Building as if part of std requires nightly features to be available
      - run: cargo build --manifest-path crates/as-if-std/Cargo.toml
        if: matrix.rust == 'nightly'
      - run: cargo build --manifest-path crates/as-if-std/Cargo.toml --no-default-features
        if: matrix.rust == 'nightly'

  windows_arm64:
    name: Windows AArch64
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
        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

  ios:
    name: iOS
    runs-on: macos-latest
    strategy:
      matrix:
        include:
          - target: aarch64-apple-ios
          - target: x86_64-apple-ios
          - target: aarch64-apple-ios-macabi
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - run: rustup target add ${{ matrix.target }}
      - name: Run tests
        run: cargo test ${{ contains(matrix.target, 'macabi') && '' || '--no-run' }} --target ${{ matrix.target }}
        env:
          RUSTFLAGS: -Dwarnings

  docker:
    name: Docker
    runs-on: ubuntu-24.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@v4
        with:
          submodules: true
      - name: Install Rust
        run: rustup update stable --no-self-update && 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-24.04
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Install Rust
        run: rustup update stable --no-self-update && rustup default stable && rustup component add rustfmt
      - run: cargo fmt --all -- --check

  build:
    name: Build Targets
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        target:
          - wasm32-unknown-unknown
          - wasm32-wasip1
          - x86_64-unknown-fuchsia
          - x86_64-fortanix-unknown-sgx
          - x86_64-unknown-illumos
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Install Rust
        run: rustup update nightly --no-self-update && 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: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-24.04
          - os: windows-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Install Rust
        run: rustup update 1.82.0 --no-self-update && rustup default 1.82.0
      - run: cargo build

  miri:
    name: Miri
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
        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
