Skip to content

Commit fda84fb

Browse files
committed
meson: Add a Meson based build system
This is primarily intended to be useful for projects using Meson wishing to consume cxxopts as a subproject. By hosting the files upstream users get the benefit of automatic parity with the CMake based install provided by an OS vendor or distribution. The implementation attempts to mirror the CMake build as much as possible, with the exception of generating the cmake-config files, which Meson doesn't currently support. It uses a small python script to parse the cxxopts.hpp header to extract the version, due to a concious design decision of Meson to leave such complex logic to external scripting languages like Python.
1 parent dcf3e1a commit fda84fb

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

meson.build

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright © 2024 Dylan Baker
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
project(
22+
'cxxopts',
23+
'cpp',
24+
version : run_command(
25+
'meson/version.py', files('include/cxxopts.hpp'), capture : true, check : true).stdout().strip(),
26+
meson_version : '>= 0.64',
27+
license : 'MIT',
28+
default_options : ['cpp_std=c++11', 'warning_level=2'],
29+
)
30+
31+
cpp = meson.get_compiler('cpp')
32+
33+
with_warnings = get_option('warnings').disable_auto_if(meson.is_subproject()).allowed()
34+
if with_warnings
35+
add_project_arguments(
36+
cpp.get_supported_arguments(
37+
'-Wsuggest-override',
38+
'-Wshadow',
39+
'-Weffc++',
40+
'-Wsign-compare',
41+
'-Wshadow',
42+
'-Wwrite-strings',
43+
'-Wpointer-arith',
44+
'-Winit-self',
45+
'-Wconversion',
46+
'-Wno-sign-conversion',
47+
),
48+
language : 'cpp',
49+
)
50+
endif
51+
52+
install_headers('include/cxxopts.hpp')
53+
54+
dep_icu = dependency('icu-uc', required : get_option('icu'))
55+
if dep_icu.found()
56+
add_project_arguments('-DCXXOPTS_USE_UNICODE', language : 'cpp')
57+
endif
58+
59+
with_examples = get_option('examples').disable_auto_if(meson.is_subproject()).allowed()
60+
if with_examples
61+
executable(
62+
'example',
63+
'src/example.cpp',
64+
include_directories : 'include',
65+
dependencies : dep_icu,
66+
override_options : ['cpp_std=c++17'],
67+
)
68+
endif
69+
70+
with_tests = get_option('tests').disable_auto_if(meson.is_subproject()).allowed()
71+
if with_tests
72+
test(
73+
'link',
74+
executable(
75+
'link_test',
76+
'test/link_a.cpp', 'test/link_b.cpp',
77+
dependencies : dep_icu,
78+
include_directories : 'include',
79+
)
80+
)
81+
82+
# Fuzzer test is not as trivial as it should be
83+
84+
# Meson can generate basic cmake-configs files, but not when targets are used,
85+
# so these tests don't make sense
86+
endif
87+
88+
extra_cflags = []
89+
if dep_icu.found()
90+
extra_cflags += ['-DCXXOPTS_USE_UNICODE']
91+
endif
92+
93+
dep_cxxopts = declare_dependency(
94+
include_directories : 'include',
95+
dependencies : dep_icu,
96+
compile_args : extra_cflags,
97+
)
98+
99+
meson.override_dependency('cxxopts', dep_cxxopts)
100+
101+
pkg = import('pkgconfig')
102+
103+
pkg.generate(
104+
name : 'cxxopts',
105+
description : 'A header-only lightweight C++ command line option parser',
106+
url : 'https://github.com/jarro2783/cxxopts',
107+
extra_cflags : extra_cflags,
108+
requires : dep_icu,
109+
dataonly : not dep_icu.found(),
110+
)

meson/version.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
# Copyright © 2024 Dylan Baker
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
#
11+
# The above copyright notice and this permission notice shall be included in
12+
# all copies or substantial portions of the Software.
13+
#
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
# THE SOFTWARE.
21+
22+
"""Parse the cxxopts.hpp header to get the version."""
23+
24+
import sys
25+
26+
def main():
27+
versions = [None, None, None]
28+
with open(sys.argv[1], 'r', encoding='ascii') as f:
29+
for line in f:
30+
if line.startswith('#define CXXOPTS__VERSION_'):
31+
ver = line.rstrip().rsplit(' ', 1)[-1]
32+
if 'MAJOR' in line:
33+
versions[0] = ver
34+
elif 'MINOR' in line:
35+
versions[1] = ver
36+
elif 'PATCH' in line:
37+
versions[2] = ver
38+
if None not in versions:
39+
break
40+
41+
assert None not in versions, \
42+
"Did not find all of the expected version strings in cxxopts.hpp"
43+
print('.'.join(versions))
44+
45+
46+
if __name__ == "__main__":
47+
main()

meson_options.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright © 2024 Dylan Baker
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
option(
22+
'examples',
23+
type : 'feature',
24+
description : 'Whether to build examples. Defaults to enabled when not built as a subproject, otherwise disabled.',
25+
)
26+
option(
27+
'tests',
28+
type : 'feature',
29+
description : 'Whether to build tests. Defaults to enabled when not built as a subproject, otherwise disabled.',
30+
)
31+
option(
32+
'warnings',
33+
type : 'feature',
34+
description : 'Whether to add additional warnings. Defaults to enabled when not built as a subproject, otherwise disabled.',
35+
)
36+
option(
37+
'icu',
38+
type : 'feature',
39+
value : 'disabled',
40+
description : 'use ICU Unicode library.',
41+
)

0 commit comments

Comments
 (0)