How to extract defines from a header?

92 Views Asked by At

I would like to extract some defines from header files, and put the result into a meson variables. I can of course do that with run_command, forming a shell command of cpp, sed and so on. But I wonder if meson gives some automation to that task. Having run_preprocessor would already be a win, but there seem to be no such thing. Also get_compiler method does exist, but get_preprocessor - seemingly not. At least I could refer to a currently selected preprocessor and use the current include paths, rather than just using cpp in run_command with a hard-coded path to headers.

Does meson provide at least some help for extracting things from headers, or should it be coded by hands, with a pure run_command?

2

There are 2 best solutions below

2
On

Based on @dcbaker reply, I've come up with this:

incdir = include_directories('some_inc_dir')
cpp = meson.get_compiler('c')
VAR = cpp.get_define('MY_DEFINE',
  include_directories: incdir,
  prefix: '#include "my_hdr.h"')
2
On

This is normally done via compiler.get_define, which would look something like:

cpp = meson.get_compiler('cpp')
cpp_ver = cpp.get_define('__cplusplus')

There are additional arguments for handling dependencies, header search paths, and generating a preamble for the test, which I think the linked documentation describes well enough