name: Build latest (macOS 64 bit)
# trigger via either push to selected branches or on manual run
on:
  push:
    branches:
      - main
      - master
  workflow_dispatch:


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

jobs:
  build:
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v4
      - name: Compile 64 bit macOS builds
        shell: bash
        id: compile
        env: 
          COMMON_FLAGS:      "-O1 -s -fno-stack-protector -fno-math-errno -Qn"
          MAC_ARM64_FLAGS:   "-fvisibility=hidden -rdynamic -DCC_BUILD_ICON -DCC_BUILD_GLMODERN -arch arm64"
          MAC_INTEL64_FLAGS: "-fvisibility=hidden -rdynamic -DCC_BUILD_ICON -arch x86_64"
        run: |
          LATEST_FLAG=-DCC_COMMIT_SHA=\"$(git rev-parse --short "$GITHUB_SHA")\"           
          cd src
          
          MACOSX_DEPLOYMENT_TARGET=10.5 clang *.c Window_cocoa.m ${{ env.COMMON_FLAGS }} ${{ env.MAC_INTEL64_FLAGS }} $LATEST_FLAG -o cc-mac64-gl1 -framework Cocoa -framework OpenGL -framework IOKit -lobjc
          MACOSX_DEPLOYMENT_TARGET=10.5 clang *.c Window_cocoa.m ${{ env.COMMON_FLAGS }} ${{ env.MAC_INTEL64_FLAGS }} $LATEST_FLAG -DCC_BUILD_GLMODERN -o cc-mac64-gl2 -framework Cocoa -framework OpenGL -framework IOKit -lobjc
          
          clang *.c Window_cocoa.m ${{ env.COMMON_FLAGS }} ${{ env.MAC_ARM64_FLAGS }} $LATEST_FLAG -o cc-mac-arm64 -framework Cocoa -framework OpenGL -framework IOKit -lobjc
          # https://wiki.freepascal.org/Code_Signing_for_macOS#Ad_hoc_signing

      - uses: ./.github/actions/notify_failure
        if: ${{ always() && steps.compile.outcome == 'failure' }}
        with:
          NOTIFY_MESSAGE: 'Failed to compile 64 bit macOS builds'
          WEBHOOK_URL: '${{ secrets.WEBHOOK_URL }}'
          
   
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-mac64-gl1'
          DEST_NAME: 'ClassiCube-mac64'
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-mac64-gl2'
          DEST_NAME: 'ClassiCube-mac64-ModernGL'
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-mac-arm64'
          DEST_NAME: 'ClassiCube-mac-ARM64'
