用于从外部存储库复制文件的 cmake 预构建命令

cmake pre-build command to copy files from external repository

在我的项目中,我有一个外部依赖项(一个 Github 存储库),我想从中将某些文件复制到源代码并包含我项目的目录。我只需要外部存储库中的源文件和头文件的一个子集,所以我只想能够将我需要的文件的更新版本复制到我项目中的相关目录中。我相信 ExternalProject_Add 应该是从外部项目下载文件的正确命令,但我不知道如何将它们复制到我的源代码并在每次构建之前包含目录。是否有 ExternalProject_Add 选项来实现此目的,或者是否应该使用 add_custom_command 添加自定义构建规则,在下载外部项目后复制文件?

ou can check the CMakeLists.txt of the external repository I want to link to here. I need to compile and install the library and don't want to compile the executables

如果您的代码在子模块中,您可以通过 git submodule 添加外部代码,或者如文档中所述使用 FetchContent 添加外部代码:

include(FetchContent)
FetchContent_Declare(
       screamrepo
       GIT_REPOSITORY "https://github.com/EricssonResearch/scream"
)
FetchContent_GetProperties(screamrepo)
if(NOT screamrepo_POPULATED)
  FetchContent_Populate(screamrepo)
endif()
# use ${screamrepo_SOURCE_DIR} for source dir

如果外部代码结构良好并公开了您想要的库,那么:

add_subdirectory(the/source/path EXCLUDE_FROM_ALL)

并使用图书馆。如果不是,比如在您不喜欢第三方 cmake 配置或它带有不受支持的配置的情况下,请编写您自己的目标 add_library 和配置,并包含使用源目录中其他存储库文件的路径。