# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Bruno Pagani <archange at archlinux dot org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Evgeniy Alekseev <arcanis at archlinux dot org>
# Contributor: Michael Migliore <mcmigliore+aur@gmail.com>
# Contributor: Ray Rashif <schiv at archlinux dot org>
# Contributor: Andrzej Giniewicz <gginiu at gmail dot com>
# Contributor: Thomas Dziedzic <gostrc at gmail>

# May need bootstrapping on upgrades due to circular vtk <-> opencascade dependency
pkgname=vtk
pkgver=9.3.1
pkgrel=8.1
pkgdesc="Software system for 3D computer graphics, image processing, and visualization"
arch=(x86_64 powerpc64le powerpc64 riscv64) #FIXME: powerpc
url="https://www.vtk.org"
license=(BSD-3-Clause)
depends=(
  double-conversion
  gcc-libs
  glibc
  libtiff
  onetbb
  pugixml
)
makedepends=(
  adios2
  boost
  cgns
  cli11
  cmake
  doxygen
  eigen
  expat
  fast_float
  ffmpeg
  fmt
  freetype2
  gdal
  git
  gl2ps
  glew
  gnuplot
  hdf5
  jsoncpp
  libharu
  libjpeg-turbo
  liblas
  libogg
  libpng
  libtheora
  libxml2
  libxt
  lz4
  mariadb-libs
  netcdf
  opencascade
  openmpi
  openvdb
  openvr
  openxr
  pdal
  postgresql-libs
  proj
  python-matplotlib
  python-mpi4py
  qt6-base
  qt6-tools
  qt6-declarative
  rapidjson
  sqlite
  tk
  unixodbc
  utf8cpp
  verdict
  wget
  xz
  zfp
  zlib
)
makedepends_powerpc=('java-environment=11')
makedepends_powerpc64=(openmp 'java-environment')
makedepends_powerpc64le=(openmp 'java-environment=11')
makedepends_riscv64=(openmp)
makedepends_x86_64=(openimagedenoise ospray 'java-environment=11')
# pegtl: https://gitlab.kitware.com/vtk/vtk/-/issues/18151
# exprtk, ioss: not packaged
optdepends=(
  adios2
  cgns
  ffmpeg
  fmt
  gdal
  gl2ps
  glew
  'gnuplot: plotting tools'
  'graphviz: drawing tools'
  hdf5
  'java-runtime=11: java bindings'
  jsoncpp
  libarchive
  libharu
  liblas
  lz4
  mariadb-libs
  netcdf
  openimagedenoise
  'openmpi: OpenMPI support'
  openvdb
  openvr
  openxr
  ospray
  pdal
  postgresql-libs
  proj
  'python: python bindings'
  'python-matplotlib: for Matplotlib rendering'
  'python-mpi4py: OpenMPI python support'
  'qt5-declarative: QML plugin'
  sqlite
  'tk: tcl bindings'
  unixodbc
  verdict
)
options=(staticlibs)
[ "${CARCH}" == 'powerpc' ] && options+=(!debug !lto)
source=(${url}/files/release/${pkgver%.*}/VTK-${pkgver}.tar.gz
         vtk-occt.patch
         ospray-3.patch
         https://github.com/Kitware/VTK/commit/761aa1d15970fcb6aadb7d6152737fa9b2d4b0d0.patch
         fmt-11.patch
         vtk-9.3.0-core-octree_node.txx.patch
         vtk-9.3.0-java.patch
         vtk-9.3.0-ThirdParty-gcc15.patch
         vtk-9.3.0-ThrustPatches.patch
         vtk-9.3.1-xdmf3-32bit.patch)
sha256sums=('8354ec084ea0d2dc3d23dbe4243823c4bfc270382d0ce8d658939fd50061cab8'
            'df958eabc7dc4f5b33383ce0fb0f90a3ba202c1c2a24d3b5b9e7cfb1fb38b011'
            'faf3fd2eea2f73a07f5dcbd67920161a07ae554e008ad1e4099153fec2882278'
            '46de5cee71d696d9632472e27ed369ec49f0e276c3e4bb0a397397d928553864'
            '8161d4e184a3f2d075be26605c0d38f04314a8e26146ab5b1c33f87dabc2b0d2'
            'e2e031166c067ed684a74e9352c79e3da4f82e0ee3cc5afc06618991d2e6bfc5'
            '0580019789ea11926c00d43e03f8d1d6de60597108da35a1b4444f1d521e3105'
            '3d890233010ec9b5e46ce63bc80170069651ec6c419998aec99ec2e85848a28d'
            '40bc697f07c5ce95d3bb9e92cde843b0040fe0c217a9dd4d406632f7db11ed39'
            'c11bdad0be6ea3995bb9c7bee557fc92749b4a9babf22a9e1f55cb138e76094a')

prepare() {
  cd ${pkgname^^}-${pkgver}
  _fast_float_version=$(pacman -Q fast_float | sed -e 's/.* //; s/-.*//g')
  sed -i "s|3.9.0|${_fast_float_version}|" ThirdParty/fast_float/CMakeLists.txt

  patch -Np1 -i "$srcdir"/vtk-occt.patch
  patch -Np1 -i ../ospray-3.patch # Fix build with ospray 3.0
  patch -Np1 -i "$srcdir"/761aa1d15970fcb6aadb7d6152737fa9b2d4b0d0.patch # Fix OCCT 7.8.0
  patch -p1 -i ../fmt-11.patch # Fix build with fmt 11

  # fix 32bit builds
  case "${CARCH}" in
    powerpc) patch -R -Np1 -i "$srcdir"/vtk-9.3.1-xdmf3-32bit.patch
  esac

  patch -Np1 -i "$srcdir"/vtk-9.3.0-core-octree_node.txx.patch
  patch -Np1 -i "$srcdir"/vtk-9.3.0-java.patch
  patch -Np1 -i "$srcdir"/vtk-9.3.0-ThirdParty-gcc15.patch
  patch -Np1 -i "$srcdir"/vtk-9.3.0-ThrustPatches.patch
}

build() {
  # To help cmake find java
  export JAVA_HOME=/usr/lib/jvm/default
  # To set tcl lib path
  local _tkver=$(echo 'puts $tcl_version' | tclsh)

  _cmake_options=()
  case "${CARCH}" in
    powerpc) _cmake_options+=(
              -DVTKOSPRAY_ENABLE_DENOISER=OFF
	      -DVTKm_ENABLE_OPENMP=OFF
	      -DVTK_ENABLE_OSPRAY=OFF
              -DVTK_SMP_ENABLE_OPENMP=OFF
              -DVTK_WRAP_JAVA=OFF
	     )
             export CFLAGS+=' -fpermissive'
             export CXXFLAGS+=' -fpermissive'
	    ;;
    riscv64) _cmake_options+=(
              -DVTKOSPRAY_ENABLE_DENOISER=OFF
	      -DVTKm_ENABLE_OPENMP=OFF
	      -DVTK_ENABLE_OSPRAY=OFF
              -DVTK_SMP_ENABLE_OPENMP=ON
              -DVTK_WRAP_JAVA=OFF
	     )
	    ;;

    x86_64) _cmake_options+=(
              -DVTKOSPRAY_ENABLE_DENOISER=ON
              -DVTKm_ENABLE_OPENMP=ON
              -DVTK_SMP_ENABLE_OPENMP=ON
              -DVTK_WRAP_JAVA=ON
	    )
	    ;;
    powerpc64) _cmake_options+=(
	 -DVTKOSPRAY_ENABLE_DENOISER=OFF
         -DVTKm_ENABLE_OPENMP=ON
         -DVTK_ENABLE_OSPRAY=OFF
         -DVTK_SMP_ENABLE_OPENMP=ON
         -DVTK_WRAP_JAVA=OFF
         -DVTK_MODULE_ENABLE_VTK_IOPDAL=NO
       )
       CFLAGS='-O2 -pipe'
       CXXFLAGS='-O2 -pipe'
    ;;
    *) _cmake_options+=(
	 -DVTKOSPRAY_ENABLE_DENOISER=OFF
         -DVTKm_ENABLE_OPENMP=ON
         -DVTK_ENABLE_OSPRAY=OFF
         -DVTK_SMP_ENABLE_OPENMP=ON
         -DVTK_WRAP_JAVA=ON
       ) ;;
  esac


  cmake -B build -S ${pkgname^^}-${pkgver} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LICENSEDIR=share/licenses/vtk \
    -DCMAKE_SKIP_RPATH=OFF \
    -DCMAKE_SKIP_INSTALL_RPATH=ON \
    -DCMAKE_JAVA_COMPILE_FLAGS="-Xlint:-options" \
    -DBUILD_SHARED_LIBS=ON \
    -DVTK_BUILD_ALL_MODULES=ON \
    -DVTK_INSTALL_TCL_DIR=/usr/lib/tcl${_tkver}/vtk/ \
    -DVTK_LEGACY_REMOVE=ON \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DVTK_PYTHON_VERSION=3 \
    -DVTK_USE_MPI=ON \
    -DVTK_USE_TK=ON \
    -DVTK_VERSIONED_INSTALL=OFF \
    -DVTK_WRAP_PYTHON=ON \
    -DVTKm_ENABLE_HDF5_IO=ON \
    -DVTKm_ENABLE_MPI=ON \
    -DVTK_USE_EXTERNAL=ON \
    -DVTK_MODULE_USE_EXTERNAL_VTK_exprtk=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_ioss=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_pegtl=OFF \
    -DVTK_MODULE_ENABLE_VTK_DomainsMicroscopy=NO \
    -DVTK_MODULE_ENABLE_VTK_FiltersOpenTURNS=NO \
    -DVTK_MODULE_ENABLE_VTK_RenderingZSpace=NO \
    -DOpenGL_GL_PREFERENCE=LEGACY \
    -DVTK_IGNORE_CMAKE_CXX11_CHECKS=ON \
    -Wno-dev ${_cmake_options[@]}
#    -DFIDES_USE_EXTERNAL_RAPIDJSON=ON \
  cmake --build build
}

package() {
  DESTDIR="${pkgdir}" cmake --install build

  MYARCH=${CARCH}
  case "${CARCH}" in
    powerpc64le) MYARCH=ppc64le ;;
  esac

  case "${CARCH}" in
    powerpc64le|x86_64)
      # Move the vtk.jar to the arch-specific location…
      install -dv "${pkgdir}"/usr/share/java/vtk
      mv -v "${pkgdir}"/usr/lib/java/vtk.jar "${pkgdir}"/usr/share/java/vtk
      # …and the libs to the proper place
      mv "${pkgdir}"/usr/lib/java/vtk-Linux-${MYARCH}/*.so "${pkgdir}"/usr/lib/
      rmdir "${pkgdir}"/usr/lib/java/{vtk-Linux-${MYARCH}/,}
    ;;
  esac

  # byte-compile python modules since the CMake build does not do it
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  python -m compileall -o 0 -o 1 -o 2 --hardlink-dupes -s "${pkgdir}" "${pkgdir}"${site_packages}

  # Remove third party CMake patching for older versions than ours
  rm -rv "${pkgdir}"/usr/lib/cmake/vtk/patches/3.{1{3,6,9},20}
  # … as well as duplicate copies
  rm -rv "${pkgdir}"/usr/lib/cmake/vtk/vtkm/{Find*.cmake,cmake/{Find*.cmake,3.15}}
}
