Skip to content

Commit

Permalink
modules/rust: inject a C/C++ standard for bindgen
Browse files Browse the repository at this point in the history
Especially for C++ this is very important.
  • Loading branch information
dcbaker committed Feb 23, 2024
1 parent 05d49c6 commit a75ced6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/markdown/snippets/rust-bindgen-std.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Bindgen now uses the same C/C++ as the project as a whole

Which is very important for C++ bindings.
17 changes: 17 additions & 0 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,23 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
if language == 'cpp':
clang_args.extend(['-x', 'c++'])

# Add the C++ standard to the clang arguments. Attempt to translate VS
# extension versions into the nearest standard version
std = state.get_option('std', lang=language)
assert isinstance(std, str), 'for mypy'
if std.startswith('vc++'):
if std.endswith('latest'):
mlog.warning('Attempting to translate vc++latest into a clang compatible version.',
'Currently this is hardcoded for c++20', once=True, fatal=False)
std = 'c++20'
else:
mlog.debug('The current C++ standard is a Visual Studio extension version.',
'bindgen will use a the nearest C++ standard instead')
std = std[1:]

if std != 'none':
clang_args.append(f'-std={std}')

cmd = self._bindgen_bin.get_command() + \
[
'@INPUT@', '--output',
Expand Down
8 changes: 6 additions & 2 deletions test cases/rust/12 bindgen/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# SPDX-license-identifer: Apache-2.0
# Copyright © 2021-2022 Intel Corporation
# Copyright © 2021-2024 Intel Corporation

project('rustmod bindgen', ['c', 'cpp', 'rust'], meson_version : '>= 0.63')
project(
'rustmod bindgen',
['c', 'cpp', 'rust'],
meson_version : '>= 0.63',
default_options : ['cpp_std=c++17'])

prog_bindgen = find_program('bindgen', required : false)
if not prog_bindgen.found()
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:38: 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:42: 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 a75ced6

Please sign in to comment.