From 9e4822b373e430691f08e381e92d45147dca22eb Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Fri, 30 Mar 2018 15:39:25 +0200 Subject: [PATCH] Adding fetch information --- SUMMARY.md | 2 +- chapters/projects/fetch.md | 10 ++++++++++ chapters/testing/googletest.md | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/SUMMARY.md b/SUMMARY.md index a6ff607..8bcc195 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -20,7 +20,7 @@ * [Including Projects](chapters/projects.md) * [Submodule](chapters/projects/submodule.md) * [DownloadProject](chapters/projects/download.md) - * [Fetch (CMake 3.11) (X)](chapters/projects/fetch.md) + * [Fetch (CMake 3.11)](chapters/projects/fetch.md) * [Testing](chapters/testing.md) * [GoogleTest](chapters/testing/googletest.md) * [Catch](chapters/testing/catch.md) diff --git a/chapters/projects/fetch.md b/chapters/projects/fetch.md index 8513152..d976829 100644 --- a/chapters/projects/fetch.md +++ b/chapters/projects/fetch.md @@ -1,5 +1,15 @@ #FetchContent (CMake 3.11+) +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 [FetchContent] module. + +The [FetchContent] 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}")`) + +[FetchContent]: https://cmake.org/cmake/help/latest/module/FetchContent.html + {% hint style='working' %} This document is a work in progress. You can raise an issue or put in a merge request on [GitLab](https://gitlab.com/CLIUtils/modern-cmake). {% endhint %} diff --git a/chapters/testing/googletest.md b/chapters/testing/googletest.md index 618cd25..43bcdd5 100644 --- a/chapters/testing/googletest.md +++ b/chapters/testing/googletest.md @@ -84,6 +84,25 @@ add_gtest(SimpleTest) > add_test(SimpleTest SimpleTest) > ``` +## FetchContent: CMake 3.11 + +The example for the FetchContent module is GoogleTest: + +```cmake +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.8.0 +) + +FetchContent_GetProperties(googletest) +if(NOT googletest_POPULATED) + FetchContent_Populate(googletest) + add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}) +endif() +``` [^1]: Here I've assumed that you are working on a GitHub repository by using the relative path to googletest.