Skip to content

Commit 79fc894

Browse files
dcbakereli-schwartz
authored andcommitted
modules/rust: Specify the compiler version to bindgen when possible
bindgen by default may output code that an older rustc cannot successfully consume. To avoid this, we check if bindgen supports the rustc version we're using, if so, and if the user didn't set the `--rust-target` option, we will supply it to ensure that bindgen writes out code our rustc can use. If it does not support that version explicitly, we leave it at the default, assuming that our compiler version is newer than bindgen.
1 parent 21614ac commit 79fc894

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

mesonbuild/modules/rust.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class RustModule(ExtensionModule):
6262
def __init__(self, interpreter: Interpreter) -> None:
6363
super().__init__(interpreter)
6464
self._bindgen_bin: T.Optional[T.Union[ExternalProgram, Executable, OverrideProgram]] = None
65+
if 'rust' in interpreter.compilers.host:
66+
self._bindgen_rust_target: T.Optional[str] = interpreter.compilers.host['rust'].version
67+
else:
68+
self._bindgen_rust_target = None
6569
self.methods.update({
6670
'test': self.test,
6771
'bindgen': self.bindgen,
@@ -250,6 +254,15 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
250254

251255
if self._bindgen_bin is None:
252256
self._bindgen_bin = state.find_program('bindgen', wanted=kwargs['bindgen_version'])
257+
if self._bindgen_rust_target is not None:
258+
# ExternalCommand.command's type is bonkers
259+
_, _, err = mesonlib.Popen_safe(
260+
T.cast('T.List[str]', self._bindgen_bin.get_command()) +
261+
['--rust-target', self._bindgen_rust_target])
262+
# Sometimes this is "invalid Rust target" and sometimes "invalid
263+
# rust target"
264+
if 'Got an invalid' in err:
265+
self._bindgen_rust_target = None
253266

254267
name: str
255268
if isinstance(header, File):
@@ -317,9 +330,13 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
317330
'@INPUT@', '--output',
318331
os.path.join(state.environment.build_dir, '@OUTPUT0@')
319332
] + \
320-
kwargs['args'] + inline_wrapper_args + ['--'] + \
321-
kwargs['c_args'] + clang_args + \
322-
['-MD', '-MQ', '@INPUT@', '-MF', '@DEPFILE@']
333+
kwargs['args'] + inline_wrapper_args
334+
if self._bindgen_rust_target and '--rust-target' not in cmd:
335+
cmd.extend(['--rust-target', self._bindgen_rust_target])
336+
cmd.append('--')
337+
cmd.extend(kwargs['c_args'])
338+
cmd.extend(clang_args)
339+
cmd.extend(['-MD', '-MQ', '@INPUT@', '-MF', '@DEPFILE@'])
323340

324341
target = CustomTarget(
325342
f'rustmod-bindgen-{name}'.replace('/', '_'),

0 commit comments

Comments
 (0)