如何在cmake中使用COMPONENTS配置项目

时间:2019-02-15 04:10:48

标签: c++ cmake components

我想使用cmake创建一个项目项目,该项目的访问方式与Poco相似。我发现以Poco为例,比较拥挤且难以理解,因此我试图创建一个没有宏的最小版本,以便我能看到发生了什么。我在这里为此示例构建了一个存储库。

https://github.com/markeastwood82/nomnoms

这个,以及下面写的,是我对“现代CMake”进行几天的阅读/学习后,如何解决这个问题的最佳猜测,除了它不能完全起作用。本质上,我有一个包含组件nomsfruit的库veg,我想从应用程序munch动态链接。我可以安装noms库,但无法通过munch找到它。有人可以帮我把这东西放在一起吗?

这两个项目的结构如下:

noms
|---- CMakeLists.txt
+---- fruit
|     |---- CMakeLists.txt
|     |---- fruit-config.cmake.in
|     +---- src
|     |     |----apple.cpp
|     |
|     +---- include/noms/fruit
|           |----apple.h
|
+---- veg
      |---- CMakeLists.txt
      |---- veg-config.cmake.in
      +---- src
      |     |---- asparagus.cpp
      |
      +---- include/noms/veg
            |---- asparagus.h
munch
|---- CmakeLists.txt
+---- src
      |---- main.cpp

文件noms/CMakeLists.txt包含以下内容。

cmake_minimum_required(VERSION 3.0)
set(project noms)
set(version 1.0.0)

project(${project})

add_subdirectory(fruit)
add_subdirectory(veg)

# UPDATE: implement advice from Tsyvarev

configure_file(${project}-config.cmake
  "${CMAKE_BINARY_DIR}/${project}-config.cmake"
  @ONLY
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  "${CMAKE_BINARY_DIR}/${project}-config-version.cmake"
  VERSION ${version}
  COMPATIBILITY AnyNewerVersion
)

install(
  FILES
    "${CMAKE_BINARY_DIR}/${project}-config.cmake"
  DESTINATION lib/cmake/${project}
)

文件noms/fruit/CMakeLists.txt(与noms/veg/CMakeLists.txt几乎相同)是

set(component fruit)

add_library(${component} SHARED src/apple.cpp)

# namespaced alias
add_library(${project}::${component} ALIAS ${component})

target_include_directories(${component}
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

install(TARGETS ${component} EXPORT ${component}-targets
  COMPONENT ${component}
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

install(EXPORT ${component}-targets
  FILE "${project}-${component}-targets.cmake"
  NAMESPACE ${project}::
  DESTINATION lib/cmake/${project}
  COMPONENT ${component}
)

# This seems like a kludge, but it does place the file in the correct location
# on my machine (Ubuntu 18.04). Idea taken from Poco
configure_file("${component}-config.cmake.in"
  "${CMAKE_BINARY_DIR}/${project}-${component}-config.cmake"
  @ONLY
)

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
  "${CMAKE_BINARY_DIR}/${project}-${component}-config-version.cmake"
  VERSION ${version}
  COMPATIBILITY AnyNewerVersion
)

install(
  FILES
    "${CMAKE_BINARY_DIR}/${project}-${component}-config.cmake"
    "${CMAKE_BINARY_DIR}/${project}-${component}-config-version.cmake"
  DESTINATION lib/cmake/${project}
  COMPONENT ${component}
)

# DESTINATION will be automatically prefixed by ${CMAKE_INSTALL_PREFIX}
install(
  DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include
  COMPONENT ${component}
  DESTINATION ${CMAKE_INSTALL_PREFIX}
)

最后,noms/fruit/fruit-config.cmake.in的文件(与noms/veg/veg-config.cmake几乎相同)是

include(${CMAKE_CURRENT_LIST_DIR}/noms-fruit-config-version.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/noms-fruit-config-targets.cmake)

对于项目munch,文件munch/CMakeLists.txt很简单

cmake_minimum_required(VERSION 3.0)
set(project munch)

find_package(noms REQUIRED COMPONENTS fruit)

add_executable(${project} src/main.cpp)

target_include_directories(${project}
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:$include>
)

target_link_libraries(${project}
  PUBLIC
    noms::fruit
)

安装noms后,我尝试构建munch,但是cmake找不到noms-config.cmake文件。我不知道它应该包含什么,也不知道应该在哪里放置它。我也不确定在configure_file中使用noms/<food-type>/CMakeLists.txt的方法是否合适,因为在此示例中似乎不需要使用此方法

https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf

我的问题与此处提出的问题基本相同,该问题目前尚未得到答案(接受的答案不正确,正如我在该线程中所评论的)。实际上,公平地说,由于该线程已经使用了6年,因此在编写本文时可能是正确的,但似乎并未使用现在推荐的xxx-config.cmake方法。

How to export libraries with components in CMAKE?

请随时向我提起我犯的任何错误,或者可以做得更好的任何事情。谢谢

*更新*

除了上面更新的noms/CMakeLists.txt文件之外,我还根据下面@Tsyvarev的建议实施了以下noms/noms-config.cmake文件...

foreach(component ${noms_FIND_COMPONENTS})
    include(${CMAKE_CURRENT_LIST_DIR}/noms-${component}-config.cmake)
endforeach()

现在生成的代码有效,谢谢!


1 个答案:

答案 0 :(得分:1)

CMake不会自动处理COMPONENTS列表。它将任务留给noms-config.cmake脚本,当一个命令发出后,脚本会被搜索并执行

find_package(noms COMPONENTS fruit veg)

摘自find_package文档:

  

在Config模式下,find_package自动处理REQUIREDQUIET[version]选项,但将其留在程序包配置文件中以对组件有意义的方式处理组件包装。

在此配置文件中,您可以从COMPONENTS变量中提取请求的组件列表(通过find_package()的{​​{1}}选项),并执行适当的操作。例如:

noms-config.cmake

noms_FIND_COMPONENTS

这意味着,您将把脚本foreach(component ${noms_FIND_COMPONENTS}) # For requested component, execute its "config" script include(${CMAKE_CURRENT_LIST_DIR}/${component}-config.cmake) endforeach() 安装到noms-config.cmake子目录中,靠近项目中已经安装的其他lib/cmake/noms脚本。