2017-05-03 17:38:41 +08:00
---
short-description: Build options to configure project properties
...
2017-03-30 02:03:43 +08:00
# Build options
2017-08-27 04:41:53 +08:00
Most non-trivial builds require user-settable options. As an example a
program may have two different data backends that are selectable at
build time. Meson provides for this by having a option definition
file. Its name is `meson_options.txt` and it is placed at the root of
your source tree.
2017-03-30 02:03:43 +08:00
Here is a simple option file.
```meson
option('someoption', type : 'string', value : 'optval', description : 'An option')
option('other_one', type : 'boolean', value : false)
option('combo_opt', type : 'combo', choices : ['one', 'two', 'three'], value : 'three')
```
2017-08-27 04:41:53 +08:00
This demonstrates the three basic option types and their usage. String
option is just a free form string and a boolean option is,
unsurprisingly, true or false. The combo option can have any value
from the strings listed in argument `choices` . If `value` is not set,
it defaults to empty string for strings, `true` for booleans or the
first element in a combo. You can specify `description` , which is a
free form piece of text describing the option. It defaults to option
name.
2017-03-30 02:03:43 +08:00
These options are accessed in Meson code with the `get_option` function.
```meson
optval = get_option('opt_name')
```
2017-08-27 04:41:53 +08:00
This function also allows you to query the value of Meson's built-in
project options. For example, to get the installation prefix you would
issue the following command:
2017-03-30 02:03:43 +08:00
```meson
prefix = get_option('prefix')
```
2017-08-27 04:41:53 +08:00
It should be noted that you can not set option values in your Meson
2017-09-22 22:16:36 +08:00
scripts. They have to be set externally with the `meson configure` command
line tool. Running `meson configure` without arguments in a build dir shows
you all options you can set.
To change their values use the `-D`
2017-08-27 04:41:53 +08:00
option:
2017-03-30 02:03:43 +08:00
```console
2017-09-22 22:16:36 +08:00
$ meson configure -Doption=newvalue
2017-03-30 02:03:43 +08:00
```
2017-09-22 22:16:36 +08:00
**NOTE:** If you cannot call `meson configure` you likely have a old version of Meson. In that case you can call `mesonconf` instead, but that is deprecated in newer versions