name: TheRock CI Linux

on:
  workflow_call:
    inputs:
      cmake_options:
        type: string
      amdgpu_families:
        type: string
      test_runs_on:
        type: string

permissions:
  contents: read

jobs:
  therock-build-linux:
    name: Build Linux Packages
    runs-on: azure-linux-scale-rocm
    permissions:
      id-token: write
    container:
      image: ghcr.io/rocm/therock_build_manylinux_x86_64@sha256:044b113562629f4bd2ec5d2e64b32eee11562d48fb1a75d7493daec9dd8d8292
      options: -v /runner/config:/home/awsconfig/
    env:
      AMDGPU_FAMILIES: ${{ inputs.amdgpu_families }}
      TEATIME_FORCE_INTERACTIVE: 0
      AWS_SHARED_CREDENTIALS_FILE: /home/awsconfig/credentials.ini
      CACHE_DIR: ${{ github.workspace }}/.container-cache
      # The ccache.conf will be written by setup_ccache.py before this gets used.
      CCACHE_CONFIGPATH: ${{ github.workspace }}/.ccache/ccache.conf
    steps:
      - name: "Checking out repository for rocm-libraries"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          repository: "ROCm/rocm-libraries"

      - name: Checkout composable_kernel repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          path: "composable_kernel"

      - name: Checkout TheRock repository
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          repository: "ROCm/TheRock"
          ref: dc05d637054ad197c84b00e24b6262af0ec797c6 # 10-03-2025 commit
          path: "TheRock"

      - name: Setup ccache
        run: |
          ./TheRock/build_tools/setup_ccache.py \
            --config-preset "github-oss-presubmit" \
            --dir "$(dirname $CCACHE_CONFIGPATH)" \
            --local-path "$CACHE_DIR/ccache"
          echo "namespace = ext_composable_kernel" >> $CCACHE_CONFIGPATH
          echo "[*] ccache_config contents:"
          cat $CCACHE_CONFIGPATH

      - name: Runner Health Settings
        run: |
          ./TheRock/build_tools/health_status.py

      - name: Fetch sources
        run: |
          ./TheRock/build_tools/fetch_sources.py --jobs 12 --no-include-rocm-libraries --no-include-ml-frameworks

      - name: Patch rocm-libraries
        run: |
          git config --global --add safe.directory '*'
          git -c user.name="therockbot" -c "user.email=therockbot@amd.com" am --whitespace=nowarn ./TheRock/patches/amd-mainline/rocm-libraries/*.patch

      - name: Install python deps
        run: |
          pip install -r TheRock/requirements.txt
          pip freeze

      - name: Configure Projects
        env:
          amdgpu_families: ${{ env.AMDGPU_FAMILIES }}
          package_version: ADHOCBUILD
          extra_cmake_options: ${{ inputs.cmake_options }}
          BUILD_DIR: build
        run: |
          python3 TheRock/build_tools/github_actions/build_configure.py

      - name: Build TheRock
        run: cmake --build TheRock/build

      - name: Build therock-archives
        run: cmake --build TheRock/build --target therock-archives

      - name: Report
        if: ${{ !cancelled() }}
        run: |
          echo "Full SDK du:"
          echo "------------"
          du -h -d 1 TheRock/build/dist/rocm
          echo "Artifact Archives:"
          echo "------------------"
          ls -lh TheRock/build/artifacts/*.tar.xz
          echo "Artifacts:"
          echo "----------"
          du -h -d 1 TheRock/build/artifacts
          echo "CCache Stats:"
          echo "-------------"
          ccache -s -v
          tail -v -n +1 .ccache/compiler_check_cache/* > TheRock/build/logs/ccache_compiler_check_cache.log

      - name: Configure AWS Credentials for non-forked repos
        if: ${{ always() && !github.event.pull_request.head.repo.fork }}
        uses: aws-actions/configure-aws-credentials@7474bc4690e29a8392af63c5b98e7449536d5c3a # v4.3.1
        with:
          aws-region: us-east-2
          role-to-assume: arn:aws:iam::692859939525:role/therock-artifacts-external

      - name: Post Build Upload
        if: always()
        run: |
          python3 TheRock/build_tools/github_actions/post_build_upload.py \
            --run-id ${{ github.run_id }} \
            --amdgpu-family ${{ env.AMDGPU_FAMILIES }} \
            --build-dir TheRock/build \
            --upload

  therock-test-linux:
    name: "Test"
    needs: [therock-build-linux]
    uses: ./.github/workflows/therock-test-packages.yml
    with:
      project_to_test: "miopen"
      amdgpu_families: ${{ inputs.amdgpu_families }}
      test_runs_on: ${{ inputs.test_runs_on }}
      platform: "linux"
