| 1 |
--- |
| 2 |
short-description: Build-time configuration options |
| 3 |
... |
| 4 |
|
| 5 |
# Configuration |
| 6 |
|
| 7 |
If there are multiple configuration options, passing them through |
| 8 |
compiler flags becomes very burdensome. It also makes the |
| 9 |
configuration settings hard to inspect. To make things easier, Meson |
| 10 |
supports the generation of configure files. This feature is similar to |
| 11 |
one found in other build systems such as CMake. |
| 12 |
|
| 13 |
Suppose we have the following Meson snippet: |
| 14 |
|
| 15 |
```meson |
| 16 |
conf_data = [[#configuration_data]] |
| 17 |
conf_data.set('version', '1.2.3') |
| 18 |
[[#configure_file]](input : 'config.h.in', |
| 19 |
output : 'config.h', |
| 20 |
configuration : conf_data) |
| 21 |
``` |
| 22 |
|
| 23 |
and that the contents of `config.h.in` are |
| 24 |
|
| 25 |
```c |
| 26 |
#define VERSION_STR "@version@" |
| 27 |
``` |
| 28 |
|
| 29 |
Meson will then create a file called `config.h` in the corresponding |
| 30 |
build directory whose contents are the following. |
| 31 |
|
| 32 |
```c |
| 33 |
#define VERSION_STR "1.2.3" |
| 34 |
``` |
| 35 |
|
| 36 |
More specifically, Meson will find all strings of the type `@varname@` |
| 37 |
and replace them with respective values set in `conf_data`. You can |
| 38 |
use a single `configuration_data` object as many times as you like, |
| 39 |
but it becomes immutable after being passed to the `configure_file` |
| 40 |
function. That is, after it has been used once to generate output the |
| 41 |
`set` function becomes unusable and trying to call it causes an error. |
| 42 |
Copy of immutable `configuration_data` is still immutable. |
| 43 |
|
| 44 |
For more complex configuration file generation Meson provides a second |
| 45 |
form. To use it, put a line like this in your configuration file. |
| 46 |
|
| 47 |
#mesondefine TOKEN |
| 48 |
|
| 49 |
The replacement that happens depends on what the value and type of TOKEN is: |
| 50 |
|
| 51 |
```c |
| 52 |
#define TOKEN // If TOKEN is set to boolean true. |
| 53 |
#undef TOKEN // If TOKEN is set to boolean false. |
| 54 |
#define TOKEN 4 // If TOKEN is set to an integer or string value. |
| 55 |
/* undef TOKEN */ // If TOKEN has not been set to any value. |
| 56 |
``` |
| 57 |
|
| 58 |
Note that if you want to define a C string, you need to do the quoting |
| 59 |
yourself like this: |
| 60 |
|
| 61 |
```meson |
| 62 |
conf_data.set('TOKEN', '"value"') |
| 63 |
``` |
| 64 |
|
| 65 |
Since this is such a common operation, Meson provides a convenience |
| 66 |
method: |
| 67 |
|
| 68 |
```meson |
| 69 |
plain_var = 'value' |
| 70 |
conf_data.set_quoted('TOKEN', plain_var) # becomes #define TOKEN "value" |
| 71 |
``` |
| 72 |
|
| 73 |
Often you have a boolean value in Meson but need to define the C/C++ |
| 74 |
token as 0 or 1. Meson provides a convenience function for this use |
| 75 |
case. |
| 76 |
|
| 77 |
```meson |
| 78 |
conf_data.set10(token, boolean_value) |
| 79 |
# The line above is equivalent to this: |
| 80 |
if boolean_value |
| 81 |
conf_data.set(token, 1) |
| 82 |
else |
| 83 |
conf_data.set(token, 0) |
| 84 |
endif |
| 85 |
``` |
| 86 |
|
| 87 |
## Configuring without an input file |
| 88 |
|
| 89 |
If the input file is not defined then Meson will generate a header |
| 90 |
file with all the entries in the configuration data object. The |
| 91 |
replacements are the same as when generating `#mesondefine` entries: |
| 92 |
|
| 93 |
```meson |
| 94 |
conf_data.set('FOO', '"string"') => #define FOO "string" |
| 95 |
conf_data.set('FOO', 'a_token') => #define FOO a_token |
| 96 |
conf_data.set('FOO', true) => #define FOO |
| 97 |
conf_data.set('FOO', false) => #undef FOO |
| 98 |
conf_data.set('FOO', 1) => #define FOO 1 |
| 99 |
conf_data.set('FOO', 0) => #define FOO 0 |
| 100 |
``` |
| 101 |
|
| 102 |
In this mode, you can also specify a comment which will be placed |
| 103 |
before the value so that your generated files are self-documenting. |
| 104 |
|
| 105 |
```meson |
| 106 |
conf_data.set('BAR', true, description : 'Set BAR if it is available') |
| 107 |
``` |
| 108 |
|
| 109 |
Will produce: |
| 110 |
|
| 111 |
```c |
| 112 |
/* Set BAR if it is available */ |
| 113 |
#define BAR |
| 114 |
``` |
| 115 |
|
| 116 |
## Dealing with file encodings |
| 117 |
|
| 118 |
The default Meson file encoding to configure files is utf-8. If you |
| 119 |
need to configure a file that is not utf-8 encoded the `encoding` |
| 120 |
keyword will allow you to specify which file encoding to use. It is |
| 121 |
however strongly advised to convert your non utf-8 file to utf-8 |
| 122 |
whenever possible. Supported file encodings are those of python3, see |
| 123 |
[standard-encodings](https://docs.python.org/3/library/codecs.html#standard-encodings). |
| 124 |
|
| 125 |
## Using dictionaries |
| 126 |
|
| 127 |
Since *0.49.0* [[configuration_data]] takes an optional dictionary as |
| 128 |
first argument. If provided, each key/value pair is added into the |
| 129 |
`configuration_data` as if `set()` method was called for each of them. |
| 130 |
[[configure_file]]'s `configuration` kwarg also accepts a dictionary |
| 131 |
instead of a configuration_data object. |
| 132 |
|
| 133 |
Example: |
| 134 |
```meson |
| 135 |
cdata = configuration_data({ |
| 136 |
'STRING' : '"foo"', |
| 137 |
'INT' : 42, |
| 138 |
'DEFINED' : true, |
| 139 |
'UNDEFINED' : false, |
| 140 |
}) |
| 141 |
|
| 142 |
configure_file(output : 'config1.h', |
| 143 |
configuration : cdata, |
| 144 |
) |
| 145 |
|
| 146 |
configure_file(output : 'config2.h', |
| 147 |
configuration : { |
| 148 |
'STRING' : '"foo"', |
| 149 |
'INT' : 42, |
| 150 |
'DEFINED' : true, |
| 151 |
'UNDEFINED' : false, |
| 152 |
} |
| 153 |
) |
| 154 |
|
| 155 |
``` |
| 156 |
|
| 157 |
# A full example |
| 158 |
|
| 159 |
Generating and using a configuration file requires the following steps: |
| 160 |
|
| 161 |
- generate the file |
| 162 |
- create an include directory object for the directory that holds the file |
| 163 |
- use it in a target |
| 164 |
|
| 165 |
We are going to use the traditional approach of generating a header |
| 166 |
file in the top directory. The common name is `config.h` but we're |
| 167 |
going to use an unique name. This avoids the problem of accidentally |
| 168 |
including the wrong header file when building a project with many |
| 169 |
subprojects. |
| 170 |
|
| 171 |
At the top level we generate the file: |
| 172 |
|
| 173 |
```meson |
| 174 |
conf_data = configuration_data() |
| 175 |
# Set data |
| 176 |
configure_file(input : 'projconfig.h.in', |
| 177 |
output : 'projconfig.h', |
| 178 |
configuration : conf_data) |
| 179 |
``` |
| 180 |
|
| 181 |
Immediately afterwards we generate the include object. |
| 182 |
|
| 183 |
```meson |
| 184 |
configuration_inc = include_directories('.') |
| 185 |
``` |
| 186 |
|
| 187 |
Finally we specify this in a target that can be in any subdirectory. |
| 188 |
|
| 189 |
```meson |
| 190 |
executable(..., include_directories : configuration_inc) |
| 191 |
``` |
| 192 |
|
| 193 |
Now any source file in this target can include the configuration |
| 194 |
header like this: |
| 195 |
|
| 196 |
```c |
| 197 |
#include<projconfig.h> |
| 198 |
``` |