Can specify a string to print when dep not found. Closes #2407.

pull/4658/head
Jussi Pakkanen 6 years ago
parent 7d5660dcbe
commit 4df9006ca4
  1. 2
      docs/markdown/Reference-manual.md
  2. 38
      docs/markdown/snippets/notfound_message.md
  3. 18
      mesonbuild/interpreter.py
  4. 6
      test cases/linuxlike/1 pkg-config/meson.build

@ -395,6 +395,8 @@ are also supported. This function supports the following keyword arguments:
the build machine system rather than the host system (i.e. where the the build machine system rather than the host system (i.e. where the
cross compiled binary will run on), usually only needed if you build cross compiled binary will run on), usually only needed if you build
a tool to be used during compilation. a tool to be used during compilation.
- `not_found_message` *(added 0.50.0)* is an optional string that will
be printed as a `message()` if the dependency was not found.
- `required`, when set to false, Meson will proceed with the build - `required`, when set to false, Meson will proceed with the build
even if the dependency is not found. Since *0.47.0* the value of a even if the dependency is not found. Since *0.47.0* the value of a
[`feature`](Build-options.md#features) option can also be passed. [`feature`](Build-options.md#features) option can also be passed.

@ -0,0 +1,38 @@
## New `not_found_message` for dependency
You can now specify a `not_found_message` that will be printed if the
specified dependency was not found. The point is to convert constructs
that look like this:
```meson
d = dependency('something', required: false)
if not d.found()
message('Will not be able to do something.')
endif
```
Into this:
```meson
d = dependency('something',
required: false,
not_found_message: 'Will not be able to do something.')
```
Or constructs like this:
```meson
d = dependency('something', required: false)
if not d.found()
error('Install something by doing XYZ.')
endif
```
into this:
```meson
d = dependency('something',
not_found_message: 'Install something by doing XYZ.')
```
Which works, because the default value of `required` is `true`.

@ -1900,6 +1900,7 @@ permitted_kwargs = {'add_global_arguments': {'language', 'native'},
'modules', 'modules',
'optional_modules', 'optional_modules',
'native', 'native',
'not_found_message',
'required', 'required',
'static', 'static',
'version', 'version',
@ -2645,6 +2646,9 @@ external dependencies (including libraries) must go to "dependencies".''')
@noKwargs @noKwargs
def func_message(self, node, args, kwargs): def func_message(self, node, args, kwargs):
argstr = self.get_message_string_arg(node) argstr = self.get_message_string_arg(node)
self.message_impl(argstr)
def message_impl(self, argstr):
mlog.log(mlog.bold('Message:'), argstr) mlog.log(mlog.bold('Message:'), argstr)
@FeatureNew('warning', '0.44.0') @FeatureNew('warning', '0.44.0')
@ -3013,13 +3017,27 @@ external dependencies (including libraries) must go to "dependencies".''')
@FeatureNewKwargs('dependency', '0.49.0', ['disabler']) @FeatureNewKwargs('dependency', '0.49.0', ['disabler'])
@FeatureNewKwargs('dependency', '0.40.0', ['method']) @FeatureNewKwargs('dependency', '0.40.0', ['method'])
@FeatureNewKwargs('dependency', '0.38.0', ['default_options']) @FeatureNewKwargs('dependency', '0.38.0', ['default_options'])
@FeatureNewKwargs('dependency', '0.50.0', ['not_found_message'])
@disablerIfNotFound @disablerIfNotFound
@permittedKwargs(permitted_kwargs['dependency']) @permittedKwargs(permitted_kwargs['dependency'])
def func_dependency(self, node, args, kwargs): def func_dependency(self, node, args, kwargs):
self.validate_arguments(args, 1, [str]) self.validate_arguments(args, 1, [str])
name = args[0] name = args[0]
display_name = name if name else '(anonymous)' display_name = name if name else '(anonymous)'
not_found_message = kwargs.get('not_found_message', '')
if not isinstance(not_found_message, str):
raise InvalidArguments('The not_found_message must be a string.')
try:
d = self.dependency_impl(name, display_name, kwargs)
except Exception:
if not_found_message:
self.message_impl(not_found_message)
raise
if not d.found() and not_found_message:
self.message_impl(not_found_message)
return d
def dependency_impl(self, name, display_name, kwargs):
disabled, required, feature = extract_required_kwarg(kwargs, self.subproject) disabled, required, feature = extract_required_kwarg(kwargs, self.subproject)
if disabled: if disabled:
mlog.log('Dependency', mlog.bold(display_name), 'skipped: feature', mlog.bold(feature), 'disabled') mlog.log('Dependency', mlog.bold(display_name), 'skipped: feature', mlog.bold(feature), 'disabled')

@ -2,7 +2,8 @@ project('external dependency', 'c')
# Zlib is probably on all dev machines. # Zlib is probably on all dev machines.
dep = dependency('zlib', version : '>=1.2') dep = dependency('zlib', version : '>=1.2',
not_found_message: 'DANGER! DANGER! THIS MUST NEVER BE SEEN!')
exe = executable('zlibprog', 'prog-checkver.c', exe = executable('zlibprog', 'prog-checkver.c',
dependencies : dep, dependencies : dep,
c_args : '-DFOUND_ZLIB="' + dep.version() + '"') c_args : '-DFOUND_ZLIB="' + dep.version() + '"')
@ -29,7 +30,8 @@ test('zlibtest2', exe2)
# Try to find a nonexistent library to ensure requires:false works. # Try to find a nonexistent library to ensure requires:false works.
dep = dependency('nvakuhrabnsdfasdf', required : false) dep = dependency('nvakuhrabnsdfasdf', required : false,
not_found_message : 'This dependency was not found as was expected.')
# Try to compile a test that takes a dep and an include_directories # Try to compile a test that takes a dep and an include_directories

Loading…
Cancel
Save