# TODO: refactor to avoid duplication with the ci.yml file.
name: Failures

on:
  push:
    branches:
      - master
  pull_request:

permissions:
  contents: read

env:
  # Enable backtraces for easier debugging
  RUST_BACKTRACE: 1

jobs:
  build:
    runs-on: ubuntu-24.04

    strategy:
      fail-fast: false
      matrix:
        libgccjit_version:
          - gcc: "libgccjit.so"
            artifacts_branch: "master"
          - gcc: "libgccjit_without_int128.so"
            artifacts_branch: "master-without-128bit-integers"
          - gcc: "libgccjit12.so"
            artifacts_branch: "gcc12"
            extra: "--no-default-features"
            # FIXME(antoyo): we need to set GCC_EXEC_PREFIX so that the linker can find the linker plugin.
            # Not sure why it's not found otherwise.
            env_extra: "TEST_FLAGS='-Cpanic=abort -Zpanic-abort-tests' GCC_EXEC_PREFIX=/usr/lib/gcc/"

    steps:
    - uses: actions/checkout@v4

    # `rustup show` installs from rust-toolchain.toml
    - name: Setup rust toolchain
      run: rustup show

    - name: Setup rust cache
      uses: Swatinem/rust-cache@v2

    - name: Install packages
      run: sudo apt-get install ninja-build ripgrep

    - name: Install libgccjit12
      if: matrix.libgccjit_version.gcc == 'libgccjit12.so'
      run: sudo apt-get install libgccjit-12-dev

    - name: Setup path to libgccjit
      if: matrix.libgccjit_version.gcc == 'libgccjit12.so'
      run: |
          echo 'gcc-path = "/usr/lib/gcc/x86_64-linux-gnu/12"' > config.toml
          echo "LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12" >> $GITHUB_ENV

    - name: Download artifact
      if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
      run: curl -LO https://github.com/rust-lang/gcc/releases/latest/download/gcc-15.deb

    - name: Setup path to libgccjit
      if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
      run: |
          sudo dpkg --force-overwrite -i gcc-15.deb
          echo 'gcc-path = "/usr/lib"' > config.toml
          
          

    - name: Set env
      run: |
        echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV

    #- name: Cache rust repository
      #uses: actions/cache@v3
      #id: cache-rust-repository
      #with:
        #path: rust
        #key: ${{ runner.os }}-packages-${{ hashFiles('rust/.git/HEAD') }}

    - name: Git config
      run: |
        git config --global user.email "user@example.com"
        git config --global user.name "User"

    - name: Prepare dependencies
      if: matrix.libgccjit_version.gcc == 'libgccjit12.so'
      run: ./y.sh prepare --libgccjit12-patches

    - name: Prepare dependencies
      if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
      run: ./y.sh prepare

    - name: Run tests
      # TODO: re-enable those tests for libgccjit 12.
      if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
      id: tests
      run: |
        ${{ matrix.libgccjit_version.env_extra }} ./y.sh test --release --clean --build-sysroot --test-failing-rustc ${{ matrix.libgccjit_version.extra }} 2>&1 | tee output_log
        rg --text "test result" output_log >> $GITHUB_STEP_SUMMARY

    - name: Run failing ui pattern tests for ICE
      # TODO: re-enable those tests for libgccjit 12.
      if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
      id: ui-tests
      run: |
        ${{ matrix.libgccjit_version.env_extra }} ./y.sh test --release --test-failing-ui-pattern-tests ${{ matrix.libgccjit_version.extra }} 2>&1 | tee output_log_ui
        if grep -q "the compiler unexpectedly panicked" output_log_ui; then
          echo "Error: 'the compiler unexpectedly panicked' found in output logs. CI Error!!"
          exit 1
        fi

  # Summary job for the merge queue.
  # ALL THE PREVIOUS JOBS NEED TO BE ADDED TO THE `needs` SECTION OF THIS JOB!
  success_failures:
    needs: [build]
    # We need to ensure this job does *not* get skipped if its dependencies fail,
    # because a skipped job is considered a success by GitHub. So we have to
    # overwrite `if:`. We use `!cancelled()` to ensure the job does still not get run
    # when the workflow is canceled manually.
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    steps:
      # Manually check the status of all dependencies. `if: failure()` does not work.
      - name: Conclusion
        run: |
          # Print the dependent jobs to see them in the CI log
          jq -C <<< '${{ toJson(needs) }}'
          # Check if all jobs that we depend on (in the needs array) were successful.
          jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'
