Table Of Contents

Previous topic

Adding Regression Tests

Next topic

Building a Binary Installer

This Page

Modularizing a Library with CMake

Warning

Don’t Do This! This doc is a placeholder.

Boost’s CMake-based build system supports the notion of “modular” libraries, which are libraries that are contained entirely within a single directory structure. Since modular libraries are self-contained, it is easier to bring in libraries of different versions and select specific subsets of libraries. Additionally, modular libraries explicitly declare their dependencies on other libraries (or “modules”), making it possible to build and install coherent subsets of Boost. For example, the [wiki:CMakeBinaryInstaller binary installer] for Windows allows one to turn on or off installation of each modular library.

Eventually, we hope that most of Boost’s libraries will be modular, to make it easier for users to install the subset of Boost that they are interested in. Many “core” libraries, on which most users and many other libraries depend, may remain in the core Boost distribution and will not be modularized. Even then, modularizing Boost is an evolutionary process, and it is best to work on modularizing libraries on which no other libraries depend (first) and then libraries on which other modularized libraries depend, moving from the more peripheral libraries (that no other libraries depend on) toward the core libraries.

Layout of a modular library

A modular library has a similar layout to non-modular libraries. The main difference is in the handling of include files, which are stored within the library’s directory in libs/libname/include rather than in the main “boost” include directory. A modular library will typically have the following subdirectories:

libs/libname - Main library directory
  include/   - Library headers. Since most Boost headers go into boost/, the actual library headers will be in the subdirectory include/boost (or its subdirectoiries)
  src/       - Source files for compiled library binaries (if any)
  test/      - Regression tests
  example/   - Example programs, libraries, and applications
  doc/       - Documentation

Throughout this document, we will use the Filesystem library as an example of a modular library. Please refer to the contents of libs/filesystem to see a fully-working modular library’s description.

Informing CMake that the library is modular

The CMake build system needs to know that the layout of the Boost library follows the rules of a modular library, which also instructs it to add the appropriate include paths when compiling itself and any of its dependencies. To label the library as modular, edit the CMakeLists.txt file contained in the library’s subdirectory (e.g., libs/filesystem/CMakeLists.txt, and add the argument MODULAR to the use of boost_library_project). After this change, Filesystem library’s CMakeLists.txt looks like this:

boost_library_project(
  Filesystem
  SRCDIRS src
  TESTDIRS test
  MODULAR
  DESCRIPTION "Provides portable facilities to query and manipulate paths, files, and directories."
  AUTHORS "Beman Dawes <bdawes -at- acm.org>"
  )

If the library you’re modularizing does not have DESCRIPTION, AUTHORS, or MAINTAINERS arguments, please add them! Short library descriptions are available at http://www.boost.org/doc/ along with author information; additional maintainer information can be found in http://svn.boost.org/svn/boost/trunk/libs/maintainers.txt.

Library dependencies

Each modular library must declare the libraries on which it depends. This declaration is provided by the file module.cmake within the library’s directory, and uses the boost_modular command to explicitly declare dependencies via its DEPENDS argument. The contents on the Filesystem library’s libs/filesystem/module.cmake follow:

boost_module(Filesystem DEPENDS system)

The first argument to boost_module is the name of the library we’re description. The arguments following DEPENDS (there may be more than one!) are the names of the libraries on which this library depends. Those libraries may or may not be modular yet: it does not matter. Thus, the Filesystem library depends on the System library. If the System library were not available for some reason (say, the user forgot to include it in the subset of Boost she downloaded), the Filesystem library would not attempt to build.

Testing the modular library

Once a library has been modularized, it is important to build the library and all of the regression tests, including the regression tests for other libraries (that might depend on the modularized library). Follow the instructors for [wiki:CMakeTesting building and running the regression tests]. Most of the failures that will crop up from this exercise will come in the form of “include file not found” messages due to missing dependency information. When this happens, add the appropriate dependencies to module.cmake and try again. The result is well worth it!