#2 handling per-target dependencies
Opened 5 years ago by ignatenkobrain. Modified 5 months ago

For example, we can have "target": "cfg(windows)" which we obviously should exclude from dependency list, but how we determine what we should include and what not?


Probably we should enforce --target=%{_target_platform} in cargo execution, so we will know target in advance. Looks like it's also possible to set it from config:

[build]
target = "thumbv6m-none-eabi"

Good example for testing would be thread-id:

[target.'cfg(unix)'.dependencies]
libc = "0.2.6"

[target.'cfg(windows)'.dependencies]
kernel32-sys = "0.2.1"

Another interesting example is in libgit2-sys:

[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]
openssl-sys = { version = "0.9", optional = true }

Relevant code in Cargo:

impl Platform {
    pub fn matches(&self, name: &str, cfg: Option<&[Cfg]>) -> bool {
        match *self {
            Platform::Name(ref p) => p == name,
            Platform::Cfg(ref p) => {
                match cfg {
                    Some(cfg) => p.matches(cfg),
                    None => false,
                }
            }
        }
    }
}

impl FromStr for Platform {
    type Err = Box<CargoError>;

    fn from_str(s: &str) -> CargoResult<Platform> {
        if s.starts_with("cfg(") && s.ends_with(")") {
            let s = &s[4..s.len()-1];
            s.parse().map(Platform::Cfg).chain_error(|| {
                human(format!("failed to parse `{}` as a cfg expression", s))
            })
        } else {
            Ok(Platform::Name(s.to_string()))
        }
    }
}

AGREED: we will just drop all non-fedora target dependencies manually from Cargo.toml (ignatenkobrain, 21:22:31)

Any progress on this? Or do I have to manually patch all offending Cargo.toml? I got here while trying to package 'bytes' which brought in 'iovec' which has this:

[target.'cfg(unix)'.dependencies]
libc = "0.2.35"

[target.'cfg(windows)'.dependencies]
winapi = "0.2"

and this is the error that occurs:

$ rpmbuild -bb iovec.spec
+ /usr/bin/cargo build --release -j8
error: no matching package named winapi found
location searched: registry https://github.com/rust-lang/crates.io-index
required by package iovec v0.1.0 (file:///usr/src/rpmbuild/BUILD/iovec-0.1.0)
error: Bad exit status from /var/tmp/rpm-tmp.KBhpS8 (%build)

at this point, it's manual =(

Metadata Update from @ignatenkobrain:
- Issue assigned to zbyszek
- Issue set to the milestone: 6

3 years ago

Upstream issue: https://github.com/rust-lang/cargo/issues/5896
I think it makes more sense to work with upstream on this.

Metadata Update from @ignatenkobrain:
- Issue set to the milestone: 7 (was: 6)

3 years ago

Metadata Update from @ignatenkobrain:
- Issue set to the milestone: None (was: 7)

3 years ago

BTW: There's a few Rust crates for parsing cfg expressions now, and a while ago I wrote a tool that actually works around the broken dependency generator by rewriting Cargo.toml files - removing non-linux dependencies automatically, based on this recursive evaluation algorithm: https://github.com/ironthree/cargoman/blob/master/src/eval.rs

Login to comment on this ticket.

Metadata
Related Pull Requests
  • #183 Last updated a month ago