# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.11.10.0
pkgrel=2
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ilmbase"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-partio"
         "${MINGW_PACKAGE_PREFIX}-pugixml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs' '!emptydirs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenShadingLanguage/archive/Release-${pkgver}.tar.gz
        0001-install-dirs.patch
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0005-Fix-cast-from-void-ptr.patch
        0007-Fix-bad-casts-with-intptr_t.patch
        0010-macros-undef-mingw.patch
        0012-isatty-mingw.patch
        0013-disable-assert-partio.patch
        0014-fix-testshade-library-name.patch
        0015-llvm-linking.patch
        0016-OIIO-API.patch)
sha256sums=('e60b049b666f7eff519a64608ca166946c3de5db49515a36d379837f82310f05'
            'e3f6ad09619d122ada129cbc77d7778751abfecef9ac7e0a60d9550c811df643'
            '70d4ce3c69ed072eee0d2bb84cecea5ff9ff35e258a24a7a78ec94e4b626e493'
            '6b1e4065292361c724a53f6961b72ff74dde9349a3c1e42a1ab8e883d902f5e4'
            '8bc55ff08257d9482d32efe25a416b02acca345897acc46c09c71b6a496e55cc'
            '756ed1e1bf3acad0766ac17be979ff84e51fac5cd6adab99e9118a872f3011bf'
            'd6fe2f65d219b124f9155a7c362cffb085134acd0882786870d72b519994f4cc'
            'c7c125e6fed4650f9770d92ea24771e5518b344718dde291a6d5d4ad945c6222'
            'c0d66bd0c38fc3627d8bf17c7f9331ac66a2a02e22820208d81400f2ed01543b'
            '3146942267540de2a3146ef8222d59fdb265dc8fffa3f3a2207bbc851fc99ab8'
            '1b2c1e7faae162078696049f63c2b55e8c111ffeff25abaa49d67877c81a71d1'
            '01a9e6543159b8c966fa31755781f028b156fb799ae1c8eab94cd8d4c2ce48a6')

prepare() {
  cd ${srcdir}/OpenShadingLanguage-Release-${pkgver}
  #patch -p1 -i ${srcdir}/0001-install-dirs.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
  patch -p1 -i ${srcdir}/0010-macros-undef-mingw.patch
  patch -p1 -i ${srcdir}/0012-isatty-mingw.patch
  patch -p1 -i ${srcdir}/0013-disable-assert-partio.patch
  patch -p1 -i ${srcdir}/0014-fix-testshade-library-name.patch
  patch -p1 -i ${srcdir}/0015-llvm-linking.patch
  patch -p1 -i ${srcdir}/0016-OIIO-API.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  CFLAGS+=" -Wno-attributes -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -Wno-attributes -DTINYFORMAT_ALLOW_WCHAR_STRINGS -D_USE_MATH_DEFINES"

  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSTOP_ON_WARNING=OFF \
    -DLLVM_STATIC=1 \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DCMAKE_CXX_STANDARD=14 \
    ${extra_config} \
    ../OpenShadingLanguage-Release-${pkgver}

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH=${srcdir}/build-${MINGW_CHOST}/src/liboslcomp:"${PATH}" make #VERBOSE=1
}

package() {
  cd ${srcdir}//build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  
  install -Dm644 "${srcdir}/OpenShadingLanguage-Release-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OSL/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
