Repository: mesonbuild/meson

Issue:
Expose `Environment::get_build_command()` to meson.build
Hi,

As part of the build generation I would like to generate tests / scripts that in invoke meson. One example is generating a script that runs `DESTDIR=/sometemp meson install` and then runs tests against that temporary installation. I can't just use `find_program('meson')` because meson may not be on `PATH` or the meson on `PATH` isn't the same as used in a build directory.

I obviously can just invoke `ninja install ` in such scripts / tests, but that will not work in case a different backend is used. Hence the desire to expose `get_build_command()`.

Would a patch adding that feature have a decent chance of getting accepted?

A second, less crucial, use-case for this is that I would like to generate a script that uses meson introspect + knowledge of the source-tree to compares - and optionally resyncs -  the list of tests existing in the source tree and the tests that the corresponding meson.build knows about.

`get_build_command()` effectively already is exposed via `MESONINTROSPECT` (including inside `run_command` and `add_postconf_script`, which doesn't seem to be documented). Exposing just the meson invocation as an array to meson.build seems a lot cleaner however than needing to do shell-splitting inside commands.

Regards,

Andres


Discussion:
Seems like a reasonable addition. For some reason or another I seemed to remember that we already expose that info. Maybe we did in the past and it was removed for some reason.
To clarify what you are asking for: Do you want to have a `MESONCOMMAND` variable like `MESONINTROSPECT` in `run_command` or do you want a command in `meson.build` that returns an array with the meson command?
Hi,

On Fri, Apr 2, 2021, at 08:43, behlec wrote:
> To clarify what you are asking for: Do you want to have a 
> `MESONCOMMAND` variable like `MESONINTROSPECT` in `run_command` or do 
> you want a command in `meson.build` that returns an array with the 
> meson command?

The latter.  That way no shell string parsing has to be done.

Regards,

Andres

> That way no shell string parsing has to be done.

Well there is still need for some parsing logic even if it is passed as an argument. The array may contain one or two entries (I guess theoretically it could have even more than that). Making it work reliably requires code in every script that uses this.
> > That way no shell string parsing has to be done.
> 
> Well there is still need for some parsing logic even if it is passed as an argument. The array may contain one or two entries (I guess theoretically it could have even more than that). Making it work reliably requires code in every script that uses this.

I'm not quite following? What *parsing* logic would there need to be? If you want to interpolate it into a script (e.g. as part of a configure_file), then you'd need some *escaping* to deal e.g. spaces in paths - but no parsing? And if you want to run a command from within meson.build, isn't all that's needed an array concat to add the arguments?

Is there any update on this issue? I am willing to make the necessary changes but I am waiting for input or a decision on how to proceed?
@jpakkane ping? There's a PR, implementing this - but it seems to be stuck waiting for design input?

Resolving patch:
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py
--- a/mesonbuild/interpreter/interpreter.py
+++ b/mesonbuild/interpreter/interpreter.py
@@ -1665,6 +1665,10 @@ def program_lookup(self, args: T.List[mesonlib.FileOrString], for_machine: Machi
         if progobj:
             return progobj
 
+        if args[0] == 'meson':
+            # Override find_program('meson') to return what we were invoked with
+            return ExternalProgram('meson', self.environment.get_build_command(), silent=True)
+
         fallback = None
         wrap_mode = self.coredata.get_option(OptionKey('wrap_mode'))
         if wrap_mode != WrapMode.nofallback and self.environment.wrap_resolver:


Test patch: