-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathCMakeLists.txt
225 lines (183 loc) · 6.89 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2017-2018 by Martin Moene
#
# https://github.com/martinmoene/optional-lite
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
if( NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION )
cmake_minimum_required( VERSION 3.5 FATAL_ERROR )
endif()
project( test LANGUAGES CXX )
set( unit_name "optional" )
set( PACKAGE ${unit_name}-lite )
set( PROGRAM ${unit_name}-lite )
set( SOURCES ${unit_name}-main.t.cpp ${unit_name}.t.cpp )
set( TWEAKD "." )
message( STATUS "Subproject '${PROJECT_NAME}', programs '${PROGRAM}-*'")
set( OPTIONS "" )
set( DEFCMN "" )
set( HAS_STD_FLAGS FALSE )
set( HAS_CPP98_FLAG FALSE )
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( MSVC )
message( STATUS "Matched: MSVC")
set( HAS_STD_FLAGS TRUE )
set( OPTIONS -W3 -EHsc )
set( DEFINITIONS -D_SCL_SECURE_NO_WARNINGS ${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|Clang|AppleClang" )
message( STATUS "CompilerId: '${CMAKE_CXX_COMPILER_ID}'")
set( HAS_STD_FLAGS TRUE )
set( HAS_CPP98_FLAG TRUE )
set( OPTIONS -Wall -Wextra -Wconversion -Wsign-conversion -Wno-missing-braces -fno-elide-constructors )
set( DEFINITIONS ${DEFCMN} )
# GNU: available -std flags depends on version
if( CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
message( STATUS "Matched: 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()
# AppleClang: available -std flags depends on version
elseif( CMAKE_CXX_COMPILER_ID MATCHES "AppleClang" )
message( STATUS "Matched: AppleClang")
if( NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0.0 )
set( HAS_CPP11_FLAG TRUE )
endif()
if( NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1.0 )
set( HAS_CPP14_FLAG TRUE )
endif()
if( NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.2.0 )
set( HAS_CPP17_FLAG TRUE )
endif()
# Clang: available -std flags depends on version
elseif( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
message( STATUS "Matched: 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
message( STATUS "Matched: Intel")
else()
# as is
message( STATUS "Matched: nothing")
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 )
message( STATUS "Make target: '${std}'" )
add_executable ( ${target} ${SOURCES} )
target_include_directories( ${target} SYSTEM PRIVATE lest )
target_include_directories( ${target} PRIVATE ${TWEAKD} )
target_link_libraries ( ${target} PRIVATE ${PACKAGE} )
target_compile_options ( ${target} PRIVATE ${OPTIONS} )
target_compile_definitions( ${target} PRIVATE ${DEFINITIONS} )
if( std )
if( MSVC )
target_compile_options( ${target} PRIVATE -std:c++${std} )
else()
# Necessary for clang 3.x:
target_compile_options( ${target} PRIVATE -std=c++${std} )
# Ok for clang 4 and later:
# set( CMAKE_CXX_STANDARD ${std} )
# set( CMAKE_CXX_STANDARD_REQUIRED ON )
# set( CMAKE_CXX_EXTENSIONS OFF )
endif()
endif()
endfunction()
# add generic executable, unless -std flags can be specified:
if( NOT HAS_STD_FLAGS )
make_target( ${PROGRAM}.t "" )
else()
# unconditionally add C++98 variant as MSVC has no option for it:
if( HAS_CPP98_FLAG )
make_target( ${PROGRAM}-cpp98.t 98 )
else()
make_target( ${PROGRAM}-cpp98.t "" )
endif()
if( HAS_CPP11_FLAG )
make_target( ${PROGRAM}-cpp11.t 11 )
endif()
if( HAS_CPP14_FLAG )
make_target( ${PROGRAM}-cpp14.t 14 )
endif()
if( HAS_CPP17_FLAG )
set( std17 17 )
if( CMAKE_CXX_COMPILER_ID MATCHES "AppleClang" )
set( std17 1z )
endif()
make_target( ${PROGRAM}-cpp17.t ${std17} )
enable_msvs_guideline_checker( ${PROGRAM}-cpp17.t )
endif()
if( HAS_CPPLATEST_FLAG )
make_target( ${PROGRAM}-cpplatest.t latest )
endif()
endif()
# with C++17, honour explicit request for std::optional or nonstd::optional:
if( HAS_CPP17_FLAG )
set( WHICH optional_OPTIONAL_DEFAULT )
if( OPTIONAL_LITE_OPT_SELECT_STD )
set( WHICH optional_OPTIONAL_STD )
elseif( OPTIONAL_LITE_OPT_SELECT_NONSTD )
set( WHICH optional_OPTIONAL_NONSTD )
endif()
target_compile_definitions( ${PROGRAM}-cpp17.t PRIVATE optional_CONFIG_SELECT_OPTIONAL=${WHICH} )
if( HAS_CPPLATEST_FLAG )
target_compile_definitions( ${PROGRAM}-cpplatest.t PRIVATE optional_CONFIG_SELECT_OPTIONAL=${WHICH} )
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 ${PROGRAM}-cpp98.t )
if( HAS_CPP11_FLAG )
add_test( NAME test-cpp11 COMMAND ${PROGRAM}-cpp11.t )
endif()
if( HAS_CPP14_FLAG )
add_test( NAME test-cpp14 COMMAND ${PROGRAM}-cpp14.t )
endif()
if( HAS_CPP17_FLAG )
add_test( NAME test-cpp17 COMMAND ${PROGRAM}-cpp17.t )
endif()
if( HAS_CPPLATEST_FLAG )
add_test( NAME test-cpplatest COMMAND ${PROGRAM}-cpplatest.t )
endif()
else()
add_test( NAME test COMMAND ${PROGRAM}.t --pass )
add_test( NAME list_version COMMAND ${PROGRAM}.t --version )
add_test( NAME list_tags COMMAND ${PROGRAM}.t --list-tags )
add_test( NAME list_tests COMMAND ${PROGRAM}.t --list-tests )
endif()
# end of file