|
| 1 | +cmake_minimum_required(VERSION 3.11) # for FetchContent |
| 2 | +project(scope_guard) |
| 3 | + |
| 4 | +# handle inclusions and dependencies |
| 5 | +include(CheckCXXSymbolExists) |
| 6 | +include(CheckCXXCompilerFlag) |
| 7 | +include(GNUInstallDirs) |
| 8 | + |
| 9 | + |
| 10 | +add_library(scope_guard INTERFACE) |
| 11 | +add_library(scope_guard::scope_guard ALIAS scope_guard) # for consumption via add_subdirectory() |
| 12 | + |
| 13 | +target_include_directories(scope_guard |
| 14 | + INTERFACE |
| 15 | + include # assuming we move the header file to an `include` directory |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +# Include(FetchContent) |
| 22 | + |
| 23 | +# FetchContent_Declare( |
| 24 | +# Catch2 |
| 25 | +# GIT_REPOSITORY https://github.com/catchorg/Catch2.git |
| 26 | +# GIT_TAG v2.13.6 |
| 27 | +# ) |
| 28 | + |
| 29 | +# FetchContent_MakeAvailable(Catch2) |
| 30 | + |
| 31 | +# # global configurations |
| 32 | +# set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 33 | +# set(CMAKE_CXX_EXTENSIONS OFF) |
| 34 | + |
| 35 | +# # check for compiler support of noexcept in type |
| 36 | +# set(CMAKE_REQUIRED_FLAGS "-std=c++1z") # only for this check |
| 37 | +# CHECK_CXX_SYMBOL_EXISTS(__cpp_noexcept_function_type "" HAS_NOEXCEPT_IN_TYPE) |
| 38 | +# unset(CMAKE_REQUIRED_FLAGS) |
| 39 | + |
| 40 | +# # request C++11 explicitly by default - works around newer compilers otherwise |
| 41 | +# # omitting `-std=c++11` despite 11 being requested with target_compile_features |
| 42 | +# set(CMAKE_CXX_STANDARD 11) |
| 43 | + |
| 44 | +# # compiler warnings |
| 45 | +# if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 46 | +# set(clang_warnings "-Werror -Wall -Wextra -pedantic -Wno-c++98-compat \ |
| 47 | +# -Wno-unreachable-code -Wno-padded -Wno-exit-time-destructors \ |
| 48 | +# -Wno-global-constructors -Wno-unused-variable -Wno-unused-member-function \ |
| 49 | +# -Wno-unused-function -Wno-class-varargs") |
| 50 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${clang_warnings}") |
| 51 | +# elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") |
| 52 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wpedantic \ |
| 53 | +# -Wno-unused") |
| 54 | +# if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10) |
| 55 | +# # earlier versions complain of unused "nodiscard" results in unevaluated contexts |
| 56 | +# # (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89070) |
| 57 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-result") |
| 58 | +# endif() |
| 59 | + |
| 60 | +# check_cxx_compiler_flag("-Wnoexcept-type" HAS_NOEXCEPT_TYPE_WARNING) |
| 61 | +# if(HAS_NOEXCEPT_TYPE_WARNING) |
| 62 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-noexcept-type") |
| 63 | +# endif() |
| 64 | + |
| 65 | +# option(ENABLE_COVERAGE "Enable coverage reporting" FALSE) |
| 66 | +# elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
| 67 | +# if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") |
| 68 | +# string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 69 | +# else() |
| 70 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") |
| 71 | +# endif() |
| 72 | +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") |
| 73 | +# endif() |
| 74 | + |
| 75 | +# # utility to derive a string with success or failure from boolean param |
| 76 | +# function(expect_str ret should_succeed) |
| 77 | +# if(${should_succeed}) |
| 78 | +# set(${ret} "success" PARENT_SCOPE) |
| 79 | +# else() |
| 80 | +# set(${ret} "failure" PARENT_SCOPE) |
| 81 | +# endif() |
| 82 | +# endfunction() |
| 83 | + |
| 84 | +# # utility to derive noexcept requirement status string from boolean param |
| 85 | +# function(noexc_str ret require_noexcept) |
| 86 | +# if(${require_noexcept}) |
| 87 | +# set(${ret} "reqnoexc" PARENT_SCOPE) |
| 88 | +# else() |
| 89 | +# set(${ret} "allowexc" PARENT_SCOPE) |
| 90 | +# endif() |
| 91 | +# endfunction() |
| 92 | + |
| 93 | +# # utility to derive standard number from boolean param |
| 94 | +# function(std_num ret use_cxx17) |
| 95 | +# if(${use_cxx17}) |
| 96 | +# set(${ret} 17 PARENT_SCOPE) |
| 97 | +# else() |
| 98 | +# set(${ret} 11 PARENT_SCOPE) |
| 99 | +# endif() |
| 100 | +# endfunction() |
| 101 | + |
| 102 | +# # utility to derive a string describing the c++ standard |
| 103 | +# function(std_str ret stdn) |
| 104 | +# set(${ret} cpp${stdn} PARENT_SCOPE) |
| 105 | +# endfunction() |
| 106 | + |
| 107 | +# # utility to derive the c++ standard compile feature |
| 108 | +# function(std_ftr ret stdn) |
| 109 | +# set(${ret} cxx_std_${stdn} PARENT_SCOPE) |
| 110 | +# endfunction() |
| 111 | + |
| 112 | +# # utility to derive strings for test configuration |
| 113 | +# function(derive_common_test_strings |
| 114 | +# tst_ret exe_ret ftr_ret # out params |
| 115 | +# base_name should_succeed cxx17 require_noexcept # in params |
| 116 | +# ) |
| 117 | +# expect_str(expect ${should_succeed}) |
| 118 | +# noexc_str(noexc ${require_noexcept}) |
| 119 | +# std_num(stdn ${cxx17}) |
| 120 | +# std_str(std ${stdn}) |
| 121 | +# std_ftr(${ftr_ret} ${stdn}) |
| 122 | + |
| 123 | +# string(CONCAT ${exe_ret} ${base_name} "_" ${expect} "_" ${std} "_" |
| 124 | +# ${noexc}) |
| 125 | +# string(CONCAT ${tst_ret} "test_" ${${exe_ret}}) |
| 126 | + |
| 127 | +# # return these |
| 128 | +# set(${tst_ret} ${${tst_ret}} PARENT_SCOPE) |
| 129 | +# set(${exe_ret} ${${exe_ret}} PARENT_SCOPE) |
| 130 | +# set(${ftr_ret} ${${ftr_ret}} PARENT_SCOPE) |
| 131 | +# endfunction() |
| 132 | + |
| 133 | +# # utility to add executable and configure common properties |
| 134 | +# function(add_test_exe exe src ftr require_noexcept) |
| 135 | +# add_executable(${exe} ${src}) |
| 136 | +# target_compile_features(${exe} PRIVATE ${ftr}) |
| 137 | +# if(${require_noexcept}) |
| 138 | +# target_compile_definitions(${exe} PRIVATE SG_REQUIRE_NOEXCEPT_IN_CPP17) |
| 139 | +# endif() |
| 140 | +# endfunction() |
| 141 | + |
| 142 | +# # utility to add a batch of catch tests with the specified c++ standard and |
| 143 | +# # noexcept requirement |
| 144 | +# function(add_catch_tests_batch exe_ret src cxx17 require_noexcept) |
| 145 | +# derive_common_test_strings(tst exe ftr # out params |
| 146 | +# "catch_batch" TRUE ${cxx17} ${require_noexcept}) # in params |
| 147 | +# add_test_exe(${exe} ${src} ${ftr} ${require_noexcept}) |
| 148 | +# target_link_libraries(${exe} PRIVATE Catch2::Catch2) |
| 149 | + |
| 150 | +# add_test(NAME ${tst} COMMAND ${exe} "--order" "lex") |
| 151 | + |
| 152 | +# set(${exe_ret} ${exe} PARENT_SCOPE) # return |
| 153 | +# endfunction() |
| 154 | + |
| 155 | +# # utility to add a compilation test, with the specified C++ standard and |
| 156 | +# # noexcept requirement, along with a success/failure expectation and a counter |
| 157 | +# # that identifies what parts of the code to activate |
| 158 | +# function(add_compilation_test src should_succeed cxx17 require_noexcept countid) |
| 159 | +# derive_common_test_strings(tst exe ftr # out params |
| 160 | +# "compilation" ${should_succeed} ${cxx17} ${require_noexcept}) # in params |
| 161 | + |
| 162 | +# string(APPEND tst "_" ${countid}) |
| 163 | +# string(APPEND exe "_" ${countid}) |
| 164 | +# string(CONCAT def "test_" ${countid}) |
| 165 | +# add_test_exe(${exe} ${src} ${ftr} ${require_noexcept}) |
| 166 | + |
| 167 | +# # only build this when running tests (building _is_ the test) |
| 168 | +# set_target_properties(${exe} |
| 169 | +# PROPERTIES EXCLUDE_FROM_ALL ON |
| 170 | +# EXCLUDE_FROM_DEFAULT_BUILD ON) |
| 171 | + |
| 172 | +# # add macro to select active code section |
| 173 | +# target_compile_definitions(${exe} PRIVATE ${def}) |
| 174 | + |
| 175 | +# # determine the test command |
| 176 | +# set(tst_cmd ${CMAKE_COMMAND} --build . --target ${exe} --config $<CONFIG>) |
| 177 | + |
| 178 | +# # if using MSBuild, prevent logo and warning summary |
| 179 | +# if(${CMAKE_GENERATOR} MATCHES "[vV]isual [sS]tudio") |
| 180 | +# list(APPEND tst_cmd -- /nologo /verbosity:minimal) |
| 181 | +# endif() |
| 182 | + |
| 183 | +# # create the test |
| 184 | +# add_test(NAME ${tst} |
| 185 | +# COMMAND ${tst_cmd} |
| 186 | +# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) |
| 187 | + |
| 188 | +# # configure the test to expect compilation success or failure |
| 189 | +# if(${should_succeed}) |
| 190 | +# set_tests_properties(${tst} # confirm no warnings in successful test |
| 191 | +# PROPERTIES FAIL_REGULAR_EXPRESSION "warn;Warn;WARN") |
| 192 | +# else() |
| 193 | +# set_tests_properties(${tst} PROPERTIES WILL_FAIL TRUE) # expect a failure |
| 194 | +# endif() |
| 195 | +# endfunction() |
| 196 | + |
| 197 | +# # utility to determine whether a compilation test should expect building to fail |
| 198 | +# function(expect_result ret count cxx17 reqne) |
| 199 | +# if(${count} LESS 35) |
| 200 | +# set(${ret} TRUE PARENT_SCOPE) |
| 201 | + |
| 202 | +# elseif(${count} LESS 52) |
| 203 | +# if(${cxx17} AND ${reqne}) |
| 204 | +# set(${ret} FALSE PARENT_SCOPE) |
| 205 | +# else() |
| 206 | +# set(${ret} TRUE PARENT_SCOPE) |
| 207 | +# endif() |
| 208 | +# else() |
| 209 | +# set(${ret} FALSE PARENT_SCOPE) |
| 210 | +# endif() |
| 211 | +# endfunction() |
| 212 | + |
| 213 | +# set(cxx17_possibilities FALSE) |
| 214 | +# if(HAS_NOEXCEPT_IN_TYPE) |
| 215 | +# list(APPEND cxx17_possibilities TRUE) |
| 216 | +# endif() |
| 217 | + |
| 218 | +# # actually add the tests |
| 219 | +# foreach(cxx17 ${cxx17_possibilities}) |
| 220 | +# foreach(reqne FALSE TRUE) |
| 221 | +# # add catch tests for this standard/noexcept-requirement combination |
| 222 | +# add_catch_tests_batch(catch_batch_exe catch_tests.cpp ${cxx17} ${reqne}) |
| 223 | + |
| 224 | +# if(ENABLE_COVERAGE) # configure catch tests for coverage if needed |
| 225 | +# target_compile_options(${catch_batch_exe} PRIVATE --coverage -O0) |
| 226 | +# target_link_libraries(${catch_batch_exe} PRIVATE --coverage) |
| 227 | +# else() # only run compile time tests in non-coverage builds |
| 228 | +# # add noexcept compilation tests for this standard/noexcept-requirement |
| 229 | +# # combination |
| 230 | +# foreach(count RANGE 71) # range inclusive in cmake (so 0 or 72 tests) |
| 231 | +# # 0-34: always succeed (0 = all test macros off) |
| 232 | +# # 35-51: fail when requiring noexcept |
| 233 | +# # 52-71: always fail |
| 234 | +# expect_result(success ${count} ${cxx17} ${reqne}) |
| 235 | +# add_compilation_test(compile_time_tests.cpp |
| 236 | +# ${success} ${cxx17} ${reqne} ${count}) |
| 237 | +# endforeach() |
| 238 | +# endif() |
| 239 | +# endforeach() |
| 240 | +# endforeach() |
| 241 | + |
| 242 | +# add_custom_target(test_verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose) |
| 243 | +# enable_testing() |
0 commit comments