The Meson Language
meson.build files are written in Meson's own small language. It looks a bit like Python, but it is deliberately limited: it is not Turing-complete (there are no user-defined functions and no unbounded loops), variables are immutable objects, and there is no way to run arbitrary code at configure time without saying so explicitly (run_command()). The result is that every meson.build reads top to bottom like a declaration, and two people write the same thing the same way.
Types and syntax
mkdir -p lang && cd lang
cat > meson.build <<'M'
project('lang', 'c', version: '2.3.1')
# strings: single quotes only, .format() and f-strings
name = meson.project_name()
message('project @0@ version @1@'.format(name, meson.project_version()))
major = meson.project_version().split('.')[0]
message(f'major is @major@')
# integers and booleans
jobs = 4
message('jobs * 2 = ' + (jobs * 2).to_string())
debug = get_option('buildtype').startswith('debug')
message('debug build? ' + debug.to_string())
# arrays: += returns a NEW array assigned to the same name
srcs = ['a.c', 'b.c']
srcs += 'c.c'
message('sources: ' + ', '.join(srcs))
# dictionaries (string keys)
cfg = {'level': 3, 'name': 'demo'}
message('level is ' + cfg['level'].to_string() + ', keys: ' + ' '.join(cfg.keys()))
# if / elif / else and foreach (over arrays and dicts)
if host_machine.system() == 'linux'
message('building on Linux, cpu family ' + host_machine.cpu_family())
elif host_machine.system() == 'windows'
message('building on Windows')
else
message('something else')
endif
foreach key, value : cfg
message(f'@key@ = @value@')
endforeach
M
meson setup build | grep MessageMessage: project lang version 2.3.1
Message: major is 2
Message: jobs * 2 = 8
Message: debug build? true
Message: sources: a.c, b.c, c.c
Message: level is 3, keys: level name
Message: building on Linux, cpu family x86_64
Message: level = 3
Message: name = demo
Things to notice:
- Strings use single quotes.
"double"quotes are a syntax error (there is also'''multi-line'''). @0@placeholders for.format(), andf'…@name@…'f-strings (Meson 0.58+). An f-string only substitutes plain variable names, not expressions such as@ver[0]@, so store the value in a variable first.+=does not mutate: it builds a new array and rebinds the name. That is why it is safe: no function can change an object you passed it.foreachis the only loop; there is nowhile, andbreak/continueexist only insideforeach.- Built-in objects:
meson(the project),build_machine,host_machine,target_machine(for cross builds).
Strict types
Meson does not convert types silently. Concatenating a number to a string is an error, and so is calling an unknown method. Errors point at the file, line and column:
mkdir -p strict && cd strict
cat > meson.build <<'M'
project('strict', 'c')
count = 3
message('count is ' + count)
M
meson setup build 2>&1 | grep -A1 ERRORmeson.build:3:22: ERROR: The `+` operator of str does not accept objects of type int (3)
Functions return objects
Almost everything you call returns an object you keep in a variable and pass on: executable() returns a target, dependency() a dependency, find_program() a program, files() a list of file objects. Objects have methods:
mkdir -p objs && cd objs
cat > meson.build <<'M'
project('objs', 'c')
cc = meson.get_compiler('c')
message('compiler: ' + cc.get_id() + ' ' + cc.version().split('.')[0])
message('has -Wshadow? ' + cc.has_argument('-Wshadow').to_string())
sh = find_program('sh')
message('sh found: ' + sh.found().to_string())
missing = find_program('no-such-tool', required: false)
message('no-such-tool found: ' + missing.found().to_string())
exe = executable('app', 'main.c')
message('target name: ' + exe.name())
M
echo 'int main(void) { return 0; }' > main.c
meson setup build | grep -E 'Message|Program|Compiler'Message: compiler: gcc 13
Compiler for C supports arguments -Wshadow: YES
Message: has -Wshadow? true
Program sh found: YES (/usr/bin/sh)
Message: sh found: true
Program no-such-tool found: NO
Message: no-such-tool found: false
Message: target name: app
required: false is how you make anything optional; the default is required: true, which stops the configure step with an error. (The Compiler for C supports arguments -Wshadow: YES line is Meson caching the result of a check; every check is logged in build/meson-logs/meson-log.txt.)
What the language leaves out, on purpose
| Missing | Why | What to use instead |
|---|---|---|
| user-defined functions | keeps every file readable top to bottom | loops over arrays/dicts, subdir() |
globbing (*.c) |
a new file would not trigger a reconfigure | list sources explicitly (see Field Notes) |
| mutable state | nothing can change a value behind your back | build new values with + |
| arbitrary code | configure must be fast and reproducible | run_command(), custom_target(), generator() with an explicit command |
Test yourself
Which line is valid Meson?
msg = "hello"msg = 'hello ' + 3msg = 'hello @0@'.format(3)def greet(): return 'hi'
What does srcs += 'c.c' do to an array that was also passed to a function earlier?
- Changes the array everywhere it is referenced
- Creates a new array and binds
srcsto it; earlier uses are unaffected - It is an error: arrays are immutable
- Appends only if
c.cexists
How do you make a find_program() call not fail the configure step when the tool is missing?
- Wrap it in
try - Pass
required: falseand test.found() - Use
optional_program() - It never fails