---
name: Build FreeBSD

on:
  push:
    branches:
      - master
      - "bugfix-release/*"
      - "release/*"
  pull_request:
  workflow_dispatch:
  merge_group:

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

env:
  C2_ENABLE_LTO: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/bugfix-release/') || startsWith(github.ref, 'refs/heads/release/') }}
  CHATTERINO_REQUIRE_CLEAN_GIT: On

jobs:
  build-vm:
    name: "Build FreeBSD"
    runs-on: ubuntu-latest

    env:
      C2_ENABLE_LTO: false

    steps:
      - uses: actions/checkout@v7
        with:
          submodules: recursive
          fetch-depth: 0 # allows for tags access

      - name: Get memory
        id: mem
        run: |
          MEM_MB=$(awk '/MemTotal/ {print int($2/1024)}' /proc/meminfo)
          echo "mem_mb=$MEM_MB" >> $GITHUB_OUTPUT

      - name: Build using FreeBSD
        uses: vmactions/freebsd-vm@v1.5.2
        with:
          envs: "C2_ENABLE_LTO"
          release: "14.4"
          arch: x86_64
          mem: ${{ steps.mem.outputs.mem_mb }}
          cache-after-prepare: true
          prepare: |
            pkg install -y boost-libs git libnotify qt6-base qt6-svg qt6-5compat qt6-imageformats qtkeychain-qt6 cmake pkgconf
          run: |
            git config --global --add safe.directory "$GITHUB_WORKSPACE"
            git submodule init
            git submodule update
            mkdir build
            cd build
            c++ --version
            cmake \
                -DCMAKE_C_COMPILER="cc" \
                -DCMAKE_CXX_COMPILER="c++" \
                -DCMAKE_C_FLAGS="-O2 -pipe  -fstack-protector-strong -fno-strict-aliasing "  \
                -DCMAKE_CXX_FLAGS="-O2 -pipe -fstack-protector-strong -fno-strict-aliasing  " \
                -DLINK_OPTIONS="-fstack-protector-strong" \
                -DCMAKE_INSTALL_PREFIX="/usr/local" \
                -DUSE_SYSTEM_QTKEYCHAIN="ON" \
                -DCMAKE_BUILD_TYPE="release" \
                -DCMAKE_EXPORT_COMPILE_COMMANDS="ON" \
                -DSKIP_JSON_GENERATION="ON" \
                ..
            cat compile_commands.json
            make -j $(getconf _NPROCESSORS_ONLN)
