forked from martinmoene/optional-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
174 lines (143 loc) · 5 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright 2017-2018 by Martin Moene
#
# https://github.com/martinmoene/optional-lite
#
# This code is licensed under the MIT License (MIT).
cmake_minimum_required( VERSION 3.0 )
project( test )
set( MAIN_BASE optional-main )
set( HDRNAME optional.hpp )
set( SOURCES ${MAIN_BASE}.t.cpp optional.t.cpp )
set( HDRDIR ${PROJECT_SOURCE_DIR}/../include/nonstd )
set( HDRPATH ${HDRDIR}/${HDRNAME} )
set( OPTIONS "" )
set( DEFCMN "" )
set( HAS_STD_FLAGS FALSE )
set( HAS_CPP98_FLAG FALSE )
set( HAS_CPP03_FLAG FALSE )
set( HAS_CPP11_FLAG FALSE )
set( HAS_CPP14_FLAG FALSE )
set( HAS_CPP17_FLAG FALSE )
set( HAS_CPP20_FLAG FALSE )
if( MSVC )
set( HAS_STD_FLAGS TRUE )
set( STD_OPT -std: )
set( OPTIONS -W3 -EHsc )
set( DEFINITIONS -D_SCL_SECURE_NO_WARNINGS -DNOMINMAX ${DEFCMN} )
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 )
endif()
elseif( CMAKE_CXX_COMPILER_ID MATCHES GNU OR
CMAKE_CXX_COMPILER_ID MATCHES Clang )
set( HAS_STD_FLAGS TRUE )
set( HAS_CPP98_FLAG TRUE )
set( HAS_CPP03_FLAG TRUE )
set( STD_OPT -std= )
set( OPTIONS -Wall -Wno-missing-braces -fno-elide-constructors )
set( DEFINITIONS ${DEFCMN} )
# GNU: available -std flags depends on version
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()
# Clang: available -std flags depends on version
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()
elseif( CMAKE_CXX_COMPILER_ID MATCHES Intel )
# as is
else()
# as is
endif()
# enable MS C++ Core Guidelines checker if MSVC:
function( enable_msvs_guideline_checker target )
if( MSVC )
set_target_properties( ${target} PROPERTIES
VS_GLOBAL_EnableCppCoreCheck true
VS_GLOBAL_CodeAnalysisRuleSet CppCoreCheckRules.ruleset
VS_GLOBAL_RunCodeAnalysis true )
endif()
endfunction()
# make target, compile for given standard if specified:
function( make_target target std )
if( std )
set( OPTIONAL_STD ${STD_OPT}${std} )
endif()
add_executable ( ${target} ${SOURCES} ${HDRPATH} )
target_compile_options ( ${target} PRIVATE ${OPTIONS} ${OPTIONAL_STD} )
target_compile_definitions( ${target} PRIVATE ${DEFINITIONS} )
target_include_directories( ${target} PRIVATE ${HDRDIR} )
endfunction()
# add generic executable, unless -std flags can be specified:
if( NOT HAS_STD_FLAGS )
make_target( ${MAIN_BASE}.t "" )
else()
# unconditionally add C++98 variant as MSVC has no option for it:
if( HAS_CPP98_FLAG )
make_target( ${MAIN_BASE}-cpp98.t c++98 )
else()
make_target( ${MAIN_BASE}-cpp98.t "" )
endif()
if( HAS_CPP03_FLAG )
make_target( ${MAIN_BASE}-cpp03.t c++03 )
endif()
if( HAS_CPP11_FLAG )
make_target( ${MAIN_BASE}-cpp11.t c++11 )
endif()
if( HAS_CPP14_FLAG )
make_target( ${MAIN_BASE}-cpp14.t c++14 )
endif()
if( HAS_CPP17_FLAG )
make_target( ${MAIN_BASE}-cpp17.t c++17 )
enable_msvs_guideline_checker( ${MAIN_BASE}-cpp17.t )
endif()
if( HAS_CPPLATEST_FLAG )
make_target( ${MAIN_BASE}-cpplatest.t c++latest )
endif()
endif()
# configure unit tests via CTest:
enable_testing()
if( HAS_STD_FLAGS )
# unconditionally add C++98 variant for MSVC:
add_test( NAME test-cpp98 COMMAND ${MAIN_BASE}-cpp98.t )
if( HAS_CPP03_FLAG )
add_test( NAME test-cpp03 COMMAND ${MAIN_BASE}-cpp03.t )
endif()
if( HAS_CPP11_FLAG )
add_test( NAME test-cpp11 COMMAND ${MAIN_BASE}-cpp11.t )
endif()
if( HAS_CPP14_FLAG )
add_test( NAME test-cpp14 COMMAND ${MAIN_BASE}-cpp14.t )
endif()
if( HAS_CPP17_FLAG )
add_test( NAME test-cpp17 COMMAND ${MAIN_BASE}-cpp17.t )
endif()
if( HAS_CPPLATEST_FLAG )
add_test( NAME test-cpplatest COMMAND ${MAIN_BASE}-cpplatest.t )
endif()
else()
add_test( NAME test COMMAND ${MAIN_BASE}.t --pass )
add_test( NAME list_version COMMAND ${MAIN_BASE}.t --version )
add_test( NAME list_tags COMMAND ${MAIN_BASE}.t --list-tags )
add_test( NAME list_tests COMMAND ${MAIN_BASE}.t --list-tests )
endif()
# end of file