diff mbox series

[5/9] meson: wire up generation of distribution archive

Message ID 20250113-b4-pks-meson-additions-v1-5-97f6a93f691d@pks.im (mailing list archive)
State Superseded
Headers show
Series meson: a couple of additions | expand

Commit Message

Patrick Steinhardt Jan. 13, 2025, 8:33 a.m. UTC
Meson knows to generate distribution archives via `meson dist`. Despite
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 itself to populate that file. There are two smallish
gotchas:

  - The script is executed in the build directory, not in the directory
    where we generate the archive. The target directory is propagated
    via the "MESON_DIST_ROOT" environment variable, but because we don't
    use a shell to execute GIT-VERSION-GEN we cannot populate the envvar
    into its arguments. We thus adapt GIT-VERSION-GEN to handle this for
    us.

  - We use the "GIT-VERSION-FILE.in" template to generate the version,
    which contains a "GIT_VERSION=" prefix that we need to strip after
    reading the "version" file. We could avoid this extra logic if we
    used a template that only had the `@GIT_VERSION@` placeholder, but
    it would require us to add one more template file.

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

Comments

Junio C Hamano Jan. 13, 2025, 5:55 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> Meson knows to generate distribution archives via `meson dist`. Despite
> 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.

My reading hiccupped at "Despite" that does not seem to say anything
contradicting to what follows.  Did you mean the same thing as "In
addition to" there?

> diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
> index 95d7d41d233450774a6580c0de332e7b8f69eb4e..1f0fb4098da392511f02a34cdcc84f3889771001 100755
> --- a/GIT-VERSION-GEN
> +++ b/GIT-VERSION-GEN
> @@ -19,6 +19,11 @@ then
>  	exit 1
>  fi
>  
> +if test -n "$OUTPUT" && test -n "$MESON_DIST_ROOT"
> +then
> +    OUTPUT="$MESON_DIST_ROOT/$OUTPUT"
> +fi
> +
>  DEF_VER=$(cat "$SOURCE_DIR"/GIT-VERSION)
>  
>  # Protect us from reading Git version information outside of the Git directory
> @@ -33,7 +38,7 @@ then
>  	# then try git-describe, then default.
>  	if test -f "$SOURCE_DIR"/version
>  	then
> -		VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
> +		VN=$(cat "$SOURCE_DIR"/version) && VN=${VN#GIT_VERSION=} || VN="$DEF_VER"

It used to be that the contents in the "version" file was the
ultimate truth to be used as-is, but now somebody may write it with
or without GIT_VERSION= prefix, and this one place is now prepared
to strip the extra prefix, but everybody else who has been happily
reading the "version" file is now broken until it is adjusted in the
same way?
Patrick Steinhardt Jan. 14, 2025, 9:14 a.m. UTC | #2
On Mon, Jan 13, 2025 at 09:55:58AM -0800, Junio C Hamano wrote:
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > Meson knows to generate distribution archives via `meson dist`. Despite
> > 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.
> 
> My reading hiccupped at "Despite" that does not seem to say anything
> contradicting to what follows.  Did you mean the same thing as "In
> addition to" there?

Yup, will fix.

> > diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
> > index 95d7d41d233450774a6580c0de332e7b8f69eb4e..1f0fb4098da392511f02a34cdcc84f3889771001 100755
> > --- a/GIT-VERSION-GEN
> > +++ b/GIT-VERSION-GEN
> > @@ -19,6 +19,11 @@ then
> >  	exit 1
> >  fi
> >  
> > +if test -n "$OUTPUT" && test -n "$MESON_DIST_ROOT"
> > +then
> > +    OUTPUT="$MESON_DIST_ROOT/$OUTPUT"
> > +fi
> > +
> >  DEF_VER=$(cat "$SOURCE_DIR"/GIT-VERSION)
> >  
> >  # Protect us from reading Git version information outside of the Git directory
> > @@ -33,7 +38,7 @@ then
> >  	# then try git-describe, then default.
> >  	if test -f "$SOURCE_DIR"/version
> >  	then
> > -		VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
> > +		VN=$(cat "$SOURCE_DIR"/version) && VN=${VN#GIT_VERSION=} || VN="$DEF_VER"
> 
> It used to be that the contents in the "version" file was the
> ultimate truth to be used as-is, but now somebody may write it with
> or without GIT_VERSION= prefix, and this one place is now prepared
> to strip the extra prefix, but everybody else who has been happily
> reading the "version" file is now broken until it is adjusted in the
> same way?

I think it would be fine and that other callers don't have to get
adjusted as long as they haven't been writing files like this. But
this snippet is going away in v2 anyway as I approach the problem a bit
differently now.

Patrick
diff mbox series

Patch

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 95d7d41d233450774a6580c0de332e7b8f69eb4e..1f0fb4098da392511f02a34cdcc84f3889771001 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -19,6 +19,11 @@  then
 	exit 1
 fi
 
+if test -n "$OUTPUT" && test -n "$MESON_DIST_ROOT"
+then
+    OUTPUT="$MESON_DIST_ROOT/$OUTPUT"
+fi
+
 DEF_VER=$(cat "$SOURCE_DIR"/GIT-VERSION)
 
 # Protect us from reading Git version information outside of the Git directory
@@ -33,7 +38,7 @@  then
 	# then try git-describe, then default.
 	if test -f "$SOURCE_DIR"/version
 	then
-		VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
+		VN=$(cat "$SOURCE_DIR"/version) && VN=${VN#GIT_VERSION=} || VN="$DEF_VER"
 	elif {
 			test -d "$SOURCE_DIR/.git" ||
 			test -d "${GIT_DIR:-.git}" ||
diff --git a/meson.build b/meson.build
index 0b559215e4f105ac87bd580d755f88c32b7b36ca..771bdded484a0c0e8638e7c6555e3f4e09e64025 100644
--- a/meson.build
+++ b/meson.build
@@ -1940,6 +1940,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(),
+  meson.current_source_dir() / 'GIT-VERSION-FILE.in',
+  'version',
+)
+
 summary({
   'curl': curl.found(),
   'expat': expat.found(),