TExcelTAD
Home › Meson, Autotools & xmake › Options: meson configure & meson.options

Options: meson configure & meson.options

Meson has two kinds of options: built-in options that every project has (buildtype, prefix, warning_level, default_library, c_std, b_sanitize…), and project options that you declare yourself. Both are set the same way, with -Dname=value, and both are stored in the build directory.

Declaring project options

Project options live in their own file next to the top-level meson.build. Since Meson 1.1 the file is called meson.options; the older name meson_options.txt still works (Meson 1.12 reads either; if both exist they must be the same file, i.e. one is a symlink to the other, or setup fails with meson.options and meson_options.txt both exist, but are not the same file). A project that wants to build with Meson older than 1.1 must keep the old name, and Meson warns you if you use the new name without declaring meson_version: '>=1.1':

bash · try it yourself
mkdir -p opts && cd opts
echo "project('opts', 'c')" > meson.build
echo "option('greeting', type: 'string', value: 'hello')" > meson.options
meson setup build 2>&1 | grep -A1 WARNING
WARNING: Project does not target a minimum version but uses feature introduced in '1.1': meson.options file. Use meson_options.txt instead
Project name: opts
--
WARNING: Project specifies no minimum version but uses features which were added in versions:
 * 1.1: {'meson.options file'}

Option types:

Type Example
string option('greeting', type: 'string', value: 'hello')
boolean option('docs', type: 'boolean', value: false)
combo (one of a list) option('poller', type: 'combo', choices: ['epoll', 'poll'], value: 'epoll')
integer option('max_conn', type: 'integer', min: 1, max: 1024, value: 64)
array option('drivers', type: 'array', choices: ['a', 'b', 'c'], value: ['a'])
feature option('zstd', type: 'feature', value: 'auto'): enabled, disabled or auto

Using and changing options

bash · try it yourself
mkdir -p opts && cd opts
cat > meson.build <<'M'
project('opts', 'c', meson_version: '>=1.1')
message('greeting=' + get_option('greeting'))
message('poller=' + get_option('poller'))
message('max_conn=' + get_option('max_conn').to_string())
M
cat > meson.options <<'M'
option('greeting', type: 'string', value: 'hello', description: 'Text printed at start-up')
option('poller', type: 'combo', choices: ['epoll', 'poll'], value: 'epoll')
option('max_conn', type: 'integer', min: 1, max: 1024, value: 64)
M
meson setup build -Dgreeting=hi | grep Message
echo "--- the project options as meson configure shows them"
meson configure build | sed -n '/Project options/,$p'
echo "--- change two options; the next build reconfigures"
meson configure build -Dpoller=poll -Dmax_conn=8
meson compile -C build -j 1 | grep Message
echo "--- invalid values are rejected"
meson configure build -Dpoller=select 2>&1 | tail -1
meson configure build -Dmax_conn=0 2>&1 | tail -1
Message: greeting=hi
Message: poller=epoll
Message: max_conn=64
--- the project options as meson configure shows them
  Project options                Current Value                    Possible Values                  Description
  -----------------              -------------                    ---------------                  -----------
  greeting                       hi                                                                Text printed at start-up
  max_conn                       64                               [>= 1, <= 1024]                  max_conn
  poller                         epoll                            [epoll, poll]                    poller

There are no option augments.
--- change two options; the next build reconfigures
Message: greeting=hi
Message: poller=poll
Message: max_conn=8
--- invalid values are rejected
ERROR: Value "select" (of type "string") for option "poller" is not one of the choices. Possible choices are (as string): "epoll", "poll".
ERROR: Value 0 for option "max_conn" is less than minimum value 1.

meson configure build with no -D lists every option with its current value, the allowed values and the description: it is the first command to run in an unfamiliar Meson project (without a build directory, meson configure in the source root lists the defaults). With -D it changes them; the build then reconfigures automatically.

Built-in options worth knowing

bash · try it yourself
mkdir -p b && cd b
echo "project('b', 'c')" > meson.build
meson setup build > /dev/null
meson configure build | grep -E '^  (buildtype|debug|optimization|default_library|warning_level|werror|b_sanitize|b_lto|b_ndebug|prefix|libdir) ' | sort   # Meson lists base options in no fixed order
  b_lto                          false                            [True, False]                    Use link time optimization
  b_ndebug                       false                            [true, false, if-release]        Disable asserts
  b_sanitize                     []                                                                Code sanitizer to use
  buildtype                      debug                            [plain, debug, debugoptimized,   Build type to use
  debug                          true                             [True, False]                    Enable debug symbols and other information
  default_library                shared                           [shared, static, both]           Default library type
  libdir                         lib/x86_64-linux-gnu                                              Library directory
  optimization                   0                                [plain, 0, g, 1, 2, 3, s]        Optimization level
  prefix                         /usr/local                                                        Installation prefix
  warning_level                  1                                [0, 1, 2, 3, everything]         Compiler warning level to use
  werror                         false                            [True, False]                    Treat warnings as errors
  • buildtype is a shortcut that sets debug + optimization: plain, debug (-O0 -g), debugoptimized (-O2 -g), release (-O3), minsize (-Os). Set debug/optimization directly for anything else.
  • warning_level 0–3 or everything maps to -Wall, -Wextra, -Wpedantic… for each compiler, so you don't write compiler-specific flags.
  • b_* are base options: b_sanitize=address,undefined, b_lto=true, b_ndebug=if-release, b_coverage=true, b_pgo=generate|use.
  • libdir is lib/x86_64-linux-gnu here because Meson follows Debian's multiarch layout when the prefix is /usr/local or /usr.

feature options: the right way to make dependencies optional

A feature option is three-state. Passing it as required: to dependency() gives the behaviour packagers expect: enabled fails if the library is missing, disabled doesn't even look, auto uses it when it is there.

bash · try it yourself
mkdir -p feat && cd feat
cat > meson.build <<'M'
project('feat', 'c', meson_version: '>=1.1')
zlib = dependency('zlib', method: 'pkg-config', required: get_option('compression'))
message('compression support: ' + zlib.found().to_string())
M
echo "option('compression', type: 'feature', value: 'auto')" > meson.options
for v in auto disabled enabled; do
  echo "--- -Dcompression=$v"
  meson setup build-$v -Dcompression=$v 2>&1 | grep -E 'Message|ERROR|skipped'
done
--- -Dcompression=auto
Message: compression support: false
--- -Dcompression=disabled
Dependency zlib for host machine skipped: feature compression disabled
Message: compression support: false
--- -Dcompression=enabled
meson.build:2:7: ERROR: Dependency "zlib" not found (tried pkg-config)

The built-in auto_features option turns every auto feature into enabled or disabled at once: distributions build with -Dauto_features=enabled so that a missing library is an error instead of a silently smaller package.

Test yourself

Which file name declares project options in a project that requires Meson >= 1.1?

  1. meson.options (the older meson_options.txt also still works)
  2. options.meson
  3. meson.ini
  4. Only meson_options.txt

An option is declared type: 'feature' and passed as required: to dependency(). What happens with -Dx=auto when the library is missing?

  1. Configure fails
  2. The dependency is reported not found and the build continues
  3. Meson downloads it
  4. The option is reset to enabled

What is the quickest way to see every option of a configured Meson build, with current and allowed values?

  1. cat meson.options
  2. meson configure build
  3. ninja -t options
  4. meson setup --help