cargo: Call into meson subdir if it exists

This allows projects to manually add extra rust args and deps. This is
intended to replace build.rs logic.
pull/11867/head
Xavier Claessens 1 year ago
parent 4d55645c39
commit 435e881c18
  1. 8
      docs/markdown/Wrap-dependency-system-manual.md
  2. 35
      mesonbuild/cargo/interpreter.py
  3. 1
      test cases/rust/22 cargo subproject/subprojects/foo-rs/meson/meson.build
  4. 1
      test cases/rust/22 cargo subproject/subprojects/foo-rs/src/lib.rs

@ -356,6 +356,14 @@ project(...,
)
```
In addition, if the file `meson/meson.build` exists, Meson will call `subdir('meson')`
where the project can add manual logic that would usually be part of `build.rs`.
Some naming conventions need to be respected:
- The `extra_args` variable is pre-defined and can be used to add any Rust arguments.
This is typically used as `extra_args += ['--cfg', 'foo']`.
- The `extra_deps` variable is pre-defined and can be used to add extra dependencies.
This is typically used as `extra_deps += dependency('foo')`.
## Using wrapped projects
Wraps provide a convenient way of obtaining a project into your

@ -366,6 +366,14 @@ def _options_varname(depname: str) -> str:
return f'{fixup_meson_varname(depname)}_options'
def _extra_args_varname() -> str:
return 'extra_args'
def _extra_deps_varname() -> str:
return 'extra_deps'
def _create_project(cargo: Manifest, build: builder.Builder) -> T.List[mparser.BaseNode]:
"""Create a function call
@ -561,6 +569,25 @@ def _create_dependencies(cargo: Manifest, build: builder.Builder) -> T.List[mpar
return ast
def _create_meson_subdir(cargo: Manifest, build: builder.Builder) -> T.List[mparser.BaseNode]:
# Allow Cargo subprojects to add extra Rust args in meson/meson.build file.
# This is used to replace build.rs logic.
# extra_args = []
# extra_deps = []
# fs = import('fs')
# if fs.is_dir('meson')
# subdir('meson')
# endif
return [
build.assign(build.array([]), _extra_args_varname()),
build.assign(build.array([]), _extra_deps_varname()),
build.assign(build.function('import', [build.string('fs')]), 'fs'),
build.if_(build.method('is_dir', build.identifier('fs'), [build.string('meson')]),
build.block([build.function('subdir', [build.string('meson')])]))
]
def _create_lib(cargo: Manifest, build: builder.Builder, crate_type: manifest.CRATE_TYPE) -> T.List[mparser.BaseNode]:
dependencies: T.List[mparser.BaseNode] = []
dependency_map: T.Dict[mparser.BaseNode, mparser.BaseNode] = {}
@ -570,7 +597,12 @@ def _create_lib(cargo: Manifest, build: builder.Builder, crate_type: manifest.CR
if name != package_name:
dependency_map[build.string(fixup_meson_varname(package_name))] = build.string(name)
rust_args: T.List[mparser.BaseNode] = [build.identifier('features_args')]
rust_args: T.List[mparser.BaseNode] = [
build.identifier('features_args'),
build.identifier(_extra_args_varname())
]
dependencies.append(build.identifier(_extra_deps_varname()))
posargs: T.List[mparser.BaseNode] = [
build.string(fixup_meson_varname(cargo.package.name)),
@ -660,6 +692,7 @@ def interpret(subp_name: str, subdir: str, env: Environment) -> T.Tuple[mparser.
ast += [build.assign(build.function('import', [build.string('rust')]), 'rust')]
ast += _create_features(cargo, build)
ast += _create_dependencies(cargo, build)
ast += _create_meson_subdir(cargo, build)
# Libs are always auto-discovered and there's no other way to handle them,
# which is unfortunate for reproducability

@ -2,6 +2,7 @@ extern "C" {
fn extra_func() -> i32;
}
#[cfg(feature = "foo")]
#[no_mangle]
pub extern "C" fn rust_func() -> i32 {
let v: i32;

Loading…
Cancel
Save