find_program: add a version() method to match the one for dependencies

It is often useful to check the found version of a program without
checking whether you can successfully find
`find_program('foo', required: false, version: '>=XXX')`
pull/10079/head
Eli Schwartz 3 years ago
parent 8b573d7dc6
commit 0f2f87a003
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 5
      docs/markdown/snippets/find_program-version.md
  2. 8
      docs/yaml/objects/external_program.yaml
  3. 12
      mesonbuild/interpreter/interpreterobjects.py
  4. 3
      test cases/common/26 find program/meson.build

@ -0,0 +1,5 @@
## found programs now have a version method
The return value of [[find_program]] can now check the exact version of the
found program, independent of the minimum version requirement. This can be used
e.g. to perform different actions depending on the exact version detected.

@ -24,6 +24,14 @@ methods:
run_command(find_program('foo'), 'arg1', 'arg2') run_command(find_program('foo'), 'arg1', 'arg2')
``` ```
- name: version
returns: str
since: 0.62.0
description: |
The version number as a string, for example `1.2.8`.
`unknown` if the program cannot determine the version via a `--version` argument.
- name: full_path - name: full_path
returns: str returns: str
since: 0.55.0 since: 0.55.0

@ -516,6 +516,7 @@ class ExternalProgramHolder(ObjectHolder[ExternalProgram]):
super().__init__(ep, interpreter) super().__init__(ep, interpreter)
self.methods.update({'found': self.found_method, self.methods.update({'found': self.found_method,
'path': self.path_method, 'path': self.path_method,
'version': self.version_method,
'full_path': self.full_path_method}) 'full_path': self.full_path_method})
@noPosargs @noPosargs
@ -543,6 +544,17 @@ class ExternalProgramHolder(ObjectHolder[ExternalProgram]):
assert path is not None assert path is not None
return path return path
@noPosargs
@noKwargs
@FeatureNew('ExternalProgram.version', '0.62.0')
def version_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
if not self.found():
raise InterpreterException('Unable to get the version of a not-found external program')
try:
return self.held_object.get_version(self.interpreter)
except MesonException:
return 'unknown'
def found(self) -> bool: def found(self) -> bool:
return self.held_object.found() return self.held_object.found()

@ -25,6 +25,9 @@ assert(not prog.found(), 'Version should be too old')
prog = find_program('print-version.py', version : '>=1.0') prog = find_program('print-version.py', version : '>=1.0')
assert(prog.found(), 'Program version should match') assert(prog.found(), 'Program version should match')
prog = find_program('print-version.py')
assert(prog.version() == '1.0', 'Program version should be detectable')
prog = find_program('print-version-with-prefix.py', version : '>=1.0') prog = find_program('print-version-with-prefix.py', version : '>=1.0')
assert(prog.found(), 'Program version should match') assert(prog.found(), 'Program version should match')

Loading…
Cancel
Save