How to handle cmake COMPONENTS name change between 8.2 and 9.

How about this (this is untested). CommonCore will be always there so if find_package finds it you have VTK 9+, otherwise test for vtkCommonCore.
This gives you the VTK Version.
Then based on the version re-run find_package.

find_package(VTK  COMPONENTS CommonCore QUIET)
if (NOT VTK_FOUND)
  find_package(VTK  COMPONENTS vtkCommonCore QUIET)
  if (NOT VTK_FOUND)
    message("Skipping ${PROJECT_NAME}: ${VTK_NOT_FOUND_MESSAGE}")
    return ()
  endif()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
    find_package(VTK COMPONENTS 
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersSources
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2
  )
else()
  # new system
  find_package(VTK COMPONENTS
      CommonColor
      CommonCore
      CommonDataModel
      FiltersSources
      IOImage
      RenderingCore
      # These modules are suggested since they implement an existing module.
      # Uncomment those you need.
      InteractionStyle  # implements VTK::RenderingCore
      # RenderingFreeType # implements VTK::RenderingCore
      RenderingOpenGL2  # implements VTK::RenderingCore
  )
endif()
message(STATUS "VTK Libraries: ${VTK_LIBRARIES}")

I hope this helps. You may have to manually determine what other extra VTK 9 modules are needed.

1 Like