name: macOS

on:
  workflow_dispatch:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/macos.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/macos.yml'
  release:
    types: [published]

permissions:
  contents: read

jobs:
  build:
    permissions:
      contents: write  # for actions/upload-release-asset to upload release asset
    runs-on: macos-latest
    
    env:
      RELEASE_NAME: raylib-dev_macos
    
    steps:
    - name: Checkout
      uses: actions/checkout@master
    
    - name: Setup Release Version
      run: |
        echo "RELEASE_NAME=raylib-${{ github.event.release.tag_name }}_macos" >> $GITHUB_ENV
      shell: bash
      if: github.event_name == 'release' && github.event.action == 'published'

    - name: Setup Environment
      run: | 
        mkdir build
        cd build
        mkdir ${{ env.RELEASE_NAME }}
        cd ${{ env.RELEASE_NAME }}
        mkdir include
        mkdir lib
        cd ../..
   
    # Generating static + shared library, note that i386 architecture is deprecated
    # Defining GL_SILENCE_DEPRECATION because OpenGL is deprecated on macOS
    - name: Build Library
      run: |
        cd src
        clang --version
        
        # Extract version numbers from Makefile
        brew install grep
        RAYLIB_API_VERSION=`ggrep -Po 'RAYLIB_API_VERSION\s*=\s\K(.*)' Makefile`
        RAYLIB_VERSION=`ggrep -Po 'RAYLIB_VERSION\s*=\s\K(.*)' Makefile`
        
        # Build raylib x86_64 static
        make PLATFORM=PLATFORM_DESKTOP RAYLIB_LIBTYPE=STATIC CUSTOM_CFLAGS="-target x86_64-apple-macos10.12 -DGL_SILENCE_DEPRECATION"
        mv libraylib.a /tmp/libraylib_x86_64.a
        make clean
        
        # Build raylib arm64 static
        make PLATFORM=PLATFORM_DESKTOP RAYLIB_LIBTYPE=STATIC CUSTOM_CFLAGS="-target arm64-apple-macos11 -DGL_SILENCE_DEPRECATION" -B
        mv libraylib.a /tmp/libraylib_arm64.a
        make clean
        
        # Join x86_64 and arm64 static
        lipo -create -output ../build/${{ env.RELEASE_NAME }}/lib/libraylib.a /tmp/libraylib_x86_64.a /tmp/libraylib_arm64.a
        
        # Build raylib x86_64 dynamic
        make PLATFORM=PLATFORM_DESKTOP RAYLIB_LIBTYPE=SHARED CUSTOM_CFLAGS="-target x86_64-apple-macos10.12 -DGL_SILENCE_DEPRECATION" CUSTOM_LDFLAGS="-target x86_64-apple-macos10.12" -B
        mv libraylib.${RAYLIB_VERSION}.dylib /tmp/libraylib_x86_64.${RAYLIB_VERSION}.dylib
        make clean
        
        # Build raylib arm64 dynamic
        make PLATFORM=PLATFORM_DESKTOP RAYLIB_LIBTYPE=SHARED CUSTOM_CFLAGS="-target arm64-apple-macos11 -DGL_SILENCE_DEPRECATION" CUSTOM_LDFLAGS="-target arm64-apple-macos11" -B
        mv libraylib.${RAYLIB_VERSION}.dylib /tmp/libraylib_arm64.${RAYLIB_VERSION}.dylib
        
        # Join x86_64 and arm64 dynamic
        lipo -create -output ../build/${{ env.RELEASE_NAME }}/lib/libraylib.${RAYLIB_VERSION}.dylib /tmp/libraylib_x86_64.${RAYLIB_VERSION}.dylib /tmp/libraylib_arm64.${RAYLIB_VERSION}.dylib
        ln -sv libraylib.${RAYLIB_VERSION}.dylib  ../build/${{ env.RELEASE_NAME }}/lib/libraylib.dylib
        ln -sv libraylib.${RAYLIB_VERSION}.dylib  ../build/${{ env.RELEASE_NAME }}/lib/libraylib.${RAYLIB_API_VERSION}.dylib
        cd ..
  
    - name: Generate Artifacts
      run: |
        cp -v ./src/raylib.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./src/raymath.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./src/rlgl.h ./build/${{ env.RELEASE_NAME }}/include
        cp -v ./CHANGELOG ./build/${{ env.RELEASE_NAME }}/CHANGELOG
        cp -v ./README.md ./build/${{ env.RELEASE_NAME }}/README.md
        cp -v ./LICENSE ./build/${{ env.RELEASE_NAME }}/LICENSE
        cd build
        tar -czvf ${{ env.RELEASE_NAME }}.tar.gz ${{ env.RELEASE_NAME }}
        
    - name: Upload Artifacts
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.RELEASE_NAME }}.tar.gz
        path: ./build/${{ env.RELEASE_NAME }}.tar.gz
        
    - name: Upload Artifact to Release
      uses: softprops/action-gh-release@v1
      with:
        files: ./build/${{ env.RELEASE_NAME }}.tar.gz
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: github.event_name == 'release' && github.event.action == 'published'
