From 5c589dd138305e9073694dc9213fe839f9d9dce5 Mon Sep 17 00:00:00 2001 From: Martin Stump <11492152+globberwops@users.noreply.github.com> Date: Wed, 9 Dec 2020 11:37:01 +0100 Subject: [PATCH 1/4] Add MAIN_PROJECT check for test and install options --- CMakeLists.txt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa77a5aed..93f7986a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,13 @@ cmake_minimum_required(VERSION 3.1) ## project(nlohmann_json VERSION 3.9.1 LANGUAGES CXX) +## +## MAIN_PROJECT CHECK +## +if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + set(MAIN_PROJECT ON) +endif() + ## ## INCLUDE ## @@ -21,8 +28,8 @@ if (POLICY CMP0077) cmake_policy(SET CMP0077 NEW) endif () -option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ON) -option(JSON_Install "Install CMake targets during install step." ON) +option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${MAIN_PROJECT}) +option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT}) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) option(JSON_ImplicitConversions "Enable implicit conversions." ON) @@ -101,9 +108,8 @@ CONFIGURE_FILE( ## TESTS ## create and configure the unit test target ## -include(CTest) #adds option BUILD_TESTING (default ON) - -if(BUILD_TESTING AND JSON_BuildTests) +if (JSON_BuildTests) + include(CTest) #adds option BUILD_TESTING (default ON) enable_testing() add_subdirectory(test) endif() From 790508887e75a5be3f2ccc35a42ca474876bb86c Mon Sep 17 00:00:00 2001 From: Martin Stump <11492152+globberwops@users.noreply.github.com> Date: Wed, 9 Dec 2020 12:39:44 +0100 Subject: [PATCH 2/4] Set MAIN_PROJECT=OFF initially --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 93f7986a8..06761947b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ project(nlohmann_json VERSION 3.9.1 LANGUAGES CXX) ## ## MAIN_PROJECT CHECK ## +set(MAIN_PROJECT OFF) if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(MAIN_PROJECT ON) endif() From ea759d03600b24af619daa7a3a1e277df50ca5b3 Mon Sep 17 00:00:00 2001 From: Martin Stump <11492152+globberwops@users.noreply.github.com> Date: Sun, 13 Dec 2020 20:34:51 +0100 Subject: [PATCH 3/4] Compare to CMAKE_CURRENT_SOURCE_DIR for main project check --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 06761947b..7c4d8ad8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,10 @@ project(nlohmann_json VERSION 3.9.1 LANGUAGES CXX) ## ## MAIN_PROJECT CHECK +## determine if nlohmann_json is built as a subproject (using add_subdirectory) or if it is the main project ## set(MAIN_PROJECT OFF) -if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(MAIN_PROJECT ON) endif() From 1771e9249fd09819d1f857d364e49b4178abd572 Mon Sep 17 00:00:00 2001 From: Martin Stump <11492152+globberwops@users.noreply.github.com> Date: Mon, 14 Dec 2020 10:59:38 +0100 Subject: [PATCH 4/4] Remove comment on CTest inclusion Co-authored-by: Niels Lohmann --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c4d8ad8c..44ede3e79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,7 @@ CONFIGURE_FILE( ## create and configure the unit test target ## if (JSON_BuildTests) - include(CTest) #adds option BUILD_TESTING (default ON) + include(CTest) enable_testing() add_subdirectory(test) endif()