Skip to content

Commit

Permalink
cargo: Call into meson subdir if it exists
Browse files Browse the repository at this point in the history
This allows projects to manually add extra rust args and deps. This is
intended to replace build.rs logic.
  • Loading branch information
xclaesse committed Nov 22, 2023
1 parent 711f2d8 commit 3d02f9e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/markdown/Wrap-dependency-system-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ project(...,
)
```

In addition, if `meson/meson.build` file exists, Meson will call `subdir('meson')`
where the project can add manual logic that would usually be part of `build.rs`.
Some naming conventions needs to be respected:
- `extra_args` variable is pre-defined and can be used to add any Rust arguments.
This is typically used as `extra_args += ['--cfg', 'foo']`.
- `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
Expand Down
35 changes: 34 additions & 1 deletion mesonbuild/cargo/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = {}
Expand All @@ -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)),
Expand Down Expand Up @@ -662,6 +694,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extra_args += ['--cfg', 'feature="foo"']
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3d02f9e

Please sign in to comment.