diff mbox series

[v2,06/11] meson: wire up generation of distribution archive

Message ID 20250114-b4-pks-meson-additions-v2-6-8d7ec676cfd9@pks.im (mailing list archive)
State Superseded
Headers show
Series meson: a couple of additions | expand

Commit Message

Patrick Steinhardt Jan. 14, 2025, 11:56 a.m. UTC
Meson knows to generate distribution archives via `meson dist`. In
addition to generating the archive itself, this target also knows to
compile and execute tests from that archive, which helps to ensure that
the result is an adequate drop-in replacement for the versioned project.

While this already works as-is, one omission is that we don't propagate
the commit that this is built from into the resulting archive. This can
be fixed though by adding a distribution script that propagates the
version into the "version" file, which GIT-VERSION-GEN knows to read if
present.

Use GIT-VERSION-GEN to populate that file. As the script is executed in
the build directory, not in the directory where we generate the archive,
we have adapt it to honor the "MESON_DIST_ROOT" environment variable.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 GIT-VERSION-GEN |  4 ++++
 meson.build     | 12 ++++++++++++
 2 files changed, 16 insertions(+)

Comments

Toon Claes Jan. 21, 2025, 12:37 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> Meson knows to generate distribution archives via `meson dist`. In
> addition to generating the archive itself, this target also knows to
> compile and execute tests from that archive, which helps to ensure that
> the result is an adequate drop-in replacement for the versioned project.
>
> While this already works as-is, one omission is that we don't propagate
> the commit that this is built from into the resulting archive. This can
> be fixed though by adding a distribution script that propagates the
> version into the "version" file, which GIT-VERSION-GEN knows to read if
> present.
>
> Use GIT-VERSION-GEN to populate that file. As the script is executed in
> the build directory, not in the directory where we generate the archive,
> we have adapt it to honor the "MESON_DIST_ROOT" environment variable.

I failed to understand why you couldn't pass the absolute path of the
output file to GIT-VERSION-GEN. So I looked at the previous version of
this patch, and it seems you explain better over there.

I was testing things locally and tried this line for the last argument
to the script:

  run_command(shell, '-c', 'echo $MESON_DIST_ROOT', capture: true, check: true).stdout().strip() / 'version',

And I think I understand it better now. Meson does not execute this when
you run `meson dist`, but when it (re)generates it's build files. At
that stage $MESON_DIST_ROOT is not set.

It's unfortunate we have to learn GIT-VERSION-GEN about the
$MESON_DIST_ROOT environment variable, but I don't see any other way.


--
Toon
Patrick Steinhardt Jan. 22, 2025, 12:05 p.m. UTC | #2
On Tue, Jan 21, 2025 at 01:37:23PM +0100, Toon Claes wrote:
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > Meson knows to generate distribution archives via `meson dist`. In
> > addition to generating the archive itself, this target also knows to
> > compile and execute tests from that archive, which helps to ensure that
> > the result is an adequate drop-in replacement for the versioned project.
> >
> > While this already works as-is, one omission is that we don't propagate
> > the commit that this is built from into the resulting archive. This can
> > be fixed though by adding a distribution script that propagates the
> > version into the "version" file, which GIT-VERSION-GEN knows to read if
> > present.
> >
> > Use GIT-VERSION-GEN to populate that file. As the script is executed in
> > the build directory, not in the directory where we generate the archive,
> > we have adapt it to honor the "MESON_DIST_ROOT" environment variable.
> 
> I failed to understand why you couldn't pass the absolute path of the
> output file to GIT-VERSION-GEN. So I looked at the previous version of
> this patch, and it seems you explain better over there.
> 
> I was testing things locally and tried this line for the last argument
> to the script:
> 
>   run_command(shell, '-c', 'echo $MESON_DIST_ROOT', capture: true, check: true).stdout().strip() / 'version',
> 
> And I think I understand it better now. Meson does not execute this when
> you run `meson dist`, but when it (re)generates it's build files. At
> that stage $MESON_DIST_ROOT is not set.
> 
> It's unfortunate we have to learn GIT-VERSION-GEN about the
> $MESON_DIST_ROOT environment variable, but I don't see any other way.

Hm. Thinking about it a bit more there is an alternative:

    meson.add_dist_script(
      shell,
      '-c',
      '"$1" "$2" "$3" --format="@GIT_VERSION@" "$MESON_DIST_ROOT/version"',
      'GIT-VERSION-GEN',
      shell,
      meson.current_source_dir() / 'GIT-VERSION-GEN',
      meson.current_source_dir(),
    )

I think this is a much better solution as it doesn't require us to teach
the script about `MESON_DIST_ROOT`.

Patrick
diff mbox series

Patch

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 9d201a98fd2766911544225c62159cbfe8dff5fe..f6764555ce6fd46ca4ddbaebb3b48809707e60f8 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -28,6 +28,10 @@  case "$2" in
 esac
 
 OUTPUT="$3"
+if test -n "$OUTPUT" && test -n "$MESON_DIST_ROOT"
+then
+    OUTPUT="$MESON_DIST_ROOT/$OUTPUT"
+fi
 
 # Protect us from reading Git version information outside of the Git directory
 # in case it is not a repository itself, but embedded in an unrelated
diff --git a/meson.build b/meson.build
index ab4f229436d3070de692a24c3a196a79d214b46f..5d074134195e5689d08da5597f0859d9623d014e 100644
--- a/meson.build
+++ b/meson.build
@@ -1947,6 +1947,18 @@  devenv.set('GIT_BUILD_DIR', meson.current_build_dir())
 devenv.prepend('PATH', meson.current_build_dir() / 'bin-wrappers')
 meson.add_devenv(devenv)
 
+# Generate the 'version' file in the distribution tarball. This is used via
+# `meson dist -C <builddir>` to populate the source archive with the Git
+# version that the archive is being generated from. GIT-VERSION-GEN knows to
+# pick up this file.
+meson.add_dist_script(
+  shell,
+  meson.current_source_dir() / 'GIT-VERSION-GEN',
+  meson.current_source_dir(),
+  '--format=@GIT_VERSION@',
+  'version',
+)
+
 summary({
   'curl': curl.found(),
   'expat': expat.found(),