Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules/rust: allow customizing env vars on bindgen #13631

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/markdown/Rust-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ It takes the following keyword arguments
- `dependencies`: a list of `Dependency` objects to pass to the underlying clang call (*since 1.0.0*)
- `language`: A literal string value of `c` or `cpp`. When set this will force bindgen to treat a source as the given language. Defaults to checking based on the input file extension. *(since 1.4.0)*
- `bindgen_version`: a list of string version values. When set the found bindgen binary must conform to these constraints. *(since 1.4.0)*
- `env`: environment variables to set, such as {'NAME1': 'value1', 'NAME2': 'value2'} or ['NAME1=value1', 'NAME2=value2'], or an env object which allows more sophisticated environment juggling. *(since 1.6.0)*

```meson
rust = import('unstable-rust')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Bindgen now allows customizing environment variables

Bindgen is configurable through environment variables on how it searches for
clang and what extra arguments it passes to the compiler. The bindgen() function
of the rust module now accepts an `env` keyword argument for customizing
environment variables. The type of the argument is the same as `env` of
[[custom_target]].
9 changes: 7 additions & 2 deletions mesonbuild/modules/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
CustomTarget, InvalidArguments, Jar, StructuredSources, SharedLibrary)
from ..compilers.compilers import are_asserts_disabled, lang_suffixes
from ..interpreter.type_checking import (
DEPENDENCIES_KW, LINK_WITH_KW, SHARED_LIB_KWS, TEST_KWS, OUTPUT_KW,
DEPENDENCIES_KW, LINK_WITH_KW, SHARED_LIB_KWS, TEST_KWS, OUTPUT_KW, ENV_KW,
INCLUDE_DIRECTORIES, SOURCES_VARARGS, NoneType, in_set_validator
)
from ..interpreterbase import ContainerTypeInfo, InterpreterException, KwargInfo, typed_kwargs, typed_pos_args, noPosargs, permittedKwargs
from ..mesonlib import File
from ..mesonlib import (
File, EnvironmentVariables,
)
from ..programs import ExternalProgram

if T.TYPE_CHECKING:
Expand Down Expand Up @@ -49,6 +51,7 @@ class FuncBindgen(TypedDict):
output: str
output_inline_wrapper: str
dependencies: T.List[T.Union[Dependency, ExternalLibrary]]
env: T.Optional[EnvironmentVariables]
language: T.Optional[Literal['c', 'cpp']]
bindgen_version: T.List[str]

Expand Down Expand Up @@ -198,6 +201,7 @@ def test(self, state: ModuleState, args: T.Tuple[str, BuildTarget], kwargs: Func
KwargInfo('bindgen_version', ContainerTypeInfo(list, str), default=[], listify=True, since='1.4.0'),
INCLUDE_DIRECTORIES.evolve(since_values={ContainerTypeInfo(list, str): '1.0.0'}),
OUTPUT_KW,
ENV_KW.evolve(since='1.6.0'),
KwargInfo(
'output_inline_wrapper',
str,
Expand Down Expand Up @@ -330,6 +334,7 @@ def bindgen(self, state: ModuleState, args: T.List, kwargs: FuncBindgen) -> Modu
extra_depends=depends,
depend_files=depend_files,
backend=state.backend,
env=kwargs['env'],
description='Generating bindings for Rust {}',
)

Expand Down
9 changes: 9 additions & 0 deletions test cases/rust/12 bindgen/env/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-license-identifer: Apache-2.0

# pragma once

#include "other.h"

#ifdef EXPOSE_API
int32_t add(const int32_t, const int32_t);
#endif
22 changes: 22 additions & 0 deletions test cases/rust/12 bindgen/env/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-license-identifer: Apache-2.0

gen_env = rust.bindgen(
input : 'header.h',
output : 'header.rs',
env : {'BINDGEN_EXTRA_CLANG_ARGS' : '-DEXPOSE_API'},
include_directories : inc,
)

f_env = configure_file(
input : '../src/main.rs',
output : 'main.rs',
copy : true,
)

rust_bin_env = executable(
'rust_bin_env',
[f_env, gen_env],
link_with : c_lib,
)

test('generate with custom environment variables', rust_bin_env)
1 change: 1 addition & 0 deletions test cases/rust/12 bindgen/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ endif

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

gp = rust.bindgen(
input : 'src/global-project.h',
Expand Down
Loading