CMake:如何构建外部项目并包含其目标

3htmauhk  于 2023-06-29  发布在  其他
关注(0)|答案(7)|浏览(148)

我有一个项目A,它将一个静态库导出为目标:

install(TARGETS alib DESTINATION lib EXPORT project_a-targets)
install(EXPORT project_a-targets DESTINATION lib/alib)

现在我想使用项目A作为项目B的外部项目,并包含其构建目标:

ExternalProject_Add(project_a
  URL ...project_a.tar.gz
  PREFIX ${CMAKE_CURRENT_BINARY_DIR}/project_a
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

include(${CMAKE_CURRENT_BINARY_DIR}/lib/project_a/project_a-targets.cmake)

问题是,当运行项目B的CMakeLists时,包含文件还不存在。
是否有一种方法可以使include依赖于正在构建的外部项目?

更新:基于这个问题和我遇到的其他常见问题,我写了一个简短的CMake by Example tutorial

hc2pp10m

hc2pp10m1#

我觉得你混淆了两种不同的思维模式。
正如您所注意到的,高度灵活的ExternalProject模块在构建时运行其命令,因此您不能直接使用项目A的导入文件,因为它仅在安装项目A后创建。
如果你想include项目A的导入文件,你将 * 必须 * 在调用项目B的CMakeLists.txt之前手动安装项目A-就像通过这种方式或通过find_file/find_library/find_package添加的任何其他第三方依赖一样。
如果你想使用ExternalProject_Add,你需要在CMakeLists.txt中添加如下内容:

ExternalProject_Add(project_a
  URL ...project_a.tar.gz
  PREFIX ${CMAKE_CURRENT_BINARY_DIR}/project_a
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)

include(${CMAKE_CURRENT_BINARY_DIR}/lib/project_a/project_a-targets.cmake)

ExternalProject_Get_Property(project_a install_dir)
include_directories(${install_dir}/include)

add_dependencies(project_b_exe project_a)
target_link_libraries(project_b_exe ${install_dir}/lib/alib.lib)
shyt4zoc

shyt4zoc2#

This post有一个合理的答案:
CMakeLists.txt.in

cmake_minimum_required(VERSION 2.8.2)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

CMakeLists.txt

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in
               googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )

# Prevent GoogleTest from overriding our compiler/linker options
# when building with Visual Studio
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add googletest directly to our build. This adds
# the following targets: gtest, gtest_main, gmock
# and gmock_main
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
                 ${CMAKE_BINARY_DIR}/googletest-build)

# The gtest/gmock targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
  include_directories("${gtest_SOURCE_DIR}/include"
                      "${gmock_SOURCE_DIR}/include")
endif()

# Now simply link your own targets against gtest, gmock,
# etc. as appropriate

然而,它看起来相当hacky。我想提出一个替代方案-使用Git子模块。

cd MyProject/dependencies/gtest
git submodule add https://github.com/google/googletest.git
cd googletest
git checkout release-1.8.0
cd ../../..
git add *
git commit -m "Add googletest"

然后在MyProject/dependencies/gtest/CMakeList.txt中,你可以这样做:

cmake_minimum_required(VERSION 3.3)

if(TARGET gtest) # To avoid diamond dependencies; may not be necessary depending on you project.
    return()
endif()

add_subdirectory("googletest")

我还没有尝试过这个广泛的,但它似乎更干净。
编辑:这种方法有一个缺点:该子目录可能会运行您不想要的install()命令。This post has an approach to disable them但它是buggy和不为我工作。
编辑2:如果使用add_subdirectory("googletest" EXCLUDE_FROM_ALL),似乎意味着默认情况下不使用子目录中的install()命令。

bejyjqdl

bejyjqdl3#

编辑:CMake现在对此有内置支持。请参阅使用FetchContentnew answer
还可以在辅助make进程中强制生成依赖目标
参见my answer相关主题。

noj0wjuj

noj0wjuj4#

我正在寻找类似的解决方案。这里的回复和上面的教程是信息丰富的。我研究了这里提到的帖子/博客,以建立我的成功。我张贴完整的CMakeLists.txt为我工作。我想,这将是一个基本的模板,为初学者的帮助。
"CMakeLists.txt"

cmake_minimum_required(VERSION 3.10.2)

# Target Project
project (ClientProgram)

# Begin: Including Sources and Headers
include_directories(include)
file (GLOB SOURCES "src/*.c")
# End: Including Sources and Headers

# Begin: Generate executables
add_executable (ClientProgram ${SOURCES})
# End: Generate executables

# This Project Depends on External Project(s) 
include (ExternalProject)

# Begin: External Third Party Library
set (libTLS ThirdPartyTlsLibrary)
ExternalProject_Add (${libTLS}
PREFIX          ${CMAKE_CURRENT_BINARY_DIR}/${libTLS}
# Begin: Download Archive from Web Server
URL             http://myproject.com/MyLibrary.tgz
URL_HASH        SHA1=<expected_sha1sum_of_above_tgz_file>
DOWNLOAD_NO_PROGRESS ON
# End: Download Archive from Web Server

# Begin: Download Source from GIT Repository
#    GIT_REPOSITORY  https://github.com/<project>.git
#    GIT_TAG         <Refer github.com releases -> Tags>
#    GIT_SHALLOW     ON
# End: Download Source from GIT Repository

# Begin: CMAKE Comamnd Argiments
CMAKE_ARGS      -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/${libTLS}
CMAKE_ARGS      -DUSE_SHARED_LIBRARY:BOOL=ON
# End: CMAKE Comamnd Argiments    
)

# The above ExternalProject_Add(...) construct wil take care of \
# 1. Downloading sources
# 2. Building Object files
# 3. Install under DCMAKE_INSTALL_PREFIX Directory

# Acquire Installation Directory of 
ExternalProject_Get_Property (${libTLS} install_dir)

# Begin: Importing Headers & Library of Third Party built using ExternalProject_Add(...)
# Include PATH that has headers required by Target Project
include_directories (${install_dir}/include)

# Import librarues from External Project required by Target Project
add_library (lmytls SHARED IMPORTED)
set_target_properties (lmytls PROPERTIES IMPORTED_LOCATION ${install_dir}/lib/libmytls.so)
add_library (lmyxdot509 SHARED IMPORTED)
set_target_properties(lmyxdot509 PROPERTIES IMPORTED_LOCATION ${install_dir}/lib/libmyxdot509.so)

# End: Importing Headers & Library of Third Party built using ExternalProject_Add(...)
# End: External Third Party Library

# Begin: Target Project depends on Third Party Component
add_dependencies(ClientProgram ${libTLS})
# End: Target Project depends on Third Party Component

# Refer libraries added above used by Target Project
target_link_libraries (ClientProgram lmytls lmyxdot509)
esyap4oy

esyap4oy5#

cmake的ExternalProject_Add确实可以使用,但我不喜欢它的地方是它在构建,连续轮询等过程中执行某些操作……我更喜欢在构建阶段构建项目,没有别的。我曾多次尝试覆盖ExternalProject_Add,不幸的是没有成功。
然后我也尝试添加git子模块,但这会拖动整个git存储库,而在某些情况下,我只需要整个git存储库的子集。我已经检查了-确实可以执行稀疏git checkout ,但这需要单独的函数,我在下面写了。

#-----------------------------------------------------------------------------
#
# Performs sparse (partial) git checkout
#
#   into ${checkoutDir} from ${url} of ${branch}
#
# List of folders and files to pull can be specified after that.
#-----------------------------------------------------------------------------
function (SparseGitCheckout checkoutDir url branch)
    if(EXISTS ${checkoutDir})
        return()
    endif()

    message("-------------------------------------------------------------------")
    message("sparse git checkout to ${checkoutDir}...")
    message("-------------------------------------------------------------------")

    file(MAKE_DIRECTORY ${checkoutDir})

    set(cmds "git init")
    set(cmds ${cmds} "git remote add -f origin --no-tags -t master ${url}")
    set(cmds ${cmds} "git config core.sparseCheckout true")

    # This command is executed via file WRITE
    # echo <file or folder> >> .git/info/sparse-checkout")

    set(cmds ${cmds} "git pull --depth=1 origin ${branch}")

    # message("In directory: ${checkoutDir}")

    foreach( cmd ${cmds})
        message("- ${cmd}")
        string(REPLACE " " ";" cmdList ${cmd})

        #message("Outfile: ${outFile}")
        #message("Final command: ${cmdList}")

        if(pull IN_LIST cmdList)
            string (REPLACE ";" "\n" FILES "${ARGN}")
            file(WRITE ${checkoutDir}/.git/info/sparse-checkout ${FILES} )
        endif()

        execute_process(
            COMMAND ${cmdList}
            WORKING_DIRECTORY ${checkoutDir}
            RESULT_VARIABLE ret
        )

        if(NOT ret EQUAL "0")
            message("error: previous command failed, see explanation above")
            file(REMOVE_RECURSE ${checkoutDir})
            break()
        endif()
    endforeach()

endfunction()

SparseGitCheckout(${CMAKE_BINARY_DIR}/catch_197 https://github.com/catchorg/Catch2.git v1.9.7 single_include)
SparseGitCheckout(${CMAKE_BINARY_DIR}/catch_master https://github.com/catchorg/Catch2.git master single_include)

我在下面添加了两个函数调用,只是为了说明如何使用该函数。
有些人可能不喜欢 checkout master / trunk,因为那个可能已经坏了--然后总是可以指定特定的标签。
在清除该高速缓存文件夹之前,将只执行一次 checkout 。

dgiusagp

dgiusagp6#

首先使用FetchContent

include(FetchContent)
FetchContent_Declare(glog
    GIT_REPOSITORY    https://github.com/google/glog.git
    SOURCE_DIR        ${CMAKE_CURRENT_SOURCE_DIR}/third_party/glog
    SUBBUILD_DIR      third_party/glog/subbuild
    BINARY_DIR        third_party/glog/build
)
option(WITH_GFLAGS "" OFF)
option(WITH_GTEST "" OFF)
option(WITH_GMOCK "" OFF)
option(WITH_UNWIND "" OFF)
option(BUILD_SHARED_LIBS "" OFF)
option(BUILD_TESTING "" OFF)
FetchContent_MakeAvailable(glog)

....

add_library(libsomething STATIC)
target_link_libraries(libsomething PUBLIC glog::glog)

注意,所有CMake魔术都按预期工作:你不需要为libsomething指定glog的include目录或构建工件。

bq9c1y66

bq9c1y667#

我也会推荐FetchContent用于简单的项目。但是,有时需要对正在下载的库进行更多控制。ExternalProject非常有用,因为它有许多自定义选项。秘诀在于了解您试图下载的库。阅读它的CMakeLists.txt文件,了解如何修改安装目录,或者在构建或安装之前需要什么配置。
以下示例将下载ZeroMQ(A.K.A. zmq)这个库需要在库中运行一个bash脚本来正确配置,它有一些选项可以用来定制你的安装。
我个人不喜欢保存文件的标准位置,以及不需要在我的系统中安装库。所以我想把它安装在我的项目中。因此,您可以根据需要指定目录。
在我的项目中,我覆盖CMAKE_INSTALL_PREFIX和CMAKE_INCLUDE_PREFIX来指向我希望文件所在的文件夹。

set(ZMQ_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}")
set(ZMQ_DIR "${CMAKE_CURRENT_BINARY_DIR}/ZeroMQ")
set(ZMQ_SRC_DIR "${ZMQ_DIR}/src")
set(ZMQ_INSTALL_INCLUDEDIR "${CMAKE_INCLUDE_PREFIX}/ZeroMQ")

include(ExternalProject)
ExternalProject_Add(ZeroMQ
  URL https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz
  PREFIX "${ZMQ_DIR}/prefix"
  TMP_DIR "${ZMQ_DIR}/tmp"
  STAMP_DIR "${ZMQ_DIR}/stamp"
  DOWNLOAD_DIR "${ZMQ_DIR}"
  SOURCE_DIR "${ZMQ_SRC_DIR}"
  BINARY_DIR "${ZMQ_DIR}/build"
  CONFIGURE_COMMAND cd ${ZMQ_SRC_DIR} && ./configure --with-pic --prefix=${ZMQ_INSTALL_DIR}
  BUILD_COMMAND ""
  INSTALL_COMMAND cd ${ZMQ_SRC_DIR} && $(MAKE) install
)

请注意,ZeroMQ不是实际的库目标,它是我为ExternalProject目标选择的名称。库目标是zmq。因此库文件将是libzmq.so。
在构建或安装之后,您的主目标需要显示它依赖于外部库,以便按照正确的顺序构建。因此,您需要在目标所在的CMakeLists.txt(或任何父CMakeLists.txt)中添加以下内容:

include_directories("${CMAKE_INCLUDE_PREFIX}/ZeroMQ")
add_dependencies(your_target ZeroMQ)     # Depends on the ExternalProject
target_link_libraries(your_target zmq)   # Link the library target

如果你需要下载另一个依赖于zmq的外部库,你可以在ExternalProject_Add中添加一个DEPENDS参数:

ExternalProject_Add(
    cppzmqDownload
    URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.10.0.tar.gz
    DEPENDS ZeroMQ
    CMAKE_ARGS
      -DCMAKE_MODULE_PATH:PATH=${CMAKE_INSTALL_PREFIX}
      -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
      -DCMAKE_INSTALL_INCLUDEDIR:PATH=${CMAKE_INCLUDE_PREFIX}/cppzmq
      -DCPPZMQ_BUILD_TESTS=OFF
      -DPC_LIBZMQ_INCLUDE_DIRS=${ZMQ_INSTALL_INCLUDEDIR}
      -DPC_LIBZMQ_LIBRARY_DIRS=${CMAKE_LIB_PREFIX}
)

希望这篇文章能对大家有所帮助,虽然这是一篇很老的文章。

相关问题