我有一个C ++项目,其核心基本上是自包含的,但是有很多用户可能想要或可能不想编译的第三方代码接口。我们使用CMake构建代码,现在我正在尝试更好地组织代码。
我提出的解决方案是在顶部CMakeLists.txt文件中添加一组选项,用于确定是否应该定位依赖包。
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)
然后在子目录中的CMakeLists.txt文件中,会出现如下语句:
if(BAR_FOUND)
add_subdirectory(bar_interface)
endif(BAR_FOUND)
我并不特别喜欢这个解决方案,部分原因是因为它非常冗长,部分是因为我觉得应该有一些更标准化的方法来做到这一点。有人知道更好,更易维护的解决方案吗?
答案 0 :(得分:7)
查看以下标准CMake模块:
使用FeatureSummary的示例(来自手册):
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)