From 0639b4b0c44c54d7b846179ae001c01204f755ae Mon Sep 17 00:00:00 2001 From: Balazs Benics Date: Wed, 31 Jul 2019 18:09:26 +0200 Subject: [PATCH] Update, modernize cmakelists (#14) * remove IS_TOPLEVEL_PROJECT cmake variable from top level * rework example/CMakeLists.txt with generator expressions * rework test/CMakeLists.txt with generator expressions * fix unknown cxx_std_20 on certain cmake versions * update test_package/CMakeLists.txt with modern cmake --- CMakeLists.txt | 10 ++---- example/CMakeLists.txt | 36 ++++++------------- test/CMakeLists.txt | 70 ++++++++++++++++--------------------- test_package/CMakeLists.txt | 10 +++--- 4 files changed, 48 insertions(+), 78 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48ba557..de1001c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,14 +4,8 @@ project(nameof VERSION "0.9.0" LANGUAGES CXX) include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL) -if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) - set(IS_TOPLEVEL_PROJECT TRUE) -else() - set(IS_TOPLEVEL_PROJECT FALSE) -endif() - -option(NAMEOF_OPT_BUILD_EXAMPLES "Build nameof examples" ${IS_TOPLEVEL_PROJECT}) -option(NAMEOF_OPT_BUILD_TESTS "Build and perform nameof tests" ${IS_TOPLEVEL_PROJECT}) +option(NAMEOF_OPT_BUILD_EXAMPLES "Build nameof examples" OFF) +option(NAMEOF_OPT_BUILD_TESTS "Build and perform nameof tests" OFF) if(NAMEOF_OPT_BUILD_EXAMPLES) add_subdirectory(example) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b3f2d2c..7166004 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,27 +1,13 @@ -include(CheckCXXCompilerFlag) +# set(CMAKE_VERBOSE_MAKEFILE ON) -if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) - check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG) - if(!HAS_CPP17_FLAG) - MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support.") - endif() +add_executable(example example.cpp) +target_link_libraries(example PRIVATE nameof::nameof) +set_target_properties(example PROPERTIES CXX_EXTENSIONS OFF) - set(CMAKE_VERBOSE_MAKEFILE ON) - set(OPTIONS -Wall -Wextra -pedantic-errors -Werror -std=c++17) -elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG) - if(!HAS_CPP17_FLAG) - MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support.") - endif() - - set(OPTIONS /W4 /WX /std:c++17) - if(HAS_PERMISSIVE_FLAG) - set(OPTIONS ${OPTIONS} /permissive-) - endif() -endif() - -add_executable(example - example.cpp - ${CMAKE_SOURCE_DIR}/include/${CMAKE_PROJECT_NAME}.hpp) -target_compile_options(example PRIVATE ${OPTIONS}) -target_link_libraries(example PRIVATE ${CMAKE_PROJECT_NAME}) +target_compile_options(example + PRIVATE + $<$,$,$>: + -Wall -Wextra -pedantic-errors -Werror> + $<$: + /W4 /WX> +) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index aef3f9f..9a44569 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,46 +1,36 @@ -include(CheckCXXCompilerFlag) +find_package(Catch2 REQUIRED) -find_package(Catch2 REQUIRED) +include(CheckCXXCompilerFlag) +check_cxx_compiler_flag(-std=c++20 HAS_GNU_CPP20_FLAG) +check_cxx_compiler_flag(/std:c++20 HAS_MSVC_CPP20_FLAG) -set(SOURCES test.cpp) +set(HAS_CPP20 (HAS_GNU_CPP20_FLAG OR HAS_MSVC_CPP20_FLAG)) -if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - set(OPTIONS /W4 /WX) - check_cxx_compiler_flag(/permissive HAS_PERMISSIVE_FLAG) - if(HAS_PERMISSIVE_FLAG) - set(OPTIONS ${OPTIONS} /permissive-) - endif() - check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG) - check_cxx_compiler_flag(/std:c++20 HAS_CPP20_FLAG) - check_cxx_compiler_flag(/std:c++latest HAS_CPPLATEST_FLAG) -elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - set(CMAKE_VERBOSE_MAKEFILE ON) - set(OPTIONS -Wall -Wextra -pedantic-errors -Werror) +add_executable(test-17 test.cpp) +target_link_libraries(test-17 PRIVATE Catch2::Catch2 nameof::nameof) +set_target_properties(test-17 PROPERTIES CXX_EXTENSIONS OFF) +target_compile_features(test-17 PRIVATE cxx_std_17) - check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG) - check_cxx_compiler_flag(-std=c++20 HAS_CPP20_FLAG) -endif() - -function(make_test target std) - add_executable(${target} ${SOURCES}) - target_compile_options(${target} PRIVATE ${OPTIONS}) - target_link_libraries(${target} PRIVATE ${CMAKE_PROJECT_NAME}) - target_link_libraries(${target} PRIVATE Catch2::Catch2) - if(std) - if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - target_compile_options(${target} PRIVATE /std:${std}) - else() - target_compile_options(${target} PRIVATE -std=${std}) - endif() - endif() - add_test(NAME ${target} COMMAND ${target}) -endfunction() - -if(HAS_CPP17_FLAG) - make_test(${CMAKE_PROJECT_NAME}-cpp17.t c++17) -endif() - -if(HAS_CPPLATEST_FLAG) - make_test(${CMAKE_PROJECT_NAME}-cpplatest.t c++latest) +target_compile_options(test-17 + PRIVATE + $<$,$,$>: + -Wall -Wextra -pedantic-errors -Werror> + $<$: + /W4 /WX> +) + +if(HAS_CPP20 AND (cxx_std_20 IN_LIST CMAKE_CXX_COMPILE_FEATURES)) + add_executable(test-20 test.cpp) + target_link_libraries(test-20 PRIVATE Catch2::Catch2 nameof::nameof) + set_target_properties(test-20 PROPERTIES CXX_EXTENSIONS OFF) + target_compile_features(test-20 PRIVATE cxx_std_20) + + target_compile_options(test-20 + PRIVATE + $<$,$,$>: + -Wall -Wextra -pedantic-errors -Werror> + $<$: + /W4 /WX> + ) endif() diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt index a3911f2..4b46d7a 100644 --- a/test_package/CMakeLists.txt +++ b/test_package/CMakeLists.txt @@ -1,11 +1,11 @@ -cmake_minimum_required(VERSION 2.8.12) +cmake_minimum_required(VERSION 3.8) project(test_package CXX) -set(CMAKE_VERBOSE_MAKEFILE TRUE) +set(CMAKE_VERBOSE_MAKEFILE ON) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup() +conan_basic_setup(TARGETS) add_executable(test_package test_package.cpp) -target_link_libraries(test_package ${CONAN_LIBS}) -set_target_properties(test_package PROPERTIES CXX_STANDARD 17) +target_link_libraries(test_package PRIVATE CONAN_PKG::nameof) +target_compile_features(test_package PRIVATE cxx_std_17)