Skip to content

Commit eebe77a

Browse files
committed
Add tests/meson.build and make it adjustable via the build option -Dtests=true
1 parent 72282bc commit eebe77a

File tree

3 files changed

+58
-50
lines changed

3 files changed

+58
-50
lines changed

meson.build

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,54 +57,6 @@ import('pkgconfig').generate(
5757
subdirs: ['.']
5858
)
5959

60-
if not meson.is_subproject()
61-
62-
catch2_dep = dependency('catch2', required : get_option('system-catch2'))
63-
if not catch2_dep.found()
64-
catch2_dep = declare_dependency(compile_args: ['-DBUNDLED_CATCH2'])
65-
else
66-
if catch2_dep.version().version_compare('>=3.0')
67-
catch2_dep = [catch2_dep, declare_dependency(compile_args: ['-DCATCH3'])]
68-
endif
69-
endif
70-
71-
testlib = static_library('testlib', 'tests/catch_main.cpp', dependencies: [catch2_dep])
72-
testdep_socket = []
73-
if host_machine.system() == 'sunos'
74-
testdep_socket = cxx.find_library('socket')
75-
endif
76-
77-
test_cpp_args = []
78-
feraiseexcept_test_code = '''
79-
#define _GNU_SOURCE
80-
#include <fenv.h>
81-
82-
int main() {
83-
feenableexcept(FE_DIVBYZERO);
84-
feraiseexcept(FE_DIVBYZERO);
85-
}
86-
'''
87-
if cxx.links(feraiseexcept_test_code,
88-
name: 'feraiseexcept')
89-
test_cpp_args += ['-DTEST_ENABLE_FERAISEEXCEPT']
90-
endif
91-
92-
test_no_opts_cpp_args = []
93-
94-
# The tests are by necessity made using undefined behavior (after all it needs to test what happens in situations that
95-
# are, for good reason, undefined behavior).
96-
# To make this a bit more likely to work, just disable link time optimization if possible.
97-
if cxx.has_argument('-fno-lto')
98-
test_no_opts_cpp_args += ['-fno-lto', '-O0']
99-
endif
100-
101-
verbose_kwargs = {}
102-
if meson.version().version_compare('>=0.62')
103-
verbose_kwargs += {'verbose': true}
104-
endif
105-
106-
test('tests', executable('tests', 'tests/tests.cpp', link_with: [testlib],
107-
dependencies: [posixsignalmanager_dep, qt5_dep, librt, catch2_dep, testdep_socket],
108-
cpp_args: test_cpp_args + test_no_opts_cpp_args),
109-
kwargs: verbose_kwargs)
60+
if get_option('tests')
61+
subdir('tests')
11062
endif

meson_options.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
# SPDX-License-Identifier: BSL-1.0
2+
3+
option('tests', type : 'boolean', value : true)
14
option('system-catch2', type : 'feature', value : 'disabled')

tests/meson.build

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-License-Identifier: BSL-1.0
2+
3+
if not meson.is_subproject()
4+
5+
catch2_dep = dependency('catch2', required : get_option('system-catch2'))
6+
if not catch2_dep.found()
7+
catch2_dep = declare_dependency(compile_args: ['-DBUNDLED_CATCH2'])
8+
else
9+
if catch2_dep.version().version_compare('>=3.0')
10+
catch2_dep = [catch2_dep, declare_dependency(compile_args: ['-DCATCH3'])]
11+
endif
12+
endif
13+
14+
testlib = static_library('testlib', 'catch_main.cpp', dependencies: [catch2_dep])
15+
testdep_socket = []
16+
if host_machine.system() == 'sunos'
17+
testdep_socket = cxx.find_library('socket')
18+
endif
19+
20+
test_cpp_args = []
21+
feraiseexcept_test_code = '''
22+
#define _GNU_SOURCE
23+
#include <fenv.h>
24+
25+
int main() {
26+
feenableexcept(FE_DIVBYZERO);
27+
feraiseexcept(FE_DIVBYZERO);
28+
}
29+
'''
30+
if cxx.links(feraiseexcept_test_code,
31+
name: 'feraiseexcept')
32+
test_cpp_args += ['-DTEST_ENABLE_FERAISEEXCEPT']
33+
endif
34+
35+
test_no_opts_cpp_args = []
36+
37+
# The tests are by necessity made using undefined behavior (after all it needs to test what happens in situations that
38+
# are, for good reason, undefined behavior).
39+
# To make this a bit more likely to work, just disable link time optimization if possible.
40+
if cxx.has_argument('-fno-lto')
41+
test_no_opts_cpp_args += ['-fno-lto', '-O0']
42+
endif
43+
44+
verbose_kwargs = {}
45+
if meson.version().version_compare('>=0.62')
46+
verbose_kwargs += {'verbose': true}
47+
endif
48+
49+
test('tests', executable('tests', 'tests.cpp', link_with: [testlib],
50+
dependencies: [posixsignalmanager_dep, qt5_dep, librt, catch2_dep, testdep_socket],
51+
cpp_args: test_cpp_args + test_no_opts_cpp_args),
52+
kwargs: verbose_kwargs)
53+
endif

0 commit comments

Comments
 (0)