jobs: # skip-master skip-pr skip-stable

  # This is ci/actions-templates/windows-builds-template.yaml
  # Do not edit this file in .github/workflows
  build-windows-pr: # job-name skip-master skip-stable
  build-windows-master: # job-name skip-pr skip-stable
  build-windows-stable: # job-name skip-master skip-pr
    runs-on: windows-latest
    if: ${{ contains('["pull_request", "merge_group"]', github.event_name) }} # skip-master skip-stable
    if: ${{ (github.event_name == 'push' && github.ref_name == 'master') }} # skip-pr skip-stable
    if: ${{ github.event_name == 'push' && github.ref_name == 'stable' || github.event_name == 'schedule' }} # skip-pr skip-master
    env:
      RUSTFLAGS: -Ctarget-feature=+crt-static
      RUST_MIN_STACK: 16777216
    permissions:
      id-token: write
      contents: read
    strategy:
      fail-fast: false
      matrix:
        mode:
          - dev
          - release
        target:
          - x86_64-pc-windows-msvc
          - i686-pc-windows-msvc # skip-pr skip-master
          - aarch64-pc-windows-msvc
          - x86_64-pc-windows-gnu
          - i686-pc-windows-gnu # skip-pr skip-master
        include:
          - target: x86_64-pc-windows-msvc
            run_tests: YES
          - target: x86_64-pc-windows-gnu
            arch: x86_64
            mingwdir: mingw64
            gcc: x86_64-w64-mingw32-gcc
          - target: i686-pc-windows-gnu # skip-pr skip-master
            arch: i686 # skip-pr skip-master
            mingwdir: mingw32 # skip-pr skip-master
            gcc: i686-w64-mingw32-gcc # skip-pr skip-master
    steps:
      - uses: actions/checkout@v4
        # v2 defaults to a shallow checkout, but we need at least to the previous tag
        with:
          fetch-depth: 0
      - name: Acquire tags for the repo
        run: |
          git fetch --no-tags --prune --depth=1 origin +refs/tags/*:refs/tags/*
      - name: Display the current git status
        run: |
          git status
          git describe
      - name: Prep cargo dirs
        run: |
          New-Item "${env:USERPROFILE}\.cargo\registry" -ItemType Directory -Force
          New-Item "${env:USERPROFILE}\.cargo\git" -ItemType Directory -Force
        shell: powershell
      - name: Install NASM
        # Building `aws-lc-rs` for Windows MSVC depends on `NASM`.
        # See: https://aws.github.io/aws-lc-rs/requirements/windows.html
        uses: ilammy/setup-nasm@v1
        if: matrix.mingwdir == ''
      - name: Install mingw
        uses: bwoodsend/setup-winlibs-action@v1
        if: matrix.mingwdir != ''
        with:
          architecture: ${{ matrix.arch }}
      - name: Verify mingw gcc installation
        shell: powershell
        if: matrix.mingwdir != ''
        run: |
          Get-Command ${{ matrix.gcc }}
          ${{ matrix.gcc }} --version
      - name: Set PATH
        run: |
          echo "%USERPROFILE%\.cargo\bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
          echo "TARGET=${{ matrix.target }}" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8
      - name: Skip tests
        if: matrix.run_tests == '' || matrix.mode == 'release'
        run: |
          echo "SKIP_TESTS=yes" >> $GITHUB_ENV
        shell: bash
      - name: Cache cargo registry and git trees
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Get rustc commit hash
        id: cargo-target-cache
        run: |
          echo "{rust_hash}={$(rustc -Vv | grep commit-hash | awk '{print $2}')}" >> $GITHUB_OUTPUT
        shell: bash
      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ github.base_ref }}-${{ github.head_ref }}-${{ matrix.target }}-${{ matrix.mode }}-cargo-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ github.base_ref }}-${{ matrix.target }}-${{ matrix.mode }}-cargo-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rustup using win.rustup.rs
        run: |
          # Disable the download progress bar which can cause perf issues
          $ProgressPreference = "SilentlyContinue"
          Invoke-WebRequest https://win.rustup.rs/ -OutFile rustup-init.exe
          .\rustup-init.exe -y --default-host=x86_64-pc-windows-msvc --profile=minimal
          del rustup-init.exe
        shell: powershell
      - name: Ensure stable toolchain is up to date
        run: rustup update stable
        shell: bash
      - name: Install the target
        run: |
          rustup target install ${{ matrix.target }}
      - name: Run a full build
        env:
          TARGET: ${{ matrix.target }}
          BUILD_PROFILE: ${{ matrix.mode }}
        run: bash ci/run.bash
      - name: Run cargo check
        if: matrix.mode != 'release'
        env:
          TARGET: ${{ matrix.target }}
        # os-specific code leads to lints escaping if we only run this in one target
        run: |
          cargo check --all --all-targets --features test
          git ls-files -- '*.rs' | xargs touch
      - name: Run cargo clippy
        if: matrix.mode != 'release' && matrix.mingwdir == ''
        env:
          TARGET: ${{ matrix.target }}
        run: |
          cargo clippy --all-targets --all-features -- -D warnings
      - name: Upload the built artifact
        if: matrix.mode == 'release'
        uses: actions/upload-artifact@v4
        with:
          name: rustup-init-${{ matrix.target }}
          path: |
            target/${{ matrix.target }}/release/rustup-init.exe
          retention-days: 7
      - name: Ensure that awscli is installed
        if: github.event_name == 'push' && matrix.mode == 'release' && (github.ref == 'refs/heads/stable' || github.ref == 'refs/heads/master')
        run: |
          Get-Command aws
          aws --version
      - name: Prepare the dist
        if: github.event_name == 'push' && matrix.mode == 'release' && (github.ref == 'refs/heads/stable' || github.ref == 'refs/heads/master')
        run: |
          .\ci\prepare-deploy.ps1
        shell: powershell
      - name: Deploy build to dev-static dist tree for release team
        if: github.event_name == 'push' && github.ref == 'refs/heads/stable' && matrix.mode == 'release'
        run: |
          aws --debug s3 cp --recursive dist s3://dev-static-rust-lang-org/rustup/dist
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-west-1
      - name: Configure AWS credentials
        if: github.event_name == 'push' && github.ref == 'refs/heads/master' && matrix.mode == 'release'
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::890664054962:role/ci--rust-lang--rustup
          aws-region: us-east-1
      - name: Deploy build to rustup-builds bucket for release team
        if: github.event_name == 'push' && github.ref == 'refs/heads/master' && matrix.mode == 'release'
        run: |
          aws --debug s3 cp --recursive dist s3://rustup-builds/${{ github.sha }}/dist
        env:
          AWS_DEFAULT_REGION: us-east-1
      - name: Clear the cargo caches
        run: |
          cargo install cargo-cache --no-default-features --features ci-autoclean
          cargo-cache
