Syntax
The syntax of Meson's specification language has been kept as simple as possible. It is strongly typed so no object is ever converted to another under the covers. Variables have no visible type which makes Meson dynamically typed (also known as duck typed).
The main building blocks of the language are variables, numbers, booleans, strings, arrays, function calls, method calls, if statements and includes.
Usually one Meson statement takes just one line. There is no way to have multiple statements on one line as in e.g. C. Function and method calls' argument lists can be split over multiple lines. Meson will autodetect this case and do the right thing.
In other cases, (added 0.50) you can get multi-line statements by
ending the line with a \
. Apart from line ending whitespace has no
syntactic meaning.
Variables
Variables in Meson work just like in other high level programming languages. A variable can contain a value of any type, such as an integer or a string. Variables don't need to be predeclared, you can just assign to them and they appear. Here's how you would assign values to two different variables.
var1 = 'hello'
var2 = 102
One important difference in how variables work in Meson is that all objects are immutable. When you see an operation which appears like a mutation, actually a new object is created and assigned to the name. This is different from, for example, how Python works for objects, but similar to e.g. Python strings.
var1 = [1, 2, 3]
var2 = var1
var2 += [4]
# var2 is now [1, 2, 3, 4]
# var1 is still [1, 2, 3]
Numbers
Meson supports only integer numbers. They are declared simply by writing them out. Basic arithmetic operations are supported.
x = 1 + 2
y = 3 * 4
d = 5 % 3 # Yields 2.
Hexadecimal literals are supported since version 0.45.0:
int_255 = 0xFF
Octal and binary literals are supported since version 0.47.0:
int_493 = 0o755
int_1365 = 0b10101010101
Strings can be converted to a number like this:
string_var = '42'
num = string_var.to_int()
Numbers can be converted to a string:
int_var = 42
string_var = int_var.to_string()
Booleans
A boolean is either true
or false
.
truth = true
Booleans can be converted to a string or to a number:
bool_var = true
string_var = bool_var.to_string()
int_var = bool_var.to_int()
Strings
Strings in Meson are declared with single quotes. To enter a literal single quote do it like this:
single_quote = 'contains a \' character'
The full list of escape sequences is:
-
\\
Backslash -
\'
Single quote -
\a
Bell -
\b
Backspace -
\f
Formfeed -
\n
Newline -
\r
Carriage Return -
\t
Horizontal Tab -
\v
Vertical Tab -
\ooo
Character with octal value ooo -
\xhh
Character with hex value hh -
\uxxxx
Character with 16-bit hex value xxxx -
\Uxxxxxxxx
Character with 32-bit hex value xxxxxxxx -
\N{name}
Character named name in Unicode database
As in python and C, up to three octal digits are accepted in \ooo
.
Unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string.
String concatenation
Strings can be concatenated to form a new string using the +
symbol.
str1 = 'abc'
str2 = 'xyz'
combined = str1 + '_' + str2 # combined is now abc_xyz
String path building
(Added 0.49)
You can concatenate any two strings using /
as an operator to build paths.
This will always use /
as the path separator on all platforms.
If any one of the individual segments is an absolute path, all segments before
it are dropped. For example:
joined = '/usr/share' / 'projectname' # => /usr/share/projectname
joined = '/usr/local' / '/etc/name' # => /etc/name
joined = 'C:\\foo\\bar' / 'builddir' # => C:/foo/bar/builddir
joined = 'C:\\foo\\bar' / 'D:\\builddir' # => D:/builddir