name: Build Artifacts (FreeBSD Binary)
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
  pull_request:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  push:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  release:
    types: [published]
  schedule:
    - cron: '0 16 * * *'
concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.run_id }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}
env:
  CACHE_EPOCH: 148-2
  GOPROXY: direct
jobs:
  freebsd-binary-release:
    strategy:
      fail-fast: false
      matrix:
        include:
          - abi: 13
            arch: amd64
            build_type: Release
          - abi: 13
            arch: i386
            build_type: Release
          - abi: 13
            arch: aarch64
            build_type: Release
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    env:
      ARCH: ${{ matrix.arch }}
      BUILD_TYPE: ${{ matrix.build_type }}
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Cache toolchain
        id: toolchain-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/llvm-build/Release+Asserts
          key: ${{ runner.os }}-toolchain-${{ hashFiles('CLANG_REVISION') }}-v${{ env.CACHE_EPOCH }}
      - name: Cache sysroot
        id: sysroot-cache
        uses: actions/cache@v5
        with:
          path: |
            freebsd-${{ matrix.abi }}-${{ matrix.arch }}-toolchain
          key: freebsd-sysroot-${{ matrix.abi }}-${{ matrix.arch }}-${{ hashFiles('scripts/install-sysroot-freebsd.py') }}-v3-v5-v7
      - uses: actions/setup-go@v6
        with:
          go-version: '>=1.20.0'
          cache-dependency-path: |
            tools/go.sum
            third_party/boringssl/src/go.sum
      - name: Build build tool
        run: |
          cd tools
          go build
      - name: "Install dependency: prebuilt clang and clang-tidy binaries"
        if: ${{ steps.toolchain-cache.outputs.cache-hit != 'true' }}
        run: |
          ./scripts/download-clang-prebuilt-binaries.py
          rm -f third_party/llvm-build/Release+Asserts/*.tgz
      - name: "Install dependency: sysroot"
        if: ${{ steps.sysroot-cache.outputs.cache-hit != 'true' }}
        run: |
          ./scripts/install-sysroot-freebsd.py ${{ matrix.abi }} ${{ matrix.arch }}
          rm -f *.txz *.xz
      - name: Populate depedencies
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y libglib2.0-dev-bin gettext
          # required by unpacking pkg file
          sudo apt-get install -y zstd
      - name: Patch System Header
        run: |
          # fix warning about
          # /usr/include/machine/proc.h:40:8: error: definition of type 'ptrauth_key' conflicts with typedef of the
          sed -i s/__arm__/__aarch64__/g $PWD/freebsd-${{ matrix.abi }}-${{ matrix.arch }}-toolchain/usr/include/machine/proc.h
      - name: Build Binary (CLI)
        run: |
          ./tools/build --variant cli --system freebsd --arch ${{ matrix.arch }} --freebsd-abi ${{ matrix.abi }} \
            --sysroot $PWD/freebsd-${{ matrix.abi }}-${{ matrix.arch }}-toolchain \
            -nc -build-benchmark -build-test
      - name: Build Binary (GUI)
        run: |
          ./tools/build --variant gui --system freebsd --arch ${{ matrix.arch }} --freebsd-abi ${{ matrix.abi }} \
            --sysroot $PWD/freebsd-${{ matrix.abi }}-${{ matrix.arch }}-toolchain \
            -nc
      - name: Build Binary (DYLIB)
        run: |
          ./tools/build --variant dylib --system freebsd --arch ${{ matrix.arch }} --freebsd-abi ${{ matrix.abi }} \
            --sysroot $PWD/freebsd-${{ matrix.abi }}-${{ matrix.arch }}-toolchain \
            -nc
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-gui
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/yass
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.so*
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-cli
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/yass_cli
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.so*
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-dylib
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/devel
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/yass.py
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.so*
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-test
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/yass_test
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.so*
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-benchmark
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/yass_benchmark
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.so*
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
            !build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: freebsd-release-${{ matrix.build_type }}-${{ matrix.abi}}-${{ matrix.arch }}-${{ github.run_id }}-dbg
          path: |
            build-freebsd${{ matrix.abi }}-${{ matrix.arch }}/*.dbg
          compression-level: 9 # maximum compression
      - name: Upload dist tarball (including debuginfo)
        if: ${{ github.event_name == 'release' && matrix.build_type == 'Release' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.event.release.tag_name }} yass*.tgz
