How to organize a hierarchy of cmake files with several optional dependencies

I have a C ++ project with a kernel that is basically self-sufficient, but with a lot of interfaces for third-party codes that the user may or may not want to compile. We create the code using CMake, and now I'm trying to arrange the code a little better.

The solution I came up with is to add a set of parameters to the top CMakeLists.txt file that determine whether the dependent package should be installed or not.

option(WITH_FOO "Compile the interface to Foo, if found" ON) option(REQUIRE_FOO "Require that the Foo interface to be compiled" OFF) option(WITH_BAR "Compile the interface to Bar, if found" ON) option(REQUIRE_BAR "Require that the Bar interface to be compiled" OFF) ... if(WITH_FOO) if(REQUIRE_FOO) find_package(Foo REQUIRED) else(REQUIRE_FOO) find_package(Foo) endif(REQUIRE_FOO) else(WITH_FOO) set(FOO_FOUND FALSE) endif(WITH_FOO) if(WITH_BAR) if(REQUIRE_BAR) find_package(Bar REQUIRED) else(REQUIRE_BAR) find_package(Bar) endif(REQUIRE_BAR) else(WITH_BAR) set(BAR_FOUND FALSE) endif(WITH_BAR) 

Then, in the CMakeLists.txt files in subdirectories, expressions such as:

 if(BAR_FOUND) add_subdirectory(bar_interface) endif(BAR_FOUND) 

I don’t really like this solution, partly because it is very verbose and partly because I feel that there needs to be a more standardized way to do this. Does anyone know of a better, more maintainable solution?

+7
source share
1 answer

Take a look at the following standard CMake modules:

Examples of using FeatureSummary (from the manual):

 option(WITH_FOO "Help for foo" ON) add_feature_info(Foo WITH_FOO "The Foo feature provides very cool stuff.") find_package(LibXml2) set_package_properties(LibXml2 PROPERTIES DESCRIPTION "A XML processing library." URL "http://xmlsoft.org/") set_package_properties(LibXml2 PROPERTIES TYPE RECOMMENDED PURPOSE "Enables HTML-import in MyWordProcessor") set_package_properties(LibXml2 PROPERTIES TYPE OPTIONAL PURPOSE "Enables odt-export in MyWordProcessor") feature_summary(WHAT ALL) 
+7
source

All Articles