Skip to content

Commit

Permalink
rust: pass global and project C args to clang in bindgen
Browse files Browse the repository at this point in the history
This means that any arguments given project wide or globally (such as
preprocessor defines) will be set.

Fixes: mesonbuild#12065
  • Loading branch information
dcbaker committed Sep 23, 2023
1 parent d0b0989 commit b0066ad
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ 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', []))

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)
7 changes: 7 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,7 @@
#ifndef GLOBAL_ARG
char * success(void);
#endif
#ifndef PROJECT_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());
};
}
2 changes: 1 addition & 1 deletion test cases/rust/12 bindgen/test.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"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 b0066ad

Please sign in to comment.