Skip to content

Commit 1541e82

Browse files
committed
Add Meson support
1 parent e63911d commit 1541e82

4 files changed

Lines changed: 312 additions & 0 deletions

File tree

examples/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
foreach example_name : [
2+
'cpp_class1',
3+
'cpp_class2',
4+
'cpp_class3',
5+
'cpp_class4',
6+
'cpp_inheritance',
7+
]
8+
executable(example_name, example_name + '.cpp', dependencies: internal_dep)
9+
endforeach

meson.build

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
project(
2+
'libcppgenerate',
3+
'cpp',
4+
version: '0.2',
5+
default_options: ['warning_level=3', 'cpp_std=c++11', 'default_library=both'],
6+
)
7+
8+
# Version of dynamic library
9+
library_version = '0.0.2'
10+
11+
# Do not install unnecessary things when used as a subproject
12+
install = not meson.is_subproject()
13+
14+
src = files(
15+
'cppgenerate/argument.cpp',
16+
'cppgenerate/class.cpp',
17+
'cppgenerate/codeblock.cpp',
18+
'cppgenerate/constructor.cpp',
19+
'cppgenerate/cppgenerateutils.cpp',
20+
'cppgenerate/enum.cpp',
21+
'cppgenerate/membervariable.cpp',
22+
'cppgenerate/method.cpp',
23+
'cppgenerate/printer.cpp',
24+
'cppgenerate/variable.cpp',
25+
)
26+
27+
# Set on Windows by default. When enabled, use .lib suffix for static libraries
28+
# and do not build shared ones in Windows.
29+
cmake_compatible_behavior = (
30+
host_machine.system() in ['windows', 'cygwin']
31+
and get_option('cmake-compatible-libraries')
32+
)
33+
34+
shared_lib = []
35+
building_shared_lib = (
36+
not cmake_compatible_behavior
37+
and get_option('default_library') in ['shared', 'both']
38+
)
39+
static_lib = []
40+
building_static_lib = (
41+
cmake_compatible_behavior
42+
or get_option('default_library') in ['shared', 'both']
43+
)
44+
45+
if (cmake_compatible_behavior)
46+
if (get_option('default_library') == 'shared')
47+
warning(
48+
'Shared libraries will not be generated on Windows.',
49+
'Set -Dcmake-compatible-libraries=false to override',
50+
)
51+
endif
52+
static_lib = static_library(
53+
'cppgenerate',
54+
src,
55+
name_suffix: 'lib',
56+
)
57+
elif building_shared_lib and building_static_lib
58+
if install
59+
both_libs = both_libraries(
60+
'cppgenerate',
61+
src,
62+
version: library_version,
63+
install: true,
64+
)
65+
static_lib = both_libs.get_static_lib()
66+
shared_lib = both_libs.get_shared_lib()
67+
else
68+
# When in a subproject, only install the necessary shared library
69+
static_lib = static_library(
70+
'cppgenerate',
71+
src,
72+
)
73+
shared_lib = shared_library(
74+
'cppgenerate',
75+
src,
76+
version: library_version,
77+
install: true,
78+
)
79+
endif
80+
elif building_shared_lib
81+
shared_lib = shared_library(
82+
'cppgenerate',
83+
src,
84+
version: library_version,
85+
install: true,
86+
)
87+
elif building_static_lib
88+
static_lib = static_library(
89+
'cppgenerate',
90+
src,
91+
install: install,
92+
)
93+
endif
94+
95+
if install
96+
install_headers(
97+
'cppgenerate/accessmodifier.h',
98+
'cppgenerate/argument.h',
99+
'cppgenerate/class.h',
100+
'cppgenerate/codeblock.h',
101+
'cppgenerate/constructor.h',
102+
'cppgenerate/cppgenerateutils.h',
103+
'cppgenerate/enum.h',
104+
'cppgenerate/membervariable.h',
105+
'cppgenerate/method.h',
106+
'cppgenerate/printer.h',
107+
'cppgenerate/variable.h',
108+
subdir: 'cppgenerate',
109+
)
110+
endif
111+
112+
# Configure .pc files
113+
fs = import('fs')
114+
115+
# Base configuration object used for shared library
116+
dynamic_lib_cfg = configuration_data()
117+
dynamic_lib_cfg.set('PROJECT_VERSION', meson.project_version())
118+
dynamic_lib_cfg.set('PKG_CONFIG_REQUIRES', '')
119+
dynamic_lib_cfg.set('CMAKE_INSTALL_PREFIX', get_option('prefix'))
120+
if fs.is_absolute(get_option('includedir'))
121+
dynamic_lib_cfg.set('PKG_CONFIG_INCLUDEDIR', get_option('includedir'))
122+
else
123+
dynamic_lib_cfg.set(
124+
'PKG_CONFIG_INCLUDEDIR',
125+
'${prefix}' / get_option('includedir'),
126+
)
127+
endif
128+
if fs.is_absolute(get_option('libdir'))
129+
dynamic_lib_cfg.set('PKG_CONFIG_LIBDIR', get_option('libdir'))
130+
else
131+
dynamic_lib_cfg.set('PKG_CONFIG_LIBDIR', '${prefix}' / get_option('libdir'))
132+
endif
133+
dynamic_lib_cfg.set('PKG_CONFIG_LIBS', '-L${libdir} -lcppgenerate')
134+
dynamic_lib_cfg.set('PKG_CONFIG_CFLAGS', '-I${includedir}')
135+
136+
# Static library configuration object based off of dynamic_lib_cfg
137+
static_lib_cfg = dynamic_lib_cfg
138+
139+
static_lib_cfg.set('PKG_CONFIG_STATIC_LIBS', '-L${libdir} -l:libcppgenerate.a')
140+
141+
# Configure and install pkg-config files if the relevant library is being built
142+
if host_machinene.system() not in ['windows', 'cygwin']
143+
if install and building_shared_lib
144+
configure_file(
145+
input: 'cppgenerate.pc.cmake',
146+
output: 'cppgenerate.pc',
147+
configuration: dynamic_lib_cfg,
148+
format: 'cmake',
149+
install: true,
150+
install_dir: get_option('libdir') / 'pkgconfig',
151+
)
152+
endif
153+
if install and building_static_lib
154+
configure_file(
155+
input: 'cppgenerate-static.pc.cmake',
156+
output: 'cppgenerate-static.pc',
157+
configuration: static_lib_cfg,
158+
format: 'cmake',
159+
install: true,
160+
install_dir: get_option('libdir') / 'pkgconfig',
161+
)
162+
endif
163+
endif
164+
165+
# Declare dependency when used as a subproject
166+
if building_shared_lib
167+
shared_dep = declare_dependency(
168+
link_with: shared_lib,
169+
include_directories: '.',
170+
)
171+
meson.override_dependency('cppgenerate', shared_dep)
172+
endif
173+
if building_static_lib
174+
static_dep = declare_dependency(
175+
link_with: static_lib,
176+
include_directories: '.',
177+
)
178+
meson.override_dependency('cppgenerate-static', static_dep)
179+
endif
180+
181+
# Pick an appropriate dependency object for building tests and examples
182+
if get_option('default_library') == 'both'
183+
if meson.version().version_compare('>=1.6.0')
184+
if (
185+
get_option('default_both_libraries') in ['shared', 'auto']
186+
and building_shared_lib
187+
)
188+
internal_lib = shared_lib
189+
else
190+
internal_lib = static_lib
191+
endif
192+
elif building_shared_lib
193+
internal_lib = shared_lib
194+
else
195+
internal_lib = static_lib
196+
endif
197+
elif building_shared_lib
198+
internal_lib = shared_lib
199+
elif building_static_lib
200+
internal_lib = static_lib
201+
endif
202+
203+
internal_dep = declare_dependency(
204+
link_with: internal_lib,
205+
include_directories: '.',
206+
)
207+
208+
if get_option('enable-tests')
209+
subdir('tests')
210+
endif
211+
if get_option('enable-examples')
212+
subdir('examples')
213+
endif

meson_options.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
option(
2+
'enable-tests',
3+
type: 'boolean',
4+
value: false,
5+
description: 'Enable the unit tests',
6+
)
7+
8+
option(
9+
'enable-examples',
10+
type: 'boolean',
11+
value: false,
12+
description: 'Enable the examples',
13+
)
14+
15+
option(
16+
'cmake-compatible-libraries',
17+
type: 'boolean',
18+
value: true,
19+
description: 'Use .lib suffix for static libraries and do not generate dynamic libraries on Windows. Uses default Meson behavior when false (this enables dynamic library generation on Windows).',
20+
)

tests/meson.build

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# Class tests
3+
#
4+
5+
# Iterate over list of test data. The list is comprised of:
6+
# [generator name, [files generated by generator,], test name]
7+
foreach test_data : [
8+
['generate-class1', ['FooClass.h', 'FooClass.cpp'], 'test1'],
9+
[
10+
'generate-namespace-class',
11+
['NamespaceClass.h', 'NamespaceClass.cpp'],
12+
'namespace-class-test',
13+
],
14+
[
15+
'generate-class-with-method',
16+
['ClassWithMethod.h', 'ClassWithMethod.cpp'],
17+
'class-with-method',
18+
],
19+
[
20+
'generate-imperative-math',
21+
['MathOperations.h', 'MathOperations.cpp'],
22+
'math-class',
23+
],
24+
[
25+
'generate-class-with-default-values',
26+
['ClassWithDefaultValues.h', 'ClassWithDefaultValues.cpp'],
27+
'test-default-function-values',
28+
],
29+
]
30+
base_generator_name = test_data[0]
31+
output_files = test_data[1]
32+
test_name = test_data[2]
33+
34+
generator_exe = executable(
35+
base_generator_name + '-generate',
36+
base_generator_name + '.cpp',
37+
dependencies: internal_dep,
38+
)
39+
40+
generated_source_files = custom_target(
41+
command: [
42+
generator_exe,
43+
meson.current_build_dir(),
44+
],
45+
output: output_files,
46+
)
47+
48+
tested_exe = executable(test_name, test_name + '.cpp', generated_source_files)
49+
50+
test(test_name, tested_exe)
51+
endforeach
52+
53+
#
54+
# Enum tests
55+
#
56+
57+
foreach test_name : [
58+
'enum_name',
59+
'enum_with_space',
60+
'enum_single_value',
61+
'enum_multiple_value',
62+
'enum_name_bad_chars',
63+
]
64+
test_exe = executable(
65+
'test-' + test_name,
66+
test_name + '.cpp',
67+
dependencies: internal_dep,
68+
)
69+
test(test_name, test_exe)
70+
endforeach

0 commit comments

Comments
 (0)