TExcelTAD
Home › Meson, Autotools & xmake › configure_file & Compiler Checks

configure_file & Compiler Checks

Portable C code often needs to know things about the platform it is built on: does <sys/epoll.h> exist? Is there a strlcpy? How big is a long? Autotools made the answer famous: a generated config.h full of #define HAVE_… lines. Meson does the same with two pieces: compiler checks on the compiler object, and configure_file() to write the results into a header.

Compiler checks

bash · try it yourself
mkdir -p checks && cd checks
cat > meson.build <<'M'
project('checks', 'c')
cc = meson.get_compiler('c')
message('epoll.h:      ' + cc.has_header('sys/epoll.h').to_string())
message('kqueue:       ' + cc.has_header('sys/event.h').to_string())
message('strlcpy:      ' + cc.has_function('strlcpy', prefix: '#include <string.h>').to_string())
message('sizeof(long): ' + cc.sizeof('long').to_string())
message('-Wformat=2:   ' + cc.has_argument('-Wformat=2').to_string())
message('libm found:   ' + cc.find_library('m', required: false).found().to_string())
M
meson setup build | grep -vE '^(The Meson|Version|Source|Build|Project|C |Host|Found ninja|$)'
Has header "sys/epoll.h" : YES
Message: epoll.h:      true
Has header "sys/event.h" : NO
Message: kqueue:       false
Checking for function "strlcpy" : YES
Message: strlcpy:      true
Checking for size of "long" : 8
Message: sizeof(long): 8
Compiler for C supports arguments -Wformat=2: YES
Message: -Wformat=2:   true
Library m found: YES
Message: libm found:   true

Each check compiles (and sometimes links) a tiny test program, exactly what a configure script does, but Meson runs them in-process and caches each result. The Checking… / Has header… lines are Meson's own log of the checks, so you can see what was decided without opening the log file. (strlcpy is present because glibc 2.38 added it; on older glibc the answer is NO.)

The most used checks:

Method Answers
cc.has_header('x.h') can #include <x.h> be compiled?
cc.has_function('f', prefix: '#include …') can f be linked?
cc.has_header_symbol('x.h', 'SYM') is a macro/function/type declared in that header?
cc.sizeof('type'), cc.alignment('type') numbers, computed even when cross-compiling
cc.has_argument('-flag'), cc.get_supported_arguments([...]) does the compiler accept these flags?
cc.find_library('m', required: false) a dependency object for a plain -lm library
cc.compiles(code), cc.links(code), cc.run(code) anything else (run doesn't work when cross-compiling)

configure_file(): writing config.h

configuration_data() is a dictionary-like object you fill with set(), set10() and set_quoted(). configure_file() then writes it into the build directory, either from a template or as a plain list of #defines:

bash · try it yourself
mkdir -p cfg && cd cfg
cat > meson.build <<'M'
project('cfg', 'c', version: '3.2.0')
cc = meson.get_compiler('c')

conf = configuration_data()
conf.set_quoted('PACKAGE_VERSION', meson.project_version())
conf.set10('HAVE_EPOLL', cc.has_header('sys/epoll.h'))          # 1 or 0
conf.set('HAVE_KQUEUE', cc.has_header('sys/event.h'))           # defined or #undef
conf.set('SIZEOF_LONG', cc.sizeof('long'))

# 1. no template: one #define per entry
configure_file(output: 'config.h', configuration: conf)
# 2. a template with @VAR@ and #mesondefine placeholders
configure_file(input: 'version.h.in', output: 'version.h', configuration: conf)

executable('cfg', 'main.c')
M
cat > version.h.in <<'T'
#pragma once
#define CFG_VERSION @PACKAGE_VERSION@
#mesondefine HAVE_KQUEUE
T
cat > main.c <<'SRC'
#include <stdio.h>
#include "config.h"
#include "version.h"
int main(void) {
    printf("version %s, epoll %d, sizeof(long) %d\n", CFG_VERSION, HAVE_EPOLL, SIZEOF_LONG);
#ifdef HAVE_KQUEUE
    puts("using kqueue");
#endif
    return 0;
}
SRC
meson setup build > /dev/null
echo "--- build/config.h"; cat build/config.h
echo "--- build/version.h"; cat build/version.h
meson compile -C build -j 1 > /dev/null && ./build/cfg
--- build/config.h
/*
 * Autogenerated by the Meson build system.
 * Do not edit, your changes will be lost.
 */

#pragma once

#define HAVE_EPOLL 1

#undef HAVE_KQUEUE

#define PACKAGE_VERSION "3.2.0"

#define SIZEOF_LONG 8

--- build/version.h
#pragma once
#define CFG_VERSION "3.2.0"
#undef HAVE_KQUEUE
version 3.2.0, epoll 1, sizeof(long) 8

Notes:

  • set_quoted adds the C string quotes, so the template says @PACKAGE_VERSION@, not "@PACKAGE_VERSION@" (that would give ""3.2.0"" and a compile error); set10 writes 1/0 (use with #if); set with a boolean writes #define X or #undef X (use with #ifdef).
  • In a template, @VAR@ is replaced by the value, and #mesondefine VAR becomes #define VAR … or #undef VAR. format: 'cmake' accepts CMake-style ${VAR}/#cmakedefine templates, which helps when porting from CMake.
  • The generated header is in the build directory, and the -I. Meson adds for every target makes #include "config.h" work. Nothing is written into the source tree.

Other ways to generate files

Need Meson
fill a template at configure time configure_file(input:, output:, configuration:)
copy a file into the build tree configure_file(input:, output:, copy: true) (or fs.copyfile())
run a tool at build time (e.g. a code generator) custom_target('gen', input:, output:, command: [prog, '@INPUT@', '@OUTPUT@'])
run a tool on many inputs generator() + .process(files)
stamp the git version vcs_tag(input: 'v.h.in', output: 'v.h'), re-run on every build

Test yourself

Where does configure_file(output: 'config.h', …) write the file?

  1. Next to meson.build in the source tree
  2. In the build directory (the matching subdirectory)
  3. In /usr/include
  4. In meson-private/

You want #if HAVE_EPOLL to work whether or not epoll exists. Which call?

  1. conf.set('HAVE_EPOLL', found)
  2. conf.set10('HAVE_EPOLL', found)
  3. conf.set_quoted('HAVE_EPOLL', found)
  4. conf.mesondefine('HAVE_EPOLL')

Which compiler check cannot be used when cross-compiling (without an exe wrapper)?

  1. cc.has_header()
  2. cc.sizeof()
  3. cc.run()
  4. cc.has_function()