From 734e9ec6e1dfbe07235851e91ade4b2f4ca726e4 Mon Sep 17 00:00:00 2001 From: Neargye Date: Wed, 29 Aug 2018 16:57:44 +0500 Subject: [PATCH] update build --- CMakeLists.txt | 17 +------ example/CMakeLists.txt | 19 ++++++-- test/CMakeLists.txt | 102 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 115 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e320558..1c18d1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,6 @@ cmake_minimum_required(VERSION 3.2) -include(CheckCXXCompilerFlag) -project(nameof) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) - -if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) - add_compile_options(-Wall -Wextra -pedantic-errors) - set(CMAKE_VERBOSE_MAKEFILE ON) -elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - add_compile_options(/W4) - check_cxx_compiler_flag(/permissive HAS_PERMISSIVE_FLAG) - if(HAS_PERMISSIVE_FLAG) - add_compile_options(/permissive-) - endif() -endif() +project(nameof LANGUAGES CXX) add_subdirectory(example) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index db3f464..e60b0b8 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,5 +1,16 @@ -include_directories(${CMAKE_SOURCE_DIR}/include) +set(SOURCES example.cpp ${CMAKE_SOURCE_DIR}/include/nameof.hpp) -add_executable(${PROJECT_NAME}_example - ${CMAKE_SOURCE_DIR}/include/nameof.hpp - example.cpp) +include_directories(${CMAKE_SOURCE_DIR}/include) + +add_executable(example ${SOURCES}) + +if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) + set(CMAKE_VERBOSE_MAKEFILE ON) + target_compile_options(example PRIVATE -Wall -Wextra -pedantic-errors) + target_compile_options(example PRIVATE -std=c++11) +elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + target_compile_options(example PRIVATE /W4) + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.11) + target_compile_options(example PRIVATE /permissive-) + endif() +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index afeb0d9..e872645 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,101 @@ -include_directories(3rdparty/Catch2) +set(SOURCES test.cpp) + +include_directories(3rdparty/Catch2) include_directories(${CMAKE_SOURCE_DIR}/include) -add_executable(${PROJECT_NAME}_test test.cpp) -add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) +set(OPTIONS "") + +set(HAS_CPP11_FLAG FALSE) +set(HAS_CPP14_FLAG FALSE) +set(HAS_CPP17_FLAG FALSE) +set(HAS_CPP20_FLAG FALSE) +set(HAS_CPPLATEST_FLAG FALSE) + +if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + set(OPTIONS /W4) + + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.00) + set(HAS_CPP14_FLAG TRUE) + set(HAS_CPPLATEST_FLAG TRUE) + endif() + + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.11) + set(HAS_CPP17_FLAG TRUE) + set(OPTIONS ${OPTIONS} /permissive-) + endif() +elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + set(CMAKE_VERBOSE_MAKEFILE ON) + set(OPTIONS -Wall -Wextra -pedantic-errors) + + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8.0) + set(HAS_CPP11_FLAG TRUE) + endif() + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9.2) + set(HAS_CPP14_FLAG TRUE) + endif() + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.1.0) + set(HAS_CPP17_FLAG TRUE) + endif() + endif() + + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3.0) + set(HAS_CPP11_FLAG TRUE) + endif() + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.4.0) + set(HAS_CPP14_FLAG TRUE) + endif() + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0.0) + set(HAS_CPP17_FLAG TRUE) + endif() + endif() +endif() + +function(enable_msvs_guideline_checker target) + if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + set_target_properties(${target} PROPERTIES + VS_GLOBAL_EnableCppCoreCheck true + VS_GLOBAL_CodeAnalysisRuleSet CppCoreCheckRules.ruleset + VS_GLOBAL_RunCodeAnalysis true) + endif() +endfunction() + +function(make_target target std) + add_executable(${target} ${SOURCES}) + target_compile_options(${target} PRIVATE ${OPTIONS}) + 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() +endfunction() + +if(HAS_CPP11_FLAG) + make_target(nameof-cpp11.t c++11) +elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + make_target(nameof-cpp11.t "") +endif() +add_test(NAME test-cpp11 COMMAND nameof-cpp11.t) + +if(HAS_CPP14_FLAG) + make_target(nameof-cpp14.t c++14) + add_test(NAME test-cpp14 COMMAND nameof-cpp14.t) +endif() + +if(HAS_CPP17_FLAG) + set(std17 c++17) + if(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang") + set(std17 c++1z) + endif() + make_target(nameof-cpp17.t ${std17}) + enable_msvs_guideline_checker(nameof-cpp17.t) + add_test(NAME test-cpp17 COMMAND nameof-cpp17.t) +endif() + +if(HAS_CPPLATEST_FLAG) + make_target(nameof-cpplatest.t c++latest) + add_test(NAME test-cpplatest COMMAND nameof-cpplatest.t) +endif()