From db51dcfa47d66b094a13eb127e09d3134d8dbf1f Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Fri, 2 Feb 2024 16:23:21 +0100 Subject: [PATCH 1/2] mdist: Hoist leftover late import From 23d3b98, the meson command is exposed through a function. Therefore, the caveat no longer applies. --- mesonbuild/mdist.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mesonbuild/mdist.py b/mesonbuild/mdist.py index 67442885a389..93ef44eb1609 100644 --- a/mesonbuild/mdist.py +++ b/mesonbuild/mdist.py @@ -21,7 +21,7 @@ from glob import glob from pathlib import Path from mesonbuild.environment import detect_ninja -from mesonbuild.mesonlib import (MesonException, RealPathAction, quiet_git, +from mesonbuild.mesonlib import (MesonException, RealPathAction, get_meson_command, quiet_git, windows_proof_rmtree, setup_vsenv, OptionKey) from mesonbuild.msetup import add_arguments as msetup_argparse from mesonbuild.wrap import wrap @@ -327,9 +327,6 @@ def run(options: argparse.Namespace) -> int: b = build.load(options.wd) need_vsenv = T.cast('bool', b.environment.coredata.get_option(OptionKey('vsenv'))) setup_vsenv(need_vsenv) - # This import must be load delayed, otherwise it will get the default - # value of None. - from mesonbuild.mesonlib import get_meson_command src_root = b.environment.source_dir bld_root = b.environment.build_dir priv_dir = os.path.join(bld_root, 'meson-private') From 524a66116b38a65210b7516781d26ed6efb690b9 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Fri, 2 Feb 2024 18:20:37 +0100 Subject: [PATCH 2/2] Add MESONREWRITE to `meson dist` scripts Fixes #688 --- docs/markdown/Creating-releases.md | 27 +++++++++++++++++++ docs/markdown/Reference-tables.md | 1 + docs/yaml/builtins/meson.yaml | 11 ++++++++ mesonbuild/mdist.py | 12 +++++---- .../subprojects/sub/dist-script.py | 7 +++++ .../subprojects/sub/meson.build | 6 ++++- .../subprojects/sub/version-test.py | 5 ++++ 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 test cases/unit/35 dist script/subprojects/sub/version-test.py diff --git a/docs/markdown/Creating-releases.md b/docs/markdown/Creating-releases.md index 638125c87d45..e0c80af5b0e8 100644 --- a/docs/markdown/Creating-releases.md +++ b/docs/markdown/Creating-releases.md @@ -86,3 +86,30 @@ meson setup builddir meson dist -C builddir ``` This produces `builddir/meson-dist/mysubproject-1.0.tar.xz` tarball. + +## Cement a version obtained from VCS + +*Since 1.4.0* the `meson dist` command enables rewriting the build +configuration of the distribution tarball. This is needed when the +configuration depends on metadata from revision control such as in the +following example. + +`meson.build`: +```meson +project('tig', 'c', + version : run_command('version.sh', 'get-vcs').stdout.strip()) + +meson.add_dist_script('version.sh', 'set-dist', meson.project_version()) +``` +`version.sh`: +```sh +#!/bin/sh + +if [ "$1" = "get-vcs" ]; then + git -C "$MESON_SOURCE_ROOT" describe --always --dirty +elif [ "$1" = "set-dist" ]; then + $MESONREWRITE --sourcedir="$MESON_PROJECT_DIST_ROOT" kwargs set project / version "$2" +else + exit 1 +fi +``` diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md index af6a9b9f0bf4..02ac083e50b1 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -85,6 +85,7 @@ For languages that don't have separate dynamic linkers such as C# and Java, the | Value | Comment | | ----- | ------- | | MESONINTROSPECT | Command to run to run the introspection command, may be of the form `python /path/to/meson introspect`, user is responsible for splitting the path if necessary. | +| MESONREWRITE | Command to run to run the rewriting command, only set when running `dist` scripts | | MESON_BUILD_ROOT | Absolute path to the build dir | | MESON_DIST_ROOT | Points to the root of the staging directory, only set when running `dist` scripts | | MESON_SOURCE_ROOT | Absolute path to the source dir | diff --git a/docs/yaml/builtins/meson.yaml b/docs/yaml/builtins/meson.yaml index 0d4eacf608c0..516e41c3d9ae 100644 --- a/docs/yaml/builtins/meson.yaml +++ b/docs/yaml/builtins/meson.yaml @@ -32,6 +32,17 @@ methods: scripts, but for subproject scripts they have the path to the root of the subproject appended, usually `subprojects/`. + *(since 1.4.0)* The `MESONREWRITE` environment variable contains the path + to the rewrite command that corresponds to the `meson` executable that + was used to configure the build. (This might be a different path than the + first executable found in `PATH`.) It can be used to remove or replace + any [[run_command]] that depends on the revision control system from the + build configuration. Note that the value will contain many parts. For + example, it may be `python3 /path/to/meson.py introspect`. The user is + responsible for splitting the string to an array if needed by splitting + lexically like a UNIX shell would. If your script uses Python, + `shlex.split()` is the easiest correct way to do this. + posargs: script_name: type: str | file | external_program diff --git a/mesonbuild/mdist.py b/mesonbuild/mdist.py index 93ef44eb1609..23cc58035fcf 100644 --- a/mesonbuild/mdist.py +++ b/mesonbuild/mdist.py @@ -20,7 +20,7 @@ from dataclasses import dataclass from glob import glob from pathlib import Path -from mesonbuild.environment import detect_ninja +from mesonbuild.environment import Environment, detect_ninja from mesonbuild.mesonlib import (MesonException, RealPathAction, get_meson_command, quiet_git, windows_proof_rmtree, setup_vsenv, OptionKey) from mesonbuild.msetup import add_arguments as msetup_argparse @@ -102,10 +102,12 @@ def create_dist(self, archives: T.List[str]) -> T.List[str]: def run_dist_scripts(self) -> None: assert os.path.isabs(self.distdir) - env = {} - env['MESON_DIST_ROOT'] = self.distdir - env['MESON_SOURCE_ROOT'] = self.src_root - env['MESON_BUILD_ROOT'] = self.bld_root + mesonrewrite = Environment.get_build_command() + ['rewrite'] + env = {'MESON_DIST_ROOT': self.distdir, + 'MESON_SOURCE_ROOT': self.src_root, + 'MESON_BUILD_ROOT': self.bld_root, + 'MESONREWRITE': ' '.join(shlex.quote(x) for x in mesonrewrite), + } for d in self.dist_scripts: if d.subproject and d.subproject not in self.subprojects: continue diff --git a/test cases/unit/35 dist script/subprojects/sub/dist-script.py b/test cases/unit/35 dist script/subprojects/sub/dist-script.py index 1274f291bebd..5f1b4a12443a 100644 --- a/test cases/unit/35 dist script/subprojects/sub/dist-script.py +++ b/test cases/unit/35 dist script/subprojects/sub/dist-script.py @@ -2,11 +2,18 @@ import os import pathlib +import shlex +import subprocess import sys assert sys.argv[1] == 'success' source_root = pathlib.Path(os.environ['MESON_PROJECT_DIST_ROOT']) +mesonrewrite = shlex.split(os.environ['MESONREWRITE']) +rewrite_cmd = ['kwargs', 'set', 'project', '/', 'version', 'release'] + +subprocess.run([*mesonrewrite, '-s', source_root, *rewrite_cmd], check=True) + modfile = source_root / 'prog.c' with modfile.open('w') as f: f.write('int main(){return 0;}') diff --git a/test cases/unit/35 dist script/subprojects/sub/meson.build b/test cases/unit/35 dist script/subprojects/sub/meson.build index a41a3b685786..9dc047c56199 100644 --- a/test cases/unit/35 dist script/subprojects/sub/meson.build +++ b/test cases/unit/35 dist script/subprojects/sub/meson.build @@ -1,4 +1,5 @@ -project('sub', 'c') +project('sub', 'c', + version : 'vcs') if get_option('broken_dist_script') # Make sure we can add a dist script in a subproject, but it won't be run @@ -8,4 +9,7 @@ else # The dist script replace prog.c with something that actually build. meson.add_dist_script('dist-script.py', 'success') executable('prog', 'prog.c') + + versiontest = find_program('version-test.py') + test('dist version replacement', versiontest, args : meson.project_version()) endif diff --git a/test cases/unit/35 dist script/subprojects/sub/version-test.py b/test cases/unit/35 dist script/subprojects/sub/version-test.py new file mode 100644 index 000000000000..1dcafc49ab73 --- /dev/null +++ b/test cases/unit/35 dist script/subprojects/sub/version-test.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +from sys import argv + +assert argv[1] == 'release'