| 1 |
--- |
| 2 |
title: Rust |
| 3 |
short-description: Working with Rust in Meson |
| 4 |
... |
| 5 |
|
| 6 |
# Using Rust with Meson |
| 7 |
|
| 8 |
## Avoid using `extern crate` |
| 9 |
|
| 10 |
Meson can't track dependency information for crates linked by rustc as |
| 11 |
a result of `extern crate` statements in Rust source code. If your |
| 12 |
crate dependencies are properly expressed in Meson, there should be no |
| 13 |
need for `extern crate` statements in your Rust code, as long as you use the |
| 14 |
Rust 2018 edition or later. This means adding `rust_std=2018` (or later) to the |
| 15 |
`project(default_options)` argument. |
| 16 |
|
| 17 |
An example of the problems with `extern crate` is that if you delete a |
| 18 |
crate from a Meson build file, other crates that depend on that crate |
| 19 |
using `extern crate` might continue linking with the leftover rlib of |
| 20 |
the deleted crate rather than failing to build, until the build |
| 21 |
directory is cleaned. |
| 22 |
|
| 23 |
This limitation could be resolved in future with rustc improvements, |
| 24 |
for example if the [`-Z |
| 25 |
binary-dep-depinfo`](https://github.com/rust-lang/rust/issues/63012) |
| 26 |
feature is stabilized. |
| 27 |
|
| 28 |
## Mixing Rust and non-Rust sources |
| 29 |
|
| 30 |
Meson currently does not support creating a single target with Rust and non Rust |
| 31 |
sources mixed together, therefore one must compile multiple libraries and link |
| 32 |
them. |
| 33 |
|
| 34 |
```meson |
| 35 |
rust_lib = static_library( |
| 36 |
'rust_lib', |
| 37 |
sources : 'lib.rs', |
| 38 |
... |
| 39 |
) |
| 40 |
|
| 41 |
c_lib = static_library( |
| 42 |
'c_lib', |
| 43 |
sources : 'lib.c', |
| 44 |
link_with : rust_lib, |
| 45 |
) |
| 46 |
``` |
| 47 |
This is an implementation detail of Meson, and is subject to change in the future. |
| 48 |
|
| 49 |
## Mixing Generated and Static sources |
| 50 |
|
| 51 |
*Note* This feature was added in 0.62 |
| 52 |
|
| 53 |
You can use a [[@structured_src]] for this. Structured sources are a dictionary |
| 54 |
mapping a string of the directory, to a source or list of sources. |
| 55 |
When using a structured source all inputs *must* be listed, as Meson may copy |
| 56 |
the sources from the source tree to the build tree. |
| 57 |
|
| 58 |
Structured inputs are generally not needed when not using generated sources. |
| 59 |
|
| 60 |
As an implementation detail, Meson will attempt to determine if it needs to copy |
| 61 |
files at configure time and will skip copying if it can. Copying is done at |
| 62 |
build time (when necessary), to avoid reconfiguring when sources change. |
| 63 |
|
| 64 |
```meson |
| 65 |
executable( |
| 66 |
'rust_exe', |
| 67 |
[[#structured_sources]]( |
| 68 |
'main.rs', |
| 69 |
{ |
| 70 |
'foo' : ['bar.rs', 'foo/lib.rs', generated_rs], |
| 71 |
'foo/bar' : [...], |
| 72 |
'other' : [...], |
| 73 |
} |
| 74 |
) |
| 75 |
) |
| 76 |
``` |
| 77 |
|
| 78 |
## Use with rust-analyzer |
| 79 |
|
| 80 |
*Since 0.64.0.* |
| 81 |
|
| 82 |
Meson will generate a `rust-project.json` file in the root of the build |
| 83 |
directory if there are any rust targets in the project. Most IDEs will need to |
| 84 |
be configured to use the file as it's not in the source root (Meson does not |
| 85 |
write files into the source directory). [See the upstream |
| 86 |
docs](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects) for |
| 87 |
more information on how to configure that. |
| 88 |
|
| 89 |
## Linking with standard libraries |
| 90 |
|
| 91 |
Meson will link the Rust standard libraries (e.g. libstd) statically, unless the |
| 92 |
target is a proc macro or dylib, or it depends on a dylib, in which case [`-C |
| 93 |
prefer-dynamic`](https://doc.rust-lang.org/rustc/codegen-options/index.html#prefer-dynamic) |
| 94 |
will be passed to the Rust compiler, and the standard libraries will be |
| 95 |
dynamically linked. |