name: Nightly

# Scheduled nightly artifact build. This used to run on every push to main,
# rebuilding six release targets per merge; PR CI already builds/tests the
# same commits on macOS/Windows and CNB covers Linux, so a daily cadence
# keeps the artifact stream fresh without burning ~28 runner-minutes per
# merge. Use workflow_dispatch for an on-demand build.
on:
  schedule:
    - cron: '17 9 * * *'
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: nightly-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  RUSTFLAGS: -Dwarnings
  CODEWHALE_BUILD_SHA: ${{ github.sha }}

jobs:
  build:
    name: Build ${{ matrix.platform }}
    timeout-minutes: 90
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            platform: linux-x64
            binary: codewhale
            primary_artifact: codewhale-linux-x64
            alias_artifact: codew-linux-x64
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-musl
            platform: linux-arm64
            binary: codewhale
            primary_artifact: codewhale-linux-arm64
            alias_artifact: codew-linux-arm64
          - os: macos-latest
            target: x86_64-apple-darwin
            platform: macos-x64
            binary: codewhale
            primary_artifact: codewhale-macos-x64
            alias_artifact: codew-macos-x64
          - os: macos-latest
            target: aarch64-apple-darwin
            platform: macos-arm64
            binary: codewhale
            primary_artifact: codewhale-macos-arm64
            alias_artifact: codew-macos-arm64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            platform: windows-x64
            binary: codewhale.exe
            primary_artifact: codewhale-windows-x64.exe
            alias_artifact: codew-windows-x64.exe
          - os: windows-11-arm
            target: aarch64-pc-windows-msvc
            platform: windows-arm64
            binary: codewhale.exe
            primary_artifact: codewhale-windows-arm64.exe
            alias_artifact: codew-windows-arm64.exe
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          targets: ${{ matrix.target }}
      - uses: mozilla-actions/sccache-action@v0.0.11
        id: sccache
        continue-on-error: true
      - name: Enable sccache
        if: steps.sccache.outcome == 'success'
        shell: bash
        run: |
          {
            echo "SCCACHE_GHA_ENABLED=true"
            echo "RUSTC_WRAPPER=sccache"
            echo "SCCACHE_IGNORE_SERVER_IO_ERROR=1"
          } >> "${GITHUB_ENV}"
      - uses: Swatinem/rust-cache@v2
        with:
          cache-bin: false
      - name: Install Linux GNU system dependencies
        if: matrix.target == 'x86_64-unknown-linux-gnu'
        run: |
          for i in 1 2 3 4 5; do
            sudo apt-get update && break
            echo "apt-get update failed (attempt $i); retrying in 15s"
            sleep 15
          done
          sudo apt-get install -y libdbus-1-dev pkg-config
      - name: Install Linux ARM64 musl toolchain
        if: matrix.target == 'aarch64-unknown-linux-musl'
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -y binutils musl-tools
          rustup target add --toolchain stable aarch64-unknown-linux-musl
      - name: Build
        shell: bash
        # Nightly artifacts are disposable smoke binaries (14-day retention),
        # so skip fat LTO; tagged releases keep the full optimized profile via
        # the Release workflow. (`codegen-units` is not overridden here --
        # [profile.release] already sets 16, so restating it changed nothing.)
        #
        # RUST_MIN_STACK sizes the stack of the LLVM worker threads rustc
        # spawns to run `optimize module <crate>-cgu.N`. With `lto=off` the
        # per-CGU optimization pipeline runs while the *library* is compiled
        # rather than being deferred to the ThinLTO stage, and one crates/tui
        # codegen unit needs more stack than a 1 MiB thread provides. Measured
        # on aarch64 by holding the crate and every flag fixed and varying only
        # this value: 1 MiB crashes rustc, 2 MiB and 4 MiB succeed. Unix std
        # defaults to 2 MiB and passes; windows-11-arm sat under the
        # requirement and failed every nightly from 2026-08-16 with
        # `thread 'optimize module codewhale_tui...-cgu.13' has overflowed its
        # stack`, deterministically on the same codegen unit across all three
        # build attempts. This is a property of the crate's size, not of
        # Windows, so it is set for every target: at 788k lines in crates/tui
        # the other platforms are one refactor away from crossing 2 MiB too.
        # The value is reserved address space, not committed memory.
        env:
          CARGO_PROFILE_RELEASE_LTO: 'off'
          RUST_MIN_STACK: '16777216'
        run: |
          for attempt in 1 2 3; do
            if cargo build --release --locked --target ${{ matrix.target }} -p codewhale-cli; then
              exit 0
            fi
            if [ "${attempt}" -lt 3 ]; then
              echo "Build attempt ${attempt} failed; retrying in 30s..."
              sleep 30
            fi
          done
          echo "Build failed after 3 attempts" >&2
          exit 1
      - name: Verify static Linux ARM64 binary and launch
        if: matrix.target == 'aarch64-unknown-linux-musl' && runner.arch == 'ARM64'
        shell: bash
        run: |
          set -euo pipefail
          bin_path="target/${{ matrix.target }}/release/${{ matrix.binary }}"
          if readelf -l "${bin_path}" | grep -Fq 'INTERP'; then
            echo "Expected a static musl binary, but ${bin_path} has an ELF interpreter" >&2
            exit 1
          fi
          "${bin_path}" --version
      - name: Smoke binary on matching native runners
        if: >-
          (startsWith(matrix.target, 'x86_64-') && runner.arch == 'X64') ||
          (startsWith(matrix.target, 'aarch64-') && runner.arch == 'ARM64')
        shell: bash
        run: |
          bin_dir="target/${{ matrix.target }}/release"
          "${bin_dir}/${{ matrix.binary }}" --version
      - name: Stage artifact
        id: stage
        shell: bash
        run: |
          short_sha="${GITHUB_SHA::12}"
          bin_path="target/${{ matrix.target }}/release/${{ matrix.binary }}"
          if [ ! -f "${bin_path}" ]; then
            echo "Binary not at ${bin_path}; searching target/ for ${{ matrix.binary }}:"
            find target -name "${{ matrix.binary }}" -type f
            exit 1
          fi

          stage_copy() {
            local artifact="$1"
            local dir="$2"
            mkdir -p "${dir}"
            cp "${bin_path}" "${dir}/${artifact}"
            cat > "${dir}/nightly-build-info.txt" <<INFO
          repository=${GITHUB_REPOSITORY}
          ref=${GITHUB_REF_NAME}
          commit=${GITHUB_SHA}
          artifact=${artifact}
          profile=release-nightly-no-lto
          INFO
          }

          stage_copy "${{ matrix.primary_artifact }}" nightly-primary
          stage_copy "${{ matrix.alias_artifact }}" nightly-alias
          cmp -s \
            "nightly-primary/${{ matrix.primary_artifact }}" \
            "nightly-alias/${{ matrix.alias_artifact }}"
          echo "primary_name=${{ matrix.primary_artifact }}-${short_sha}" >> "${GITHUB_OUTPUT}"
          echo "alias_name=${{ matrix.alias_artifact }}-${short_sha}" >> "${GITHUB_OUTPUT}"
      - uses: actions/upload-artifact@v7
        with:
          name: ${{ steps.stage.outputs.primary_name }}
          path: nightly-primary/*
          retention-days: 14
      - uses: actions/upload-artifact@v7
        with:
          name: ${{ steps.stage.outputs.alias_name }}
          path: nightly-alias/*
          retention-days: 14