summaryrefslogtreecommitdiff
path: root/docs/markdown/Configuration.md
blob: 8b79bc6e91d5ac1f9b130a8b9cbe33097300f8cc (plain)
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
---
short-description: Build-time configuration options
...

# Configuration

If there are multiple configuration options, passing them through
compiler flags becomes very burdensome. It also makes the
configuration settings hard to inspect. To make things easier, Meson
supports the generation of configure files. This feature is similar to
one found in other build systems such as CMake.

Suppose we have the following Meson snippet:

```meson
conf_data = configuration_data()
conf_data.set('version', '1.2.3')
configure_file(input : 'config.h.in',
               output : 'config.h',
               configuration : conf_data)
```

and that the contents of `config.h.in` are

```c
#define VERSION_STR "@version@"
```

Meson will then create a file called `config.h` in the corresponding
build directory whose contents are the following.

```c
#define VERSION_STR "1.2.3"
```

More specifically, Meson will find all strings of the type `@varname@`
and replace them with respective values set in `conf_data`. You can
use a single `configuration_data` object as many times as you like,
but it becomes immutable after being passed to the `configure_file`
function. That is, after it has been used once to generate output the
`set` function becomes unusable and trying to call it causes an error.
Copy of immutable `configuration_data` is still immutable.

For more complex configuration file generation Meson provides a second
form. To use it, put a line like this in your configuration file.

    #mesondefine TOKEN

The replacement that happens depends on what the value and type of TOKEN is:

```c
#define TOKEN     // If TOKEN is set to boolean true.
#undef TOKEN      // If TOKEN is set to boolean false.
#define TOKEN 4   // If TOKEN is set to an integer or string value.
/* undef TOKEN */ // If TOKEN has not been set to any value.
```

Note that if you want to define a C string, you need to do the quoting
yourself like this:

```meson
conf_data.set('TOKEN', '"value"')
```

Since this is such a common operation, Meson provides a convenience
method:

```meson
plain_var = 'value'
conf_data.set_quoted('TOKEN', plain_var) # becomes #define TOKEN "value"
```

Often you have a boolean value in Meson but need to define the C/C++
token as 0 or 1. Meson provides a convenience function for this use
case.

```meson
conf_data.set10(token, boolean_value)
# The line above is equivalent to this:
if boolean_value
  conf_data.set(token, 1)
else
  conf_data.set(token, 0)
endif
```

## Configuring without an input file

If the input file is not defined then Meson will generate a header
file all the entries in the configuration data object. The
replacements are the same as when generating `#mesondefine` entries:

```meson
conf_data.set('FOO', '"string"') => #define FOO "string"
conf_data.set('FOO', 'a_token')  => #define FOO a_token
conf_data.set('FOO', true)       => #define FOO
conf_data.set('FOO', false)      => #undef FOO
conf_data.set('FOO', 1)          => #define FOO 1
conf_data.set('FOO', 0)          => #define FOO 0
```

In this mode, you can also specify a comment which will be placed
before the value so that your generated files are self-documenting.

```meson
conf_data.set('BAR', true, description : 'Set BAR if it is available')
```

Will produce:

```c
/* Set BAR if it is available */
#define BAR
```

## Dealing with file encodings

The default meson file encoding to configure files is utf-8. If you need to
configure a file that is not utf-8 encoded the encoding keyword will allow
you to specify which file encoding to use. It is however strongly advised to
convert your non utf-8 file to utf-8 whenever possible. Supported file
encodings are those of python3, see [standard-encodings](https://docs.python.org/3/library/codecs.html#standard-encodings).

# A full example

Generating and using a configuration file requires the following steps:

 - generate the file
 - create an include directory object for the directory that holds the file
 - use it in a target

We are going to use the traditional approach of generating a header
file in the top directory. The common name is `config.h` but we're
going to use an unique name. This avoids the problem of accidentally
including the wrong header file when building a project with many
subprojects.

At the top level we generate the file:

```meson
conf_data = configuration_data()
# Set data
configure_file(input : 'projconfig.h.in',
  output : 'projconfig.h',
  configuration : conf_data)
```

Immediately afterwards we generate the include object.

```meson
configuration_inc = include_directories('.')
```

Finally we specify this in a target that can be in any subdirectory.

```meson
executable(..., include_directories : configuration_inc)
```

Now any source file in this target can include the configuration
header like this:

```c
#include<projconfig.h>
```