1cmake_minimum_required(VERSION 3.0.2)
2
3project(GSLTests CXX)
4
5# will make visual studio generated project group files
6set_property(GLOBAL PROPERTY USE_FOLDERS ON)
7
8list(APPEND CATCH_CMAKE_ARGS
9    "-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external"
10    "-DNO_SELFTEST=true"
11)
12
13if(GIT_FOUND)
14    # add catch
15    ExternalProject_Add(
16        catch
17        PREFIX ${CMAKE_BINARY_DIR}/catch
18        GIT_REPOSITORY https://github.com/catchorg/Catch2.git
19        GIT_TAG v2.0.1
20        CMAKE_ARGS ${CATCH_CMAKE_ARGS}
21        LOG_DOWNLOAD 1
22        UPDATE_DISCONNECTED 1
23    )
24else()
25    # assume catch is installed in a system directory
26    add_custom_target(catch)
27endif()
28
29if (MSVC AND (GSL_CXX_STANDARD EQUAL 17))
30    set(GSL_CPLUSPLUS_OPT -Zc:__cplusplus -permissive-)
31endif()
32
33# this interface adds compile options to how the tests are run
34# please try to keep entries ordered =)
35add_library(gsl_tests_config INTERFACE)
36if(MSVC) # MSVC or simulating MSVC
37    target_compile_options(gsl_tests_config INTERFACE
38        ${GSL_CPLUSPLUS_OPT}
39        /EHsc
40        /W4
41        /WX
42        $<$<CXX_COMPILER_ID:Clang>:
43          -Weverything
44          -Wno-c++98-compat
45          -Wno-c++98-compat-pedantic
46          -Wno-missing-braces
47          -Wno-missing-prototypes
48          -Wno-unknown-attributes
49          $<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
50        >
51    )
52else()
53    target_compile_options(gsl_tests_config INTERFACE
54        -fno-strict-aliasing
55        -Wall
56        -Wcast-align
57        -Wconversion
58        -Wctor-dtor-privacy
59        -Werror
60        -Wextra
61        -Wpedantic
62        -Wshadow
63        -Wsign-conversion
64        $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
65          -Weverything
66          -Wno-c++98-compat
67          -Wno-c++98-compat-pedantic
68          -Wno-missing-braces
69          -Wno-missing-prototypes
70          -Wno-padded
71          -Wno-unknown-attributes
72          $<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
73          -Wno-weak-vtables
74        >
75        $<$<CXX_COMPILER_ID:Clang>:
76          $<$<CXX_COMPILER_VERSION:5.0.2>:-Wno-undefined-func-template>
77        >
78    )
79endif(MSVC)
80
81# for tests to find the catch header
82target_include_directories(gsl_tests_config INTERFACE
83    ${CMAKE_BINARY_DIR}/external/include
84)
85
86# set definitions for tests
87target_compile_definitions(gsl_tests_config INTERFACE
88    GSL_THROW_ON_CONTRACT_VIOLATION
89)
90
91# create the main executable for each test. this reduces the compile time
92# of each test by pre-compiling catch.
93add_library(test_catch STATIC test.cpp)
94target_link_libraries(test_catch
95    GSL
96    gsl_tests_config
97)
98add_dependencies(test_catch catch)
99set_property(TARGET test_catch PROPERTY FOLDER "GSL_tests")
100
101function(add_gsl_test name)
102    add_executable(${name} ${name}.cpp)
103    target_link_libraries(${name}
104        GSL
105        test_catch
106        gsl_tests_config
107    )
108    add_dependencies(${name} catch)
109    add_test(
110      ${name}
111      ${name}
112    )
113    # group all tests under GSL_tests
114    set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests")
115endfunction()
116
117add_gsl_test(span_tests)
118add_gsl_test(multi_span_tests)
119add_gsl_test(strided_span_tests)
120add_gsl_test(string_span_tests)
121add_gsl_test(at_tests)
122add_gsl_test(bounds_tests)
123add_gsl_test(notnull_tests)
124add_gsl_test(assertion_tests)
125add_gsl_test(utils_tests)
126add_gsl_test(owner_tests)
127add_gsl_test(byte_tests)
128add_gsl_test(algorithm_tests)
129add_gsl_test(strict_notnull_tests)
130
131
132# No exception tests
133
134foreach(flag_var
135        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
136        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
137    STRING (REGEX REPLACE "/EHsc" "" ${flag_var} "${${flag_var}}")
138endforeach(flag_var)
139
140# this interface adds compile options to how the tests are run
141# please try to keep entries ordered =)
142add_library(gsl_tests_config_noexcept INTERFACE)
143if(MSVC) # MSVC or simulating MSVC
144    target_compile_definitions(gsl_tests_config_noexcept INTERFACE
145        _HAS_EXCEPTIONS=0
146    )
147    target_compile_options(gsl_tests_config_noexcept INTERFACE
148        ${GSL_CPLUSPLUS_OPT}
149        /W4
150        /WX
151        $<$<CXX_COMPILER_ID:MSVC>:
152          /wd4577
153          /wd4702
154        >
155        $<$<CXX_COMPILER_ID:Clang>:
156          -Weverything
157          -Wno-c++98-compat
158          -Wno-c++98-compat-pedantic
159          -Wno-missing-prototypes
160          -Wno-unknown-attributes
161        >
162    )
163else()
164    target_compile_options(gsl_tests_config_noexcept INTERFACE
165        -fno-exceptions
166        -fno-strict-aliasing
167        -Wall
168        -Wcast-align
169        -Wconversion
170        -Wctor-dtor-privacy
171        -Werror
172        -Wextra
173        -Wpedantic
174        -Wshadow
175        -Wsign-conversion
176        $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
177          -Weverything
178          -Wno-c++98-compat
179          -Wno-c++98-compat-pedantic
180          -Wno-missing-prototypes
181          -Wno-unknown-attributes
182          -Wno-weak-vtables
183        >
184    )
185endif(MSVC)
186
187# set definitions for tests
188target_compile_definitions(gsl_tests_config_noexcept INTERFACE
189    GSL_TERMINATE_ON_CONTRACT_VIOLATION
190)
191
192function(add_gsl_test_noexcept name)
193    add_executable(${name} ${name}.cpp)
194    target_link_libraries(${name}
195        GSL
196        gsl_tests_config_noexcept
197    )
198    add_test(
199      ${name}
200      ${name}
201    )
202    # group all tests under GSL_tests_noexcept
203    set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests_noexcept")
204endfunction()
205
206add_gsl_test_noexcept(no_exception_throw_tests)
207add_gsl_test_noexcept(no_exception_ensure_tests)
208