name: build

on:
  push:
    paths-ignore:
      - 'docs/**'
      - 'web/**'
      - '**.md'
  pull_request:
    paths-ignore:
      - 'docs/**'
      - 'web/**'
      - '**.md'
jobs:
  build_win32_amalgamated:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
    - uses: ilammy/msvc-dev-cmd@v1
    - name: Compile
      shell: powershell
      run: |
        python amalgamate.py
        cd amalgamated
        cl.exe /std:c11 /experimental:c11atomics /utf-8 /Ox /I. pocketpy.c main.c /link Ws2_32.lib /out:pkpy.exe
  build_win32:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
    - uses: ilammy/msvc-dev-cmd@v1
    - name: Compile
      shell: bash
      run: |
        mkdir -p output/x86_64
        python cmake_build.py Release -DPK_BUILD_MODULE_LZ4=ON -DPK_BUILD_MODULE_CUTE_PNG=ON -DPK_BUILD_MODULE_MSGPACK=ON
        cp main.exe output/x86_64
        cp pocketpy.dll output/x86_64
    - uses: actions/upload-artifact@v4
      with:
        name: windows
        path: output
    - name: Unit Test
      run: python scripts/run_tests.py
    - name: Benchmark
      run: python scripts/run_tests.py benchmark
  build_linux:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
    - name: Setup Clang
      uses: egor-tensin/setup-clang@v1
      with:
        version: 17
        platform: x64
    - name: Run Sanitizers
      run: |
        sudo apt-get install -y libclang-rt-17-dev
        bash build_g.sh
        bash run_tests.sh
        rm -rf ./main
    - name: Run Script Check
      run: |
        python scripts/check_pragma_once.py include
        python scripts/check_undef.py src
    - name: Unit Test with Coverage
      run: bash run_tests.sh
    - name: Upload coverage reports to Codecov
      uses: codecov/codecov-action@v4
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        directory: .coverage
      if: github.ref == 'refs/heads/main'
    - name: Compile and Test
      run: |
        python scripts/check_pragma_once.py include
        mkdir -p output/x86_64
        python cmake_build.py Release -DPK_BUILD_MODULE_LZ4=ON -DPK_BUILD_MODULE_CUTE_PNG=ON -DPK_BUILD_MODULE_MSGPACK=ON
        python scripts/run_tests.py
        cp main output/x86_64
        cp libpocketpy.so output/x86_64
      env:
        CC: clang
    - uses: actions/upload-artifact@v4
      with:
        name: linux
        path: output
    - name: Benchmark
      run: python scripts/run_tests.py benchmark
  build_darwin:
      runs-on: macos-latest
      steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Compile and Test
        run: |
          python cmake_build.py Release -DPK_BUILD_MODULE_LZ4=ON -DPK_BUILD_MODULE_CUTE_PNG=ON -DPK_BUILD_MODULE_MSGPACK=ON
          python scripts/run_tests.py
      - name: Benchmark
        run: python scripts/run_tests.py benchmark
      - name: Test Amalgamated Build
        run: python amalgamate.py
  build_android_libs:
      runs-on: ubuntu-latest
      steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: nttld/setup-ndk@v1
        id: setup-ndk
        with:
          ndk-version: r23
          local-cache: false
          add-to-path: false
      - name: Compile Shared Library
        run: |
          bash build_android_libs.sh arm64-v8a
          bash build_android_libs.sh armeabi-v7a
          bash build_android_libs.sh x86_64

          mkdir -p output/arm64-v8a
          mkdir -p output/armeabi-v7a
          mkdir -p output/x86_64

          cp build/android/arm64-v8a/libpocketpy.so output/arm64-v8a
          cp build/android/armeabi-v7a/libpocketpy.so output/armeabi-v7a
          cp build/android/x86_64/libpocketpy.so output/x86_64
        env:
          ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
      - uses: actions/upload-artifact@v4
        with:
          name: android
          path: output
  build_darwin_libs:
      runs-on: macos-latest
      steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Compile Frameworks
        run: |
          bash build_darwin_libs.sh
          mkdir -p output
          cp build/libpocketpy.a output/libpocketpy.a
      - uses: actions/upload-artifact@v4
        with:
          name: darwin
          path: output
  build_ios_libs:
      runs-on: macos-latest
      steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Compile Frameworks
        run: |
          bash build_ios_libs.sh
          mkdir -p output
          cp -r build/pocketpy.xcframework output/pocketpy.xcframework
      - uses: actions/upload-artifact@v4
        with:
          name: ios
          path: output
  build_linux_multiarch:
      runs-on: ubuntu-latest
      strategy:
        matrix:
          arch: [x86, aarch64, armv7]
      steps:
        - uses: actions/checkout@v4
          with:
            submodules: recursive
        - name: Setup Alpine Linux for ${{ matrix.arch }}
          uses: jirutka/setup-alpine@v1
          with:
            arch: ${{ matrix.arch }}
            packages: gcc g++ make cmake libc-dev linux-headers python3 git
        - name: Build and Test
          run: |
            echo "Building for architecture: ${{ matrix.arch }}"
            uname -m
            python -c "import struct; print(8 * struct.calcsize('P'))"
  
            python cmake_build.py
            python scripts/run_tests.py
          shell: alpine.sh --root {0}

  merge:
      runs-on: ubuntu-latest
      needs: [ build_win32, build_linux, build_darwin, build_android_libs, build_darwin_libs, build_ios_libs ]
      steps:
      - name: "Create output directory"
        run: "mkdir $GITHUB_WORKSPACE/output"
        
      - name: "Merge win32"
        uses: actions/download-artifact@v4.1.7
        with:
          name: windows
          path: $GITHUB_WORKSPACE/output/windows

      - name: "Merge linux"
        uses: actions/download-artifact@v4.1.7
        with:
          name: linux
          path: $GITHUB_WORKSPACE/output/linux

      - name: "Merge android"
        uses: actions/download-artifact@v4.1.7
        with:
          name: android
          path: $GITHUB_WORKSPACE/output/android
          
      - name: "Merge darwin"
        uses: actions/download-artifact@v4.1.7
        with:
          name: darwin
          path: $GITHUB_WORKSPACE/output/darwin

      - name: "Merge ios"
        uses: actions/download-artifact@v4.1.7
        with:
          name: ios
          path: $GITHUB_WORKSPACE/output/ios

      - name: "Upload merged artifact"
        uses: actions/upload-artifact@v4.3.3
        with:
          name: all-in-one
          path: $GITHUB_WORKSPACE/output
