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

Add cfg for crate_type? #45182

Closed
joshlf opened this issue Oct 10, 2017 · 9 comments
Closed

Add cfg for crate_type? #45182

joshlf opened this issue Oct 10, 2017 · 9 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@joshlf
Copy link
Contributor

joshlf commented Oct 10, 2017

I have some code that needs to be conditionally compiled only when the artifact that's being produced is a dynamic library, and the target platform is Mac (here's the code). Right now, I'm accomplishing that by providing a dylib feature that the crate's user is responsible for enabling if they're compiling a dynamic library.

This is ugly from a UX perspective, and introduces the possibility of bugs if the user fails to enable the feature when they should. It'd be better if there were a cfg setting that indicated the value of the crate_type so I could do this automatically without user help.

@hanna-kruppe
Copy link
Contributor

It is possible to set multiple crate types at once and get artifacts for all of them in one compilation session. That makes it kind of difficult to provide a single crate_type="foo" cfg, though perhaps there could still be a set of flags like cfg_crate_type_cdylib, cfg_crate_staticlib, etc.?

@joshlf
Copy link
Contributor Author

joshlf commented Oct 10, 2017

Yeah, I think that'd work. Something like #[cfg(crate_type = "cdylib")], #[cfg(crate_type = "staticlib")], etc (where multiple could be true at once similar to #[cfg(feature = "...")]).

In my particular use case, I'd probably need to ensure that only a single artifact was generated at a time by doing something like:

#[cfg(all(crate_type = "cdylib", any(crate_type = "staticlib", ...)))]
compile_fail!(...);

But yeah that'd work for me.

@XAMPPRocky XAMPPRocky added A-build C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2018
@glandium
Copy link
Contributor

glandium commented Mar 1, 2018

where multiple could be true at once

Interestingly, that would not be useful in some use cases. Specifically, I was looking at building a crate as both a cdylib and staticlib today, and needed both to export a function under a different name... I tried #[cfg(crate_type = "cdylib")] on function with one name and #[cfg(crate_type = "staticlib")] on another, but that (annoyingly, I must say) silently exposed none of them. This use case would not be fulfillable if multiple could be true at once.

@retep998
Copy link
Member

retep998 commented Mar 2, 2018

Why don't we just allow a cargo package to have multiple cdylib and staticlib crates distinct from the primary library crate? Then you could just put all the common stuff in the library crate and the cdylib/staticlib crates would have the stuff specific to them, so you wouldn't have to cfg based on how the crate is being built.

@joshlf
Copy link
Contributor Author

joshlf commented Mar 2, 2018 via email

@retep998
Copy link
Member

retep998 commented Mar 2, 2018

Ah, but you could have a shared module. Two separate crates can both include the same module, and that could define the common private functionality.

@glandium
Copy link
Contributor

glandium commented Mar 2, 2018

How can two separate crates share a module?

@glandium
Copy link
Contributor

glandium commented Mar 2, 2018

Ah! #[path].

@jonas-schievink jonas-schievink added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) and removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-build labels Apr 21, 2019
@Mark-Simulacrum
Copy link
Member

I'm going to loosely say this is probably RFC-worthy territory and close this issue (it's definitely not a rustbuild bug).

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants