Meson: Conditional options

Created on 29 Sep 2017  Â·  5Comments  Â·  Source: mesonbuild/meson

For some projects of mine I have an option to turn on additional compiler warnings, like -Wpadded (gcc, clang), which usually generate noise but might be interesting to look at occasionally.
I used to include this option only for the supported compilers, but as far as I could gather, options must be defined in meson_options.txt, which does not allow any statement besides option().

It would be nice to be able to nest option()s in conditional branches checking the build environment.
And although I don't have a use-case at hand right now, it seems useful to be able to hide or show certain options depending on the value of other options.

enhancement options

All 5 comments

The build environment is not available at the time of option parsing, and cannot be (since it's a circular dependency), so this would be difficult to do. In your case, what I would do is to have a generic additional-warnings boolean option which applies to all build environments, and does nothing when appropriate.

I am also interested in conditional build options. I my case I am only interested changing the build options based on host_machine.system(). This should be available at the time of option parsing.

I want to be able to enable options only on platforms that the option is applicable to. I would also like to be able to change the default value based on the platform. eg an option for font configuration would have a default of of freetype on unix platforms and win32 fonts on windows platforms.

I am interested in the ability to conditionally set the project's default_options.

Backstory: there's this environ symbol that is undefined at shared library link time (comes from the C runtime). Meson defaults to b_lundef=true, which sets -Wl,--no-undefined at shared library link time, so the linker fails with undefined reference to 'environ'. On Linux, access to environ is not necessary for this project's use case, because there's clearenv. So we'd like to have something like this:

project(…
    default_options: [
        host_machine.system().startswith('linux') ? 'b_lundef=true' : 'b_lundef=false',
    ],
)

I build the same source in both an embedded system, and hostside. Obviously, there is a cross file for the embedded system. It would be nice if I could, for example, set b_staticpic explicitly off in the cross file, and have the project default to on.

@wasim42 You can do that manually in meson.build by fetching a variable from the [properties] tag of the cross file and setting the option to that property if it's cross compiling like so:

if meson.is_cross_build()
    b_staticp =  meson.get_cross_property('b_staticp')
else
    b_staticp = get_option('b_staticp')
endif
Was this page helpful?
0 / 5 - 0 ratings