# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.7 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.5.3
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.blender.org"
msys2_repository_url="https://projects.blender.org/blender/blender"
msys2_references=(
  'archlinux: blender'
  'cpe: cpe:/a:blender:blender'
)
license=('spdx:GPL-2.0-or-later')
# build conflict with "${MINGW_PACKAGE_PREFIX}-glog"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         "${MINGW_PACKAGE_PREFIX}-audaspace"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         #"${MINGW_PACKAGE_PREFIX}-bullet-dp"
         #"${MINGW_PACKAGE_PREFIX}-draco" It builds an internal library
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-gflags"
         #"${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-glslang"
         "${MINGW_PACKAGE_PREFIX}-imath"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-manifold"
         #"${MINGW_PACKAGE_PREFIX}-materialx"
         #"${MINGW_PACKAGE_PREFIX}-usd"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimagedenoise"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openpgl"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         "${MINGW_PACKAGE_PREFIX}-opensubdiv"
         "${MINGW_PACKAGE_PREFIX}-openvdb"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
             $([[ "$CARCH" == "aarch64" ]] && echo "${MINGW_PACKAGE_PREFIX}-sse2neon"))
install=blender-${MSYSTEM}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        platform_win32_mingw64.cmake
        comctl_v6.exe.manifest
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0003-disable-static_assert_align-on-mingw.patch
        0004-fix-rad2-conflict.patch
        0005-fix-WINTAB_PRINTF-on-mingw.patch
        0006-mingw-DEBUG-avoid-too-many-sections.patch
        0008-mingw-use-X11-like-path-layout.patch
        0009-include-missing-headers.patch
        0010-conflict-near-far-macroses.patch
        0011-no-TIME_UTC-on-msvcrt.patch
        0013-force-draco.patch
        0014-fix-find-numpy-2.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0024-openvdb-shared.patch
        0025-callback-function-prototype-fix.patch
        0028-use-std-namespace.patch
        0029-display-driver-build.patch
        0035-Enable-SSE2NEON-on-Windows-ARM.patch
        0039-no-WRL-on-mingw-w64.patch
        0050-fix-vulkan-import-lib-lookup.patch
        https://github.com/blender/blender/commit/f5f30131131025a24be93eced7d04f9d96cf5cbf.patch)
sha256sums=('f92af47f3761eb68bc70b74d7dddcb72fcb3038b76d847c6441e5b90b6f19275'
            'e6e5460c67ed2101004c64a8ca1cfcf2828880ea98c97f8f9f220b96e143fc25'
            '233b56fc38fe0aac714b0cfacdc8250dd64c1984d1a6785b9898a2d2c8a015ed'
            'f7081e3d7c9ae40481db8fecae2fd111898305c9a5ef05b31c6131ef8642849c'
            '2d898e81118771405fc6ae1a7f7fd75c58398c6b7a195b9cd0be0f01ba281ff3'
            'f82e9be14a1f683c3a4d6d198d7bb552e83637098dfd2a88341461347daec919'
            '2794fd231f35627681273d793895b9d66fb6546b7b5003fd6ee85d86e87b90ae'
            'af467653b9a4d3dfee8abf07e8bf23b9759ec205709dabd8735005bd263ce3c7'
            '6b86a93dfcace5a1dfb0bdc8de6b0a6318af206a738947d039bce2607ae07f7a'
            '0ec9942a62feeeca43a3cd78ac0473165bc4d43bb28cbe46faf0f574a05bf366'
            '76ef7c97a373b07b89bd16d183965dd8fae4ecfcd160d3d8b62f08f58c0a81d0'
            '9e2541912f106171be109aaf11c5406d9ddd62b6d930628aaa0982ceb8216a66'
            '84fdb7b1c4f408aff199172cbb09460ac60443156bc78fc3eaf3ba90e6fb500e'
            'e9e26a1c9b540fbdd7f63e83c0bbdf71c08a0e68f7ceb57fcb322113a97c9a62'
            'ae2270a6db620a93655255a6a5400b1857b5a5d29e2999cd7649b54f63c0d5fc'
            'fd2230e71f8deb6a3252c5ea439803bdb6abb5124d0cc2bc64dc856de7bd00cf'
            '9600e82135f63924ebcf7de84b983a2fc031acdec5a382b1a713ed6fd064de62'
            '620c06f1db232fb838f3d7fb3fdaeda862ece0782cdba517b43cb9c170f57e2b'
            'ae1daa500f50c74ffa906979ea1d41cda28b36f95d1ac0aab8e714c4a55642cb'
            'f2e166b76a9cfb2b753072cf0a45a241c84ff3f539e8c3c43d96c2679a144c81'
            '28c121996d4d652910ef8661723ee5a54e52913361c5fc1e3b5f0aaa82fe5543'
            '99fb88ff39e10273725e71aad62d12c480adc5f5c4a2c3a6181b4094d9327bae'
            '3d9903bbc99a533723e84dcb465fab9ba7b0cc3fd7cec664633cbc860de9c4d6'
            'caa7b9dd66086fe0274ce8ae55d3084e5ded2e68189c9857582c4da7b211ca33'
            '260d52ff166635145dd3b3e51ba291eba1edc01274f3385cfe89c02ea1489761'
            '3cc151106b16adf40a9d1a751519365eeaf2ebd4bacb359272cc04d5f55c61be'
            'd4a02b177c08c37551087e391237bf15ed534ec405edfe961783b8bb4cf99c6a'
            '1a5e091054edee6507e148bd697b1426a4d3234cdb6b704151e67e36b884131f'
            'c5137eea29301cf1fbc0e62ac159c8c63d4e5bb9536461c3c76ff749e7be39f6')
#options=(debug)

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  cp -f ${srcdir}/platform_win32_mingw64.cmake build_files/cmake/platform/
  
  cp -f ${srcdir}/comctl_v6.exe.manifest release/windows/icons/blender.exe.manifest
  cp -f ${srcdir}/comctl_v6.exe.manifest ./tests.exe.manifest

  apply_patch_with_msg \
    0001-mingw-buildsystem.patch \
    0002-fix-mingw-w64-definitions.patch \
    0003-disable-static_assert_align-on-mingw.patch \
    0004-fix-rad2-conflict.patch \
    0005-fix-WINTAB_PRINTF-on-mingw.patch \
    0008-mingw-use-X11-like-path-layout.patch \
    0009-include-missing-headers.patch \
    0010-conflict-near-far-macroses.patch \
    0011-no-TIME_UTC-on-msvcrt.patch \
    0013-force-draco.patch \
    0014-fix-find-numpy-2.patch \
    0015-mingw-python-like-msvc.patch \
    0016-mingw-windres.patch \
    0019-fix-casts.patch \
    0020-blendthumb-build.patch \
    0022-find-llvm-convert-paths.patch \
    0023-link-errors.patch \
    0024-openvdb-shared.patch \
    0025-callback-function-prototype-fix.patch \
    0028-use-std-namespace.patch \
    0029-display-driver-build.patch \
    0035-Enable-SSE2NEON-on-Windows-ARM.patch \
    0039-no-WRL-on-mingw-w64.patch \
    0050-fix-vulkan-import-lib-lookup.patch \
    f5f30131131025a24be93eced7d04f9d96cf5cbf.patch

  if check_option "debug" "y"; then
    apply_patch_with_msg \
      0006-mingw-DEBUG-avoid-too-many-sections.patch
  fi

  # Remove *.orig files
  find . -type f -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM}  && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug" "-DWITH_CYCLES_DEBUG=ON" "-DWITH_GHOST_DEBUG=ON" "-DWITH_DRAW_DEBUG=ON")
  fi

  CXXFLAGS+=" -w"
  CFLAGS+=" -w"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
    CFLAGS+=" -Wno-unreachable-code-generic-assoc"
  fi

  if [[ $MINGW_PACKAGE_PREFIX != *-clang-* ]]; then
    CFLAGS+=" -Wno-incompatible-pointer-types"
  fi

  if [[ $CARCH == "aarch64" ]]; then
    _extra_config+=("-DSSE2NEON_ROOT_DIR=${MINGW_PREFIX}/include/sse2neon")
  fi

  export XR_OPENXR_SDK_ROOT_DIR=${MINGW_PREFIX}
  export EMBREE_ROOT_DIR=${MINGW_PREFIX}

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  CXXFLAGS+=" -D_USE_MATH_DEFINES" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_SYSTEM_AUDASPACE=ON \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_SYSTEM_FREETYPE=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_NANOVDB=OFF \
    -DWITH_OPENCOLORIO=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_VULKAN_BACKEND=ON \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  cp -r bin "${pkgdir}"${MINGW_PREFIX}/
  rm "${pkgdir}"${MINGW_PREFIX}/bin/msgfmt.exe

  # Manually install draco bindings
  #_pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  #mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}"/
  #mv "${pkgdir}"${MINGW_PREFIX}/share/blender/4*/python/lib/* "${pkgdir}"${MINGW_PREFIX}/lib/
  #rm -r "${pkgdir}"${MINGW_PREFIX}/share/blender/4*/python

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}"
  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/startup \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/modules \
    "${pkgdir}"${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/addons_core

  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  for file in "${srcdir}/${_realname}-${pkgver}/doc/license/"*; do
    install -Dm644 "${file}" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  done
}
