meson icon indicating copy to clipboard operation
meson copied to clipboard

Conditional options

Open z33ky opened this issue 8 years ago • 10 comments

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.

z33ky avatar Sep 29 '17 12:09 z33ky

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.

nirbheek avatar Sep 29 '17 20:09 nirbheek

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.

adrianjohnson avatar Sep 30 '17 23:09 adrianjohnson

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',
	],
)

valpackett avatar Oct 10 '17 21:10 valpackett

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 avatar Jul 17 '18 16:07 wasim42

@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

creikey avatar Aug 13 '19 21:08 creikey

hi everyone. just a friendly ping here to check if there is any update about this topic

xmnlab avatar Aug 11 '22 20:08 xmnlab

This issue predates my involvement, but hmm, you can disable lundef if you know it can't be used, by using override_options rather than changing the default but allowing users to break the build by setting it manually. I actually find this example to be an argument against conditional options (but there are other arguments for it).

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.

Cross files can (these days) include setting built-in or project options. No need for properties anymore.

eli-schwartz avatar Aug 11 '22 21:08 eli-schwartz

Hidden options based on the host_system do seem potentially useful, but afaik no one is actively working on this.

eli-schwartz avatar Aug 11 '22 21:08 eli-schwartz

thanks for the information @eli-schwartz !

xmnlab avatar Aug 12 '22 01:08 xmnlab

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

I'm interested in this too. My use case is that I want the default build type to be debugoptimized for git checkouts, but release for release tarballs. In our autotools-based setup (I'm starting to convert to meson) we normally have the version set to something like 1.2.3-git-$GIT_HASH, and then when we cut a release, we have a script that removes the suffix. Then we have a conditional in configure.ac that sets the default CFLAGS appropriately for debug or release depending on if the version string has a git hash suffix.

(Although, since we have a script to bump the version for release, I suppose it could also update the default buildtype option as well. But it would be more convenient to not have to do that.)

kelnos avatar Apr 06 '24 12:04 kelnos