From 571a244dbd93770c7912642e10053c9a9f6e49ec Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 5 Aug 2020 16:09:16 -0400 Subject: [PATCH 1/2] feat: new example --- examples/CMakeLists.txt | 3 ++- examples/fetch/CMakeLists.txt | 19 +++++++++++++++++++ examples/fetch/main.cpp | 13 +++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 examples/fetch/CMakeLists.txt create mode 100644 examples/fetch/main.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index c215994..8c6186d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11...3.16) +cmake_minimum_required(VERSION 3.11...3.18) project(ModernCMakeExamples) set(MODERN_CMAKE_BUILD_TESTING ON) @@ -7,6 +7,7 @@ include(CTest) add_subdirectory(simple-project) add_subdirectory(extended-project) +add_subdirectory(fetch) add_subdirectory(root-usefile) add_subdirectory(root-simple) diff --git a/examples/fetch/CMakeLists.txt b/examples/fetch/CMakeLists.txt new file mode 100644 index 0000000..1d320e6 --- /dev/null +++ b/examples/fetch/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.14...3.18) + +project(FetchExample LANGUAGES CXX) + +include(FetchContent) +include(CTest) + +FetchContent_Declare( + catch + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v2.13.0 +) + +# CMake 3.14+ +FetchContent_MakeAvailable(catch) + +add_executable(fetch_example main.cpp) +target_link_libraries(fetch_example PRIVATE Catch2::Catch2) +add_test(NAME fetch_example COMMAND fetch_example) diff --git a/examples/fetch/main.cpp b/examples/fetch/main.cpp new file mode 100644 index 0000000..303f3a0 --- /dev/null +++ b/examples/fetch/main.cpp @@ -0,0 +1,13 @@ +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file +#include "catch2/catch.hpp" + +unsigned int Factorial( unsigned int number ) { + return number <= 1 ? number : Factorial(number-1)*number; +} + +TEST_CASE( "Factorials are computed", "[factorial]" ) { + REQUIRE( Factorial(1) == 1 ); + REQUIRE( Factorial(2) == 2 ); + REQUIRE( Factorial(3) == 6 ); + REQUIRE( Factorial(10) == 3628800 ); +} From 8fb3c4fcbb5fd3a679e2477fed4b853e9f32a3c4 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 5 Aug 2020 16:17:00 -0400 Subject: [PATCH 2/2] fix: names need to match to share fetches --- chapters/projects/fetch.md | 4 +++- examples/extended-project/tests/CMakeLists.txt | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/chapters/projects/fetch.md b/chapters/projects/fetch.md index ea33b04..e23bbc3 100644 --- a/chapters/projects/fetch.md +++ b/chapters/projects/fetch.md @@ -14,7 +14,7 @@ For example, to download Catch2: FetchContent_Declare( catch GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v2.9.1 + GIT_TAG v2.13.0 ) # CMake 3.14+ @@ -48,4 +48,6 @@ endif() Now you have the CMake 3.14+ syntax in CMake 3.11+. +See the example [here](https://gitlab.com/CLIUtils/modern-cmake/-/tree/master/examples/fetch). + [FetchContent]: https://cmake.org/cmake/help/latest/module/FetchContent.html diff --git a/examples/extended-project/tests/CMakeLists.txt b/examples/extended-project/tests/CMakeLists.txt index 80ce3cf..96c9129 100644 --- a/examples/extended-project/tests/CMakeLists.txt +++ b/examples/extended-project/tests/CMakeLists.txt @@ -1,9 +1,9 @@ # Testing library FetchContent_Declare( - catch2 + catch GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v2.9.1) -FetchContent_MakeAvailable(catch2) +FetchContent_MakeAvailable(catch) # Adds Catch2::Catch2 # Tests need to be added as executables first