Modernizing VTK: How CMake Transformed a Complex Build System

September 30, 2024
CMake and VTK logos

As software systems are more complex, the need for an efficient and flexible build system becomes critical. Kitware has extensive experience managing large software build and package systems, which is showcased through our work with the Visualization Toolkit (VTK). VTK is a powerful library for scientific data processing and visualization that originally adopted CMake as a build system in 2000. Since then, VTK has seen several iterations of improvements to the CMake code used to build it. However, in 2019, it underwent a major overhaul that resulted in a more efficient, “modern” build system.

Why VTK’s CMake Build System Matters

VTK is a substantial project, comprising 5.8 million lines of C++ and 150,000 lines of CMake. Creating an efficient build system was essential given its size and complexity, including its extensive libraries and interdependencies. VTK’s previous build system, which utilized pre-CMake 3.0 patterns, had become outdated, leading to inefficiencies during configuration, long build times, and challenges with managing module dependencies.

The primary goal of the new system was to create a more modular, scalable, and flexible build process that allowed for turning on or off various features like MPI support or language wrapping for Python. The new module system ensures that each module knows what it depends on and can be configured accordingly, allowing users to adjust builds based on project-specific needs.

By redesigning the VTK build system with CMake’s modern, target-based approach, Kitware dramatically reduced the configuration time from over a minute to under 10 seconds. This improvement is particularly relevant for projects where frequent builds are necessary and time savings can quickly add up.

Hear from Bill Hoffman, chief technical officer at Kitware and the original author and lead architect of CMake, to learn more about the importance of the build system target model.

Requirements Gathering and Design

By 2017, CMake’s target-based usage requirements were well-established, and VTK adopted these as the foundation for its new module system. However, due to VTK’s complexity, additional features were needed beyond the standard “add subdirectories and link” approach. One key challenge was that VTK’s build is highly customizable, with many modules and features that can be turned on or off. The build system was designed with a two-phase process to handle this flexibility.

In the first phase, the build system identifies which modules are available, their interdependencies, and what needs to be built based on the selected configuration. Previously, VTK relied on a file module.cmake that could contain complex logic, which became increasingly difficult to manage. The new system replaced this with a cleaner, more reliable vtk.module file, which is purely declarative—meaning it simply lists key information like:

  • Module dependencies (e.g. public, private, optional, test, or optional test)
  • Module description
  • Language wrapper instructions (i.e. whether to ignore the module or not)
  • System prerequisites (e.g. only works on Windows)
  • Group memberships

This structure helps ensure that the build process remains organized and accurate. During the second phase, the system scans the metadata from the vtk.module files to decide which modules need to be built. Each module is categorized into one of four states:

  • Required (ON): Must be built
  • Wanted (WANT): Should be built, but can be skipped if it depends on a rejected module
  • Not-required (DONT_WANT): Won’t be built unless a needed module depends on it
  • Rejected (OFF): Must not be built

Additionally, a DEFAULT setting determines how modules are treated based on group membership and default preferences. This flexibility allows for fine-tuned control, ensuring that even if a user disables a feature like MPI, stray dependencies won’t mistakenly include it. This structured approach helps users maintain full control over their builds, ensuring consistency with their configuration options.

Key Takeaways for Enhancing Your Project

Although VTK’s build system includes custom features, many of its lessons can be applied to other large software systems. These practices not only streamline the build process but also ensure that your project remains adaptable as it grows. While in this specific example, VTK has extra functionality built on CMake’s usage requirements, projects wishing to learn from it can implement only the parts needed. For a full reference to VTK’s CMake build system see this documentation:

https://docs.vtk.org/en/latest/api/cmake/ModuleSystem.html

If you’re looking to modernize or optimize your build system, Kitware has the expertise to help. Contact us today to learn how we can partner with you to design a tailored solution that meets your project’s specific needs, ensuring long-term success and efficiency.

Leave a Reply