Skip to content

Commit 150e097

Browse files
committed
Add Meson support
1 parent e63911d commit 150e097

4 files changed

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