From 82ae0e6746192cf94b68ddfcdaa8feaf61200cbf Mon Sep 17 00:00:00 2001 From: "David P. Sicilia" Date: Sat, 22 Jun 2019 13:35:46 -0400 Subject: [PATCH 1/2] CMake: add subproject detection and fix source location. The subproject detection avoids adding things into the build that are not necessary (i.e., standardese, unit tests) and/or may cause conflicts (i.e., `tests` target name) when building as a submodule in a parent project. Also change CMAKE_SOURCE_LOCATION --> CMAKE_CURRENT_SOURCE_LOCATION which is more correct and is needed when the source location is not the top level folder in the project. --- CMakeLists.txt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a653d8..4a0bfc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,15 @@ cmake_minimum_required(VERSION 3.0) +# Determine if we are building as part of a parent project. +get_directory_property(is_subproject PARENT_DIRECTORY) + project(function_ref) +add_library(function_ref INTERFACE) +target_sources(function_ref INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/function_ref.hpp) +target_include_directories(function_ref INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) + +if(NOT is_subproject) # Prepare "Catch" library for other executables set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/test) add_library(Catch INTERFACE) @@ -16,10 +24,6 @@ set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp add_executable(tests ${TEST_SOURCES}) -add_library(function_ref INTERFACE) -target_sources(function_ref INTERFACE ${CMAKE_SOURCE_DIR}/function_ref.hpp) -target_include_directories(function_ref INTERFACE ${CMAKE_SOURCE_DIR}) - target_link_libraries(tests Catch function_ref) set_property(TARGET tests PROPERTY CXX_STANDARD 14) @@ -33,3 +37,4 @@ standardese_generate(function_ref CONFIG ${CMAKE_SOURCE_DIR}/standardese.config INPUT function_ref.hpp) endif () +endif () From ebd109c0d3e2e0d4275392a157c145d41bdeafe6 Mon Sep 17 00:00:00 2001 From: "David P. Sicilia" Date: Tue, 10 Sep 2019 21:13:26 -0400 Subject: [PATCH 2/2] Enable tests by default only when not a subproject. Without this then a parent project has to specifically turn off function_ref testing by setting the appropriate flag. This makes testing opt-in when function_ref is a subproject, and can still be turned on. --- CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc0c860..edcbe33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,12 @@ cmake_minimum_required(VERSION 3.11) project(tl-function_ref VERSION 1.0.0 LANGUAGES CXX) -option(FUNCTION_REF_ENABLE_TESTS "Enable tests." ON) +get_directory_property(is_subproject PARENT_DIRECTORY) +if(NOT is_subproject) + set(is_mainproject YES) +endif() + +option(FUNCTION_REF_ENABLE_TESTS "Enable tests." ${is_mainproject}) include(FetchContent) FetchContent_Declare(