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
pull/12289/head
Dylan Baker 1 year ago
parent c1ac252f4f
commit d5546bdcea
  1. 6
      mesonbuild/modules/rust.py
  2. 18
      test cases/rust/12 bindgen/meson.build
  3. 10
      test cases/rust/12 bindgen/src/global-project.h
  4. 5
      test cases/rust/12 bindgen/src/global.c
  5. 14
      test cases/rust/12 bindgen/src/global.rs
  6. 5
      test cases/rust/12 bindgen/test.json

@ -236,6 +236,12 @@ class RustModule(ExtensionModule):
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')

@ -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.
#
@ -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)

@ -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);

@ -0,0 +1,5 @@
#include "src/global-project.h"
int success(void) {
return 0;
}

@ -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());
};
}

@ -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]."
}
]
}

Loading…
Cancel
Save