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

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.8.0
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/"
msys2_references=(
  'archlinux: openshadinglanguage'
  'gentoo: media-libs/osl'
)
license=("spdx:BSD-3-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         #"${MINGW_PACKAGE_PREFIX}-clang-libs"
         #"${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-partio"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-imath"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-robin-map"
             "${MINGW_PACKAGE_PREFIX}-clang-20"
             "${MINGW_PACKAGE_PREFIX}-llvm-20"
             "${MINGW_PACKAGE_PREFIX}-qt6-base")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python module"
            "${MINGW_PACKAGE_PREFIX}-qt6-base: for osltoy")
source=(https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-dont-add-fno-rtti-compile-option.patch
        0002-dont-add-flex-include-dir.patch
        0010-macros-undef-mingw.patch
        0012-isatty-mingw.patch
        0015-llvm-linking.patch)
sha256sums=('7916ed97403766b7c853f52506cff0dc2b4a2f5f20386fb0d86817ca8725e18f'
            '36d7d16bcf9dedebd254c24fa2f4f70ef5bdf4cea8beee380931cfe5b8c45d7d'
            'b65a21053f0090a9e85ad026fef31d99f65f59f3c79822f452543db3ca8737b6'
            '8b42553133e487077363a96520131f84453a81afdd9d198ef6827e0395ff1208'
            'c7c125e6fed4650f9770d92ea24771e5518b344718dde291a6d5d4ad945c6222'
            '1b2c1e7faae162078696049f63c2b55e8c111ffeff25abaa49d67877c81a71d1')

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

prepare() {
  cd "${srcdir}"/OpenShadingLanguage-${pkgver}

  apply_patch_with_msg \
    0001-dont-add-fno-rtti-compile-option.patch \
    0002-dont-add-flex-include-dir.patch \
    0010-macros-undef-mingw.patch \
    0012-isatty-mingw.patch \
    0015-llvm-linking.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd 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")
  fi

  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}/opt/llvm-20
  CFLAGS+=" -Wno-attributes"
  CXXFLAGS+=" -Wno-attributes -DTINYFORMAT_ALLOW_WCHAR_STRINGS"

  if [[ ${CARCH} == x86_64 ]]; then
    _extra_config+=("-DUSE_SIMD=sse3")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DCMAKE_CXX_STANDARD=17 \
    -DOSL_BUILD_TESTS=OFF \
    -DSTOP_ON_WARNING=OFF \
    -DLLVM_STATIC=OFF \
    -DLLVM_ROOT=${MINGW_PREFIX}/opt/llvm-20 \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    "${_extra_config[@]}" \
    ../OpenShadingLanguage-${pkgver}

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)s
  PATH="${srcdir}"/build-${MSYSTEM}/src/liboslcomp:"${PATH}" \
  ${MINGW_PREFIX}/bin/cmake --build .
}

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

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

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}/OpenShadingLanguage-${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
}
