# 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=2.92.0
pkgrel=1
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
license=('GPL')
# buid conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         "${MINGW_PACKAGE_PREFIX}-audaspace"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${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}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         $([[ "$CARCH" == "x86_64" ]] && echo "${MINGW_PACKAGE_PREFIX}-openimagedenoise")
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         #"${MINGW_PACKAGE_PREFIX}-opensubdiv"
         #"${MINGW_PACKAGE_PREFIX}-openvdb" # there are missing export symbols in openvdb DLL
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
#[[ $CARCH == x86_64 ]] && makedepends+=('cuda')
#optdepends=('cuda: cycles renderer cuda support')
options=(!strip staticlibs !debug)
install=blender-${CARCH}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0004-fix-finding-glew.patch
        0005-fix-define-syntax.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0008-mingw-use-X11-like-path-layout.patch
        0011-c++11-defines-__i386__-not-i386.patch
        0012-re-enable-SSE-on-mingw-w64-i686.patch
        0014-libstdc++-not-support-utf-stream.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0017-find-pugixml.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0021-comctl32-v6-for-TaskDialogIndirect.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0025-callback-function-prototype-fix.patch
        0026-embree-shared.patch
        0027-broken-32bit-support.patch)
sha256sums=('e791cfc403292383577c3c8ce2cd34e5aa2cd8da0a7483041049a1609ddb4595'
            '9cc285a90050e51af66320357976b99ba7bc4ff918173d6d744869f78859813a'
            '80bb43c71b8dcaa92c1fd8e611f311c8281382435882382db11b1b93267f5200'
            'bcaa5b105463aa2dd9ab0f69ed4d422c23f9ae1a00a93e012c62113489f3db56'
            '87036a005a817bce9edd5fb7f18ba3b7a0b8d309d6f1f9bb0f813c931d889c0d'
            'cb603fd30a2e1421f653a187c3beb8e767d043a0d948213e884095cf0a27a411'
            '10d5d251132d2fd19d44e15249e8f80692ba613dd77cc8b7526cfdd5faa5a19b'
            '7fca0ae64741a9d9edcec3e933651111e8df6faaefa318581f2f94286cbf2911'
            'f3e4a0cdec45a08df28f4d93dea508352df683d2fc8cf4b73f289db83eaf5d9e'
            '6c3e47c8051345bd7c98acdc8ea592889f650498793fd5053728cec4ac659303'
            '4ac8822d7620661f34327155b6d2d4491c3704dae5de025ced89830fe622f492'
            'd5c1c5944ce49982633e5d65cdedfb64d0aaa3bd1c8ae4877c4dfc3e5ae03dac'
            '5ce05eed73408045c78cd1be262393b2c6815b77dd2979964a8090e00a3ce5ac'
            '47b3cc6712934fcc42fe0519c767ffac622562f32cb099a851952fe9b75f66b2'
            '3b51fa1a7cf3d6afd201efffbbe61ab10ad2dfaba370d95b84214369ba551580'
            '9f94a11de1502ffc20bb1336ebf2405e5d5b30eb34b45bb0eda1f67c3ccf67f0'
            '8be734582b9980ad0a3c5a82c200767ce558084f18e3cca11947991a14b0d68a'
            'db5999ba865a40b401244a0b540a8a76d377530152e19e59630891af6ec80534'
            '61c0873f9c661f80eb189b7391a4c4c1eef2f7db4178177ce7ff7c47342384ae'
            '86814ae1e91100b903fb65ceb61d67766ff6386e55ae892aa72278a6dd20f86a'
            '55b6d79dbbfd1ac182c26b868c16867e42729a5537da29f3527e9017b19b41f6')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  [[ -d build_files/cmake/platform/platform_win32_mingw64.cmake ]] && rm build_files/cmake/platform/platform_win32_mingw64.cmake
  patch -p1 -i "${srcdir}"/0001-mingw-buildsystem.patch
  patch -p1 -i "${srcdir}"/0002-fix-mingw-w64-definitions.patch
  patch -p1 -i "${srcdir}"/0004-fix-finding-glew.patch
  patch -p1 -i "${srcdir}"/0005-fix-define-syntax.patch
  patch -p1 -i "${srcdir}"/0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
  patch -p1 -i "${srcdir}"/0008-mingw-use-X11-like-path-layout.patch
  patch -p1 -i "${srcdir}"/0011-c++11-defines-__i386__-not-i386.patch
  patch -p1 -i "${srcdir}"/0012-re-enable-SSE-on-mingw-w64-i686.patch
  patch -p1 -i "${srcdir}"/0014-libstdc++-not-support-utf-stream.patch
  patch -p1 -i "${srcdir}"/0015-mingw-python-like-msvc.patch
  patch -p1 -i "${srcdir}"/0016-mingw-windres.patch
  patch -p1 -i "${srcdir}"/0017-find-pugixml.patch
  patch -p1 -i "${srcdir}"/0019-fix-casts.patch
  patch -p1 -i "${srcdir}"/0020-blendthumb-build.patch
  patch -p1 -i "${srcdir}"/0021-comctl32-v6-for-TaskDialogIndirect.patch
  patch -p1 -i "${srcdir}"/0022-find-llvm-convert-paths.patch
  patch -p1 -i "${srcdir}"/0023-link-errors.patch
  patch -p1 -i "${srcdir}"/0025-callback-function-prototype-fix.patch
  patch -p1 -i "${srcdir}"/0026-embree-shared.patch
  patch -p1 -i "${srcdir}"/0027-broken-32bit-support.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir "${srcdir}"/build-${CARCH}  && cd "${srcdir}"/build-${CARCH}

  # i686 SSE is problematic with Cycles: https://developer.blender.org/T36316
  # .. but commit:
  # https://lists.blender.org/pipermail/bf-blender-cvs/2013-October/059705.html
  # .. breaks the build:
  # undefined reference to `ccl::kernel_cpu_sse2_path_trace` .. etc
  # This is because it prevents the definition of __KERNEL_SSE2__ without having
  # ensured WITH_CYCLES_OPTIMIZED_KERNEL_SSE2 is undefined. Let's hope that the
  # bugs have been fixed as patch 0011 re-enables __KERNEL_SSE2__. If they're not
  # fixed then we'll have to pass ENABLESSE2 to cmake instead, -DWITH_CPU_SSE being
  # the correct one (probably!)
  # [[ $CARCH == i686 ]] && ENABLESSE2="-DSUPPORT_SSE2_BUILD=OFF -DWITH_CPU_SSE=OFF"
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  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])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMINGW_LIBS=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DWITH_SYSTEM_AUDASPACE=ON \
    -DWITH_SYSTEM_GLEW=ON \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENCOLLADA=ON \
    -DWITH_ALEMBIC=ON \
    -DWITH_JACK=OFF \
    -DWITH_CODEC_FFMPEG=ON \
    -DWITH_CODEC_SNDFILE=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_FFTW3=ON \
    -DWITH_OPENVDB=OFF \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DPython_ROOT_DIR=${MINGW_PREFIX} \
    -DPython_FIND_REGISTRY=NEVER \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=python${_pyver} \
    -DPYTHON_INCLUDE_DIRS=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DCYCLES_OSL=${MINGW_PREFIX} \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR=${pkgdir} install
  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe

  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}"
  ${MINGW_PREFIX}/bin/python -m compileall \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/startup \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/modules \
  ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver}/scripts/addons
}
