# Maintainer: Bruno Pagani <archange at archlinux dot org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>
# 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>

pkgname=vtk
# May need bootstrapping on upgrades due to circular vtk <-> opencascade dependency
pkgver=9.5.2
pkgrel=5
pkgdesc="Software system for 3D computer graphics, image processing, and visualization"
arch=(x86_64 aarch64)
url="https://www.vtk.org"
license=(BSD-3-Clause)
depends=(
  gcc-libs  # used by >400 libs
  glibc     # used by >400 libs
  onetbb    # used by 43 direct libs, 381 total libs
  double-conversion       # libvtkCommonCore.so (1 direct lib, 380 total libs)
  verdict                 # libvtkFiltersVerdict.so (1 direct lib, 194 total libs)
  fmt                     # 8 direct libs, 191 total libs
  libarchive              # libvtkCommonArchive.so (1 direct lib, 2 total libs)
  # dependencies of libvtkCommonDataModel.so and libvtkIOCore.so
  lz4                     # 1 direct lib, 178 total libs
  pugixml                 # 4 direct libs, 359 total libs
  xz                      # 1 direct lib, 178 total libs
  zlib                    # 5 direct libs, 206 total libs
  # dependencies of libvtkIOImage.so
  libjpeg-turbo           # 1 direct lib, 95 total libs
  libpng                  # 1 direct lib, 95 total libs
  libtiff                 # 1 direct lib, 95 total libs
  # common data libraries
  expat                   # 1 direct lib, 103 total libs
  jsoncpp                 # 7 direct libs, 26 total libs
  libxml2                 # 3 direct libs, 13 total libs
)
makedepends=(
  # build system
  cmake
  ninja
  # bindings
  java-environment=11
  python
  python-matplotlib
  python-mpi4py
  # graphical toolkits
  qt6-base                # libvtkGUISupportQt.so etc. (5 direct libs, 6 total libs)
  qt6-declarative         # libvtkGUISupportQtQuick.so (1 direct lib, 1 total lib)
  qt6-tools
  tk                      # libvtkRenderingTk.so (1 direct lib, 1 total lib)
  # direct dependencies of "some" VTK libs/modules
  freetype2               # libvtkRenderingFreeType.so, libvtkRenderingFreeTypeFontConfig.so (2 direct libs, 87 total libs)
  openmpi                 # 13 direct libs, 67 total libs
  libx11                  # libvtkRenderingTk.so, libvtkRenderingOpenGL2.so, libvtkglad.so, libvtkRenderingUI.so (4 direct libs, 54 total libs)
  libxcursor              # libvtkRenderingOpenGL2.so (1 direct lib, 45 total libs)
  hdf5                    # 14 direct libs, 33 total libs
  netcdf                  # libvtkIOParallelNetCDF.so, libvtkIONetCDF.so, libvtkexodusII.so, libvtkIOMINC.so, libvtkioss.so (5 direct libs, 14 total libs)
  sqlite                  # libvtkIOSQL.so (1 direct lib, 10 total libs)
  viskores                # 6 direct libs, 10 total libs
  adios2                  # libvtkfides.so, libvtkIOADIOS2.so (2 direct libs, 5 total libs)
  cgns                    # libvtkIOCGNSReader.so, libvtkioss.so (2 direct libs, 5 total libs)
  paraview-catalyst       # libvtkioss.so, libvtkIOCatalystConduit.so (2 direct libs, 5 total libs)
  gdal                    # libvtkGeovisGDAL.so, libvtkIOGDAL.so (2 direct libs, 4 total libs)
  gl2ps                   # libvtkRenderingGL2PSOpenGL2.so, libvtkIOExportGL2PS.so (2 direct libs, 4 total libs)
  proj                    # libvtkGeovisCore.so, libvtkIOCesium3DTiles.so (2 direct libs, 4 total libs)
  alembic                 # libvtkIOAlembic.so (1 direct lib, 2 total libs)
  anari-sdk               # libvtkRenderingAnari.so (1 direct lib, 2 total libs)
  ffmpeg                  # libvtkIOFFMPEG.so (1 direct lib, 2 total libs)
  imath                   # libvtkIOOpenVDB.so (1 direct lib, 2 total libs)
  libharu                 # libvtkIOExportPDF.so (1 direct lib, 2 total libs)
  liblas                  # libvtkIOLAS.so (1 direct lib, 2 total libs)
  libogg                  # libvtkIOOggTheora.so (1 direct lib, 2 total libs)
  libtheora               # libvtkIOOggTheora.so (1 direct lib, 2 total libs)
  mariadb-libs            # libvtkIOMySQL.so (1 direct lib, 2 total libs)
  opencascade             # libvtkIOOCCT.so (1 direct lib, 2 total libs)
  openimagedenoise        # libvtkRenderingRayTracing.so (1 direct lib, 2 total libs)
  openvdb                 # libvtkIOOpenVDB.so (1 direct lib, 2 total libs)
  openvr                  # libvtkRenderingOpenVR.so (1 direct lib, 2 total libs)
  openxr                  # libvtkRenderingOpenXR.so (1 direct lib, 2 total libs)
  ospray                  # libvtkRenderingRayTracing.so (1 direct lib, 2 total libs)
  pdal                    # libvtkIOPDAL.so (1 direct lib, 2 total libs)
  postgresql-libs         # libvtkIOPostgreSQL.so (1 direct lib, 2 total libs)
  unixodbc                # libvtkIOODBC.so (1 direct lib, 2 total libs)
  fontconfig              # libvtkRenderingFreeTypeFontConfig.so (1 direct lib, 1 total lib)
  # header-only libraries (all except two are only private or test dependencies
  # in all modules that use them)
  boost
  cli11
  eigen
  fast_float      # interface dependency of the VTK::CommonCore module
  nlohmann-json   # interface dependency of the VTK::CommonCore module
  utf8cpp
)
# pegtl: https://gitlab.kitware.com/vtk/vtk/-/issues/18151
# exprtk, ioss: not packaged
optdepends=(
  # additional tools not listed in makedepends
  'graphviz: drawing tools'
  'libglvnd: OpenGL rendering'  # checked at runtime rather than compile-time
  # bindings
  'java-runtime=11: java bindings'
  'python: python bindings'
  'python-matplotlib: for Matplotlib rendering'
  'python-mpi4py: OpenMPI python support'
  # graphical toolkits
  'qt6-declarative: QML plugin'
  'tk: tcl bindings'
  # direct dependencies of "some" VTK libs/modules
  'libx11: rendering'
  'libxcursor: rendering'
  'fontconfig: rendering fonts with fontconfig support'
  'freetype2: rendering fonts'
  'gl2ps: rendering to PostScript, PDF, and SVG'
  'anari-sdk: ANARI rendering module'
  'openvr: rendering for virtual reality'
  'openxr: rendering for virtual and augmented reality'
  'openimagedenoise: rendering with raytracing support'
  'ospray: rendering with raytracing support'
  'openmpi: OpenMPI support'
  'viskores: accelerators support'
  'adios2: IO module'
  'alembic: IO module'
  'cgns: IO module'
  'ffmpeg: IO module'
  'gdal: IO module'
  'hdf5: IO module'
  'imath: IO module'
  'libharu: IO module'
  'liblas: IO module'
  'libogg: IO module'
  'libtheora: IO module'
  'mariadb-libs: IO module'
  'netcdf: IO module'
  'opencascade: IO module'
  'openvdb: IO module'
  'paraview-catalyst: IO module'
  'pdal: IO module'
  'postgresql-libs: IO module'
  'proj: IO module'
  'sqlite: IO module'
  'unixodbc: IO module'
  # building downstream projects
  'fast_float: for building downstream projects'
  'nlohmann-json: for building downstream projects'
  'openmp: OpenMP support for downstream projects built with Clang'
)
options=(staticlibs)
source=(
  $url/files/release/${pkgver%.*}/VTK-$pkgver.tar.gz
  fmt-12.patch
)
sha256sums=('cee64b98d270ff7302daf1ef13458dff5d5ac1ecb45d47723835f7f7d562c989'
            '9c9adccbe548ec28fab5799576520fa583511a5b4b4fd4512636022b239b47c3')
b2sums=('6e7dab56c4f48d066ca44637f8839e9abc973d2831381ef5aec860aa1daa49ac9beca223e439c12dda2c33056b45395043f2edc54f55f9d169719b6c96499f40'
        'd31e69469782fac0507536c5aeb2ca08944eefec47bb989a85adcc701ea62c32b1ae36f282ccad4c665bcd50dcfb88b81237ce7536c85f4603ebe5d8e8c393cf')

prepare() {
  cd ${pkgname^^}-${pkgver}
  _fast_float_version=$(pacman -Q fast_float | sed -e 's/.* //; s/-.*//g')
  sed -i "s|7.0.0|${_fast_float_version}|" ThirdParty/fast_float/CMakeLists.txt
# Use cmake's FindHDF5
  rm CMake/patches/99/FindHDF5.cmake
  sed -e '/FindHDF5/d' -i CMake/vtkInstallCMakePackage.cmake
# Fix build with fmt 12
  patch -d ThirdParty/ioss/vtkioss -p6 < ../fmt-12.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 -B build -S ${pkgname^^}-${pkgver} -G Ninja \
    -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 \
    -DBUILD_SHARED_LIBS=ON \
    -DVTK_BUILD_ALL_MODULES=ON \
    -DVTK_ENABLE_CATALYST=ON \
    -DVTK_INSTALL_TCL_DIR=/usr/lib/tcl${_tkver}/vtk/ \
    -DVTK_LEGACY_REMOVE=ON \
    -DVTK_SMP_ENABLE_OPENMP=ON \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DVTK_PYTHON_VERSION=3 \
    -DVTK_QT_VERSION=6 \
    -DVTK_USE_MPI=ON \
    -DVTK_USE_TK=ON \
    -DVTK_VERSIONED_INSTALL=OFF \
    -DVTK_WRAP_JAVA=ON \
    -DVTK_WRAP_PYTHON=ON \
    -DVTKOSPRAY_ENABLE_DENOISER=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_USE_EXTERNAL_VTK_scn=OFF \
    -DVTK_MODULE_USE_EXTERNAL_VTK_token=OFF \
    -DVTK_MODULE_ENABLE_VTK_DomainsMicroscopy=NO \
    -DVTK_MODULE_ENABLE_VTK_FiltersOpenTURNS=NO \
    -DVTK_MODULE_ENABLE_VTK_IOCatalystConduit=YES \
    -DHDF5_NO_FIND_PACKAGE_CONFIG_FILE=ON \
    -DHDF5_C_COMPILER_EXECUTABLE=h5hlcc \
    -DHDF5_CXX_COMPILER_EXECUTABLE=h5hlc++ \
    -Wno-dev
#    -DFIDES_USE_EXTERNAL_RAPIDJSON=ON \
  cmake --build build
}

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

  # 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-${CARCH}/*.so "${pkgdir}"/usr/lib/
  rmdir "${pkgdir}"/usr/lib/java/{vtk-Linux-${CARCH}/,}

  # 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.*
}
