name: 'Install LLVM'
description: 'Install LLVM'
inputs:
  clang_version:
    description: 'version string of Clang to download'
    required: true
  llvm_asset_suffix:
    description: 'extra info about how to download from LLVM binaries'
    required: false
    default: ''

runs:
  using: composite
  steps:
    - name: Install LLVM tools (Windows)
      shell: bash
      run: |
        curl -fsSLO https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ inputs.clang_version }}/LLVM-${{ inputs.clang_version }}-win64.exe
        7z x LLVM-${{ inputs.clang_version }}-win64.exe -y -o"llvm"
        echo "$(pwd)/llvm/bin" >> $GITHUB_PATH
        echo "CC=$(pwd)/llvm/bin/clang.exe" >> $GITHUB_ENV
        echo "AR=$(pwd)/llvm/bin/llvm-ar.exe" >> $GITHUB_ENV
        echo "NM=$(pwd)/llvm/bin/llvm-nm.exe" >> $GITHUB_ENV
      if: runner.os == 'Windows'

    - name: Override llvm-nm with one from rustup (Windows)
      shell: bash
      run: |
        rustup update stable
        rustup default stable
        rustup component add llvm-tools-preview
        echo "NM=$(rustc --print sysroot|sed 's|C:|/c|'|sed 's|\\|/|g')/lib/rustlib/x86_64-pc-windows-msvc/bin/llvm-nm.exe" >> $GITHUB_ENV
      if: runner.os == 'Windows'

    - name: Install LLVM tools (MacOS)
      shell: bash
      run: |
        case ${{ inputs.clang_version }} in
          wasi-sdk-*)
            curl -sSfL https://github.com/WebAssembly/wasi-sdk/releases/download/${{ inputs.clang_version }}/${{ inputs.clang_version }}.0-arm64-macos.tar.gz | tar xzf -
            export CLANG_DIR=`pwd`/${{ inputs.clang_version }}.0-arm64-macos/bin
            ;;
          *)
            curl -sSfL https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ inputs.clang_version }}/clang+llvm-${{ inputs.clang_version }}-${{ inputs.llvm_asset_suffix }}.tar.xz | tar xJf -
            export CLANG_DIR=`pwd`/clang+llvm-${{ inputs.clang_version }}-${{ inputs.llvm_asset_suffix }}/bin
            ;;
        esac
        echo "$CLANG_DIR" >> $GITHUB_PATH
        echo "CC=$CLANG_DIR/clang" >> $GITHUB_ENV
        echo "AR=$CLANG_DIR/llvm-ar" >> $GITHUB_ENV
        echo "NM=$CLANG_DIR/llvm-nm" >> $GITHUB_ENV
      if: runner.os == 'macOS'

    # Use apt repositories to install various clang versions on Linux as those
    # generally work the best and are more available than per-release downloads
    # of LLVM. Note that apt.llvm.org may be used for new enough versions of
    # clang which aren't in Ubuntu 24.04.
    - name: Install LLVM tools (Linux)
      shell: bash
      run: |
        set -ex
        v=${{ inputs.clang_version }}
        # For LLVM versions 21+ install from apt.llvm.org because the ubuntu
        # 24.04 repositories don't have these binaries.
        if [ $v -ge 21 ]; then
          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
          rel=$(lsb_release -cs)
          sudo apt-add-repository "deb http://apt.llvm.org/$rel/ llvm-toolchain-$rel-$v main"
        fi
        sudo apt-get update
        sudo apt-get install -y clang-$v clang-tools-$v lld-$v
        echo "CC=clang-$v" >> $GITHUB_ENV
        echo "AR=llvm-ar-$v" >> $GITHUB_ENV
        echo "NM=llvm-nm-$v" >> $GITHUB_ENV
      if: runner.os == 'Linux'
