jobs: # skip-x86_64 skip-aarch64

  # This is ci/actions-templates/macos-builds-template.yaml
  # Do not edit this file in .github/workflows
  build-macos-aarch64: # job-name skip-x86_64
    runs-on: macos-latest # skip-x86_64
    env: # skip-x86_64
      MACOSX_DEPLOYMENT_TARGET: 11.0 # skip-x86_64
  build-macos-x86_64: # job-name skip-aarch64
    runs-on: macos-13 # skip-aarch64
    env: # skip-aarch64
      MACOSX_DEPLOYMENT_TARGET: 10.12 # skip-aarch64
    permissions:
      id-token: write
      contents: read
    strategy:
      matrix:
        mode:
          - dev
          - release
        target:
          - x86_64-apple-darwin # skip-aarch64
          - aarch64-apple-darwin # skip-x86_64
    steps:
      - uses: actions/checkout@v4
        with:
          # v2 defaults to a shallow checkout, but we need at least to the previous tag
          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: |
          mkdir -p ~/.cargo/{registry,git}
      - name: Set environment variables appropriately for the build
        run: |
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
          echo "TARGET=${{ matrix.target }}" >> $GITHUB_ENV
          echo "SKIP_TESTS=" >> $GITHUB_ENV
          echo "LZMA_API_STATIC=1" >> $GITHUB_ENV
      - name: Skip tests
        if: matrix.mode == 'release'
        run: |
          echo "SKIP_TESTS=yes" >> $GITHUB_ENV
      - 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 ./rustup-init.sh
        run: |
          sh ./rustup-init.sh --default-toolchain=none --profile=minimal -y
      - name: Ensure Stable is up to date
        run: |
          if rustc +stable -vV >/dev/null 2>/dev/null; then
            rustup toolchain uninstall stable
          fi
          rustup toolchain install --profile=minimal stable
      - name: Ensure we have our goal target installed
        run: |
          rustup target install "$TARGET"
      - name: Run a full build and test
        env:
          BUILD_PROFILE: ${{ matrix.mode }}
        run: bash ci/run.bash
      - name: Dump dynamic link targets
        if: matrix.mode == 'release'
        run: |
          otool -L target/${TARGET}/release/rustup-init
          if otool -L target/${TARGET}/release/rustup-init | grep -q -F /usr/local/; then
            echo >&2 "Unfortunately there are /usr/local things in the link. Fail."
            exit 1
          fi
      - 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
          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: |
          brew install --overwrite awscli
          which 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: |
          bash ci/prepare-deploy.bash
      - 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 s3 cp --recursive deploy/ s3://dev-static-rust-lang-org/rustup/
        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 deploy/ s3://rustup-builds/${{ github.sha }}
        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
      - name: Flush cache
        # This is a workaround for a bug with GitHub Actions Cache that causes
        # corrupt cache entries (particularly in the target directory). See
        # https://github.com/actions/cache/issues/403 and
        # https://github.com/rust-lang/cargo/issues/8603.
        run: sudo /usr/sbin/purge
