CMake find_package 用于同一项目中的另一个库

CMake find_package for another library in same project

我想制作一个构建器项目,检查子模块并将它们作为一个组构建,我想一次性构建它们。

builder
  submod1
  submod2 #depends on submod1 
  submod3 #depends on submod2

为了测试,我下载了 ZeroMQ 和 cppzmq 作为子模块,并使用 cppzmq/demo 构建它们以确认它们是可链接的。我选择它们是因为 cppzmq 将 libzmq 检查为目标,但演示仅链接到 find_package.

cmake_minimum_required(VERSION 3.10)
project(ZMQ_builder)
option(BUILD_TESTS "No tests" OFF)
option(CPPZMQ_BUILD_TESTS "No tests" OFF)
option(BUILD_STATIC "ninja can only build static or shared" OFF)
option(BUILD_SHARED "ninja can only build static or shared" ON)
add_subdirectory(libzmq)
set_property(TARGET libzmq PROPERTY CXX_STANDARD 17)
if (NOT TARGET libzmq AND NOT TARGET libzmq-static)
    message(WARNING "libzmq and libzmq-static don't exist after being created")
endif()
if(NOT TARGET ZeroMQ)
    message(status "ZeroMQ needs an alias") #prints this
    add_library(ZeroMQ ALIAS libzmq)
endif()
if(NOT TARGET ZeroMQ)
    message(WARNING "ZeroMQ Target Still doesn't exist")
endif()
if(NOT ZeroMQ_FOUND)
    message(WARNING "ZeroMQ marked as not found") #prints this warning
    find_package(ZeroMQ)
    #set(ZeroMQ_FOUND true)
endif()
add_subdirectory(cppzmq)
find_package(cppzmq) #dies here
add_subdirectory(cppzmq/demo)

输出

ZeroMQ marked as not found .../builder\CMakeLists.txt   28
CMake Warning at ...\builder\CMakeLists.txt:29 (find_package):
  By not providing "FindZeroMQ.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "ZeroMQ", but
  CMake did not find one.

  Could not find a package configuration file provided by "ZeroMQ" with any
  of the following names:

    ZeroMQConfig.cmake
    zeromq-config.cmake

  Add the installation prefix of "ZeroMQ" to CMAKE_PREFIX_PATH or set
  "ZeroMQ_DIR" to a directory containing one of the above files.  If "ZeroMQ"
  provides a separate development package or SDK, be sure it has been
  installed.    ...\builder\CMakeLists.txt  29  
ZeroMQ was NOT found!   .../builder/build/cppzmq/cppzmqConfig.cmake 53  

所以 find 调用忽略了 ZeroMQ 别名目标 (add_library(ZeroMQ ALIAS libzmq)),并且尽管找到了“../builder/build/cppzmq/cppzmqConfig.cmake”也错过了“.../builder/build/libzmq/ZeroMQConfig.cmake”。 =15=]

我不明白为什么 find_package 会忽略构建中已有的 ZeroMQ 目标?以及为什么构建可以找到 cppzmqConfig.cmake 而不是 ZeroMQConfig.cmake 尽管两者都存在?

find_package() 实际上并不查看全局目标列表,它有自己的目标列表,只会阻止其他查找调用重新获取

而不是在 Find<>.cmake 模块中为每个 class

创建别名目标调用 find_package_handle_standard_args

所以对于这种情况 Findcppzmq.cmake

include(FindPackageHandleStandardArgs)
find_package(ZeroMQ REQUIRED)
if(TARGET cppzmq)
    find_package_handle_standard_args(cppzmq
    REQUIRED_VARS cppzmq_BINARY_DIR)
endif()

和一个FindZeroMQ.cmake

include(FindPackageHandleStandardArgs)
if(TARGET ZeroMQ)
    find_package_handle_standard_args(ZeroMQ
    REQUIRED_VARS ZeroMQ_BINARY_DIR)
endif()

或者,如果构建器项目中的所有存储库都是独立的,您可以改为使用 ExternalProject_Add,以便在构建时按顺序生成项目。制作一个有效的包。

cmake_minimum_required(VERSION 3.16)
project(ExternalBuilder)
include(ExternalProject)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/install)
set(CMAKE_MODULE_PATH ${CMAKE_INSTALL_PREFIX})
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
ExternalProject_Add(
    ZeroMQ
    GIT_REPOSITORY https://github.com/zeromq/libzmq.git
    GIT_TAG v4.3.2
    CMAKE_ARGS -DCMAKE_MODULE_PATH:PATH=${CMAKE_INSTALL_PREFIX} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} -DBUILD_TESTS=OFF -DBUILD_STATIC=OFF
)
ExternalProject_Add(
    cppzmq
    DEPENDS ZeroMQ
    GIT_REPOSITORY https://github.com/zeromq/cppzmq.git
    GIT_TAG v4.7.1
    CMAKE_ARGS -DCMAKE_MODULE_PATH:PATH=${CMAKE_INSTALL_PREFIX} -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} -DCPPZMQ_BUILD_TESTS=OFF
)
ExternalProject_Add(
    zmqProject
    DEPENDS ZeroMQ
    ...
)