name: Setup System and Rust
description: Setup System and Rust
inputs:
  token:
    description: Github token
    required: false
    default: ''
  target:
    description: toolchain target triple
    required: false
  setup-arg:
    description: Argument for the system setup script
    required: false
    default: ''
  save-cache:
    description: Whether to save the System cache
    required: false
    default: 'false'
runs:
  using: 'composite'
  steps:
    - name: Restore cached libclang
      if: ${{ runner.os == 'Windows' }}
      id: cache-libclang
      uses: actions/cache/restore@v4
      with:
        key: libclang-15-win64
        path: C:\libclang

    - name: Download libclang for bindgen
      if: ${{ runner.os == 'Windows' && steps.cache-libclang.outputs.cache-hit != 'true' }}
      shell: powershell
      run: |
        # Only libclang.dll is needed (for bindgen). Installing the full LLVM toolchain
        # puts clang-cl.exe on PATH, which causes aws-lc-sys to mis-detect compiler
        # support for __builtin_bswap — leading to unresolved symbol errors with MSVC.
        $installer = "$env:TEMP\llvm-15.exe"
        Invoke-WebRequest -Uri "https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/LLVM-15.0.7-win64.exe" -OutFile $installer
        New-Item -ItemType Directory -Force -Path "C:\libclang"
        7z e $installer "-oC:\libclang" "bin\libclang.dll" -r -y
        Remove-Item $installer -Force
        if (!(Test-Path "C:\libclang\libclang.dll")) {
          Write-Error "Failed to extract libclang.dll"
          exit 1
        }
        Write-Host "Extracted libclang.dll to C:\libclang"

    - name: Save libclang cache
      if: ${{ runner.os == 'Windows' && inputs.save-cache == 'true' }}
      uses: actions/cache/save@v4
      with:
        key: ${{ steps.cache-libclang.outputs.cache-primary-key }}
        path: C:\libclang

    - name: Set LIBCLANG_PATH
      if: ${{ runner.os == 'Windows' }}
      shell: powershell
      run: echo "LIBCLANG_PATH=C:\libclang" >> $env:GITHUB_ENV

    - name: Install current Bash on macOS
      shell: bash
      if: runner.os == 'macOS'
      run: brew install bash

    - name: Install Nasm
      if: ${{ runner.os != 'Linux' }}
      uses: ilammy/setup-nasm@v1

    - name: Install Mold (linker)
      shell: bash
      if: ${{ runner.os == 'Linux' }}
      run: |
        curl -L# 'https://github.com/rui314/mold/releases/download/v2.4.0/mold-2.4.0-x86_64-linux.tar.gz' \
        | sudo tar -xzf- -C /usr/local

    - name: Remove 32-bit libs and incompatible pre-installed pkgs from Runner
      shell: bash
      if: ${{ runner.os == 'Linux' }}
      run: |
        set -eux
        if dpkg -l | grep i386; then
          sudo apt-get purge --allow-remove-essential libc6-i386 ".*:i386" || true
          sudo dpkg --remove-architecture i386 || true
        fi

        # https://github.com/actions/runner-images/issues/9546#issuecomment-2014940361
        sudo apt-get remove libunwind-* || true

    - name: Setup Rust and Dependencies
      uses: ./.github/actions/setup-rust
      with:
        target: ${{ inputs.target }}
        save-cache: ${{ inputs.save-cache }}

    - name: Run setup.sh script
      shell: bash
      if: ${{ runner.os == 'Linux' || runner.os == 'macOS' }}
      run: ./scripts/setup.sh ${{ inputs.setup-arg }}

    - name: Run setup.ps1 script
      shell: powershell
      if: ${{ runner.os == 'Windows' }}
      run: ./scripts/setup.ps1

    - name: Setup native dependencies
      shell: bash
      env:
        TARGET_TRIPLE: ${{ inputs.target }}
        GITHUB_TOKEN: ${{ inputs.token }}
      run: cargo run -p xtask -- setup
