Skip to content

Commit

Permalink
Auto merge of #14499 - felixmoebius:rustc-crate-type-parsing, r=weiha…
Browse files Browse the repository at this point in the history
…nglo

Fix parsing of comma separated values in --crate-type flag

### What does this PR try to resolve?

According to the documentation the `--crate-type` flag accepts a comma separated list of crate types. However, these are never actually split into individual items and passed verbatim to rustc.

Since cargo fails to associate cases such as 'staticlib,cdylib' to a specific crate type internally, it has to invoke rustc to determine the output file types for this unknown crate type, which returns only the first file type of the first crate type in the list. Consequently cargo will be looking only for a single '.a' artifact on Linux to be copied to the target directory.

Fix this by splitting the list of provided crate types into individual items before further processing them.

Fixes #14498

### How should we test and review this PR?

Updated corresponding test cases

### Additional information
  • Loading branch information
bors committed Sep 9, 2024
2 parents b958d79 + fb672fa commit 0b390cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
ops::print(&ws, &compile_opts, opt_value)?;
return Ok(());
}
let crate_types = values(args, CRATE_TYPE_ARG_NAME);

let crate_types = args
.get_many::<String>(CRATE_TYPE_ARG_NAME)
.into_iter()
.flatten()
.flat_map(|s| s.split(','))
.filter(|s| !s.is_empty())
.map(String::from)
.collect::<Vec<String>>();

compile_opts.target_rustc_crate_types = if crate_types.is_empty() {
None
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn build_with_crate_types_for_foo() {
p.cargo("rustc -v --crate-type lib,cdylib")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib,cdylib [..]`
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib --crate-type cdylib [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -302,7 +302,7 @@ fn build_with_crate_types_to_example() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib [..]`
[RUNNING] `rustc --crate-name ex --edition=2015 examples/ex.rs [..]--crate-type lib,cdylib [..]`
[RUNNING] `rustc --crate-name ex --edition=2015 examples/ex.rs [..]--crate-type lib --crate-type cdylib [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down Expand Up @@ -338,7 +338,7 @@ fn build_with_crate_types_to_one_of_multi_examples() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc --crate-name foo --edition=2015 src/lib.rs [..]--crate-type lib [..]`
[RUNNING] `rustc --crate-name ex1 --edition=2015 examples/ex1.rs [..]--crate-type lib,cdylib [..]`
[RUNNING] `rustc --crate-name ex1 --edition=2015 examples/ex1.rs [..]--crate-type lib --crate-type cdylib [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
Expand Down

0 comments on commit 0b390cd

Please sign in to comment.