Skip to content

Commit 5499613

Browse files
authored
Merge pull request mesonbuild#12804 from joukewitteveen/dist-rewriter
Support `meson dist` when getting project versions from VCS
2 parents cc4cfbc + 524a661 commit 5499613

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

docs/markdown/Creating-releases.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,30 @@ meson setup builddir
8686
meson dist -C builddir
8787
```
8888
This produces `builddir/meson-dist/mysubproject-1.0.tar.xz` tarball.
89+
90+
## Cement a version obtained from VCS
91+
92+
*Since 1.4.0* the `meson dist` command enables rewriting the build
93+
configuration of the distribution tarball. This is needed when the
94+
configuration depends on metadata from revision control such as in the
95+
following example.
96+
97+
`meson.build`:
98+
```meson
99+
project('tig', 'c',
100+
version : run_command('version.sh', 'get-vcs').stdout.strip())
101+
102+
meson.add_dist_script('version.sh', 'set-dist', meson.project_version())
103+
```
104+
`version.sh`:
105+
```sh
106+
#!/bin/sh
107+
108+
if [ "$1" = "get-vcs" ]; then
109+
git -C "$MESON_SOURCE_ROOT" describe --always --dirty
110+
elif [ "$1" = "set-dist" ]; then
111+
$MESONREWRITE --sourcedir="$MESON_PROJECT_DIST_ROOT" kwargs set project / version "$2"
112+
else
113+
exit 1
114+
fi
115+
```

docs/markdown/Reference-tables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ For languages that don't have separate dynamic linkers such as C# and Java, the
8585
| Value | Comment |
8686
| ----- | ------- |
8787
| 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. |
88+
| MESONREWRITE | Command to run to run the rewriting command, only set when running `dist` scripts |
8889
| MESON_BUILD_ROOT | Absolute path to the build dir |
8990
| MESON_DIST_ROOT | Points to the root of the staging directory, only set when running `dist` scripts |
9091
| MESON_SOURCE_ROOT | Absolute path to the source dir |

docs/yaml/builtins/meson.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ methods:
3232
scripts, but for subproject scripts they have the path to the root of the
3333
subproject appended, usually `subprojects/<subproject-name>`.
3434
35+
*(since 1.4.0)* The `MESONREWRITE` environment variable contains the path
36+
to the rewrite command that corresponds to the `meson` executable that
37+
was used to configure the build. (This might be a different path than the
38+
first executable found in `PATH`.) It can be used to remove or replace
39+
any [[run_command]] that depends on the revision control system from the
40+
build configuration. Note that the value will contain many parts. For
41+
example, it may be `python3 /path/to/meson.py introspect`. The user is
42+
responsible for splitting the string to an array if needed by splitting
43+
lexically like a UNIX shell would. If your script uses Python,
44+
`shlex.split()` is the easiest correct way to do this.
45+
3546
posargs:
3647
script_name:
3748
type: str | file | external_program

mesonbuild/mdist.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from dataclasses import dataclass
2222
from glob import glob
2323
from pathlib import Path
24-
from mesonbuild.environment import detect_ninja
25-
from mesonbuild.mesonlib import (MesonException, RealPathAction, quiet_git,
24+
from mesonbuild.environment import Environment, detect_ninja
25+
from mesonbuild.mesonlib import (MesonException, RealPathAction, get_meson_command, quiet_git,
2626
windows_proof_rmtree, setup_vsenv, OptionKey)
2727
from mesonbuild.msetup import add_arguments as msetup_argparse
2828
from mesonbuild.wrap import wrap
@@ -103,10 +103,12 @@ def create_dist(self, archives: T.List[str]) -> T.List[str]:
103103

104104
def run_dist_scripts(self) -> None:
105105
assert os.path.isabs(self.distdir)
106-
env = {}
107-
env['MESON_DIST_ROOT'] = self.distdir
108-
env['MESON_SOURCE_ROOT'] = self.src_root
109-
env['MESON_BUILD_ROOT'] = self.bld_root
106+
mesonrewrite = Environment.get_build_command() + ['rewrite']
107+
env = {'MESON_DIST_ROOT': self.distdir,
108+
'MESON_SOURCE_ROOT': self.src_root,
109+
'MESON_BUILD_ROOT': self.bld_root,
110+
'MESONREWRITE': ' '.join(shlex.quote(x) for x in mesonrewrite),
111+
}
110112
for d in self.dist_scripts:
111113
if d.subproject and d.subproject not in self.subprojects:
112114
continue
@@ -328,9 +330,6 @@ def run(options: argparse.Namespace) -> int:
328330
b = build.load(options.wd)
329331
need_vsenv = T.cast('bool', b.environment.coredata.get_option(OptionKey('vsenv')))
330332
setup_vsenv(need_vsenv)
331-
# This import must be load delayed, otherwise it will get the default
332-
# value of None.
333-
from mesonbuild.mesonlib import get_meson_command
334333
src_root = b.environment.source_dir
335334
bld_root = b.environment.build_dir
336335
priv_dir = os.path.join(bld_root, 'meson-private')

test cases/unit/35 dist script/subprojects/sub/dist-script.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import os
44
import pathlib
5+
import shlex
6+
import subprocess
57
import sys
68

79
assert sys.argv[1] == 'success'
810

911
source_root = pathlib.Path(os.environ['MESON_PROJECT_DIST_ROOT'])
12+
mesonrewrite = shlex.split(os.environ['MESONREWRITE'])
13+
rewrite_cmd = ['kwargs', 'set', 'project', '/', 'version', 'release']
14+
15+
subprocess.run([*mesonrewrite, '-s', source_root, *rewrite_cmd], check=True)
16+
1017
modfile = source_root / 'prog.c'
1118
with modfile.open('w') as f:
1219
f.write('int main(){return 0;}')

test cases/unit/35 dist script/subprojects/sub/meson.build

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
project('sub', 'c')
1+
project('sub', 'c',
2+
version : 'vcs')
23

34
if get_option('broken_dist_script')
45
# Make sure we can add a dist script in a subproject, but it won't be run
@@ -8,4 +9,7 @@ else
89
# The dist script replace prog.c with something that actually build.
910
meson.add_dist_script('dist-script.py', 'success')
1011
executable('prog', 'prog.c')
12+
13+
versiontest = find_program('version-test.py')
14+
test('dist version replacement', versiontest, args : meson.project_version())
1115
endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python3
2+
3+
from sys import argv
4+
5+
assert argv[1] == 'release'

0 commit comments

Comments
 (0)