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?
Joel
source share