name: Setup cjbind toolchain
description: Install the toolchain and optionally restore the static libclang bundle

inputs:
  static-libclang:
    description: Restore the bundled libclang used for static linking
    required: false
    default: "true"
  runner-key:
    description: Stable runner identifier used by the libclang cache
    required: true
  architecture:
    description: Target architecture used by the libclang cache
    required: true

runs:
  using: composite
  steps:
    - name: Install uv
      uses: astral-sh/setup-uv@11f9893b081a58869d3b5fccaea48c9e9e46f990 # v8.3.2
      with:
        python-version: "3.14"
        cache-dependency-glob: "scripts/*.py.lock"

    - name: Restore libclang cache
      id: libclang-cache
      if: inputs.static-libclang == 'true'
      uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      with:
        path: lib/libclang
        key: libclang-${{ inputs.runner-key }}-${{ inputs.architecture }}-${{ hashFiles('scripts/libclang.json') }}

    - name: Download libclang
      if: inputs.static-libclang == 'true' && steps.libclang-cache.outputs.cache-hit != 'true'
      shell: bash
      run: uv run scripts/download.py

    - name: Save libclang cache
      if: inputs.static-libclang == 'true' && steps.libclang-cache.outputs.cache-hit != 'true'
      uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
      with:
        path: lib/libclang
        key: ${{ steps.libclang-cache.outputs.cache-primary-key }}

    - name: Setup Go
      uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
      with:
        go-version: "1.26"
        cache: false

    - name: Setup Cangjie
      uses: Zxilly/setup-cangjie@91463316b1c359f216c2ac49e35900f9341c1571 # v3.2.0
      with:
        channel: "sts"
        version: "1.1.3"

    - name: Patch Cangjie optimizer
      shell: bash
      run: uv run scripts/patch_opt.py

    - name: Install LLVM 17 libclang
      if: runner.os == 'Linux' && inputs.static-libclang != 'true'
      shell: bash
      run: |
        sudo apt-get update
        sudo apt-get install -y ca-certificates curl gnupg
        sudo install -m 0755 -d /etc/apt/keyrings
        curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key \
          | gpg --dearmor \
          | sudo tee /etc/apt/keyrings/apt.llvm.org.gpg >/dev/null
        echo "deb [signed-by=/etc/apt/keyrings/apt.llvm.org.gpg] https://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main" \
          | sudo tee /etc/apt/sources.list.d/apt.llvm.org.list >/dev/null
        sudo apt-get update
        sudo apt-get install -y libclang-17-dev
        test -e /usr/lib/llvm-17/lib/libclang.so
        echo "LIBCLANG_PATH=/usr/lib/llvm-17/lib" >> "$GITHUB_ENV"
        echo "LD_LIBRARY_PATH=/usr/lib/llvm-17/lib:${LD_LIBRARY_PATH:-}" >> "$GITHUB_ENV"

    - name: Setup MSYS2
      if: runner.os == 'Windows'
      uses: msys2/setup-msys2@66cd2cce69caa17b53920067426061ca1de3a884 # v2.32.0
      with:
        msystem: CLANG64
        update: true
        install: mingw-w64-clang-x86_64-clang
        path-type: inherit