interpreter: new function add_project_dependencies()

This function can be used to add fundamental dependencies such as glib
to all build products in one fell swoop.  This can be useful whenever,
due to a project's coding conventions, it is not really possible to
compile any source file without including the dependency.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
pull/10347/head
Paolo Bonzini 3 years ago committed by Eli Schwartz
parent 06b76f7c9d
commit 3a960023d3
  1. 1
      data/syntax-highlighting/vim/syntax/meson.vim
  2. 11
      docs/markdown/snippets/add_project_dependencies.md
  3. 16
      docs/yaml/functions/add_project_dependencies.yaml
  4. 1
      mesonbuild/ast/interpreter.py
  5. 6
      mesonbuild/dependencies/base.py
  6. 22
      mesonbuild/interpreter/interpreter.py
  7. 2
      test cases/common/251 add_project_dependencies/inc/lib.h
  8. 14
      test cases/common/251 add_project_dependencies/lib.c
  9. 5
      test cases/common/251 add_project_dependencies/main.c
  10. 22
      test cases/common/251 add_project_dependencies/meson.build

@ -68,6 +68,7 @@ syn keyword mesonBuiltin
\ add_global_link_arguments
\ add_languages
\ add_project_arguments
\ add_project_dependencies
\ add_project_link_arguments
\ add_test_setup
\ alias_target

@ -0,0 +1,11 @@
## `add_project_dependencies()` function
Dependencies can now be added to all build products using
`add_project_dependencies()`. This can be useful in several
cases:
* with special dependencies such as `dependency('threads')`
* with system libraries such as `find_library('m')`
* with the `include_directories` keyword argument of
`declare_dependency()`, to add both source and build
directories to the include search path

@ -0,0 +1,16 @@
name: add_project_dependencies
since: 0.63.0
returns: void
description: |
Adds arguments to the compiler and linker command line, so that the
given set of dependencies is included in all build products for this
project.
varargs:
type: dep
name: dependencies
description: The dependencies to add; if internal dependencies are included,
they must not include any built object.
kwargs_inherit: add_global_arguments

@ -114,6 +114,7 @@ class AstInterpreter(InterpreterBase):
'add_global_arguments': self.func_do_nothing,
'add_global_link_arguments': self.func_do_nothing,
'add_project_arguments': self.func_do_nothing,
'add_project_dependencies': self.func_do_nothing,
'add_project_link_arguments': self.func_do_nothing,
'message': self.func_do_nothing,
'generator': self.func_do_nothing,

@ -165,6 +165,9 @@ class Dependency(HoldableObject):
else:
return 'unknown'
def get_include_dirs(self) -> T.List['IncludeDirs']:
return []
def get_include_type(self) -> str:
return self.include_type
@ -298,6 +301,9 @@ class InternalDependency(Dependency):
final_link_args, final_libraries, final_whole_libraries,
final_sources, final_deps, self.variables, [], [])
def get_include_dirs(self) -> T.List['IncludeDirs']:
return self.include_directories
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None,
configtool: T.Optional[str] = None, internal: T.Optional[str] = None,
default_value: T.Optional[str] = None,

@ -346,6 +346,7 @@ class Interpreter(InterpreterBase, HoldableObject):
'add_global_link_arguments': self.func_add_global_link_arguments,
'add_languages': self.func_add_languages,
'add_project_arguments': self.func_add_project_arguments,
'add_project_dependencies': self.func_add_project_dependencies,
'add_project_link_arguments': self.func_add_project_link_arguments,
'add_test_setup': self.func_add_test_setup,
'alias_target': self.func_alias_target,
@ -2654,6 +2655,27 @@ external dependencies (including libraries) must go to "dependencies".''')
def func_add_project_link_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwargs.FuncAddProjectArgs') -> None:
self._add_project_arguments(node, self.build.projects_link_args[kwargs['native']], args[0], kwargs)
@FeatureNew('add_project_dependencies', '0.63.0')
@typed_pos_args('add_project_dependencies', varargs=dependencies.Dependency)
@typed_kwargs('add_project_dependencies', NATIVE_KW, LANGUAGE_KW)
def func_add_project_dependencies(self, node: mparser.FunctionNode, args: T.Tuple[T.List[dependencies.Dependency]], kwargs: 'kwargs.FuncAddProjectArgs') -> None:
for_machine = kwargs['native']
for lang in kwargs['language']:
if lang not in self.compilers[for_machine]:
raise InvalidCode(f'add_project_dependencies() called before add_language() for language "{lang}"')
for d in dependencies.get_leaf_external_dependencies(args[0]):
compile_args = list(d.get_compile_args())
system_incdir = d.get_include_type() == 'system'
for i in d.get_include_dirs():
for lang in kwargs['language']:
comp = self.coredata.compilers[for_machine][lang]
for idir in i.to_string_list(self.environment.get_source_dir()):
compile_args.extend(comp.get_include_args(idir, system_incdir))
self._add_project_arguments(node, self.build.projects_args[for_machine], compile_args, kwargs)
self._add_project_arguments(node, self.build.projects_link_args[for_machine], d.get_link_args(), kwargs)
def _warn_about_builtin_args(self, args: T.List[str]) -> None:
# -Wpedantic is deliberately not included, since some people want to use it but not use -Wextra
# see e.g.

@ -0,0 +1,2 @@
#pragma once
extern int ok(void);

@ -0,0 +1,14 @@
#include <zlib.h>
#include <math.h>
#ifndef DEFINED
#error expected compile_arg not found
#endif
double zero;
int ok(void) {
void * something = deflate;
if(something != 0)
return 0;
return (int)cos(zero);
}

@ -0,0 +1,5 @@
#include "lib.h"
int main(void) {
return ok();
}

@ -0,0 +1,22 @@
project('zlib system dependency', 'c')
cc = meson.get_compiler('c')
m = cc.find_library('m', required: false)
add_project_dependencies(m, language: ['c'])
z = dependency('zlib', method: 'system', required: false)
if not z.found()
error('MESON_SKIP_TEST zlib not present')
endif
z_c_args = z.partial_dependency(compile_args: true, includes: true)
add_project_dependencies(z_c_args, language: 'c', native: false)
global_dep = declare_dependency(include_directories: include_directories('inc'),
compile_args: '-DDEFINED')
add_project_dependencies(global_dep, language: 'c', native: false)
lib = static_library('rary', 'lib.c')
exe = executable('prog', 'main.c', link_with: lib, dependencies: z)
test('test', exe)
Loading…
Cancel
Save