Search code examples
meson-build

Can I use python lambda function in meson.build?


I want to get the header names from sources, like this:

# meson.build
sources = [ 'foo.cxx', ... ]
headers = map(lambda f: f.replace('.cxx', '.hxx'), sources)
library('mylib', sources, install: true)
install_headers(headers, preserve_path: true)

it doesn't compile:

../native/meson.build:72:21: ERROR: Expecting rparen got id.
headers = map(lambda f: f.replace('.cxx', '.hxx'), sources)
             ^_______^

Any suggest?

Isn't that meson.build a python script?

Because the document discourages using of shell globs, all source filenames are hard coded here. It's not always reasonable, consider a well designed project, some subdirectories are to be fully compiled and linked all the sources. It's just repeat yourself to list the filenames again. Of course there are workarounds, such as writing a sh script to do the globbing, but I don't like such hacks. So, if you're suggesting to me "no, don't do the string replacement, just list all the headers", I'd rather to know how to do it in a shorter code.


Solution

  • Isn't that meson.build a python script?

    Short answer: No.

    There are some details here: https://mesonbuild.com/FAQ.html#why-is-meson-not-just-a-python-module-so-i-could-code-my-build-setup-in-python

    In addition to those reasons, not exposing Python or any other "real" programming language makes it possible to port Meson's implementation to a different language. This might become necessary if, for example, Python turns out to be a performance bottleneck. This is an actual problem that has caused complications for GNU Autotools and SCons.

    The Meson project is quite opinionated about this. They really don't want Meson to be Turing-complete. The syntax looks similar to Python but is not.