# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This workflow is triggered by analyze.yml
name: Perfetto CI [bazel]
on:
  workflow_call:
env:
  # /tmp/cache contains {ccache, bazelcache} and generally any other cache
  # that should be persisted across jobs, but only updated from the main
  # branch. This is populated by the "Restore cache" step below.
  PERFETTO_CACHE_DIR: /tmp/cache
  PERFETTO_ARTIFACTS_ROOT: /tmp/artifacts
  PYTHONUNBUFFERED: 1
jobs:
  bazel:
    runs-on: self-hosted
    timeout-minutes: 45
    env:
      PERFETTO_CI_BUILD_CACHE_KEY: build-cache-bazel
      PERFETTO_CI_JOB_ID: gh-${{ github.run_id }}-bazel
    steps:
      - uses: actions/checkout@v4

      - name: Setup artifacts
        shell: bash
        run: |
          PERFETTO_ARTIFACTS_DIR=$PERFETTO_ARTIFACTS_ROOT/$PERFETTO_CI_JOB_ID
          echo "PERFETTO_ARTIFACTS_DIR=$PERFETTO_ARTIFACTS_DIR" >> $GITHUB_ENV
          mkdir -p $PERFETTO_ARTIFACTS_DIR

      - name: Restore cache
        uses: ./.github/actions/cache-on-google-cloud-storage/restore
        with:
          directory: ${{ env.PERFETTO_CACHE_DIR }}
          cache_key: ${{ env.PERFETTO_CI_BUILD_CACHE_KEY }}

      - uses: ./.github/actions/install-build-deps
        with:
          install-flags: --bazel

      - name: Build Bazel
        shell: bash
        run: |
          BAZEL_DISK_CACHE_FOLDER="$PERFETTO_CACHE_DIR/bazel-disk-cache"
          # Cleanup the cache if any of the two conditions are true.
          BAZEL_DISK_CACHE_GC_OPTIONS="--experimental_disk_cache_gc_max_age=7d --experimental_disk_cache_gc_max_size=10G"
          # We don't run a bazel daemon in background, so we do a GC during the build,
          # that's why we specify _idle_delay=0.
          BAZEL_DISK_CACHE_GC_OPTIONS+=" --experimental_disk_cache_gc_idle_delay=0"
          BAZEL_DISK_CACHE_FLAGS="--disk_cache=${BAZEL_DISK_CACHE_FOLDER} ${BAZEL_DISK_CACHE_GC_OPTIONS}"
          tools/bazel build //:all ${BAZEL_DISK_CACHE_FLAGS} --verbose_failures
          tools/bazel build //python:all ${BAZEL_DISK_CACHE_FLAGS} --verbose_failures

      - name: Smoke tests
        shell: bash
        run: |
          ./bazel-bin/traced &
          ./bazel-bin/traced_probes &
          sleep 5
          TRACE="$PERFETTO_ARTIFACTS_DIR/bazel.trace"
          ./bazel-bin/perfetto -c :test -o $TRACE
          kill $(jobs -p)
          ./bazel-bin/trace_processor_shell -q <(echo 'select count(1) from sched') $TRACE

      - name: Update cache (if on main)
        if: github.ref == 'refs/heads/main'
        uses: ./.github/actions/cache-on-google-cloud-storage/save
        with:
          directory: ${{ env.PERFETTO_CACHE_DIR }}
          cache_key: ${{ env.PERFETTO_CI_BUILD_CACHE_KEY }}
