diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2022-12-11 00:09:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-11 00:09:27 +0200 |
commit | dbb33aaf92a581e87c1451933fe148eea6808ecc (patch) | |
tree | ee36ba59da94c02c30f92e18bb8bf59a5595a668 | |
parent | c8766468a30b997e637b21301fe9254c0c252bd2 (diff) | |
parent | d49e6bc0384a4a01754f13edb793cb1d6ce0b836 (diff) | |
download | meson-dbb33aaf92a581e87c1451933fe148eea6808ecc.tar.gz |
Merge pull request #11024 from dcbaker/submit/bindgen-dependencies
Add a `dependencies` keyword argument to bindgen
-rw-r--r-- | docs/markdown/Rust-module.md | 3 | ||||
-rw-r--r-- | docs/markdown/snippets/rust_bindgen_deps.md | 5 | ||||
-rw-r--r-- | mesonbuild/modules/rust.py | 40 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/dependencies/clib2.c | 5 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/dependencies/external_dep.h | 8 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/dependencies/internal_dep.h | 6 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/dependencies/internal_main.rs | 16 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/dependencies/meson.build | 43 | ||||
-rw-r--r-- | test cases/rust/12 bindgen/meson.build | 3 |
9 files changed, 112 insertions, 17 deletions
diff --git a/docs/markdown/Rust-module.md b/docs/markdown/Rust-module.md index e2c455e71..65eec64de 100644 --- a/docs/markdown/Rust-module.md +++ b/docs/markdown/Rust-module.md @@ -35,7 +35,7 @@ that automatically. Additional, test only dependencies may be passed via the dependencies argument. -### bindgen(*, input: string | BuildTarget | [](string | BuildTarget), output: string, include_directories: [](include_directories | string), c_args: []string, args: []string) +### bindgen(*, input: string | BuildTarget | [](string | BuildTarget), output: string, include_directories: [](include_directories | string), c_args: []string, args: []string, dependencies: []Dependency) This function wraps bindgen to simplify creating rust bindings around C libraries. This has two advantages over hand-rolling ones own with a @@ -54,6 +54,7 @@ It takes the following keyword arguments these are passed to clang as `-I` arguments *(string since 1.0.0)* - c_args — A list of string arguments to pass to clang untouched - args — A list of string arguments to pass to `bindgen` untouched. +- dependencies — A list of `Dependency` objects to pass to the underlying clang call (*since 1.0.0*) ```meson rust = import('unstable-rust') diff --git a/docs/markdown/snippets/rust_bindgen_deps.md b/docs/markdown/snippets/rust_bindgen_deps.md new file mode 100644 index 000000000..0e8ebef62 --- /dev/null +++ b/docs/markdown/snippets/rust_bindgen_deps.md @@ -0,0 +1,5 @@ +## rust.bindgen accepts a dependency argument + +The `bindgen` method of the `rust` module now accepts a dependencies argument. +Any include paths in these dependencies will be passed to the underlying call to +`clang`, and the call to `bindgen` will correctly depend on any generatd sources. diff --git a/mesonbuild/modules/rust.py b/mesonbuild/modules/rust.py index 4e75d59f0..d41b99ca4 100644 --- a/mesonbuild/modules/rust.py +++ b/mesonbuild/modules/rust.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations import os import typing as T @@ -19,7 +20,7 @@ from . import ExtensionModule, ModuleReturnValue, ModuleInfo from .. import mlog from ..build import BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList, IncludeDirs, CustomTarget, StructuredSources from ..dependencies import Dependency, ExternalLibrary -from ..interpreter.type_checking import TEST_KWS, OUTPUT_KW, INCLUDE_DIRECTORIES, include_dir_string_new +from ..interpreter.type_checking import DEPENDENCIES_KW, TEST_KWS, OUTPUT_KW, INCLUDE_DIRECTORIES, include_dir_string_new from ..interpreterbase import ContainerTypeInfo, InterpreterException, KwargInfo, typed_kwargs, typed_pos_args, noPosargs from ..mesonlib import File @@ -44,6 +45,7 @@ if T.TYPE_CHECKING: include_directories: T.List[IncludeDirs] input: T.List[SourceInputs] output: str + dependencies: T.List[T.Union[Dependency, ExternalLibrary]] class RustModule(ExtensionModule): @@ -52,9 +54,9 @@ class RustModule(ExtensionModule): INFO = ModuleInfo('rust', '0.57.0', stabilized='1.0.0') - def __init__(self, interpreter: 'Interpreter') -> None: + def __init__(self, interpreter: Interpreter) -> None: super().__init__(interpreter) - self._bindgen_bin: T.Optional['ExternalProgram'] = None + self._bindgen_bin: T.Optional[ExternalProgram] = None self.methods.update({ 'test': self.test, 'bindgen': self.bindgen, @@ -64,14 +66,10 @@ class RustModule(ExtensionModule): @typed_kwargs( 'rust.test', *TEST_KWS, + DEPENDENCIES_KW, KwargInfo('is_parallel', bool, default=False), - KwargInfo( - 'dependencies', - ContainerTypeInfo(list, (Dependency, ExternalLibrary)), - listify=True, - default=[]), ) - def test(self, state: 'ModuleState', args: T.Tuple[str, BuildTarget], kwargs: 'FuncTest') -> ModuleReturnValue: + def test(self, state: ModuleState, args: T.Tuple[str, BuildTarget], kwargs: FuncTest) -> ModuleReturnValue: """Generate a rust test target from a given rust target. Rust puts it's unitests inside it's main source files, unlike most @@ -176,8 +174,9 @@ class RustModule(ExtensionModule): ), INCLUDE_DIRECTORIES.evolve(feature_validator=include_dir_string_new), OUTPUT_KW, + DEPENDENCIES_KW.evolve(since='1.0.0'), ) - def bindgen(self, state: 'ModuleState', args: T.List, kwargs: 'FuncBindgen') -> ModuleReturnValue: + def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> ModuleReturnValue: """Wrapper around bindgen to simplify it's use. The main thing this simplifies is the use of `include_directory` @@ -186,7 +185,7 @@ class RustModule(ExtensionModule): header, *_deps = self.interpreter.source_strings_to_files(kwargs['input']) # Split File and Target dependencies to add pass to CustomTarget - depends: T.List['SourceOutputs'] = [] + depends: T.List[SourceOutputs] = [] depend_files: T.List[File] = [] for d in _deps: if isinstance(d, File): @@ -194,12 +193,23 @@ class RustModule(ExtensionModule): else: depends.append(d) - inc_strs: T.List[str] = [] + clang_args: T.List[str] = [] for i in state.process_include_dirs(kwargs['include_directories']): # bindgen always uses clang, so it's safe to hardcode -I here - inc_strs.extend([f'-I{x}' for x in i.to_string_list( + clang_args.extend([f'-I{x}' for x in i.to_string_list( state.environment.get_source_dir(), state.environment.get_build_dir())]) + for de in kwargs['dependencies']: + for i in de.get_include_dirs(): + clang_args.extend([f'-I{x}' for x in i.to_string_list( + state.environment.get_source_dir(), state.environment.get_build_dir())]) + clang_args.extend(de.get_all_compile_args()) + for s in de.get_sources(): + if isinstance(s, File): + depend_files.append(s) + elif isinstance(s, CustomTarget): + depends.append(s) + if self._bindgen_bin is None: self._bindgen_bin = state.find_program('bindgen') @@ -216,7 +226,7 @@ class RustModule(ExtensionModule): '@INPUT@', '--output', os.path.join(state.environment.build_dir, '@OUTPUT@') ] + \ - kwargs['args'] + ['--'] + kwargs['c_args'] + inc_strs + \ + kwargs['args'] + ['--'] + kwargs['c_args'] + clang_args + \ ['-MD', '-MQ', '@INPUT@', '-MF', '@DEPFILE@'] target = CustomTarget( @@ -236,5 +246,5 @@ class RustModule(ExtensionModule): return ModuleReturnValue([target], [target]) -def initialize(interp: 'Interpreter') -> RustModule: +def initialize(interp: Interpreter) -> RustModule: return RustModule(interp) diff --git a/test cases/rust/12 bindgen/dependencies/clib2.c b/test cases/rust/12 bindgen/dependencies/clib2.c new file mode 100644 index 000000000..63d3e0a25 --- /dev/null +++ b/test cases/rust/12 bindgen/dependencies/clib2.c @@ -0,0 +1,5 @@ +#include "internal_dep.h" + +int64_t add64(const int64_t first, const int64_t second) { + return first + second; +} diff --git a/test cases/rust/12 bindgen/dependencies/external_dep.h b/test cases/rust/12 bindgen/dependencies/external_dep.h new file mode 100644 index 000000000..284661be3 --- /dev/null +++ b/test cases/rust/12 bindgen/dependencies/external_dep.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifer: Apache-2.0 */ +/* Copyright © 2022 Intel Corporation */ + +#include <zlib.h> + +struct External { + z_stream * stream; +}; diff --git a/test cases/rust/12 bindgen/dependencies/internal_dep.h b/test cases/rust/12 bindgen/dependencies/internal_dep.h new file mode 100644 index 000000000..b0629de78 --- /dev/null +++ b/test cases/rust/12 bindgen/dependencies/internal_dep.h @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright © 2022 Intel Corporation + +#include "gen.h" + +int64_t add64(const int64_t, const int64_t); diff --git a/test cases/rust/12 bindgen/dependencies/internal_main.rs b/test cases/rust/12 bindgen/dependencies/internal_main.rs new file mode 100644 index 000000000..4890b43c2 --- /dev/null +++ b/test cases/rust/12 bindgen/dependencies/internal_main.rs @@ -0,0 +1,16 @@ +// SPDX-license-identifer: Apache-2.0 +// Copyright © 2021 Intel Corporation + +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!("internal_dep.rs"); + +use std::convert::TryInto; + +fn main() { + unsafe { + std::process::exit(add64(0, 0).try_into().unwrap_or(5)); + }; +} diff --git a/test cases/rust/12 bindgen/dependencies/meson.build b/test cases/rust/12 bindgen/dependencies/meson.build new file mode 100644 index 000000000..37e5a4234 --- /dev/null +++ b/test cases/rust/12 bindgen/dependencies/meson.build @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright © 2022 Intel Corporation + +dep_zlib = dependency('zlib', required : false, disabler : true) + +external_dep_rs = rust.bindgen( + input : 'external_dep.h', + output : 'external_dep.rs', + dependencies : dep_zlib +) + +external_dep = static_library( + 'external_dep', + [external_dep_rs], + dependencies : dep_zlib.partial_dependency(links : true), +) + +rust.test('external dep', external_dep) + +int_dep = declare_dependency( + sources : [gen_h, gen2_h], + include_directories : include_directories('..'), +) + +internal_dep_rs = rust.bindgen( + input : 'internal_dep.h', + output : 'internal_dep.rs', + dependencies : int_dep, +) + +c_lib2 = static_library( + 'clib2', + 'clib2.c', + dependencies : int_dep, +) + +rust_bin_int_dep = executable( + 'rust_bin_int_dep', + structured_sources(['internal_main.rs', internal_dep_rs]), + link_with : [c_lib, c_lib2], +) + +test('generated header dependency', rust_bin_int_dep) diff --git a/test cases/rust/12 bindgen/meson.build b/test cases/rust/12 bindgen/meson.build index b88aca2fd..c05cc0631 100644 --- a/test cases/rust/12 bindgen/meson.build +++ b/test cases/rust/12 bindgen/meson.build @@ -1,5 +1,5 @@ # SPDX-license-identifer: Apache-2.0 -# Copyright © 2021 Intel Corporation +# Copyright © 2021-2022 Intel Corporation project('rustmod bindgen', ['c', 'rust'], meson_version : '>= 0.63') @@ -80,3 +80,4 @@ rust_bin2 = executable( test('generated header', rust_bin2) subdir('sub') +subdir('dependencies') |