name: Release LLVM
on:
  workflow_dispatch:
    inputs:
      llvm_version:
        type: string
        required: true
        description: llvm version in "x.x.x" format, e.g. "18.1.8"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  create-release-draft:
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    outputs:
      version: ${{ steps.resolve-version.outputs.version }}
    steps:
      - id: resolve-version
        run: |
          echo "version=llvm-${{ inputs.llvm_version }}-revive.${GITHUB_SHA:0:7}" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: ${{ steps.resolve-version.outputs.version }}
          body: "LLVM is a dependency of revive. The LLVM releases are used by our CI to build revive."
          draft: true
          tag_name: ${{ steps.resolve-version.outputs.version }}

  build:
    strategy:
      matrix:
        target: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl, wasm32-unknown-emscripten, aarch64-apple-darwin, x86_64-apple-darwin, x86_64-pc-windows-msvc]
        include:
          - target: x86_64-unknown-linux-gnu
            builder-arg: gnu
            host: linux
            runner: parity-large
          - target: x86_64-unknown-linux-musl
            builder-arg: musl
            host: linux
            runner: parity-large
          - target: wasm32-unknown-emscripten
            builder-arg: emscripten
            host: linux
            runner: parity-large
          - target: aarch64-apple-darwin
            builder-arg: gnu
            host: macos
            runner: macos-15
          - target: x86_64-apple-darwin
            builder-arg: gnu
            host: macos
            # `macos-15-intel` will be the last x86_64 `macos` image supported by GHA.
            # It will be available until Aug. 2027 (see https://github.com/actions/runner-images/issues/13045).
            runner: macos-15-intel
          - target: x86_64-pc-windows-msvc
            builder-arg: gnu
            host: windows
            runner: windows-2022
    needs: create-release-draft
    runs-on: ${{ matrix.runner }}
    env:
      RUST_LOG: trace
    permissions:
      contents: write # for uploading assets to release
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          # without this it will override our rust flags
          rustflags: ""
          cache-key: ${{ matrix.target }}

      - name: Install Dependencies
        if: ${{ matrix.host == 'linux' }}
        run: |
          sudo apt-get update && sudo apt-get install -y cmake ninja-build curl git libssl-dev pkg-config clang lld musl

      - name: Install Dependencies
        if: ${{ matrix.host == 'macos' }}
        run: |
          brew install ninja

      - name: Install Dependencies
        if: ${{ matrix.host == 'windows' }}
        run: |
          choco install ninja

      - name: Install LLVM Builder
        run: |
          cargo install --locked --force --path crates/llvm-builder

      - name: Build LLVM
        if: ${{ matrix.target != 'wasm32-unknown-emscripten' }}
        run: |
          revive-llvm --target-env ${{ matrix.builder-arg }} build --llvm-projects lld --llvm-projects clang

      - name: Build LLVM
        if: ${{ matrix.target == 'wasm32-unknown-emscripten' }}
        run: |
          revive-llvm emsdk
          source emsdk/emsdk_env.sh
          revive-llvm --target-env ${{ matrix.builder-arg }} build --llvm-projects lld

      - name: Remove Unnecessary Binaries
        shell: bash
        run: |
          cd target-llvm/${{ matrix.builder-arg }}/target-final/bin/
          rm -f diagtool* llvm-libtool-darwin* llvm-lipo* llvm-pdbutil* llvm-dwarfdump* llvm-nm* llvm-readobj* llvm-cfi-verify* \
              sancov* llvm-debuginfo-analyzer* llvm-objdump* llvm-profgen* llvm-extract* llvm-jitlink* llvm-c-test* llvm-gsymutil* llvm-dwp* \
              dsymutil* llvm-dwarfutil* llvm-exegesis* lli clang-rename* bugpoint* clang-extdef-mapping* clang-refactor* c-index-test* \
              llvm-reduce* llvm-lto* clang-linker-wrapper* llc* llvm-lto2* llvm-otool* llvm-readelf* \
              clang-repl* clang-check* clang-scan-deps*
          cd -

      - name: Package Artifact
        shell: bash
        run: |
          mv target-llvm/${{ matrix.builder-arg }}/target-final/ llvm-${{ matrix.target }}
          tar -czf "${{ needs.create-release-draft.outputs.version }}-${{ matrix.target }}.tar.gz" llvm-${{ matrix.target }}

      - name: Add Artifact to Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.create-release-draft.outputs.version }}
          draft: true
          files: |
            ${{ needs.create-release-draft.outputs.version }}-${{ matrix.target }}.tar.gz
