| 1 |
--- |
| 2 |
short-description: Disabling options |
| 3 |
... |
| 4 |
|
| 5 |
# Disabling parts of the build |
| 6 |
|
| 7 |
*This feature is available since version 0.44.0.* |
| 8 |
|
| 9 |
The following is a common fragment found in many projects: |
| 10 |
|
| 11 |
```meson |
| 12 |
dep = dependency('foo') |
| 13 |
|
| 14 |
# In some different directory |
| 15 |
|
| 16 |
lib = shared_library('mylib', 'mylib.c', |
| 17 |
dependencies : dep) |
| 18 |
|
| 19 |
# And in a third directory |
| 20 |
|
| 21 |
exe = executable('mytest', 'mytest.c', |
| 22 |
link_with : lib) |
| 23 |
test('mytest', exe) |
| 24 |
``` |
| 25 |
|
| 26 |
This works fine but gets a bit inflexible when you want to make this |
| 27 |
part of the build optional. Basically it reduces to adding `if/else` |
| 28 |
statements around all target invocations. Meson provides a simpler way |
| 29 |
of achieving the same with a disabler object. |
| 30 |
|
| 31 |
A disabler object is created with the `disabler` function: |
| 32 |
|
| 33 |
```meson |
| 34 |
d = disabler() |
| 35 |
``` |
| 36 |
|
| 37 |
The only thing you can do to a disabler object is to ask if it has |
| 38 |
been found: |
| 39 |
|
| 40 |
```meson |
| 41 |
f = d.found() # returns false |
| 42 |
``` |
| 43 |
|
| 44 |
Any other statement that uses a disabler object will immediately |
| 45 |
return a disabler. For example assuming that `d` contains a disabler |
| 46 |
object then |
| 47 |
|
| 48 |
```meson |
| 49 |
d2 = some_func(d) # value of d2 will be disabler |
| 50 |
d3 = true or d2 # value of d3 will be true because of short-circuiting |
| 51 |
d4 = false or d2 # value of d4 will be disabler |
| 52 |
if d # neither branch is evaluated |
| 53 |
``` |
| 54 |
|
| 55 |
Thus to disable every target that depends on the dependency given |
| 56 |
above, you can do something like this: |
| 57 |
|
| 58 |
```meson |
| 59 |
if use_foo_feature |
| 60 |
d = dependency('foo') |
| 61 |
else |
| 62 |
d = disabler() |
| 63 |
endif |
| 64 |
``` |
| 65 |
|
| 66 |
This concentrates the handling of this option in one place and other |
| 67 |
build definition files do not need to be sprinkled with `if` |
| 68 |
statements. |