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)
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)

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"))
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
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -pedantic-errors -Werror>
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /WX>
)

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)
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
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -pedantic-errors -Werror>
$<$<CXX_COMPILER_ID:MSVC>:
/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
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -pedantic-errors -Werror>
$<$<CXX_COMPILER_ID:MSVC>:
/W4 /WX>
)
endif()

View file

@ -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)