on:
  workflow_call:
    inputs:
      save-rust-cache:
        required: false
        type: string
        default: "true"

permissions: {}

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_TERM_COLOR: always
  RUSTUP_MAX_RETRIES: 10

jobs:
  build-binary-linux-libc:
    name: "linux libc"
    timeout-minutes: 10
    runs-on: github-ubuntu-24.04-x86_64-8
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install mold"
        run: ./scripts/install-mold.sh

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        run: cargo build --profile no-debug

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-linux-libc-${{ github.sha }}
          path: |
            ./target/no-debug/uv
            ./target/no-debug/uvx
          retention-days: 1

  build-binary-linux-aarch64:
    name: "linux aarch64"
    timeout-minutes: 10
    runs-on: github-ubuntu-24.04-aarch64-4
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install mold"
        run: ./scripts/install-mold.sh

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        run: cargo build --profile no-debug

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-linux-aarch64-${{ github.sha }}
          path: |
            ./target/no-debug/uv
            ./target/no-debug/uvx
          retention-days: 1

  build-binary-linux-armv7-gnueabihf:
    name: "linux armv7 gnueabihf"
    timeout-minutes: 15
    runs-on: github-ubuntu-24.04-x86_64-8
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install mold"
        run: ./scripts/install-mold.sh

      - name: "Setup armv7"
        run: |
          sudo apt-get update
          sudo apt-get install gcc-arm-linux-gnueabihf
          rustup target add armv7-unknown-linux-gnueabihf

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        env:
          CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER: arm-linux-gnueabihf-gcc
        run: cargo build --profile no-debug --target armv7-unknown-linux-gnueabihf --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-linux-armv7-gnueabihf-${{ github.sha }}
          path: |
            ./target/armv7-unknown-linux-gnueabihf/no-debug/uv
            ./target/armv7-unknown-linux-gnueabihf/no-debug/uvx
          retention-days: 1

  build-binary-linux-musl:
    name: "linux musl"
    timeout-minutes: 10
    runs-on: github-ubuntu-24.04-x86_64-8
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install mold"
        run: ./scripts/install-mold.sh

      - name: "Setup musl"
        run: |
          sudo apt-get install musl-tools
          rustup target add x86_64-unknown-linux-musl

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        run: cargo build --profile no-debug --target x86_64-unknown-linux-musl --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-linux-musl-${{ github.sha }}
          path: |
            ./target/x86_64-unknown-linux-musl/no-debug/uv
            ./target/x86_64-unknown-linux-musl/no-debug/uvx
          retention-days: 1

  build-binary-macos-aarch64:
    name: "macos aarch64"
    timeout-minutes: 10
    runs-on: macos-14 # github-macos-14-aarch64-3
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}
          cache-bin: false
      - name: "Build"
        run: cargo build --profile no-debug --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-macos-aarch64-${{ github.sha }}
          path: |
            ./target/no-debug/uv
            ./target/no-debug/uvx
          retention-days: 1

  build-binary-macos-x86_64:
    name: "macos x86_64"
    timeout-minutes: 10
    runs-on: macos-latest-large # github-macos-14-x86_64-12
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}
          cache-bin: false
      - name: "Build"
        run: cargo build --profile no-debug --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-macos-x86_64-${{ github.sha }}
          path: |
            ./target/no-debug/uv
            ./target/no-debug/uvx
          retention-days: 1

  build-binary-windows-x86_64:
    name: "windows x86_64"
    timeout-minutes: 10
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Setup Dev Drive
        run: ${{ github.workspace }}/.github/workflows/setup-dev-drive.ps1

      # actions/checkout does not let us clone into anywhere outside ${{ github.workspace }}, so we have to copy the clone...
      - name: Copy Git Repo to Dev Drive
        run: |
          Copy-Item -Path "${{ github.workspace }}" -Destination "$Env:UV_WORKSPACE" -Recurse

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          workspaces: ${{ env.UV_WORKSPACE }}
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        working-directory: ${{ env.UV_WORKSPACE }}
        run: cargo build --profile no-debug --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-windows-x86_64-${{ github.sha }}
          path: |
            ${{ env.UV_WORKSPACE }}/target/no-debug/uv.exe
            ${{ env.UV_WORKSPACE }}/target/no-debug/uvx.exe
          retention-days: 1

  build-binary-windows-aarch64:
    name: "windows aarch64"
    timeout-minutes: 10
    runs-on: windows-11-arm
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Setup Dev Drive
        run: ${{ github.workspace }}/.github/workflows/setup-dev-drive.ps1

      # actions/checkout does not let us clone into anywhere outside ${{ github.workspace }}, so we have to copy the clone...
      - name: Copy Git Repo to Dev Drive
        run: |
          Copy-Item -Path "${{ github.workspace }}" -Destination "$Env:UV_WORKSPACE" -Recurse

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          workspaces: ${{ env.UV_WORKSPACE }}
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        working-directory: ${{ env.UV_WORKSPACE }}
        run: cargo build --profile no-debug --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-windows-aarch64-${{ github.sha }}
          path: |
            ${{ env.UV_WORKSPACE }}/target/no-debug/uv.exe
            ${{ env.UV_WORKSPACE }}/target/no-debug/uvx.exe
          retention-days: 1

  build-binary-msrv:
    name: "msrv"
    runs-on: github-ubuntu-24.04-x86_64-8
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - name: "Read MSRV from Cargo.toml"
        id: msrv
        run: |
          MSRV=$(grep -m1 'rust-version' Cargo.toml | sed 's/.*"\([^"]*\)".*/\1/')
          echo "value=$MSRV" >> "$GITHUB_OUTPUT"
      - name: "Install Rust toolchain"
        run: rustup default ${MSRV}
        env:
          MSRV: ${{ steps.msrv.outputs.value }}
      - name: "Install mold"
        run: ./scripts/install-mold.sh
      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}
      - run: cargo +${MSRV} build --profile no-debug
        env:
          MSRV: ${{ steps.msrv.outputs.value }}
      - run: ./target/no-debug/uv --version

  build-binary-android-aarch64:
    name: "android aarch64"
    timeout-minutes: 10
    runs-on: github-ubuntu-24.04-x86_64-8
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Setup Android NDK"
        run: |
          # Use ANDROID_NDK_ROOT if set, otherwise find the latest installed NDK
          if [ -z "${ANDROID_NDK_ROOT}" ]; then
            NDK_ROOT=$(ls -d "${ANDROID_HOME}/ndk/"* 2>/dev/null | sort -V | tail -n1)
          else
            NDK_ROOT="${ANDROID_NDK_ROOT}"
          fi
          TOOLCHAIN="${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64"

          echo "CC_aarch64_linux_android=${TOOLCHAIN}/bin/aarch64-linux-android24-clang" >> "$GITHUB_ENV"
          echo "CXX_aarch64_linux_android=${TOOLCHAIN}/bin/aarch64-linux-android24-clang++" >> "$GITHUB_ENV"
          echo "AR_aarch64_linux_android=${TOOLCHAIN}/bin/llvm-ar" >> "$GITHUB_ENV"
          echo "RANLIB_aarch64_linux_android=${TOOLCHAIN}/bin/llvm-ranlib" >> "$GITHUB_ENV"
          echo "CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=${TOOLCHAIN}/bin/aarch64-linux-android24-clang" >> "$GITHUB_ENV"
          echo "CARGO_TARGET_AARCH64_LINUX_ANDROID_RANLIB=${TOOLCHAIN}/bin/llvm-ranlib" >> "$GITHUB_ENV"

          # NDK 23+ removed libgcc, provide a stub that redirects to libunwind
          LIBDIR=$(echo "${TOOLCHAIN}"/lib/clang/*/lib/linux/aarch64)
          if [ ! -f "${LIBDIR}/libgcc.a" ]; then
            echo 'INPUT(-lunwind)' | sudo tee "${LIBDIR}/libgcc.a" > /dev/null
          fi

          rustup target add aarch64-linux-android

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Build"
        run: cargo build --profile no-debug --target aarch64-linux-android --bin uv --bin uvx

      - name: "Upload binary"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: uv-android-aarch64-${{ github.sha }}
          path: |
            ./target/aarch64-linux-android/no-debug/uv
            ./target/aarch64-linux-android/no-debug/uvx
          retention-days: 1

  build-binary-freebsd:
    name: "freebsd"
    timeout-minutes: 10
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false
      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}
      - name: "Cross build"
        run: |
          # Install cross from `freebsd-firecracker`
          wget -q -O cross https://github.com/acj/freebsd-firecracker/releases/download/v0.0.10/cross
          chmod +x cross
          mv cross /usr/local/bin/cross

          cross build --target x86_64-unknown-freebsd --profile no-debug

      - name: Test in Firecracker VM
        uses: acj/freebsd-firecracker-action@bab3e77871573c7943b80816f1641b6c1ce36896 # v0.9.1
        with:
          verbose: false
          checkout: false
          pre-run: |
            # The exclude `*` prevents examination of directories so we need to
            # include each parent directory of the binary
            include_path="$(mktemp)"
            cat <<EOF > $include_path
            target
            target/x86_64-unknown-freebsd
            target/x86_64-unknown-freebsd/no-debug
            target/x86_64-unknown-freebsd/no-debug/uv
            EOF

            rsync -r -e "ssh" \
              --relative \
              --copy-links \
              --include-from "$include_path" \
              --exclude "*" \
              . firecracker:
          run-in-vm: |
            mv target/x86_64-unknown-freebsd/no-debug/uv uv
            chmod +x uv
            ./uv --version
