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
This commit is contained in:
Balazs Benics 2019-07-31 18:09:26 +02:00 committed by GitHub
parent fea658233a
commit 0639b4b0c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 78 deletions

View file

@ -4,14 +4,8 @@ project(nameof VERSION "0.9.0" LANGUAGES CXX)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL) include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) option(NAMEOF_OPT_BUILD_EXAMPLES "Build nameof examples" OFF)
set(IS_TOPLEVEL_PROJECT TRUE) option(NAMEOF_OPT_BUILD_TESTS "Build and perform nameof tests" OFF)
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})
if(NAMEOF_OPT_BUILD_EXAMPLES) if(NAMEOF_OPT_BUILD_EXAMPLES)
add_subdirectory(example) add_subdirectory(example)

View file

@ -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")) add_executable(example example.cpp)
check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG) target_link_libraries(example PRIVATE nameof::nameof)
if(!HAS_CPP17_FLAG) set_target_properties(example PROPERTIES CXX_EXTENSIONS OFF)
MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support.")
endif()
set(CMAKE_VERBOSE_MAKEFILE ON) target_compile_options(example
set(OPTIONS -Wall -Wextra -pedantic-errors -Werror -std=c++17) PRIVATE
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG) -Wall -Wextra -pedantic-errors -Werror>
if(!HAS_CPP17_FLAG) $<$<CXX_COMPILER_ID:MSVC>:
MESSAGE(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support.") /W4 /WX>
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})

View file

@ -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) add_executable(test-17 test.cpp)
check_cxx_compiler_flag(/permissive HAS_PERMISSIVE_FLAG) target_link_libraries(test-17 PRIVATE Catch2::Catch2 nameof::nameof)
if(HAS_PERMISSIVE_FLAG) set_target_properties(test-17 PROPERTIES CXX_EXTENSIONS OFF)
set(OPTIONS ${OPTIONS} /permissive-) target_compile_features(test-17 PRIVATE cxx_std_17)
endif()
target_compile_options(test-17
check_cxx_compiler_flag(/std:c++17 HAS_CPP17_FLAG) PRIVATE
check_cxx_compiler_flag(/std:c++20 HAS_CPP20_FLAG) $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
check_cxx_compiler_flag(/std:c++latest HAS_CPPLATEST_FLAG) -Wall -Wextra -pedantic-errors -Werror>
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") $<$<CXX_COMPILER_ID:MSVC>:
set(CMAKE_VERBOSE_MAKEFILE ON) /W4 /WX>
set(OPTIONS -Wall -Wextra -pedantic-errors -Werror) )
check_cxx_compiler_flag(-std=c++17 HAS_CPP17_FLAG) if(HAS_CPP20 AND (cxx_std_20 IN_LIST CMAKE_CXX_COMPILE_FEATURES))
check_cxx_compiler_flag(-std=c++20 HAS_CPP20_FLAG) add_executable(test-20 test.cpp)
endif() target_link_libraries(test-20 PRIVATE Catch2::Catch2 nameof::nameof)
set_target_properties(test-20 PROPERTIES CXX_EXTENSIONS OFF)
function(make_test target std) target_compile_features(test-20 PRIVATE cxx_std_20)
add_executable(${target} ${SOURCES})
target_compile_options(${target} PRIVATE ${OPTIONS}) target_compile_options(test-20
target_link_libraries(${target} PRIVATE ${CMAKE_PROJECT_NAME}) PRIVATE
target_link_libraries(${target} PRIVATE Catch2::Catch2) $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
if(std) -Wall -Wextra -pedantic-errors -Werror>
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") $<$<CXX_COMPILER_ID:MSVC>:
target_compile_options(${target} PRIVATE /std:${std}) /W4 /WX>
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)
endif() endif()

View file

@ -1,11 +1,11 @@
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 3.8)
project(test_package CXX) project(test_package CXX)
set(CMAKE_VERBOSE_MAKEFILE TRUE) set(CMAKE_VERBOSE_MAKEFILE ON)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() conan_basic_setup(TARGETS)
add_executable(test_package test_package.cpp) add_executable(test_package test_package.cpp)
target_link_libraries(test_package ${CONAN_LIBS}) target_link_libraries(test_package PRIVATE CONAN_PKG::nameof)
set_target_properties(test_package PROPERTIES CXX_STANDARD 17) target_compile_features(test_package PRIVATE cxx_std_17)