How and when to make use of the cmake 3.18 option Boost_NO_BOOST_CMAKE?

Viewed 482

Current situation

The Cmake 3.18 official documentation states:

If Boost was built using the boost-cmake project or from Boost 1.70.0 on it provides a package configuration file for use with find_package’s config mode. This module looks for the package configuration file called BoostConfig.cmake or boost-config.cmake and stores the result in CACHE entry “Boost_DIR”. If found, the package configuration file is loaded and this module returns with no further action. See documentation of the Boost CMake package configuration for details on what it provides.

Set Boost_NO_BOOST_CMAKE to ON, to disable the search for boost-cmake.

Problem description

It may make sense for experts in cmake, but as this explanation lacks broader context explanation, it does not really help the cmake beginner.

Question

Could someone expand on this documentation ? For example, why to set Boost_NO_CMAKE to ON, when the default behavior could be problematic, and how to set the option for best practice ?

Basic answer

For example, there are some basic explanations here on the fact that Boost 1.70 comes with a new way of using Boost with CMake, that is making use of the Boost BoostConfig.cmake rather than the one coming from the CMake distribution.

Old way:

set(Boost_NO_BOOST_CMAKE ON)

find_package(Boost REQUIRED COMPONENTS thread timer)

include_directories(${Boost_INCLUDE_DIRS}
    ...)

link_directories(${Boost_LIBRARY_DIRS}
    ...)

target_link_libraries(...
    ${Boost_LIBRARIES}
    ...)

New way:

set(Boost_NO_BOOST_CMAKE OFF)

find_package(Boost REQUIRED COMPONENTS thread timer)

target_link_libraries(...
    Boost::thread
    Boost::timer
    ...)

Expanding on this information (for example how to make the changes when we don't have control of the code projects, or how to ensure retro-compatibility) would definitely be useful for non-experts in cmake.

1 Answers

When Boost wasn't supporting CMake officially, the latter had its own FindBoost.cmake module. With every Boost release, the CMake team ought to fix it for the latest Boost version and some other things, so the updated module get available only w/ next CMake release. The shipped module used in Module Mode of the find_package dedicated to packages w/o full-functional CMake support.

Since (relatively) recently, Boost has its official CMake support providing the BoostConfig.cmake module used by find_package in Config Mode. However, depending on CMake settings, the used find_package signature and Boost version(s) installed the find result may vary.

The Boost_NO_BOOST_CMAKE prevents the FindBoost.cmake module from use of BoostConfig.cmake. Meaning that whatever Boost version installed and found, even if it has the "native" CMake support -- FindBoost.cmake should ignore it (i.e., the module, not the found result).

Why? Because even if the CMake team tries to keep its FindBoost.cmake backward compatible w/ older CMake/Boost releases and synchronized w/ the modern Boost at the same time there is a possibility to face with a difference in their behavior. To give a user (who cares about compatibility) the full control over what and how to find (via FindBoost.cmake) the mentioned variable has added.

Related