Skip to content

Commit

Permalink
interpreter: new function add_project_dependencies()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bonzini authored and eli-schwartz committed May 3, 2022
1 parent 06b76f7 commit 3a96002
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions data/syntax-highlighting/vim/syntax/meson.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions docs/markdown/snippets/add_project_dependencies.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions docs/yaml/functions/add_project_dependencies.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions mesonbuild/ast/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(self, source_root: str, subdir: str, subproject: str, visitors: T.O
'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,
Expand Down
6 changes: 6 additions & 0 deletions mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def get_version(self) -> str:
else:
return 'unknown'

def get_include_dirs(self) -> T.List['IncludeDirs']:
return []

def get_include_type(self) -> str:
return self.include_type

Expand Down Expand Up @@ -298,6 +301,9 @@ def get_partial_dependency(self, *, compile_args: bool = False,
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,
Expand Down
22 changes: 22 additions & 0 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ def build_func_dict(self) -> None:
'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,
Expand Down Expand Up @@ -2654,6 +2655,27 @@ def func_add_project_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T
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.
Expand Down
2 changes: 2 additions & 0 deletions test cases/common/251 add_project_dependencies/inc/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
extern int ok(void);
14 changes: 14 additions & 0 deletions test cases/common/251 add_project_dependencies/lib.c
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions test cases/common/251 add_project_dependencies/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "lib.h"

int main(void) {
return ok();
}
22 changes: 22 additions & 0 deletions test cases/common/251 add_project_dependencies/meson.build
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3a96002

Please sign in to comment.