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 example, bin configurations #14378

Closed
lure23 opened this issue Aug 9, 2024 · 2 comments
Closed

Add example, bin configurations #14378

lure23 opened this issue Aug 9, 2024 · 2 comments
Labels
A-build-scripts Area: build.rs scripts C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-propose-close Status: A team member has nominated this for closing, pending further input from the team

Comments

@lure23
Copy link

lure23 commented Aug 9, 2024

Problem

I've been looking for a way to automatically enable certain features, when running examples (cargo run --example ...). There seem to be at least three 1 2 3 issues open on this matter, over the years - all are still open.

If it was possible to use #[cfg_attr(example, cfg(all()))] in build.rs, I could add a feature in there and be happy.

Tried it:

warning: unexpected `cfg` condition name: `example`
  --> build.rs:44:16
   |
44 |     #[cfg_attr(example, cfg(all()))]
   |                ^^^^^^^
   |
   = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`

From the error message it seems we have cfg(doc, ...) and cfg(test, ...) but not example and bin.

Is this an oversight or due to historical pedigree? It would feel consistent for me to have (at least) all these four equally covered (perhaps adding benchmark as well).

Proposed Solution

Have these be available reflecting the use of --example, --bin (and --benchmark) parameters of the cargo command:

#[cfg(example, ...)]
#[cfg(bin, ...)]

Notes

There are two work-arounds for this.

  1. Manually provide features

    This is somewhat tedious and personally, I've wrapped cargo run --example ... with a Makefile to keep things short. Would rather have cargo run itself be able to do the right thing, consistently.

  2. Explicitly making targets of each example in Cargo.toml

    Have not tried this. Mainly because I'd like to treat any file in examples/ the same; needing to mention them in Cargo.toml creates two tiers of examples.

@lure23 lure23 added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Aug 9, 2024
@epage epage added the A-build-scripts Area: build.rs scripts label Aug 9, 2024
@epage
Copy link
Contributor

epage commented Aug 9, 2024

build.rs is for the package, not individual build targets, so providing a --cfg for the build target kind wouldn't work. If we changed it so it did, we'd need to rebuild and re-run the build script fr every build target type currently being built. If this were done by default, this would be prohibitively slow.

Related issues

As the use case is to workaround the lack of other features, rather than being a need on its own, I would be inclined that we do not go in this direction.

From the error message it seems we have cfg(doc, ...) and cfg(test, ...) but not example and bin.

We don't even do cfg(test), see #13294. Unsure about cfg(doc).

@epage epage added S-propose-close Status: A team member has nominated this for closing, pending further input from the team and removed S-triage Status: This issue is waiting on initial triage. labels Aug 9, 2024
@lure23
Copy link
Author

lure23 commented Aug 11, 2024

Thank You for the explanation. A holistic view on how the tooling should be is appreciated!

Closing.

@lure23 lure23 closed this as completed Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-propose-close Status: A team member has nominated this for closing, pending further input from the team
Projects
None yet
Development

No branches or pull requests

2 participants