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 detection of multiple targets and select one #5

Closed
volks73 opened this issue Dec 1, 2020 · 2 comments
Closed

Add detection of multiple targets and select one #5

volks73 opened this issue Dec 1, 2020 · 2 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@volks73
Copy link
Owner

volks73 commented Dec 1, 2020

An error occurs if there is more than one Cargo target defined in a package's manifest (Cargo.toml), so the default is to add the --lib flag to select the library Cargo target. If there is only one Cargo target, and it is a library, then the --lib flag is functionally a "no-op". However, if there is only one Cargo target and it is a binary, or multiple binary Cargo targets are defined with no library target, then another error occurs: "no library targets found in package <PACKAGE_NAME>.

The CargoRustcPrintCfg builder allows for customization of the command to select the appropriate Cargo target to avoid this error, but if this crate is used within another crate, it may not be possible to customize the command for the two cases where the --lib is a problem. So, this crate needs to be smarter about selecting the Cargo target and avoid blindly using the --lib flag.

I am currently thinking of using the cargo-metadata crate to obtain a list of Cargo targets and determine usage of the --lib flag or --bin flags based on the Cargo targets listed.

@volks73 volks73 added bug Something isn't working enhancement New feature or request labels Dec 1, 2020
@volks73 volks73 self-assigned this Dec 1, 2020
@volks73
Copy link
Owner Author

volks73 commented Dec 21, 2020

It would be a lot easier if Cargo just printed the compiler configuration with some kind of cargo rustc --cfg -like command and flag. The issue for determining the Cargo target is because Cargo has a gate check that passing arguments to the compiler, i.e. using the -- argument, must have a single Cargo target selected.

Thus, I created the rust-lang/cargo#8923 in the Cargo project. This would greatly simplify the implementation of this crate and make it similar to the cargo-metadata crate. To this end, I started digging through the Cargo source code to implement a cargo rustc --cfg command. This lead to PR rust-lang/cargo#9002. If and when this PR is merged, this issue will be easily addressed but the crate will radically change in implementation. It will also be a while before the --cfg feature is stabilized...if it is accepted and merged. So, I am holding off on applying too much attention to this issue and crate until the fate of the PR for Cargo is more certain.

@volks73
Copy link
Owner Author

volks73 commented Mar 1, 2021

The rust-lang/cargo#8923 issue and PR rust-lang/cargo#9002 have been resolved and merged, respectively. This greatly simplifies the implementation of this crate, but it currently requires Cargo nightly to be installed since it will take some time for the changes to Cargo to propagate to the stable channel. The most recent merge and changes (62c90ea) account for multiple targets and the new nightly features. The API has changed significantly, but it appears to be working. There are a couple of doc tests that need to be fixed, too, but this specific issue and the previously discussed sources of headaches have been resolved.

@volks73 volks73 closed this as completed Mar 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant