# 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>

pkgname=vtk
# May need bootstrapping on upgrades due to circular vtk <-> opencascade dependency
pkgver=9.4.1
pkgrel=2
pkgdesc="Software system for 3D computer graphics, image processing, and visualization"
#FIXME: powerpc fails to build
arch=(x86_64 powerpc64le powerpc64 riscv64)
url="https://www.vtk.org"
license=(BSD-3-Clause)
depends=(
  gcc-libs  # used by >400 libs
  glibc     # used by >400 libs
  onetbb    # used by 57 direct libs, 388 total libs
  double-conversion       # libvtkCommonCore.so (1 direct lib, 372 total libs)
  verdict                 # libvtkFiltersVerdict.so (1 direct lib, 192 total libs)
  fmt                     # libvtkFiltersGeneral.so etc. (5 direct libs, 189 total libs)
  libarchive              # libvtkCommonArchive.so (1 direct lib, 2 total libs)
  # dependencies of libvtkCommonDataModel.so and libvtkIOCore.so
  lz4                     # 1 direct lib, 172 total libs
  pugixml                 # 4 direct libs, 351 total libs
  xz                      # 1 direct lib, 172 total libs
  zlib                    # 5 direct libs, 204 total libs
  # dependencies of libvtkIOImage.so
  libjpeg-turbo           # 1 direct lib, 97 total libs
  libpng                  # 1 direct lib, 97 total libs
  libtiff                 # 1 direct lib, 97 total libs
  # common data libraries
  expat                   # 1 direct lib, 101 total libs
  jsoncpp                 # 7 direct libs, 26 total libs
  libxml2                 # 3 direct libs, 13 total libs
)
makedepends=(
  # build system
  cmake
  # bindings
  python
  python-matplotlib
  python-mpi4py
  # graphical toolkits
  qt5-base                # libvtkGUISupportQt.so etc. (5 direct libs, 6 total libs)
  qt5-declarative         # libvtkGUISupportQtQuick.so (1 direct lib, 1 total lib)
  qt5-tools
  tk                      # libvtkRenderingTk.so (1 direct lib, 1 total lib)
  # direct dependencies of "some" VTK libs/modules
  libx11                  # libvtkglad.so, libvtkRenderingUI.so, libvtkRenderingTk.so, libvtkRenderingOpenGL2.so (4 direct libs, 58 total libs)
  freetype2               # libvtkRenderingFreeType.so, libvtkRenderingFreeTypeFontConfig.so (2 direct libs, 55 total libs)
  libxcursor              # libvtkRenderingOpenGL2.so (1 direct lib, 51 total lib)
  hdf5                    # 16 direct libs, 39 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)
  adios2                  # libvtkfides.so, libvtkIOADIOS2.so (2 direct libs, 5 total libs)
  cgns                    # libvtkIOCGNSReader.so, libvtkioss.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)
  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)
  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)
  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
)
makedepends_powerpc=('java-environment=11')
makedepends_powerpc64=(openmp 'java-environment=17')
makedepends_powerpc64le=(openmp 'java-environment=17')
makedepends_riscv64=(openmp 'java-environment=17')
makedepends_x86_64=(anari-sdk openimagedenoise ospray 'java-environment=17')
# 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
  'qt5-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'
  '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'
  '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
        vtk-occt.patch
        fmt-11.patch
        netcdf-4.9.3.patch)
sha256sums=('c253b0c8d002aaf98871c6d0cb76afc4936c301b72358a08d5f3f72ef8bc4529'
            'df958eabc7dc4f5b33383ce0fb0f90a3ba202c1c2a24d3b5b9e7cfb1fb38b011'
            'c6345d09c219b5ca2efa6e7419f404a22715adb4d2ca1fd6ba34affd77cea23e'
            '87535578bbb0023ede506fd64afae95cdf4fb698c543f9735e6267730634afbc')

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 "$srcdir"/fmt-11.patch
  patch -Np1 -i "$srcdir"/netcdf-4.9.3.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)

  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
              -DVTK_MODULE_ENABLE_VTK_RenderingAnari:STRING=NO
            )
             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
              -DVTK_MODULE_ENABLE_VTK_RenderingAnari:STRING=NO
            )
           ;;

    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
         -DVTK_MODULE_ENABLE_VTK_RenderingAnari:STRING=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
         -DVTK_MODULE_ENABLE_VTK_RenderingAnari:STRING=NO
       ) ;;
  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_ENABLE_OPENMP=ON \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DVTK_PYTHON_VERSION=3 \
    -DVTK_QT_VERSION=5 \
    -DVTK_USE_MPI=ON \
    -DVTK_USE_TK=ON \
    -DVTK_VERSIONED_INSTALL=OFF \
    -DVTK_WRAP_JAVA=ON \
    -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_USE_EXTERNAL_VTK_token=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}}
}
