Can read project version from a file.

pull/8258/head
Jussi Pakkanen 4 years ago
parent 9eb8b6be28
commit 3f0a0c1582
  1. 4
      docs/markdown/Reference-manual.md
  2. 12
      docs/markdown/snippets/versionfile.md
  3. 28
      mesonbuild/interpreter.py
  4. 6
      run_unittests.py
  5. 1
      test cases/common/2 cpp/VERSION
  6. 2
      test cases/common/2 cpp/meson.build

@ -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()

@ -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.

@ -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'))

@ -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):
'''

@ -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'

Loading…
Cancel
Save