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

Allow limiting required crate-types of dependencies #11232

Open
mystor opened this issue Oct 13, 2022 · 4 comments
Open

Allow limiting required crate-types of dependencies #11232

mystor opened this issue Oct 13, 2022 · 4 comments
Labels
A-crate-types Area: crate-type declaration (lib, staticlib, dylib, cdylib, etc.) C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@mystor
Copy link

mystor commented Oct 13, 2022

Problem

When depending on a crate which declares it supports multiple crate types in its [lib] section, cargo will always build all of the specified crate types when building the dependency. This means that a crate which specifies something like the following in its Cargo.toml will always build a cdylib and staticlib when built as a dependency, even if we're only going to be using the normal lib dependency.

[lib]
crate-type = ["cdylib", "staticlib", "lib"]

This leads to wasted effort building unnecessary artifacts for dependencies, and can also lead to issues when the configuration used to build the lib would cause building the cdylib or staticlib to fail to link for whatever reason (e.g. due to linker flags).

Proposed Solution

It would be nice if there was a way to specify which crate types you are depending on from your dependencies when declaring a dependency in Cargo.toml, similar to how you can specify required feature flags.

I imagine this acting in a similar way to features, where when a dependency is declared without a crate-type specification, it implicitly requires the crate types from the dependency's Cargo.toml, and a dependency with crate-type only requires the specified crate types.

This might look something like the following:

[dependencies.mycrate]
version = "1.0.0"
crate-type = ["lib"]

Notes

There may be some overlap between this and other work for artifact dependencies (#9096), although I am not familiar enough with the work in that area to know if it already covers this use-case.

@mystor mystor added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Oct 13, 2022
@weihanglo
Copy link
Member

Artifiact dependencies might not help if you just want to limit dependency to only build rlib. With the current implementation, you can only specify bin, cdylib, and staticlib.

If I were you, I may contact the author of my dependency crate. Tell them there is a newly stabilized flag cargo rustc --crate-type, which could build any crate-type on demand. See how hyper uses --crate-type to remove crate-type = […] in its Cargo.toml and speedup their build.

@weihanglo weihanglo added the A-crate-types Area: crate-type declaration (lib, staticlib, dylib, cdylib, etc.) label Oct 13, 2022
@mystor
Copy link
Author

mystor commented Oct 13, 2022

cc @BurntSushi I suppose, as I noticed this when trying to link rure as an rlib (which was added as a crate type in rust-lang/regex#909).

It seems odd to have a crate like that which is only intended to provide a c api not marked as crate-type = ["cdylib", "staticlib", ...], but I suppose it would be an option which would fix my issue. Not sure what other side effects it would have.

@BurntSushi
Copy link
Member

Yeah I'm not sure what to make of this. I don't get many complaints about rure compile time, so this particular problem isn't really on my radar.

moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Oct 14, 2022
…ts, r=#xpcom-reviewers CLOSED TREE

Cargo will attempt to build all targets for dependencies, and there
appears to be no option to turn that functionality off (see
rust-lang/cargo#11232). If we try to build the
`rure` crate as a cdylib during a PGO build, it causes linker issues,
which make the build fail. As this artifact isn't necessary for our
build, we can patch the crate to remove the cdylib and staticlib
crate-type definitions, making the build pass as only the artifact we
need is built.

Differential Revision: https://phabricator.services.mozilla.com/D159332
jamienicol pushed a commit to jamienicol/gecko that referenced this issue Oct 19, 2022
…ts, r=#xpcom-reviewers CLOSED TREE

Cargo will attempt to build all targets for dependencies, and there
appears to be no option to turn that functionality off (see
rust-lang/cargo#11232). If we try to build the
`rure` crate as a cdylib during a PGO build, it causes linker issues,
which make the build fail. As this artifact isn't necessary for our
build, we can patch the crate to remove the cdylib and staticlib
crate-type definitions, making the build pass as only the artifact we
need is built.

Differential Revision: https://phabricator.services.mozilla.com/D159332
@glandium
Copy link
Contributor

To me, the core problem is that if crate A depends on crate B, the only thing that cargo can handle is B being a rlib and using it to build A. If B is a cdylib or a staticlib, cargo won't handle the situation. So really, the only thing cargo can handle is B being a rlib. Now, if you're building A, why does cargo care to build something else than the rlib? would be the issue IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-crate-types Area: crate-type declaration (lib, staticlib, dylib, cdylib, etc.) C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants