name: Build

on:
  push:
    branches:
      - main
      - '[0-9].[0-9]*'
      - '[0-9].x'
      # special branches used to test this workflow
      # before merging/releasing
      - build_deploy*
      - 'upgrade-latest-*'
      - 'mq-working-branch**'
      # TODO: Remove this after 3.x work is done
      - 3.x-staging
  pull_request:
  release:
    types:
      - published
  workflow_dispatch:
    # Allow manually triggering, but do NOT upload the result
  schedule:
    # Nightly builds after weekdays
    - cron:  0 2 * * 2-6

jobs:
  compute_version:
    name: Compute Library Version
    runs-on: ubuntu-latest
    outputs:
      library_version: ${{ steps.compute-version.outputs.library_version }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        # Include all history and tags
        with:
          persist-credentials: false
          fetch-depth: 0
      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        name: Install Python
        with:
          python-version: '3.12'
      - name: Compute Version
        id: compute-version
        run: |
          pip install "setuptools_scm[toml]>=4"

          # If we are on the main or release branch, strip away the dev version
          if [[ "$GITHUB_REF_NAME" == "main" || \
                "$GITHUB_REF_NAME" =~ ^[0-9]+\.[0-9]+$ || \
                "$GITHUB_REF_NAME" =~ ^[0-9]+\.x$ ]]; then
            LIBRARY_VERSION=$(setuptools-scm --strip-dev)
          else
            # use version string explicitly set in the project metadata, if exists
            LIBRARY_VERSION=$(grep '^version = ' pyproject.toml | tr -d '"' | cut -d' ' -f3)
            if [[ -z $LIBRARY_VERSION ]]; then
              # All else, maintain the dev version
              LIBRARY_VERSION=$(setuptools-scm)
            fi
          fi

          echo "${LIBRARY_VERSION}" | tee version.txt
          echo "library_version=${LIBRARY_VERSION}" >> $GITHUB_OUTPUT
      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: library-version
          path: version.txt

  build_wheels:
    needs: [ "compute_version" ]
    uses: ./.github/workflows/build_python_3.yml
    with:
      cibw_build: 'cp38* cp39* cp310* cp311* cp312* cp313* cp314*'
      cibw_skip: 'cp38-win_arm64 cp39-win_arm64 cp310-win_arm64 cp314t*'
      library_version: ${{ needs.compute_version.outputs.library_version }}

  build_sdist:
    needs: [ "compute_version" ]
    name: Build source distribution
    runs-on: ubuntu-latest
    env:
      SETUPTOOLS_SCM_PRETEND_VERSION_FOR_DDTRACE: ${{ needs.compute_version.outputs.library_version }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        # Include all history and tags
        with:
          persist-credentials: false
          fetch-depth: 0
      - uses: actions-rust-lang/setup-rust-toolchain@9d7e65c320fdb52dcd45ffaa68deb6c02c8754d9 # v1.12.0
      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        name: Install Python
        with:
          python-version: '3.12'
      - name: Build sdist
        run: |
          pip install "setuptools_scm[toml]>=4" "cython" "cmake>=3.24.2,<3.28" "setuptools-rust"
          python setup.py sdist
      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: source-dist
          path: dist/*.tar.gz

  test_alpine_sdist:
    # alpine is the only environment not built/tested by cibuildwheel
    name: Test source distribution on Alpine Linux
    needs: [build_sdist]
    runs-on: ubuntu-latest
    container:
      image: python:3.9-alpine
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          name: source-dist
          path: dist

      - name: Install build dependencies
        # Rust + Cargo are needed for Cryptography
        run: apk add git gcc g++ musl-dev libffi-dev openssl-dev bash rust cargo make cmake patchelf

      - name: Check source package
        # https://github.com/theacodes/cmarkgfm/issues/84
        run: |
          pip install twine readme_renderer[md] "cmarkgfm<2025.10.20"
          twine check dist/*.tar.gz
      - name: Install source package
        env:
          CMAKE_BUILD_PARALLEL_LEVEL: 12
        run: pip install dist/*.tar.gz

      - name: Test the source package
        run: python $GITHUB_WORKSPACE/tests/smoke_test.py
        # Move out of the workspace to avoid importing ddtrace from the source
        working-directory: /
