From c1a4cd0fa70da7e804e825e37e1304e647ebcec7 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Wed, 3 Feb 2016 11:20:19 +0100 Subject: [PATCH] check if cppformat is the master project or just used as dependency Based on that information less intrusive option defaults are choosen. Additionally, packaging support is omitted. --- CMakeLists.txt | 17 ++++++++++++----- test/CMakeLists.txt | 12 +++++++++++- test/add_subdirectory-test/CMakeLists.txt | 13 +++++++++++++ test/add_subdirectory-test/main.cpp | 8 ++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 test/add_subdirectory-test/CMakeLists.txt create mode 100644 test/add_subdirectory-test/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dedd2d5..418048b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,13 @@ message(STATUS "CMake version: ${CMAKE_VERSION}") cmake_minimum_required(VERSION 2.8.12) +# determine if cppformat is built as sub project (using add_subdirectory) +# or if it is the master project +set(MASTER_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MASTER_PROJECT ON) +endif () + # Set the default CMAKE_BUILD_TYPE to Release. # This should be done before the project command since the latter can set # CMAKE_BUILD_TYPE itself (it does so for nmake). @@ -13,9 +20,9 @@ endif () option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF) # Options that control generation of various targets. -option(FMT_DOC "Generate the doc target." ON) -option(FMT_INSTALL "Generate the install target." ON) -option(FMT_TEST "Generate the test target." ON) +option(FMT_DOC "Generate the doc target." ${MASTER_PROJECT}) +option(FMT_INSTALL "Generate the install target." ${MASTER_PROJECT}) +option(FMT_TEST "Generate the test target." ${MASTER_PROJECT}) project(FORMAT) @@ -41,7 +48,7 @@ if (CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) set(PEDANTIC_COMPILE_FLAGS -Wall -Wextra -Wshadow -pedantic) endif () -if (CMAKE_GENERATOR MATCHES "Visual Studio") +if (MASTER_PROJECT AND CMAKE_GENERATOR MATCHES "Visual Studio") # If Microsoft SDK is installed create script run-msbuild.bat that # calls SetEnv.cmd to to set up build environment and runs msbuild. # It is useful when building Visual Studio projects with the SDK @@ -81,7 +88,7 @@ if (FMT_TEST) endif () set(gitignore ${PROJECT_SOURCE_DIR}/.gitignore) -if (EXISTS ${gitignore}) +if (MASTER_PROJECT AND EXISTS ${gitignore}) # Get the list of ignored files from .gitignore. file (STRINGS ${gitignore} lines) LIST(REMOVE_ITEM lines /doc/html) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 96f4fb74..49d76594 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -143,4 +143,14 @@ add_test(find-package-test ${CMAKE_CTEST_COMMAND} --build-generator ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM} --build-options "-Dcppformat_DIR=${PROJECT_BINARY_DIR}" - "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") \ No newline at end of file + "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") + +# test if the targets are findable when add_subdirectory is used +add_test(add_subdirectory-test ${CMAKE_CTEST_COMMAND} + -C ${CMAKE_BUILD_TYPE} + --build-and-test + "${CMAKE_CURRENT_SOURCE_DIR}/add_subdirectory-test" + "${CMAKE_CURRENT_BINARY_DIR}/add_subdirectory-test" + --build-generator ${CMAKE_GENERATOR} + --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-options "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") diff --git a/test/add_subdirectory-test/CMakeLists.txt b/test/add_subdirectory-test/CMakeLists.txt new file mode 100644 index 00000000..c267842a --- /dev/null +++ b/test/add_subdirectory-test/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 2.8.12) + +project(cppformat-test) + +add_subdirectory(../.. cppformat) + +add_executable(library-test "main.cpp") +target_link_libraries(library-test cppformat) + +if (TARGET cppformat-header-only) + add_executable(header-only-test "main.cpp") + target_link_libraries(header-only-test cppformat-header-only) +endif () \ No newline at end of file diff --git a/test/add_subdirectory-test/main.cpp b/test/add_subdirectory-test/main.cpp new file mode 100644 index 00000000..8736017b --- /dev/null +++ b/test/add_subdirectory-test/main.cpp @@ -0,0 +1,8 @@ +#include "cppformat/format.h" + +int main(int argc, char** argv) +{ + for(int i = 0; i < argc; ++i) + fmt::print("{}: {}\n", i, argv[i]); + return 0; +}