Skip to content

Commit 0f2f87a

Browse files
committed
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')`
1 parent 8b573d7 commit 0f2f87a

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## found programs now have a version method
2+
3+
The return value of [[find_program]] can now check the exact version of the
4+
found program, independent of the minimum version requirement. This can be used
5+
e.g. to perform different actions depending on the exact version detected.

docs/yaml/objects/external_program.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ methods:
2424
run_command(find_program('foo'), 'arg1', 'arg2')
2525
```
2626
27+
- name: version
28+
returns: str
29+
since: 0.62.0
30+
description: |
31+
The version number as a string, for example `1.2.8`.
32+
33+
`unknown` if the program cannot determine the version via a `--version` argument.
34+
2735
- name: full_path
2836
returns: str
2937
since: 0.55.0

mesonbuild/interpreter/interpreterobjects.py

+12
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def __init__(self, ep: ExternalProgram, interpreter: 'Interpreter') -> None:
516516
super().__init__(ep, interpreter)
517517
self.methods.update({'found': self.found_method,
518518
'path': self.path_method,
519+
'version': self.version_method,
519520
'full_path': self.full_path_method})
520521

521522
@noPosargs
@@ -543,6 +544,17 @@ def _full_path(self) -> str:
543544
assert path is not None
544545
return path
545546

547+
@noPosargs
548+
@noKwargs
549+
@FeatureNew('ExternalProgram.version', '0.62.0')
550+
def version_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
551+
if not self.found():
552+
raise InterpreterException('Unable to get the version of a not-found external program')
553+
try:
554+
return self.held_object.get_version(self.interpreter)
555+
except MesonException:
556+
return 'unknown'
557+
546558
def found(self) -> bool:
547559
return self.held_object.found()
548560

test cases/common/26 find program/meson.build

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ assert(not prog.found(), 'Version should be too old')
2525
prog = find_program('print-version.py', version : '>=1.0')
2626
assert(prog.found(), 'Program version should match')
2727

28+
prog = find_program('print-version.py')
29+
assert(prog.version() == '1.0', 'Program version should be detectable')
30+
2831
prog = find_program('print-version-with-prefix.py', version : '>=1.0')
2932
assert(prog.found(), 'Program version should match')
3033

0 commit comments

Comments
 (0)