Skip to content

rust: set OUT_DIR and other environment variables with cargo wraps #13752

@bobbens

Description

@bobbens

Describe the bug
Meson can wrap Rust cargo files, however, it does not provide the same environment variables that cargo does. In particular, for my case (compiling sdl2), OUT_DIR is not provided and the build.rs which uses OUT_DIR fails to compile.

Although this is related to #10030 , it is a bit different as it focuses on cargo wraps which should be able compile like with cargo itself, and thus the environment variables shouldn't necessarily be set in the main meson.build. If #10030 is merged, whatever solution is used there at the end could be used when generating the meson.build from the Cargo.toml.

To Reproduce
Either use the small provided project cargo-fail.tar.gz or using the following file as subprojects/sdl2-rs.wrap, it will fail to company when used as dependency('sdl2-0.37-rs'):

[wrap-file]
method = cargo
directory = sdl2-0.37.0
source_url = https://crates.io/api/v1/crates/sdl2/0.37.0/download
source_filename = sdl2-0.37.0.tar.gz
source_hash = 3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380
[provide]
dependency_names = sdl2-0.37-rs

The meson.build would be:

project('cargo-fail', 'rust', version : '0.1' )
exe = executable('cargo-fail', 'main.rs', dependencies: dependency('sdl2-0.37-rs') )

and main.rs would be:

fn main () {}

Expected behavior
It should compile like it does when compiled with OUT_DIR='..' meson compile. By default it will error out with:

$ meson compile
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/bin/ninja
[4/6] Compiling Rust source ../subprojects/sdl2-sys-0.37.0/src/lib.rs
FAILED: subprojects/sdl2-sys-0.37.0/libsdl2_sys.rlib
rustc -C linker=cc --color=always -C debug-assertions=yes -C overflow-checks=no --crate-type rlib --edition=2018 -g --crate-name sdl2_sys --emit dep-info=subprojects/sdl2-sys-0.37.0/sdl2_sys.d --emit link=subprojects/sdl2-sys-0.37.0/libsdl2_sys.rlib --out-dir subprojects/sdl2-sys-0.37.0/libsdl2_sys.rlib.p -C metadata=a4e236e@@sdl2_sys@sta --cfg 'feature="default"' --extern libc=subprojects/libc-0.2.155/liblibc.rlib -Lsubprojects/libc-0.2.155 ../subprojects/sdl2-sys-0.37.0/src/lib.rs
error: environment variable `OUT_DIR` not defined at compile time
  --> ../subprojects/sdl2-sys-0.37.0/src/lib.rs:14:18
   |
14 | include!(concat!(env!("OUT_DIR"), "/sdl_bindings.rs"));
   |                  ^^^^^^^^^^^^^^^
   |
   = help: Cargo sets build script variables at run time. Use `std::env::var("OUT_DIR")` instead
   = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

ninja: build stopped: subcommand failed.

system parameters

  • native build
  • linux
  • Python 3.12.6
  • meson 1.5.2
  • ninja 1.12.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions