name: Build raylib - Linux

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

permissions:
  contents: read

jobs:
  build:
    permissions:
      contents: write  # for actions/upload-release-asset to upload release asset
    strategy:
      fail-fast: false
      max-parallel: 1
      matrix:
        include:
        - bits: 32
          ARCH: "i386"
          ARCH_NAME: "i386"
          COMPILER_PATH: "/usr/bin"
          runner: "ubuntu-latest"
        - bits: 64
          ARCH: "x86_64"
          ARCH_NAME: "amd64"
          COMPILER_PATH: "/usr/bin"
          runner: "ubuntu-latest"
        - bits: 64
          ARCH: "aarch64"
          ARCH_NAME: "arm64"
          COMPILER_PATH: "/usr/bin"
          runner: "ubuntu-24.04-arm"

    runs-on: ${{ matrix.runner }}
    env:
      RELEASE_NAME: raylib-dev_linux_${{ matrix.ARCH_NAME }}

    steps:
    - name: Checkout code
      uses: actions/checkout@master

    - name: Setup Release Version
      run: |
        echo "RELEASE_NAME=raylib-${{ github.event.release.tag_name }}_linux_${{ matrix.ARCH_NAME }}" >> $GITHUB_ENV
      shell: bash
      if: github.event_name == 'release' && github.event.action == 'published'

    - name: Setup Environment
      run: |
        sudo apt-get update -qq
        sudo apt-get install -y --no-install-recommends libglfw3 libglfw3-dev libx11-dev libxcursor-dev libxrandr-dev libxinerama-dev libxi-dev libxext-dev libxfixes-dev libwayland-dev libxkbcommon-dev
        mkdir build
        cd build
        mkdir ${{ env.RELEASE_NAME }}
        cd ${{ env.RELEASE_NAME }}
        mkdir include
        mkdir lib
        cd ../../../raylib
        # ${{ matrix.ARCH }}-linux-gnu-gcc -v

    - name: Setup Environment (x86)
      run : |
          sudo apt-get install gcc-multilib
      if: matrix.bits == 32 && matrix.ARCH == 'i386'

    # TODO: Support 32bit (i386) static/shared library building
    - name: Build Library (32-bit)
      run: |
        cd src
        make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" CUSTOM_CFLAGS="-m32" -B
        # make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      if: matrix.bits == 32

    - name: Build Library (64-bit x86)
      run: |
        cd src
        make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      if: matrix.bits == 64 && matrix.ARCH == 'x86_64'

    - name: Build Library (64-bit ARM)
      run: |
        cd src
        make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=STATIC RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        make PLATFORM=PLATFORM_DESKTOP CC=gcc RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
      if: matrix.bits == 64 && matrix.ARCH == 'aarch64'

    - 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 }}
        path: |
          ./build/${{ env.RELEASE_NAME }}
          !./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'
