diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2021-01-27 20:01:43 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2021-01-29 16:42:59 +0000 |
commit | 3f0a0c1582f55e0da950a7a321d25ffcf240ec6f (patch) | |
tree | 4edcd8f7cfd1e2f40be8253999a6204d3dac470e | |
parent | 9eb8b6be2874b343ffa1819a3afc2d4ed355b050 (diff) | |
download | meson-3f0a0c1582f55e0da950a7a321d25ffcf240ec6f.zip meson-3f0a0c1582f55e0da950a7a321d25ffcf240ec6f.tar.gz meson-3f0a0c1582f55e0da950a7a321d25ffcf240ec6f.tar.bz2 |
Can read project version from a file.
-rw-r--r-- | docs/markdown/Reference-manual.md | 4 | ||||
-rw-r--r-- | docs/markdown/snippets/versionfile.md | 12 | ||||
-rw-r--r-- | mesonbuild/interpreter.py | 28 | ||||
-rwxr-xr-x | run_unittests.py | 6 | ||||
-rw-r--r-- | test cases/common/2 cpp/VERSION | 1 | ||||
-rw-r--r-- | test cases/common/2 cpp/meson.build | 2 |
6 files changed, 47 insertions, 6 deletions
diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 3970d6d..3a844da 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -1432,7 +1432,9 @@ Project supports the following keyword arguments. - `version`: which is a free form string describing the version of this project. You can access the value in your Meson build files - with `meson.project_version()`. + with `meson.project_version()`. Since 0.57.0 this can also be a + `File` object pointing to a file that contains exactly one line of + text. ### run_command() diff --git a/docs/markdown/snippets/versionfile.md b/docs/markdown/snippets/versionfile.md new file mode 100644 index 0000000..6071f2c --- /dev/null +++ b/docs/markdown/snippets/versionfile.md @@ -0,0 +1,12 @@ +## Project version can be specified with a file + +Meson can be instructed to load project's version string from an +external file like this: + +```meson +project('foo', 'c' version: files('VERSION')) +``` + +The version file must contain exactly one line of text and that will +be set as the project's version. If the line ends in a newline +character, it is removed. diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index a92ce7b..8924a46 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -1,4 +1,4 @@ -# Copyright 2012-2019 The Meson development team +# Copyright 2012-2021 The Meson development team # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -3181,9 +3181,29 @@ external dependencies (including libraries) must go to "dependencies".''') if not self.is_subproject(): self.build.project_name = proj_name self.active_projectname = proj_name - self.project_version = kwargs.get('version', 'undefined') - if not isinstance(self.project_version, str): - raise InvalidCode('The version keyword argument must be a string.') + version = kwargs.get('version', 'undefined') + if isinstance(version, list): + if len(version) != 1: + raise InvalidCode('Version argument is an array with more than one entry.') + version = version[0] + if isinstance(version, mesonlib.File): + FeatureNew.single_use('version from file', '0.57.0', self.subproject) + self.add_build_def_file(version) + ifname = version.absolute_path(self.environment.source_dir, + self.environment.build_dir) + try: + ver_data = Path(ifname).read_text(encoding='utf-8').split('\n') + except FileNotFoundError: + raise InterpreterException('Version file not found.') + if len(ver_data) == 2 and ver_data[1] == '': + ver_data = ver_data[0:1] + if len(ver_data) != 1: + raise InterpreterException('Version file must contain exactly one line of text.') + self.project_version = ver_data[0] + elif isinstance(version, str): + self.project_version = version + else: + raise InvalidCode('The version keyword argument must be a string or a file.') if self.build.project_version is None: self.build.project_version = self.project_version proj_license = mesonlib.stringlistify(kwargs.get('license', 'unknown')) diff --git a/run_unittests.py b/run_unittests.py index ffd20d5..e2c108f 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -5212,6 +5212,12 @@ class AllPlatformTests(BasePlatformTests): os.utime(str(cmakefile)) self.assertReconfiguredBuildIsNoop() + def test_version_file(self): + srcdir = os.path.join(self.common_test_dir, '2 cpp') + self.init(srcdir) + projinfo = self.introspect('--projectinfo') + self.assertEqual(projinfo['version'], '1.0.0') + class FailureTests(BasePlatformTests): ''' diff --git a/test cases/common/2 cpp/VERSION b/test cases/common/2 cpp/VERSION new file mode 100644 index 0000000..3eefcb9 --- /dev/null +++ b/test cases/common/2 cpp/VERSION @@ -0,0 +1 @@ +1.0.0 diff --git a/test cases/common/2 cpp/meson.build b/test cases/common/2 cpp/meson.build index de8b98e..47cb7c5 100644 --- a/test cases/common/2 cpp/meson.build +++ b/test cases/common/2 cpp/meson.build @@ -1,4 +1,4 @@ -project('c++ test', 'cpp') +project('c++ test', 'cpp', version: files('VERSION')) cpp = meson.get_compiler('cpp') if cpp.get_id() == 'intel' |