-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCMake-module.html
512 lines (457 loc) · 20 KB
/
CMake-module.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<!DOCTYPE html>
<html lang="en">
<head>
<base href=".">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CMake module</title>
<link rel="stylesheet" href="assets/css/dark-frontend.css" type="text/css" title="dark">
<link rel="alternate stylesheet" href="assets/css/light-frontend.css" type="text/css" title="light">
<link rel="stylesheet" href="assets/css/bootstrap-toc.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/jquery.mCustomScrollbar.min.css">
<link rel="stylesheet" href="assets/js/search/enable_search.css" type="text/css">
<link rel="stylesheet" href="assets/css/prism-tomorrow.css" type="text/css" title="dark">
<link rel="alternate stylesheet" href="assets/css/prism.css" type="text/css" title="light">
<script src="assets/js/mustache.min.js"></script>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/bootstrap.js"></script>
<script src="assets/js/scrollspy.js"></script>
<script src="assets/js/typeahead.jquery.min.js"></script>
<script src="assets/js/search.js"></script>
<script src="assets/js/compare-versions.js"></script>
<script src="assets/js/jquery.mCustomScrollbar.concat.min.js"></script>
<script src="assets/js/bootstrap-toc.min.js"></script>
<script src="assets/js/jquery.touchSwipe.min.js"></script>
<script src="assets/js/anchor.min.js"></script>
<script src="assets/js/tag_filtering.js"></script>
<script src="assets/js/language_switching.js"></script>
<script src="assets/js/styleswitcher.js"></script>
<script src="assets/js/lines_around_headings.js"></script>
<script src="assets/js/prism-core.js"></script>
<script src="assets/js/prism-autoloader.js"></script>
<script src="assets/js/prism_autoloader_path_override.js"></script>
<script src="assets/js/prism-keep-markup.js"></script>
<script src="assets/js/trie.js"></script>
<link rel="icon" type="image/png" href="assets/images/favicon.png">
<link rel="shortcut icon" href="assets/images/favicon.png">
</head>
<body class="no-script
">
<script>
$('body').removeClass('no-script');
</script>
<nav class="navbar navbar-fixed-top navbar-default" id="topnav">
<div class="container-fluid">
<div class="navbar-right">
<a id="toc-toggle">
<span class="glyphicon glyphicon-menu-right"></span>
<span class="glyphicon glyphicon-menu-left"></span>
</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-wrapper" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span title="light mode switch" class="glyphicon glyphicon-sunglasses pull-right" id="lightmode-icon"></span>
<form class="navbar-form pull-right" id="navbar-search-form">
<div class="form-group has-feedback">
<input type="text" class="form-control input-sm" name="search" id="sidenav-lookup-field" placeholder="search" disabled>
<span class="glyphicon glyphicon-search form-control-feedback" id="search-mgn-glass"></span>
</div>
</form>
</div>
<div class="navbar-header">
<a id="sidenav-toggle">
<span class="glyphicon glyphicon-menu-right"></span>
<span class="glyphicon glyphicon-menu-left"></span>
</a>
<a id="home-link" href="index.html" class="hotdoc-navbar-brand">
<img src="assets/images/meson_logo.png" alt="Home">
</a>
</div>
<div class="navbar-collapse collapse" id="navbar-wrapper">
<ul class="nav navbar-nav" id="menu">
<li class="dropdown">
<a class="dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Modules <span class="caret"></span>
</a>
<ul class="dropdown-menu" id="modules-menu">
<li>
<a href="CMake-module.html">CMake</a>
</li>
<li>
<a href="Cuda-module.html">CUDA</a>
</li>
<li>
<a href="Dlang-module.html">Dlang</a>
</li>
<li>
<a href="External-Project-module.html">External Project</a>
</li>
<li>
<a href="Fs-module.html">Filesystem</a>
</li>
<li>
<a href="Gnome-module.html">GNOME</a>
</li>
<li>
<a href="Hotdoc-module.html">Hotdoc</a>
</li>
<li>
<a href="i18n-module.html">i18n</a>
</li>
<li>
<a href="Icestorm-module.html">Icestorm</a>
</li>
<li>
<a href="Java-module.html">Java</a>
</li>
<li>
<a href="Keyval-module.html">Keyval</a>
</li>
<li>
<a href="Pkgconfig-module.html">Pkgconfig</a>
</li>
<li>
<a href="Python-3-module.html">Python 3</a>
</li>
<li>
<a href="Python-module.html">Python</a>
</li>
<li>
<a href="Qt4-module.html">Qt4</a>
</li>
<li>
<a href="Qt5-module.html">Qt5</a>
</li>
<li>
<a href="Qt6-module.html">Qt6</a>
</li>
<li>
<a href="Rust-module.html">Rust</a>
</li>
<li>
<a href="Simd-module.html">Simd</a>
</li>
<li>
<a href="SourceSet-module.html">SourceSet</a>
</li>
<li>
<a href="Wayland-module.html">Wayland</a>
</li>
<li>
<a href="Windows-module.html">Windows</a>
</li>
</ul>
</li>
<li class="dropdown">
<a class="dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Quick References <span class="caret"></span>
</a>
<ul class="dropdown-menu" id="quick-refs-menu">
<li>
<a href="Reference-manual.html">Functions</a>
</li>
<li>
<a href="Build-options.html">Options</a>
</li>
<li>
<a href="Configuration.html">Configuration</a>
</li>
<li>
<a href="Dependencies.html">Dependencies</a>
</li>
<li>
<a href="Unit-tests.html">Tests</a>
</li>
<li>
<a href="Syntax.html">Syntax</a>
</li>
</ul>
</li>
</ul>
<div class="hidden-xs hidden-sm navbar-text navbar-center">
<p><b>The Meson Build System</b></p>
</div>
</div>
</div>
</nav>
<main>
<div data-extension="core" data-hotdoc-in-toplevel="True" data-hotdoc-project="Meson-documentation" data-hotdoc-ref="CMake-module.html" class="page_container" id="page-wrapper">
<script src="assets/js/utils.js"></script>
<div class="panel panel-collapse oc-collapsed" id="sidenav" data-hotdoc-role="navigation">
<script src="assets/js/full-width.js"></script>
<div id="sitenav-wrapper">
<iframe src="hotdoc-sitemap.html" id="sitenav-frame"></iframe>
</div>
</div>
<div id="body">
<div id="main">
<div id="page-description" data-hotdoc-role="main">
<h1 id="cmake-module">CMake module</h1>
<p><strong>Note</strong>: the functionality of this module is governed by <a href="Mixing-build-systems.html">Meson's
rules on mixing build systems</a>.</p>
<p>This module provides helper tools for generating cmake package files.
It also supports the usage of CMake based subprojects, similar to
the normal <a href="Subprojects.html">Meson subprojects</a>.</p>
<h2 id="usage">Usage</h2>
<p>To use this module, just do: <strong><code>cmake = import('cmake')</code></strong>. The
following functions will then be available as methods on the object
with the name <code>cmake</code>. You can, of course, replace the name <code>cmake</code>
with anything else.</p>
<p>It is generally recommended to use the latest Meson version and
CMake >=3.17 for best compatibility. CMake subprojects will
usually also work with older CMake versions. However, this can
lead to unexpected issues in rare cases.</p>
<h2 id="cmake-subprojects">CMake subprojects</h2>
<p>Using CMake subprojects is similar to using the "normal" Meson
subprojects. They also have to be located in the <code>subprojects</code>
directory.</p>
<p>Example:</p>
<pre><code class="language-cmake">add_library(cm_lib SHARED ${SOURCES})
</code></pre>
<pre><code class="language-meson">cmake = import('cmake')
# Configure the CMake project
sub_proj = cmake.subproject('libsimple_cmake')
# Fetch the dependency object
cm_lib = sub_proj.dependency('cm_lib')
executable('exe1', ['sources'], dependencies: [cm_lib])
</code></pre>
<p>The <code>subproject</code> method is almost identical to the normal Meson
<a href="Reference-manual_functions.html#subproject"><ins><code>subproject()</code></ins></a> function. The only difference is that a CMake project
instead of a Meson project is configured.</p>
<p>The returned <code>sub_proj</code> supports the same options as a "normal"
subproject. Meson automatically detects CMake build targets, which can
be accessed with the methods listed <a href="CMake-module.html#subproject-object">below</a>.</p>
<p>It is usually enough to just use the dependency object returned by the
<code>dependency()</code> method in the build targets. This is almost identical
to using the <a href="Reference-manual_functions.html#declare_dependency"><ins><code>declare_dependency()</code></ins></a> object from a normal Meson subproject.</p>
<p>It is also possible to use executables defined in the CMake project as code
generators with the <code>target()</code> method:</p>
<pre><code class="language-cmake">add_executable(cm_exe ${EXE_SRC})
</code></pre>
<pre><code class="language-meson">cmake = import('cmake')
# Subproject with the "code generator"
sub_pro = cmake.subproject('cmCodeGen')
# Fetch the code generator exe
sub_exe = sub_pro.target('cm_exe')
# Use the code generator
generated = custom_target(
'cmake-generated',
input: [],
output: ['test.cpp'],
command: [sub_exe, '@OUTPUT@']
)
</code></pre>
<p>It should be noted that not all projects are guaranteed to work. The
safest approach would still be to create a <code>meson.build</code> for the
subprojects in question.</p>
<h3 id="configuration-options">Configuration options</h3>
<p><em>New in meson 0.55.0</em></p>
<p>Meson also supports passing configuration options to CMake and overriding
certain build details extracted from the CMake subproject.</p>
<pre><code class="language-meson">cmake = import('cmake')
opt_var = cmake.subproject_options()
# Call CMake with `-DSOME_OTHER_VAR=ON`
opt_var.add_cmake_defines({'SOME_OTHER_VAR': true})
# Globally override the C++ standard to c++11
opt_var.set_override_option('cpp_std', 'c++11')
# Override the previous global C++ standard
# with c++14 only for the CMake target someLib
opt_var.set_override_option('cpp_std', 'c++14', target: 'someLib')
sub_pro = cmake.subproject('someLibProject', options: opt_var)
# Further changes to opt_var have no effect
</code></pre>
<p>See <a href="CMake-module.html#cmake-options-object">the CMake options object</a> for a complete
reference of all supported functions.</p>
<p>The CMake configuration options object is very similar to the
<a href="Reference-manual_returned_cfg_data.html"><ins><code>cfg_data</code></ins></a> object] object returned
by <a href="Reference-manual_functions.html#configuration_data"><ins><code>configuration_data()</code></ins></a>. It
is generated by the <code>subproject_options</code> method.</p>
<p>All configuration options have to be set <em>before</em> the subproject is
configured and must be passed to the <code>subproject</code> method via the
<code>options</code> key. Altering the configuration object won't have any effect
on previous <code>cmake.subproject</code> calls.</p>
<p>In earlier Meson versions CMake command-line parameters could be set
with the <code>cmake_options</code> kwarg. However, this feature is deprecated
since 0.55.0 and only kept for compatibility. It will not work
together with the <code>options</code> kwarg.</p>
<h3 id="subproject-object">
<code>subproject</code> object</h3>
<p>This object is returned by the <code>subproject</code> method described above
and supports the following methods:</p>
<ul>
<li>
<code>dependency(target)</code> returns a dependency object for any CMake target. The
<code>include_type</code> kwarg <em>(new in 0.56.0)</em> controls the include type of the
returned dependency object similar to the same kwarg in the
<a href="Reference-manual_functions.html#dependency"><ins><code>dependency()</code></ins></a> function.</li>
<li>
<code>include_directories(target)</code> returns a Meson <a href="Reference-manual_returned_inc.html"><ins><code>inc</code></ins></a>
object for the specified target. Using this method is not necessary
if the dependency object is used.</li>
<li>
<code>target(target)</code> returns the raw build target.</li>
<li>
<code>target_type(target)</code> returns the type of the target as a string</li>
<li>
<code>target_list()</code> returns a list of all target <em>names</em>.</li>
<li>
<code>get_variable(name)</code> fetches the specified variable from inside
the subproject. Usually <code>dependency()</code> or <code>target()</code> should be
preferred to extract build targets.</li>
<li>
<code>found</code> returns true if the subproject is available, otherwise false
<em>new in Meson 0.53.2</em>
</li>
</ul>
<h3 id="cmake-options-object">
<code>cmake options</code> object</h3>
<p>This object is returned by the <code>subproject_options()</code> method and
consumed by the <code>options</code> kwarg of the <code>subproject</code> method. The
following methods are supported:</p>
<ul>
<li>
<code>add_cmake_defines({'opt1': val1, ...})</code> add additional CMake commandline defines</li>
<li>
<code>set_override_option(opt, val)</code> set specific <a href="Build-options.html">build options</a>
for targets. This will effectively add <code>opt=val</code> to the <code>override_options</code>
array of the <a href="Reference-manual_functions.html#build_target"><ins><code>build_target()</code></ins></a>
</li>
<li>
<code>set_install(bool)</code> override whether targets should be installed or not</li>
<li>
<code>append_compile_args(lang, arg1, ...)</code> append compile flags for a specific
language to the targets</li>
<li>
<code>append_link_args(arg1, ...)</code> append linker args to the targets</li>
<li>
<code>clear()</code> reset all data in the <code>cmake options</code> object</li>
</ul>
<p>The methods <code>set_override_option</code>, <code>set_install</code>,
<code>append_compile_args</code> and <code>append_link_args</code> support the optional
<code>target</code> kwarg. If specified, the set options affect the specific
target. The effect of the option is global for the subproject
otherwise.</p>
<p>If, for instance, <code>opt_var.set_install(false)</code> is called, no target
will be installed regardless of what is set by CMake. However, it is
still possible to install specific targets (here <code>foo</code>) by setting the
<code>target</code> kwarg: <code>opt_var.set_install(true, target: 'foo')</code></p>
<p>Options that are not set won't affect the generated subproject. So, if
for instance, <code>set_install</code> was not called then the values extracted
from CMake will be used.</p>
<h3 id="cross-compilation">Cross compilation</h3>
<p><em>New in 0.56.0</em></p>
<p>Meson will try to automatically guess most of the required CMake
toolchain variables from existing entries in the cross and native
files. These variables will be stored in an automatically generate
CMake toolchain file in the build directory. The remaining variables
that can't be guessed can be added by the user in the <code>[cmake]</code>
cross/native file section (<em>new in 0.56.0</em>).</p>
<p>Adding a manual CMake toolchain file is also supported with the
<code>cmake_toolchain_file</code> setting in the <code>[properties]</code> section. Directly
setting a CMake toolchain file with
<code>-DCMAKE_TOOLCHAIN_FILE=/path/to/some/Toolchain.cmake</code> in the
<code>meson.build</code> is <strong>not</strong> supported since the automatically generated
toolchain file is also used by Meson to inject arbitrary code into
CMake to enable the CMake subproject support.</p>
<p>The closest configuration to only using a manual CMake toolchain file
would be to set these options in the machine file:</p>
<pre><code class="language-ini">[properties]
cmake_toolchain_file = '/path/to/some/Toolchain.cmake'
cmake_defaults = false
[cmake]
# No entries in this section
</code></pre>
<p>This will result in a toolchain file with just the bare minimum to
enable the CMake subproject support and <code>include()</code> the
<code>cmake_toolchain_file</code> as the last instruction.</p>
<p>For more information see the <a href="Machine-files.html">cross and native file
specification</a>.</p>
<h2 id="cmake-configuration-files">CMake configuration files</h2>
<h3 id="cmakewrite_basic_package_version_file">cmake.write_basic_package_version_file()</h3>
<p>This method is the equivalent of the corresponding <a href="https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html#command:write_basic_package_version_file">CMake
function</a>,
it generates a <code>name</code> package version file.</p>
<ul>
<li>
<code>name</code>: the name of the package.</li>
<li>
<code>version</code>: the version of the generated package file.</li>
<li>
<code>compatibility</code>: a string indicating the kind of compatibility, the accepted values are
<code>AnyNewerVersion</code>, <code>SameMajorVersion</code>, <code>SameMinorVersion</code> or <code>ExactVersion</code>.
It defaults to <code>AnyNewerVersion</code>. Depending on your cmake installation some kind of
compatibility may not be available.</li>
<li>
<code>arch_independent</code>: <em>new in 0.62.0</em>, if true the generated package file
will skip architecture checks. Useful for header-only libraries.</li>
<li>
<code>install_dir</code>: optional installation directory, it defaults to <code>$(libdir)/cmake/$(name)</code>
</li>
</ul>
<p>Example:</p>
<pre><code class="language-meson">cmake = import('cmake')
cmake.write_basic_package_version_file(name: 'myProject', version: '1.0.0')
</code></pre>
<h3 id="cmakeconfigure_package_config_file">cmake.configure_package_config_file()</h3>
<p>This method is the equivalent of the corresponding <a href="https://cmake.org/cmake/help/v3.11/module/CMakePackageConfigHelpers.html#generating-a-package-configuration-file">CMake
function</a>,
it generates a <code>name</code> package configuration file from the <code>input</code>
template file. Just like the cmake function in this file the
<code>@PACKAGE_INIT@</code> statement will be replaced by the appropriate piece
of cmake code. The equivalent <code>PATH_VARS</code> argument is given through
the <code>configuration</code> parameter.</p>
<ul>
<li>
<code>name</code>: the name of the package.</li>
<li>
<code>input</code>: the template file where that will be treated for variable substitutions contained in <code>configuration</code>.</li>
<li>
<code>install_dir</code>: optional installation directory, it defaults to <code>$(libdir)/cmake/$(name)</code>.</li>
<li>
<code>configuration</code>: a <code>configuration_data</code> object that will be used for variable substitution in the template file.
<em>Since 0.62.0</em> it can take a dictionary instead.</li>
</ul>
<p>Example:</p>
<p>meson.build:</p>
<pre><code class="language-meson">cmake = import('cmake')
conf = configuration_data()
conf.set_quoted('VAR', 'variable value')
cmake.configure_package_config_file(
name: 'myProject',
input: 'myProject.cmake.in',
configuration: conf
)
</code></pre>
<p>myProject.cmake.in:</p>
<pre><code class="language-text">@PACKAGE_INIT@
set(MYVAR VAR)
</code></pre>
</div>
</div>
<div id="search_results">
<p>The results of the search are</p>
</div>
<div id="footer">
<hr>
<div class="license-description">
Website licensing information are available on the <a href="legal.html">Legal</a> page.
</div>
</div>
</div>
<div id="toc-column">
<div class="edit-button">
<a href="https://github.com/mesonbuild/meson/edit/master/docs/markdown/CMake-module.md" data-hotdoc-role="edit-button">Edit on GitHub</a>
</div>
<div id="toc-wrapper">
<nav id="toc"></nav>
</div>
</div>
</div>
</main>
<script src="assets/js/navbar_offset_scroller.js"></script>
</body>
</html>