ModernCMake-Chinese
  • Introduction
  • Modern CMake 简介
    • 安装 CMake
    • 运行 CMake
    • Do's and Don'ts
    • CMake 中的新变化
  • 基础知识简介
    • 变量和 Cache
    • 在CMake 中编程
    • 与你的代码交互
    • 如何结构化你的工程
    • 运行其他程序
  • Adding Features
    • C++11 and Beyond
    • Small but common needs
    • Utilities
    • Useful modules
    • IDEs
    • Debugging
  • Including Projects
    • Submodule
    • DownloadProject
    • Fetch (CMake 3.11)
  • Testing
    • GoogleTest
    • Catch
  • Exporting and Installing
    • Installing
    • Exporting
    • Packaging
  • Looking for libraries
    • CUDA
    • OpenMP
    • Boost
    • MPI
    • ROOT
      • UseFile Example
      • Simple Example
      • Simple Example CMake 3.11+
      • Dictionary Example
    • Minuit2
Powered by GitBook
On this page

Was this helpful?

  1. Including Projects

Fetch (CMake 3.11)

PreviousDownloadProjectNextTesting

Last updated 6 years ago

Was this helpful?

Often, you would like to do your download of data or packages as part of the configure instead of the build. This was invented several times in third party modules, but was finally added to CMake itself as part of CMake 3.11 as the module.

The module has excellent documentation that I won't try to repeat. The key ideas are:

  • Use FetchContent_Declare(MyName to get data or a package. You can set URLs, Git repositories, and more.

  • Use FetchContent_GetProperties(MyName) on the name you picked in the first step to get MyName_* variables.

  • Check MyName_POPULATED, and if not populated, use FetchContent_Populate(MyName) (and if a package, add_subdirectory("${MyName_SOURCE_DIR}" "${MyName_SOURCE_DIR}"))

For example, to download Catch2:

FetchContent_Declare(
  catch
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG        v2.2.1
)

FetchContent_GetProperties(catch)
if(NOT catch_POPULATED)
  FetchContent_Populate(catch)
  add_subdirectory(${catch_SOURCE_DIR} ${catch_BINARY_DIR})
endif()
FetchContent
FetchContent