Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated meson.build from Cargo crate references undefined variable #13855

Open
2xsaiko opened this issue Nov 1, 2024 · 5 comments
Open

Generated meson.build from Cargo crate references undefined variable #13855

2xsaiko opened this issue Nov 1, 2024 · 5 comments
Labels
bug dependencies:cargo Issues related to using cargo subprojects

Comments

@2xsaiko
Copy link

2xsaiko commented Nov 1, 2024

Describe the bug
Meson's cargo importer generates a meson.build that is referencing an undefined variable for the crate libflate_lz77, which is a transitive dependency of the crate I'm trying to import.

Might be related to these two crates depending on each other, or dev-dependencies: libflate depends on libflate_lz77 and libflate_lz77 has libflate in its dev-dependencies.

Executing subproject matfile:libflate-2-rs:libflate_lz77-2-rs method cargo

libflate_lz77-2-rs| Generated Meson AST: /Users/saiko/Src/test/build/subprojects/libflate_lz77-2.1.0/meson.build
libflate_lz77-2-rs| Project name: libflate_lz77
libflate_lz77-2-rs| Project version: 2.1.0
libflate_lz77-2-rs| Rust compiler for the host machine: rustc -C linker=cc (rustc 1.82.0)
libflate_lz77-2-rs| Rust linker for the host machine: rustc -C linker=cc ld.lld 19.1.2

subprojects/libflate_lz77-2.1.0/meson.build:25:-1: ERROR: Unknown variable "libflate_options".

The first couple lines from the meson.build in question (note libflate_options not being defined unlike the others):

project(
  'libflate_lz77',
  'rust',
  version : '2.1.0',
  meson_version : '>= 1.6.0',
  default_options : ['rust_std=2021'],
  license : 'MIT'
)
rust = import('rust')
features = {}
core2_options = {}
hashbrown_options = {}
rle_decode_fast_options = {}
required_deps = {}
if get_option('feature-default')
  required_deps += {'core2' : true, 'libflate' : true}
  features += {'std' : true, 'default' : true}
  core2_options += {'feature-std' : true}
  libflate_options += {'feature-std' : true}
endif
if get_option('feature-std')
  required_deps += {'core2' : true, 'libflate' : true}
  features += {'std' : true}
  core2_options += {'feature-std' : true}
  libflate_options += {'feature-std' : true}
endif

To Reproduce
meson.build:

project(
    'test',
    'rust',
    default_options: {'rust_std': '2021'},
    meson_version: '1.6.0',
)

matfile_proj = subproject('matfile')

matfile.wrap:

[wrap-file]
method = cargo
directory = matfile-0.5.0

source_url = https://crates.io/api/v1/crates/matfile/0.5.0/download
source_filename = matfile-0.5.0.tar.gz
source_hash = b22724209845c600f095bca5be902d636b4fc4f0b27fe5bb5d8314b5c117d0d6

[provide]
dependency_names = matfile

Expected behavior
The generated meson.build for libflate_lz77 does not reference an undefined variable.

system parameters

  • Is this a cross build or just a plain native build (for the same computer)? Native build
  • what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.) Gentoo Linux
  • what Python version are you using? 3.12.7
  • what meson --version 1.6.0 (with patch from 5769074)
  • what ninja --version if it's a Ninja build? 1.12.1
@dcbaker dcbaker added bug dependencies:cargo Issues related to using cargo subprojects labels Nov 1, 2024
@peterdelevoryas
Copy link

peterdelevoryas commented Dec 1, 2024

I also have this issue, with a different project and dependency here. It's kind of blocking me from using Meson to build Rust, because there doesn't seem to be an option to disable default features, like --no-default-features would? Based on this comment.

@eli-schwartz
Copy link
Member

Poking around a bit in the libflate example, I notice that libflate depends on libflate_lz77.

libflate itself has these features:

[features]
default = ["std"]
std = ["libflate_lz77/std", "core2/std"]

Meanwhile, libflate_lz77 has these features:

[features]
default = ["std"]
std = ["core2/std", "libflate/std"]

It looks like... each one is trying to enforce on the other, to build with a consistent set of features? And lz77 wants to force the feature active for its own parent crate, not a dependency. Which is apparently legal. Is this only the case because libflate via path ../ is a dev-dependency, or is this generally legal?

I guess meson needs to handle arbitrary features getting set on things that aren't in dependencies.

@peterdelevoryas
Copy link

peterdelevoryas commented Dec 1, 2024

Yeah I suspect libflate_lz77 has a dev-dependency on libflate because it's using it in doc-comment examples, which actually get compiled for some tests. I'm also surprised that works, because you would expect circular dependencies, but perhaps in dev-dependency mode it resolves without circular references.

https://github.com/sile/libflate/blob/6676d36d381eec60f376337e528d658b16c87c7a/libflate_lz77/src/lib.rs#L117-L118

I think the issue is this code in meson is assuming that a dependency should be added whenever a crate tries to enable a feature on a dependency crate using self_feature = ["dep/dep_feature"] syntax:

https://github.com/mesonbuild/meson/blob/master/mesonbuild/cargo/interpreter.py#L581-L582

Reference: https://doc.rust-lang.org/cargo/reference/features.html#dependency-features

I haven't quite gotten the fix right though, so, maybe I'm mistaken somehow.

peterdelevoryas added a commit to peterdelevoryas/meson that referenced this issue Dec 1, 2024
There are a few different build modes in Cargo that have
separate sets of dependencies. The features section applies
to dependencies in any mode. Sometimes, features are specified
for dependencies that aren't actually included in normal builds:
those dependencies should not be added to the build, and the
feature toggle can be ignored. [1]

[1]: https://doc.rust-lang.org/cargo/reference/features.html#dependency-features

Resolves: mesonbuild#13855
Signed-off-by: Peter Delevoryas <peter@pjd.dev>
peterdelevoryas added a commit to peterdelevoryas/meson that referenced this issue Dec 1, 2024
There are a few different build modes in Cargo that have
separate sets of dependencies. The features section applies
to dependencies in any mode. Sometimes, features are specified
for dependencies that aren't actually included in normal builds:
those dependencies should not be added to the build, and the
feature toggle can be ignored. [1]

[1]: https://doc.rust-lang.org/cargo/reference/features.html#dependency-features

Resolves: mesonbuild#13855
Signed-off-by: Peter Delevoryas <peter@pjd.dev>
@xclaesse
Copy link
Member

xclaesse commented Dec 1, 2024

First step at fixing this: #13976.

Configure passes but it has a bunch of build time errors, like:

FAILED: subprojects/zerocopy-0.7.32/libzerocopy.rlib 
rustc -C linker=cc --color=always -C debug-assertions=yes -C overflow-checks=no --crate-type rlib --edition=2018 -g --crate-name zerocopy --emit dep-info=subprojects/zerocopy-0.7.32/zerocopy.d --emit link=subprojects/zerocopy-0.7.32/libzerocopy.rlib --out-dir subprojects/zerocopy-0.7.32/libzerocopy.rlib.p -C metadata=adcab9b@@zerocopy@sta --cfg 'feature="simd"' ../subprojects/zerocopy-0.7.32/src/lib.rs
error: environment variable `CARGO_PKG_VERSION` not defined at compile time
    --> ../subprojects/zerocopy-0.7.32/src/lib.rs:1356:58
     |
1356 |     doc = concat!("[derive]: https://docs.rs/zerocopy/", env!("CARGO_PKG_VERSION"), "/zerocopy/derive.FromZeroes.html"),
     |                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_PKG_VERSION")` instead
     = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

That requires using the rustc --set-env CLI. I think it got merged upstream but I'm not sure it's stable yet.

@xclaesse
Copy link
Member

xclaesse commented Dec 1, 2024

Seems rustc decided to close the issue.... someone will have to push them harder... rust-lang/rust#119926

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug dependencies:cargo Issues related to using cargo subprojects
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants