Skip to content

Commit

Permalink
rust: apply global, project, and environment C args to bindgen
Browse files Browse the repository at this point in the history
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: mesonbuild#12065
  • Loading branch information
dcbaker committed Sep 25, 2023
1 parent c1ac252 commit e6dd580
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
elif isinstance(s, CustomTarget):
depends.append(s)

clang_args.extend(state.global_args.get('c', []))
clang_args.extend(state.project_args.get('c', []))
cargs = state.get_option('args', state.subproject, lang='c')
assert isinstance(cargs, list), 'for mypy'
clang_args.extend(cargs)

if self._bindgen_bin is None:
self._bindgen_bin = state.find_program('bindgen')

Expand Down
18 changes: 18 additions & 0 deletions test cases/rust/12 bindgen/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ if not prog_bindgen.found()
error('MESON_SKIP_TEST bindgen not found')
endif

add_project_arguments('-DPROJECT_ARG', language : 'c')
add_global_arguments('-DGLOBAL_ARG', language : 'c')

# This seems to happen on windows when libclang.dll is not in path or is not
# valid. We must try to process a header file for this to work.
#
Expand Down Expand Up @@ -81,3 +84,18 @@ test('generated header', rust_bin2)

subdir('sub')
subdir('dependencies')

gp = rust.bindgen(
input : 'src/global-project.h',
output : 'global-project.rs',
)

gp_lib = static_library('gp_lib', 'src/global.c')

gp_exe = executable(
'gp_exe',
structured_sources(['src/global.rs', gp]),
link_with : gp_lib,
)

test('global and project arguments', gp_exe)
10 changes: 10 additions & 0 deletions test cases/rust/12 bindgen/src/global-project.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GLOBAL_ARG
char * success(void);
#endif
#ifndef PROJECT_ARG
char * success(void);
#endif
#ifndef CMD_ARG
char * success(void);
#endif
int success(void);
5 changes: 5 additions & 0 deletions test cases/rust/12 bindgen/src/global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "src/global-project.h"

int success(void) {
return 0;
}
14 changes: 14 additions & 0 deletions test cases/rust/12 bindgen/src/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-license-identifer: Apache-2.0
// Copyright © 2023 Intel Corporation

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!("global-project.rs");

fn main() {
unsafe {
std::process::exit(success());
};
}
5 changes: 4 additions & 1 deletion test cases/rust/12 bindgen/test.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"env": {
"CFLAGS": "-DCMD_ARG"
},
"stdout": [
{
"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]."
"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]."
}
]
}

0 comments on commit e6dd580

Please sign in to comment.