name: build-ci

on:
  push:
    branches: [ "*" ]
    tags: [ "*" ]
    paths-ignore:
      - "**.7z"
  pull_request:
    branches: [ main ]

env:
  SDL_VER: "2.26.1"
  FFMPEG_VER: "6.0"
  QT_VER: "5.15.8"
  MVK_VER: "1.2.1"

jobs:
  SDL:
    env:      
      MACOSX_DEPLOYMENT_TARGET: "10.13"
      CCACHE_CPP2: "yes"
      CCACHE_SLOPPINESS: "time_macros"
      CC: "ccache clang"
      CXX: "ccache clang++"
      OBJC: "clang"
    runs-on: macos-latest
    steps:
      - name: Checkout
        run: |
          git clone -b release-${SDL_VER} https://github.com/libsdl-org/SDL.git --depth 1
      - name: Set up cache
        uses: actions/cache@v2
        with:
          path: |
            ~/Library/Caches/ccache
          key: ${{ runner.os }}-macos-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-macos-
      - name: Install dependencies
        run: |
          brew install p7zip ccache nasm
      - name: Build SDL
        run: |
          export PATH="/usr/local/opt/ccache/libexec:$PATH"

          mkdir -p SDL/build/sdl-${SDL_VER}
          cd SDL/build

          cmake ../ -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" -DBUILD_SHARED_LIBS=1
          make -j4
          make install DESTDIR=$(pwd)/sdl-${SDL_VER}
          
          7z a "sdl-${SDL_VER}.7z" sdl-${SDL_VER}/
      - name: Pack
        run: |
          # move the compiled archive into the artifacts directory to be uploaded
          mkdir -p artifacts/sdl2
          mv "SDL/build/sdl-${SDL_VER}.7z" artifacts/sdl2
      - name: Upload
        uses: actions/upload-artifact@v2
        with:
          name: macos
          path: |
            artifacts/

  FFmpeg:
    env:      
      MACOSX_DEPLOYMENT_TARGET: "10.13"
      CCACHE_CPP2: "yes"
      CCACHE_SLOPPINESS: "time_macros"
      CC: "ccache clang"
      CXX: "ccache clang++"
      OBJC: "clang"
    runs-on: macos-latest
    steps:
      - name: Checkout
        run: |
          git clone -b n${FFMPEG_VER} https://git.ffmpeg.org/ffmpeg.git --depth 1
      - name: Set up cache
        uses: actions/cache@v2
        with:
          path: |
            ~/Library/Caches/ccache
          key: ${{ runner.os }}-macos-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-macos-
      - name: Install dependencies
        run: |
          brew install p7zip ccache nasm
      - name: Build FFMPEG
        run: |
          export PATH="/usr/local/opt/ccache/libexec:$PATH"

          mkdir -p ffmpeg/build
          cd ffmpeg/build

          x86path=$(pwd)/x86_64
          armpath=$(pwd)/arm64

          # Build for x86_64
          mkdir -p $x86path/install
          cd $x86path
          # The included lzma is built for 10.15 can generates linker warnings
          ../../configure --enable-cross-compile --arch=x86_64 \
              --cc="${CC} -arch x86_64" --cxx="${CXX} -arch x86_64" --enable-shared --disable-static --disable-doc \
              --disable-lzma
          make -j4
          make install DESTDIR=$x86path/install

          # Build for arm64
          mkdir -p $armpath/install
          cd $armpath
          # The included lzma is built for 10.15 can generates linker warnings
          ../../configure --enable-cross-compile --arch=arm64 \
              --cc="${CC} -arch arm64" --cxx="${CXX} -arch arm64" --enable-shared --disable-static --disable-doc \
              --disable-lzma
          make -j4
          make install DESTDIR=$armpath/install
      - name: Create universal build
        run: |
          cd ffmpeg/build

          x86path=$(pwd)/x86_64
          armpath=$(pwd)/arm64
          universalpath=$(pwd)/ffmpeg-${FFMPEG_VER}

          # Fix dylibs to be path-independent.
          for dylib in $(find $(pwd) -name '*.dylib'); do
            change_id=1
            for path in $(otool -L $dylib | grep \t | cut -d' ' -f1 | xargs); do
              if [[ $path = /usr/local/lib* ]]; then
                fixed_path="@rpath/"`basename $path`
                if [ $change_id -eq 1 ]; then
                  change_id=0
                  install_name_tool -id $fixed_path $dylib
                else
                  install_name_tool -change $path $fixed_path $dylib
                fi
              fi
            done
          done

          mkdir -p $universalpath
          for f in $(find $x86path/install); do
            subpath=${f#$x86path/install}
            dest=$universalpath$subpath
            if [ -d "$f" ]; then
              mkdir -p "$dest"
            elif [ -L "$f" ]; then
              ln -s $(readlink "$f") "$dest"
            elif [[ ":$f:" == *"/bin/"* ]] || [[ $f == *.a ]] || [[ $f == *.dylib ]]; then
              lipo -create -output "$dest" "$x86path/install$subpath" "$armpath/install$subpath"
            else
              cp "$f" "$dest"
            fi
          done

          7z a "ffmpeg-${FFMPEG_VER}.7z" ffmpeg-${FFMPEG_VER}/
      - name: Pack
        run: |
          # move the compiled archive into the artifacts directory to be uploaded
          mkdir -p artifacts/ffmpeg
          mv "ffmpeg/build/ffmpeg-${FFMPEG_VER}.7z" artifacts/ffmpeg
      - name: Upload
        uses: actions/upload-artifact@v2
        with:
          name: macos
          path: |
            artifacts/

  Qt:
    env:
      MACOSX_DEPLOYMENT_TARGET: "10.13"
      CCACHE_CPP2: "yes"
      CCACHE_SLOPPINESS: "time_macros"
      CC: "ccache clang"
      CXX: "ccache clang++"
      OBJC: "clang"
    runs-on: macos-latest
    steps:
      - name: Checkout
        run: |
          wget https://download.qt.io/official_releases/qt/${QT_VER%.*}/${QT_VER}/single/qt-everywhere-opensource-src-${QT_VER}.tar.xz
          tar xf qt-everywhere-opensource-src-${QT_VER}.tar.xz
          mv qt-everywhere-src-${QT_VER} qt
      - name: Set up cache
        uses: actions/cache@v2
        with:
          path: |
            ~/Library/Caches/ccache
          key: ${{ runner.os }}-macos-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-macos-
      - name: Install dependencies
        run: |
          brew install p7zip ccache
      - name: Build Qt
        run: |
          export PATH="/usr/local/opt/ccache/libexec:$PATH"

          mkdir -p qt/build/qt-${QT_VER}
          cd qt/build

          ../configure -prefix $(pwd)/qt-${QT_VER} -device-option QMAKE_APPLE_DEVICE_ARCHS="x86_64 arm64" \
              -debug-and-release -opensource -opengl desktop -mp -ltcg \
              -nomake examples -nomake tests \
              -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcharts -skip qtconnectivity -skip qtdatavis3d \
              -skip qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtlocation -skip qtlottie -skip qtmacextras \
              -skip qtnetworkauth -skip qtpurchasing -skip qtquick3d -skip qtquickcontrols -skip qtquicktimeline \
              -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport \
              -skip qtspeech -skip qtsvg -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview \
              -skip qtx11extras -skip qtxmlpatterns -skip qtvirtualkeyboard -skip qtquickcontrols2 -skip qtdeclarative \
              -skip qtnetworkauth -skip qtwinextras -skip qtimageformats
          make -j4
          make install

          7z a "qt-${QT_VER}.7z" qt-${QT_VER}/
      - name: Pack
        run: |
          # move the compiled archive into the artifacts directory to be uploaded
          mkdir -p artifacts/qt
          mv "qt/build/qt-${QT_VER}.7z" artifacts/qt
      - name: Upload
        uses: actions/upload-artifact@v2
        with:
          name: macos
          path: |
            artifacts/

  MoltenVK:
    env:      
      MACOSX_DEPLOYMENT_TARGET: "10.13"
      CCACHE_CPP2: "yes"
      CCACHE_SLOPPINESS: "time_macros"
      CC: "clang"
      CXX: "clang++"
      OBJC: "clang"
    runs-on: macos-latest
    steps:
      - name: Checkout
        run: |
          git clone -b v${MVK_VER} https://github.com/KhronosGroup/MoltenVK.git --depth 1
      - name: Set up cache
        uses: actions/cache@v2
        with:
          path: |
            ~/Library/Caches/ccache
            External/build
            !External/build/Intermediates
          key: ${{ runner.os }}-macos-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-macos-
      - name: Install dependencies
        run: |
          brew install p7zip ccache
          cd MoltenVK
          ./fetchDependencies -v --macos
      - name: Build MoltenVK
        run: |
          export PATH="/usr/local/opt/ccache/libexec:$PATH"
          cd MoltenVK

          make macos -j4
          
          # Workaround unsupported `make install` to custom prefix
          mkdir -p moltenvk-${MVK_VER}/Library/Frameworks/
          cp -a Package/Latest/MoltenVK/MoltenVK.xcframework moltenvk-${MVK_VER}/Library/Frameworks/
          
          7z a "moltenvk-${MVK_VER}.7z" moltenvk-${MVK_VER}/
      - name: Pack
        run: |
          # move the compiled archive into the artifacts directory to be uploaded
          mkdir -p artifacts/moltenvk
          mv "MoltenVK/moltenvk-${MVK_VER}.7z" artifacts/moltenvk
      - name: Upload
        uses: actions/upload-artifact@v2
        with:
          name: macos
          path: |
            artifacts/
  PR:
    if: ${{ github.ref == 'refs/heads/main' }}
    runs-on: ubuntu-latest
    needs: [SDL, FFmpeg, Qt, MoltenVK]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/download-artifact@v3
        with:
          path: ~/
      - name: Copy Artifacts
        run: cp -r ~/macos/* ./
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          delete-branch: true
          title: '[Auto] The bump by Github Actions'
          commit-message: |
            [Auto] Bumps
            - SDL2 ${{ env.SDL_VER }}
            - FFmpeg ${{ env.FFMPEG_VER }}
            - Qt ${{ env.QT_VER }}
            - MoltenVK ${{ env.MVK_VER }}
          body: |
            Bumps
            - SDL2 ${{ env.SDL_VER }}
            - FFmpeg ${{ env.FFMPEG_VER }}
            - Qt ${{ env.QT_VER }}
            - MoltenVK ${{ env.MVK_VER }}
