# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=angleproject
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.r25748.890b5d8f
pkgrel=1
pkgdesc='A conformant OpenGL ES implementation for Windows, Mac, Linux, iOS and Android (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://angleproject.org/'
msys2_repository_url='https://chromium.googlesource.com/angle/angle'
msys2_documentation_url='https://chromium.googlesource.com/angle/angle/+/main/README.md'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-egl-headers"
         "${MINGW_PACKAGE_PREFIX}-gles-headers"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git"
             "unzip")
_commit=("890b5d8fa2988e3719e0d80421bf3e927db9cd5c")
source=("${_realname}::git+https://chromium.googlesource.com/angle/angle.git#commit=${_commit}"
        "swiftshader.zip::https://github.com/google/swiftshader/archive/c1f7fbbec4e17769c0182daae185466cb806d0d5.zip"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=70c150bc0ae989d00c15ab1ed67464198e4890d0"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=bf1e8de6cc305fdab82db1b65bed76b70bd96c8b"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=d6072980974f9a4922d2eb27e4fd244ca3017031"
        "bare-clones/spirv-headers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/SPIRV-Headers.git#commit=c8ad050fcb29e42a2f57d9f59e97488f465c436d"
        "bare-clones/spirv-tools::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/SPIRV-Tools.git#commit=257a227fbadf8176ea386c7d8fb9b889cbf08640"
        "bare-clones/vulkan-headers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Headers.git#commit=7cff847503174e2049b08253ee5f30428866fea3"
        "bare-clones/vulkan-loader::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Loader.git#commit=235d1d2cf617af03a2ecbf6e951287595138feda"
        "bare-clones/vulkan_memory_allocator::git+https://chromium.googlesource.com/external/github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git#commit=56300b29fbfcc693ee6609ddad3fdd5b7a449a21"
        "bare-clones/vulkan-tools::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Tools.git#commit=5968d14a1ef8a7eeacf342e1542099e1aff20f70"
        "bare-clones/vulkan-validation-layers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-ValidationLayers.git#commit=6f5b5e78b22cdd8d9535986e0d4c1b94ebe2d7b4"
        001-add-mingw-toolchain.patch
        002-buildflags-fixes.patch
        003-angle-src-fixes.patch
        004-swiftshader-updates.patch
        005-vulkan-memory-allocator-updates.patch
        006-spirv-updates.patch
        007-vulkan-loader-updates.patch
        008-arm64-mingw-intrinsic.patch
        jpeg.gn
        jsoncpp.gn
        png.gn
        rjson.gn
        zlib.gn
        angleproject.pc)
sha256sums=('8b51860cdeedda205ab6dc641c50943afd39018212701451f9a91d0b5f5be2cd'
            'f76c7c95066b1666ee2223f25d84cf7ddf7a891300e47b8eebd41bcd10dbba37'
            'a81c93d0eeff1f64765be1cf741aa44f300dea31441760b4e4c1dbe5ed4a0e56'
            '6478d94a8295db87759f756c99a7c079d7f3710e4fb996f9851d7f9ca8982f11'
            'f58b2605f036b8c9c7992635ad5f2070abe4d527314915874766d654ae4eb429'
            '47564925640c69eb6bef13ff7bca417d39e568208702e57c23cb6d4de3991b7a'
            '4d0c3e559c632add51f5faa365218c2c45e8ab7e6430077b269575d75ac3e1e4'
            '25ef9ac33ba80d13e44b2d3b45093451a20e41c4ed802c81f764934763408752'
            '966446961563e5e8e7d1917093a71ddfa11e5ea4a686c7f7176e97053f1e0029'
            '913fc3a85ae676025bafe63880c6413ffafe42495a04a52527ee914ee9ba3ae5'
            '84a3afb0278156a286c37e3c166e4079a877ccb8d3da85b32d7c37986d78efbc'
            '169327cafbfd0653a2937962cf17610d26c46dead8187f6ca1e21b20b637d9e1'
            'f82de30bbd868c75f7f3edd191e30fcce81c87825ff7c10713929295b5f4016f'
            'df927c5f804d51e6ce8f2c9324396d56d17aebbd674db3ba1c6ebafbebded0ac'
            '36939a33c2f9fef5ecf07d5bd508057b2a49c1582e0e01891ad3e7d9e3ffb7fc'
            '8209021bad1359426f24704395e67c2ca663a3c7b65eaaa3a2fc0e67fd155353'
            '08441bed59aadcd6eb7079662a970e5c26dae3c77d4958af0485ac959d08a2fe'
            'cb5358e80a45f54e15a69bbe12ecb07eae405b491451ee38aea01ce4579b492b'
            'b69a6ff976df8192b776673f94fd494ba4c62e798708f753d9f8390f4fc0341e'
            '6d5c3d7888a671059d378e955c86d297dede5e6eee0d6aa0aeb12eb84c9e8ca1'
            'bf878de3203a62e2a7f81bd4cd79237adc74804b4b692d9277da005ed56d4f2a'
            '86013781c2700219d4f64d7ac34ad16c40fcca9a641371385f67f642e23c643b'
            'f0fb05348bf2de599eff35e2d35e3336b9720b6bd2799af9d186c05ff45d34f0'
            '044afff420a1c8ad896f79fb8ae03878f6063bdb0a25dab0b8e4bff092e3d3ac'
            '4dc3baf79ba7c95b52cbbafce81b26e6d165d4ae7e0dcc2eabe0250466edf178'
            'a9f0b7c82d8ce614df8940508d3f098cca9596d1150c654559aac21749ca170b')
noextract=("swiftshader.zip")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${_realname}
  local _major=$(head -n 14 src/common/angle_version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 15 src/common/angle_version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s.%s" "$_major" "$_minor" "$(git rev-list --count HEAD)" "$(git rev-parse --short=8 "${_commit}")"
}

prepare() {
  echo ":: Patching build directory"
  cd "${srcdir}"/build
  apply_patch_with_msg \
    001-add-mingw-toolchain.patch
  python "${srcdir}"/build/util/lastchange.py -o "${srcdir}"/build/util/LASTCHANGE
  cp -rf "${srcdir}"/build "${srcdir}"/${_realname}
  echo "build_with_chromium = false" > "${srcdir}"/${_realname}/build/config/gclient_args.gni

  echo ":: Patching zlib directory"
  cp -f "${srcdir}"/zlib.gn "${srcdir}"/${_realname}/third_party/zlib/BUILD.gn
  cp -rf "${srcdir}"/zlib/google "${srcdir}"/${_realname}/third_party/zlib

  echo ":: Extracting SwiftShader"
  cd "${srcdir}"
  unzip -q swiftshader.zip
  mv swiftshader-* SwiftShader

  echo ":: Patching SwiftShader directory"
  cd "${srcdir}"/SwiftShader
  apply_patch_with_msg \
    004-swiftshader-updates.patch \
    008-arm64-mingw-intrinsic.patch
  cp -rf "${srcdir}"/SwiftShader "${srcdir}"/${_realname}/third_party

  echo ":: Patching vulkan_memory_allocator directory"
  cd "${srcdir}"/vulkan_memory_allocator
  apply_patch_with_msg \
    005-vulkan-memory-allocator-updates.patch
  cp -rf "${srcdir}"/vulkan_memory_allocator "${srcdir}"/${_realname}/third_party

  echo ":: Patching spirv-tools directory"
  cd "${srcdir}"/spirv-tools
  apply_patch_with_msg \
    006-spirv-updates.patch

  echo ":: Patching vulkan-loader directory"
  cd "${srcdir}"/vulkan-loader
  apply_patch_with_msg \
    007-vulkan-loader-updates.patch

  echo ":: Copying spirv and vulkan files"
  cp -rf "${srcdir}"/spirv-headers/* "${srcdir}"/${_realname}/third_party/spirv-headers/src
  cp -rf "${srcdir}"/spirv-tools/* "${srcdir}"/${_realname}/third_party/spirv-tools/src
  cp -rf "${srcdir}"/vulkan-headers/* "${srcdir}"/${_realname}/third_party/vulkan-headers/src
  cp -rf "${srcdir}"/vulkan-loader/* "${srcdir}"/${_realname}/third_party/vulkan-loader/src
  cp -rf "${srcdir}"/vulkan-tools/* "${srcdir}"/${_realname}/third_party/vulkan-tools/src
  cp -rf "${srcdir}"/vulkan-validation-layers/* "${srcdir}"/${_realname}/third_party/vulkan-validation-layers/src

  echo ":: Updating extra repos"
  cp -f "${srcdir}"/jpeg.gn "${srcdir}"/${_realname}/third_party/libjpeg_turbo/BUILD.gn
  cp -f "${srcdir}"/jsoncpp.gn "${srcdir}"/${_realname}/third_party/jsoncpp/BUILD.gn
  cp -f "${srcdir}"/png.gn "${srcdir}"/${_realname}/third_party/libpng/BUILD.gn
  cp -f "${srcdir}"/rjson.gn "${srcdir}"/${_realname}/third_party/rapidjson/BUILD.gn
  cp -rf "${srcdir}"/clang "${srcdir}"/${_realname}/tools

  echo ":: Patching angle source"
  cd "${srcdir}"/${_realname}
  apply_patch_with_msg \
    002-buildflags-fixes.patch \
    003-angle-src-fixes.patch

  echo ":: Generating angle_commit.h"
  python src/commit_id.py gen src/common/angle_commit.h

  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@NAME@|${_realname}|g" \
      -e "s|@DESCRIPTION@|${pkgdesc}|g" \
      -e "s|@VERSION@|${pkgver}|g" -i "${srcdir}"/angleproject.pc
}

build() {
  cd ${_realname}

  local _target=Release
  local _debug=false
  if check_option "debug" "y"; then
    _target=Debug
    _debug=true
  fi

  local _arch=x64
  local _cfg=true
  local _swiftshader=true
  if [[ ${CARCH} == aarch64 ]]; then
    _arch=arm64
    _cfg=false
    _swiftshader=false
  fi

  local _clang=false
  if [[ ${MSYSTEM} == CLANG* ]]; then
    _clang=true
  fi

  gn gen out/${_target}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_clang=${_clang}
    is_component_build=true
    angle_build_tests=false
    angle_enable_abseil=false
    angle_enable_cl=false
    angle_enable_metal=false
    angle_enable_swiftshader=${_swiftshader}
    angle_shared_libvulkan=false
    chrome_pgo_phase=0
    use_custom_libcxx=false
    use_libcxx_modules=false
    use_lld=false
    use_siso=false
    use_sysroot=false
    win_enable_cfg_guards=${_cfg}
    treat_warnings_as_errors=false"

  ninja -C out/${_target}-${MSYSTEM}
}

package() {
  cd ${_realname}

  local _target=Release
  if check_option "debug" "y"; then
    _target=Debug
  fi

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib,include/{EGL,GLES2}}

  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll "${pkgdir}"${MINGW_PREFIX}/bin/
  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/
  install -Dm755 out/${_target}-${MSYSTEM}/obj/lib*.a "${pkgdir}"${MINGW_PREFIX}/lib/

  # Copy public ANGLE headers
  install -Dm644 include/angle_gl.h "${pkgdir}"${MINGW_PREFIX}/include
  install -Dm644 include/angle_windowsstore.h "${pkgdir}"${MINGW_PREFIX}/include
  install -Dm644 include/EGL/eglext_angle.h "${pkgdir}"${MINGW_PREFIX}/include/EGL
  install -Dm644 include/GLES2/gl2ext_angle.h "${pkgdir}"${MINGW_PREFIX}/include/GLES2

  # Copy license
  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Copy pkg-config file
  install -Dm644 "${srcdir}"/angleproject.pc "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/angleproject.pc
}
