如何让 FetchContent 下载到特定位置?

Car*_*ood 7 cmake

遵循FetchContent文档,例如使用类似的东西

FetchContent_Declare(
    gitache_package_libcwd_r
    GIT_REPOSITORY "https://github.com/CarloWood/libcwd.git"
    GIT_TAG "master"
)
FetchContent_MakeAvailable(
    gitache_package_libcwd_r
)
Run Code Online (Sandbox Code Playgroud)

源代码被克隆到${CMAKE_BINARY_DIR}/_deps/gitache_package_libcwd_r-src/.

我怎样才能让它把源代码放在其他地方,比如/opt/gitache/libcwd_r/src/gitache_package_libcwd_r(就像我使用ExternalProjecta PREFIXof时那样/opt/gitache/libcwd_r)?

Car*_*ood 5

如果有人偶然发现这个问题寻找答案,我发现至少可以通过"${CMAKE_BINARY_DIR}/_deps"FETCHCONTENT_BASE_DIR调用之前设置来替换该部分FetchContent_Declare。我不确定是否可以进行更精细的调整。如果您使用FetchContent_Populate的扩展版本,这似乎是可能的,但恕我直言,使用它有几个缺点。

例如

set(FETCHCONTENT_BASE_DIR "/opt/gitache/libcwd_r")
FetchContent_Declare(... etc (see question)
Run Code Online (Sandbox Code Playgroud)

将克隆到/opt/gitache/libcwd_r/gitache_package_libcwd_r-src/并用作 /opt/gitache/libcwd_r/gitache_package_libcwd_r-build构建目录。


nik*_*ohn 5

您可以像使用DOWNLOAD_DIRSOURCE_DIR、等选项一样使用TMP_DIRExternalProject_Add()

Cmake FetchContent_Declare 文档

可以<contentOptions>是该命令理解的任何下载、更新或补丁选项ExternalProject_Add()。配置、构建、安装和测试步骤被明确禁用,因此与它们相关的选项将被忽略。SOURCE_SUBDIR 选项是一个例外,FetchContent_MakeAvailable()有关它如何影响行为的详细信息,请参阅 。