mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.3k stars 1.52k forks source link

meson does not run build scripts for "Cargo wraps" (Rust crates) #13144

Closed decathorpe closed 3 weeks ago

decathorpe commented 3 weeks ago

Describe the bug

Rust crates can use a build script (by convention, build.rs in the project's root directory, but this can be configured in Cargo.toml) to look at the build environment and forward compiler flags based on it - usually for conditional compilation (like -DHAVE_LIBFOO_1_1_0 for C / C++ projects).

Currently, meson does not run these build scripts, so this "configure" step is missing when building wrapped Rust crates as dependencies.

This has started to impact mesa builds in Fedora since we updated the proc-macro2 crate to the latest version:

The proc-macro2 crate introduced support for new Rust syntax that will be stabilized in Rust 1.79. The build script disables this functionality when building with Rust < 1.79. But since meson doesn't run the build script, the crate fails to compile with Rust 1.77: https://github.com/dtolnay/proc-macro2/blob/master/build.rs

To Reproduce

Try building any Rust crate dependency with a build script. The build script is not run, cfg flags are not passed as required.

Expected behavior

Meson could compile and run the build script, collect output (lines in the form of cargo:rustc-cfg=foo), and pass --cfg=foo when building the crate accordingly.

Without this, building Rust crates with meson will either fail or produce invalid results for any crate that uses a build script.

system parameters

eli-schwartz commented 3 weeks ago

Rust crates can use a build script (by convention, build.rs in the project's root directory, but this can be configured in Cargo.toml) to look at the build environment and forward compiler flags based on it - usually for conditional compilation (like -DHAVE_LIBFOO_1_1_0 for C / C++ projects).

Instead of compiling a rust program that echoes compiler flags to stdout after running the rust version of if rustc.version().version_compare('>=1.79.0'), just do this in meson.build

Meson can't run build.rs scripts because:

The crate support already allows you to drop in your own overlaid meson.build file for injecting custom logic such as adding project flags. This is the expected and idiomatic way to handle more complex crates in meson. Especially for a very simple build.rs, the meson port should be extremely easy to do.

decathorpe commented 3 weeks ago

Ok, so you consider this a bug in how mesa uses meson to build proc-macro2, because they didn't rebuild the build.rs logic in an additional meson.build file?

dcbaker commented 3 weeks ago

@decathorpe I'm both a Mesa and Meson developer, and I've spent some time thinking about this problem. I started working on a crate to make this simpler, but it still needs work and I haven't had time.

But yeah, if we're not replicating the logic in meson.build we're doing it wrong.

decathorpe commented 3 weeks ago

Thanks for confirming. Just wanted to make sure, since people started complaining to me that I broke mesa when I updated proc-macro2 crate in Fedora a few days ago :)

dcbaker commented 3 weeks ago

@decathorpe feel free to ping me on stuff like this around mesa, I'm dcbaker most places, but I'm dbaker at the fdo gitlab

eli-schwartz commented 3 weeks ago

Thanks for confirming. Just wanted to make sure, since people started complaining to me that I broke mesa when I updated proc-macro2 crate in Fedora a few days ago :)

If I understand correctly you updated the crate in fedora, and mesa uses the crate but not via cargo, so the fedora tooling also updated mesa's "wrap file" usage of proc-macro2

here: https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/subprojects/proc-macro2.wrap

but not here: https://gitlab.freedesktop.org/mesa/mesa/-/tree/main/subprojects/packagefiles/proc-macro2

and the latter (a meson.build file) requires changes to adapt to the version bump.

decathorpe commented 3 weeks ago

The mesa package replaces the wrapped / vendored Rust crate sources with versions that are packaged for Fedora. The mechanism for doing that is a bit brittle (as we see here), but we didn't want to build mesa from different sources than other Rust applications in Fedora.

dcbaker commented 3 weeks ago

If you're going to do that, you're going to need to update the meson definitions in Mesa as well.