Overriding find_program on the command line
I wish to build someone else's software that uses meson. In their meson.build file they have written:
m4 = find_program('m4', required: true)
I do not want meson to search for a program called m4. Instead, I want to inform meson which program it should use for this purpose (specifically /opt/local/bin/gm4). I don't want to patch the meson.build file. If this were an autoconf-generated configure script, it would be a simple matter of specifying an argument like ac_cv_prog_M4=/opt/local/bin/gm4 on the command line when I run ./configure. What's the meson equivalent?
I have read How do I do X in Meson? and the documentation for find_program() and meson.override_find_program() and I haven't found anything that seems applicable to this.
@eli-schwartz I think I wanted this before for Valgrind, maybe.
@ryandesign You can do what we do in our zstd packaging:
# This replaces the no-find-valgrind patch once bugfix lands in a meson
# release + we can BDEPEND on it (https://github.com/mesonbuild/meson/pull/11372)
cat >> ${native_file} <<-EOF || die
[binaries]
valgrind='valgrind-falseified'
EOF
meson setup ... --native-file "${native_file}"
A machine file can easily specify the exact command to use. Specifying this on the command line is not currently possible (but has been discussed).
Related: https://github.com/mesonbuild/meson/pull/12623. You have to convince @jpakkane
You can do what we do in our zstd packaging
Thanks. It's less convenient to have to write these options to a separate file than to specify them on the command line directly, but it works—for find_program. I'm seeing meson.build files that find python by using find_installation instead, and this method doesn't seem to work for that. How do I override the results of find_installation? How many more ways are there that meson.build files typically find programs, and how do I override their results from the command line or from a machine file?
this method doesn't seem to work for […]
find_installation
Scratch that… the meson.build file said:
python = import('python').find_installation('python3')
so I assumed what I would have to add to the machine file was:
[binaries]
python3='/opt/local/bin/python3.12'
That didn't work. What did work however was:
[binaries]
python='/opt/local/bin/python3.12'
I've now convinced myself by looking through the python module's find_installation function that the python binary key is indeed the correct one to use:
https://github.com/mesonbuild/meson/blob/9e3b3db7054c7dedecd14db3e6061ff7e2227faf/mesonbuild/modules/python.py#L493
However searching through the meson codebase for other instances of lookup_binary_entry I found the python3 module and it uses the key python3:
https://github.com/mesonbuild/meson/blob/9e3b3db7054c7dedecd14db3e6061ff7e2227faf/mesonbuild/modules/python3.py#L60
So to override a particular build system's python choice, it sounds like I have to read the meson.build file to discover whether it is using the python or python3 module.
All of this information on how to override a meson.build file's program choices would be useful to have in the documentation.
The "How do I do X in Meson?" document seems to be a mix of information targeted at meson.build authors and users building software that uses a meson.build file. It might be useful to separate it clearly into two sections for those two different audiences. The section aimed at users should not assume any prior knowledge of meson or python—most users just want to build a piece of software, not understand its build system.
The python3 module is deprecated, so I wouldn't worry about what it's doing. I'm not sure that there's even anyone using it anymore, or has been for some time.
I think in the past couple of years I have seen maybe two projects that used it, and both were only using it because they started using it before the "python" module replaced it.