Skip to content

Commit e6dd580

Browse files
committed
rust: apply global, project, and environment C args to bindgen
This means that arguments set via `add_global_arguments`, `add_project_arguments` and by either the `-Dc_args` or `CFLAGS` are applied to bindgen as well. This can be important when, among other things, #defines are set via these mechanisms. Fixes: #12065
1 parent c1ac252 commit e6dd580

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

mesonbuild/modules/rust.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
236236
elif isinstance(s, CustomTarget):
237237
depends.append(s)
238238

239+
clang_args.extend(state.global_args.get('c', []))
240+
clang_args.extend(state.project_args.get('c', []))
241+
cargs = state.get_option('args', state.subproject, lang='c')
242+
assert isinstance(cargs, list), 'for mypy'
243+
clang_args.extend(cargs)
244+
239245
if self._bindgen_bin is None:
240246
self._bindgen_bin = state.find_program('bindgen')
241247

test cases/rust/12 bindgen/meson.build

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ if not prog_bindgen.found()
88
error('MESON_SKIP_TEST bindgen not found')
99
endif
1010

11+
add_project_arguments('-DPROJECT_ARG', language : 'c')
12+
add_global_arguments('-DGLOBAL_ARG', language : 'c')
13+
1114
# This seems to happen on windows when libclang.dll is not in path or is not
1215
# valid. We must try to process a header file for this to work.
1316
#
@@ -81,3 +84,18 @@ test('generated header', rust_bin2)
8184

8285
subdir('sub')
8386
subdir('dependencies')
87+
88+
gp = rust.bindgen(
89+
input : 'src/global-project.h',
90+
output : 'global-project.rs',
91+
)
92+
93+
gp_lib = static_library('gp_lib', 'src/global.c')
94+
95+
gp_exe = executable(
96+
'gp_exe',
97+
structured_sources(['src/global.rs', gp]),
98+
link_with : gp_lib,
99+
)
100+
101+
test('global and project arguments', gp_exe)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef GLOBAL_ARG
2+
char * success(void);
3+
#endif
4+
#ifndef PROJECT_ARG
5+
char * success(void);
6+
#endif
7+
#ifndef CMD_ARG
8+
char * success(void);
9+
#endif
10+
int success(void);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/global-project.h"
2+
3+
int success(void) {
4+
return 0;
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-license-identifer: Apache-2.0
2+
// Copyright © 2023 Intel Corporation
3+
4+
#![allow(non_upper_case_globals)]
5+
#![allow(non_camel_case_types)]
6+
#![allow(non_snake_case)]
7+
8+
include!("global-project.rs");
9+
10+
fn main() {
11+
unsafe {
12+
std::process::exit(success());
13+
};
14+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
2+
"env": {
3+
"CFLAGS": "-DCMD_ARG"
4+
},
25
"stdout": [
36
{
4-
"line": "test cases/rust/12 bindgen/meson.build:27: WARNING: Project targets '>= 0.63' but uses feature introduced in '1.0.0': \"rust.bindgen\" keyword argument \"include_directories\" of type array[str]."
7+
"line": "test cases/rust/12 bindgen/meson.build:30: WARNING: Project targets '>= 0.63' but uses feature introduced in '1.0.0': \"rust.bindgen\" keyword argument \"include_directories\" of type array[str]."
58
}
69
]
710
}

0 commit comments

Comments
 (0)