name: 🔧 Build -> Package 📦
on: [push, pull_request]

jobs:
  static-checks:
    name: 📊 Static Checks (clang-format, black format, file format)
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install dependencies
        run: |
          sudo apt-get install -qq dos2unix recode clang-format-11
          sudo update-alternatives --remove-all clang-format
          sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-11 100
          sudo pip3 install black==20.8b1 pygments

      - name: File formatting checks (file_format.sh)
        run: |
          bash ./misc/scripts/file_format.sh

      - name: Style checks via clang-format (clang_format.sh)
        run: |
          bash ./misc/scripts/clang_format.sh

      - name: Python style checks via black (black_format.sh)
        run: |
          bash ./misc/scripts/black_format.sh

  build:
    runs-on: ${{ matrix.os }}
    name: 🔧 Build
    needs: static-checks
    strategy:
      fail-fast: false
      matrix:
        include:
          # Android
          - platform: android
            arch: 'x86_32'
            gdnative_flags: 'android_arch=x86'
            sconsflags: ''
            os: 'ubuntu-20.04'
          - platform: android
            arch: 'x86_64'
            gdnative_flags: 'android_arch=x86_64'
            sconsflags: ''
            os: 'ubuntu-20.04'
          - platform: android
            arch: 'arm32'
            gdnative_flags: 'android_arch=armv7'
            sconsflags: ''
            os: 'ubuntu-20.04'
          - platform: android
            arch: 'arm64'
            gdnative_flags: 'android_arch=arm64v8'
            sconsflags: ''
            os: 'ubuntu-20.04'

          # iOS
          - platform: ios
            arch: 'x86_64'
            gdnative_flags: 'ios_arch=x86_64'
            sconsflags: 'ios_simulator=true'
            os: 'macos-latest'
          - platform: ios
            arch: 'arm64'
            gdnative_flags: 'ios_arch=arm64'
            sconsflags: ''
            os: 'macos-11'


          # Linux
          - platform: linux
            arch: 'x86_32'
            gdnative_flags: 'bits=32'
            sconsflags: ''
            os: 'ubuntu-20.04'
          - platform: linux
            arch: 'x86_64'
            gdnative_flags: 'bits=64'
            sconsflags: ''
            os: 'ubuntu-20.04'

          # macOS
          - platform: osx
            arch: 'x86_64'
            gdnative_flags: 'macos_arch=x86_64 bits=64'
            sconsflags: ''
            os: 'macos-11'
          - platform: osx
            gdnative_flags: 'macos_arch=arm64 bits=64'
            arch: 'arm64'
            sconsflags: ''
            os: 'macos-11'

          # Windows
          - platform: windows
            arch: 'x86_32'
            gdnative_flags: 'bits=32'
            sconsflags: 'use_mingw=yes'
            os: 'ubuntu-20.04'
            msvc_arch: amd64_x86
          - platform: windows
            arch: 'x86_64'
            gdnative_flags: 'bits=64'
            sconsflags: 'use_mingw=yes'
            os: 'ubuntu-20.04'
            msvc_arch: amd64

    env:
      SCONSFLAGS: ${{ matrix.sconsflags }} platform=${{ matrix.platform }} arch=${{ matrix.arch }} --jobs=2

    defaults:
      run:
        shell: bash

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - name: Install Windows build dependencies
        if: ${{ matrix.platform == 'windows' }}
        run: |
          sudo apt-get update
          sudo apt-get install build-essential mingw-w64
          sudo update-alternatives --set i686-w64-mingw32-gcc /usr/bin/i686-w64-mingw32-gcc-posix
          sudo update-alternatives --set i686-w64-mingw32-g++ /usr/bin/i686-w64-mingw32-g++-posix
          sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
          sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
          dpkg -l | grep ii | grep mingw
          update-alternatives --get-selections | grep mingw

      - name: Install Linux build dependencies
        if: ${{ matrix.platform == 'linux' }}
        run: |
          sudo apt-get install build-essential gcc-multilib g++-multilib

      - name: Set up Python 3.x
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
          architecture: 'x64'

      - name: Configuring Python packages
        run: |
          python -c "import sys; print(sys.version)"
          python -m pip install scons
          python --version
          scons --version
          cmake --version

      - name: Compile Extension - debug - ${{ matrix.platform }} - ${{ matrix.arch }}
        run: |
          scons target=debug generate_bindings=yes

      - name: Compile GDNative - debug - ${{ matrix.platform }} - ${{ matrix.arch }}
        run: |
          scons target=debug generate_bindings=yes ${{ matrix.gdnative_flags }} godot_version=3

      - name: Compile Extension - release - ${{ matrix.platform }} - ${{ matrix.arch }}
        run: |
          scons target=release

      - name: Compile GDNative - release ${{ matrix.platform }} - ${{ matrix.arch }}
        run: |
          scons target=release ${{ matrix.gdnative_flags }} godot_version=3

      - uses: actions/upload-artifact@v3
        with:
          name: ${{ github.job }}-${{ matrix.platform }}-${{ matrix.arch }}
          path: bin/*

  package:
    name: 📦 Package
    needs: build
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - uses: actions/download-artifact@v3
        with:
          path: artifacts

      - name: Bundle licenses.
        run: |
          cp LICENSE artifacts/LICENSE.webrtc-native
          cp deps/libdatachannel/LICENSE artifacts/LICENSE.libdatachannel
          cp deps/openssl/LICENSE.txt artifacts/LICENSE.openssl
          cp deps/libdatachannel/deps/libjuice/LICENSE artifacts/LICENSE.libjuice
          cp deps/libdatachannel/deps/usrsctp/LICENSE.md artifacts/LICENSE.usrsctp
          cp deps/libdatachannel/deps/libsrtp/LICENSE artifacts/LICENSE.libsrtp
          cp deps/libdatachannel/deps/json/LICENSE.MIT artifacts/LICENSE.json
          cp deps/libdatachannel/deps/plog/LICENSE artifacts/LICENSE.plog

      - name: Package artifacts for release
        run: |
          mkdir release
          cd release

          ls -R

          for version in extension gdnative
          do
            for name in webrtc webrtc_debug
            do
              destdir="${version}/${name}"
              mkdir -p ${destdir}/lib
              find ../artifacts -wholename "*/${destdir}/lib/*" | xargs cp -t ${destdir}/lib/
              find ../artifacts -wholename "*/${destdir}/${name}.tres" -or -wholename "*/${destdir}/${name}.gdextension" | head -n 1 | xargs cp -t ${destdir}/
              find ../artifacts -wholename "*/LICENSE*" | xargs cp -t ${destdir}/
              cd ${version}
              zip -r ../godot-${version}-${name}.zip ${name}
              cd ..
            done
          done

          ls -R

      - uses: actions/upload-artifact@v3
        with:
          name: godot-webrtc-extension
          path: release/*-extension-*.zip

      - uses: actions/upload-artifact@v3
        with:
          name: godot-webrtc-gdnative
          path: release/*-gdnative-*.zip