Including VTK9 built from source in a wider project

Due to constraints around our build process, I would like to include the VTK source code alongside our own, for our application build configured with cmake. With VTK8 and earlier, we have been using the cmake command ADD_SUBDIRECTORY to do this after setting the cmake properties for VTK we need.

However, with VTK9, I am finding a problematic difference between the cmake output produced when doing a standalone VTK build (i.e., configuring VTK independently with cmake and then creating an installation), and the cmake output produced when using ADD_SUBDIRECTORY.

In particular, when trying to link against VTK modules using FIND_PACKAGE(VTK, ...), ultimately a file VTK-targets.cmake is required, which has not been produced by the configuration. With the standalone build, cmake creates this file in the lib/cmake/vtk-9.2 directory.

Presumably I am missing a crucial piece of configuration when trying to use VTK within my existing build - please can someone offer insight into what that is? Thank you in advance.

Hi @thomas-wright

ADD_SUBDIRECTORY

Well, it is not supported by VTK. Actually, it never was, but it “just worked” before. @ben.boeckel made sure that it does not work anymore to avoid having to support yet another usecase for VTK.

FIND_PACKAGE is the way to go.

ultimately a file VTK-targets.cmake is required, which has not been produced by the configuration.

This sounds like the issue to fix. Just build and install VTK and you are good.

Best,

@mwestphal Thank you for your reply, at least that explains why I can’t get it to work!

Note that there is a way to vendor VTK which is the way that ParaView does it. add_subdirectory won’t work as that assumes it is the top-level project (I suppose we could detect that and error…). This involves scanning and building VTK from the parent directory (basically replacing VTK’s top-level CMakeLists.txt). The set of things you need to do evolves over time and doesn’t always have explicit docs. You can see what ParaView does when it builds VTK by setting VTK_-prefixed variables from PARAVIEW_-prefixed ones at least.

It is important to note that you can fully automate this using CMake’s ExternalProject_Add command. Developers just need to build your project and your project downloads, configures, and builds VTK (and all other dependencies).

A complex but very comprehensive example (with building a large number of external dependencies, setting up Python-wrapping, packaging, etc.) is 3D Slicer’s build system. See for example how it configures and builds VTK here.

1 Like

A different example (and simpler IMO) is the F3D superbuild, which is using an architecture very mush similar to the ParaView superbuild to build VTK and a few other dependencies:

1 Like