mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.66k stars 1.64k forks source link

Support for test invoking generators at configure time #12396

Open mstorsjo opened 1 year ago

mstorsjo commented 1 year ago

If using a generator to build some files, one may want to test run it, similarly to compiler.compiles(), to detect exactly which constructs are supported by the external tool.

The specific case I have in mind is when assembling .S style assembly files with MS armasm, by invoking the gas-preprocessor tool as a custom generator, like this: https://code.videolan.org/videolan/dav1d/-/blob/1.3.0/meson.build?ref_type=tags#L450-465 As the assembler evolves and gets support for newer instruction sets, one would want to try it out to see which of these instruction set extensions it actually does support.

It's probably possible manually run the same commands with run_command() - although it probably would require specifying the command with all its arguments twice - once for the actual generator, and one for the run_command() calls. This probably works (I haven't tested it), but is inflexible. It would require storing the test input snippets in loose files in the project directory, instead of generating them dynamically inline in the meson file (or can meson write a string to a temp file and return the path to it? I doubt it).

Thus, generators would need something like compiler.compiles() in order to write a temporary file to disk (with a given suffix maybe) and invoke the generator at configure time (as opposed to at build time, as it normally otherwise is done with the generator.process() method).

This is somewhat related to #12395.

CC @ePirat

mstorsjo commented 8 months ago

FYI, see the topmost commit at https://code.videolan.org/mstorsjo/dav1d/-/commits/msvc-detect-archext for an example on how one can manually work around this issue for now - however this is probably a bit too ugly of a workaround to consider merging in its current form.

dcbaker commented 8 months ago

It sounds like what you're looking for is something like a gen.processes() method, that attempts to process a file or string written to file and then returns a bool as to whether it succeeded, analogous to compiler.compiles(), right?

mstorsjo commented 8 months ago

It sounds like what you're looking for is something like a gen.processes() method, that attempts to process a file or string written to file and then returns a bool as to whether it succeeded, analogous to compiler.compiles(), right?

Yes, that’s exactly right - thanks for rephrasing it!

mstorsjo commented 1 month ago

Based on examples by @amyspark, I've seen that this is possible to do with run_command() together with configure_file() (with the same trick as in https://github.com/mesonbuild/meson/issues/12395), which avoids the need to store the test snippets as individual files. It does require specifying the command arguments twice; once for the generator() and once for run_command(), but that's possible to mostly factorize out.

See the topmost commit at https://code.videolan.org/mstorsjo/dav1d/-/commits/msvc-detect-archext for an example of this; while it's not ideal (it produces an annoying configure message about Configuring test.S with command which distracts from the overall meson configure flow), it doesn't seem like it's possible to achieve this in any other way at the moment.