Skip to content

Commit

Permalink
modules/rust: allow setting a version constraint on bindgen
Browse files Browse the repository at this point in the history
This allows us to ensure that the bindgen we're using is suitable for
our purpose.
  • Loading branch information
dcbaker committed Feb 23, 2024
1 parent 9752b89 commit e7f20ef
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/markdown/Rust-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ It takes the following keyword arguments
- `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*)
- `language`: A literal string value of `c` or `cpp`. When set this will force bindgen to treat a source as the given language. Defaults to checking based on the input file extension. *(since 1.4.0)*
- `bindgen_version`: a list of string version values. When set the found bindgen binary must conform to these constraints. *(since 1.4.0)*

```meson
rust = import('unstable-rust')
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2021 The Meson development team
# Copyright © 2023 Intel Corporation
# Copyright © 2023-2024 Intel Corporation

from __future__ import annotations

Expand Down
4 changes: 3 additions & 1 deletion mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FuncBindgen(TypedDict):
output: str
dependencies: T.List[T.Union[Dependency, ExternalLibrary]]
language: T.Optional[Literal['c', 'cpp']]
bindgen_version: T.List[str]


class RustModule(ExtensionModule):
Expand Down Expand Up @@ -192,6 +193,7 @@ def test(self, state: ModuleState, args: T.Tuple[str, BuildTarget], kwargs: Func
required=True,
),
KwargInfo('language', (str, NoneType), since='1.4.0', validator=in_set_validator({'c', 'cpp'})),
KwargInfo('bindgen_version', ContainerTypeInfo(list, str), default=[], listify=True, since='1.4.0'),
INCLUDE_DIRECTORIES.evolve(since_values={ContainerTypeInfo(list, str): '1.0.0'}),
OUTPUT_KW,
DEPENDENCIES_KW.evolve(since='1.0.0'),
Expand Down Expand Up @@ -236,7 +238,7 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
depends.append(s)

if self._bindgen_bin is None:
self._bindgen_bin = state.find_program('bindgen')
self._bindgen_bin = state.find_program('bindgen', wanted=kwargs['bindgen_version'])

name: str
if isinstance(header, File):
Expand Down
9 changes: 7 additions & 2 deletions test cases/rust/12 bindgen/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ if result.returncode() != 0
error('MESON_SKIP_TEST bindgen does not seem to work')
endif

rust = import('unstable-rust')

# Check version, this case is obviously impossible
testcase expect_error('Program \'bindgen\' not found or not executable')
rust.bindgen(input : 'include/other.h', output : 'other.rs', bindgen_version : ['< 0.1', '> 10000'])
endtestcase

# This is to test the include_directories argument to bindgen
inc = include_directories('include')

c_lib = static_library('clib', 'src/source.c', include_directories : inc)

rust = import('unstable-rust')

gen = rust.bindgen(
input : 'src/header.h',
output : 'header.rs',
Expand Down
2 changes: 1 addition & 1 deletion test cases/rust/12 bindgen/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"stdout": [
{
"line": "test cases/rust/12 bindgen/meson.build:42: WARNING: Project targets '>= 0.63' but uses feature introduced in '1.0.0': \"rust.bindgen\" keyword argument \"include_directories\" of type array[str]."
"line": "test cases/rust/12 bindgen/meson.build:47: WARNING: Project targets '>= 0.63' but uses feature introduced in '1.0.0': \"rust.bindgen\" keyword argument \"include_directories\" of type array[str]."
}
]
}

0 comments on commit e7f20ef

Please sign in to comment.