Skip to content

Commit cfdf00e

Browse files
committed
Auto merge of #7257 - RobMor:no-spaces-between-flags, r=alexcrichton
Fix #7217: Add support for rustc-flags without spaces between flags and values Hi, I believe this pull request contains a fix for issue #7217. This is my first pull request to any open source project, much less any Rust project. I'm not super familiar with Rust at the moment, so let me know if I should change anything. Also any help/advice you can give me about this PR would be much appreciated! I do have some specific questions: - Is the test I added worthy of being its own test? I basically copied the one above it and remove the spaces between the flags and the values. Should I have added on to the test above it instead? - Would it be better if I directly unit-tested this function in some other test file? - Is the `ureachable!` macro good style?
2 parents 60b7bf0 + a82de17 commit cfdf00e

File tree

2 files changed

+84
-16
lines changed

2 files changed

+84
-16
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -514,29 +514,38 @@ impl BuildOutput {
514514
.split(|c: char| c.is_whitespace())
515515
.filter(|w| w.chars().any(|c| !c.is_whitespace()));
516516
let (mut library_paths, mut library_links) = (Vec::new(), Vec::new());
517+
517518
while let Some(flag) = flags_iter.next() {
518-
if flag != "-l" && flag != "-L" {
519+
if flag.starts_with("-l") || flag.starts_with("-L") {
520+
// Check if this flag has no space before the value as is
521+
// common with tools like pkg-config
522+
// e.g. -L/some/dir/local/lib or -licui18n
523+
let (flag, mut value) = flag.split_at(2);
524+
if value.len() == 0 {
525+
value = match flags_iter.next() {
526+
Some(v) => v,
527+
None => failure::bail! {
528+
"Flag in rustc-flags has no value in {}: {}",
529+
whence,
530+
value
531+
},
532+
}
533+
}
534+
535+
match flag {
536+
"-l" => library_links.push(value.to_string()),
537+
"-L" => library_paths.push(PathBuf::from(value)),
538+
539+
// This was already checked above
540+
_ => unreachable!(),
541+
};
542+
} else {
519543
failure::bail!(
520544
"Only `-l` and `-L` flags are allowed in {}: `{}`",
521545
whence,
522546
value
523547
)
524548
}
525-
let value = match flags_iter.next() {
526-
Some(v) => v,
527-
None => failure::bail!(
528-
"Flag in rustc-flags has no value in {}: `{}`",
529-
whence,
530-
value
531-
),
532-
};
533-
match flag {
534-
"-l" => library_links.push(value.to_string()),
535-
"-L" => library_paths.push(PathBuf::from(value)),
536-
537-
// was already checked above
538-
_ => failure::bail!("only -l and -L flags are allowed"),
539-
};
540549
}
541550
Ok((library_paths, library_links))
542551
}

tests/testsuite/build_script.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,65 @@ fn custom_build_script_rustc_flags() {
254254
.run();
255255
}
256256

257+
#[cargo_test]
258+
fn custom_build_script_rustc_flags_no_space() {
259+
let p = project()
260+
.file(
261+
"Cargo.toml",
262+
r#"
263+
[project]
264+
265+
name = "bar"
266+
version = "0.5.0"
267+
authors = ["wycats@example.com"]
268+
269+
[dependencies.foo]
270+
path = "foo"
271+
"#,
272+
)
273+
.file("src/main.rs", "fn main() {}")
274+
.file(
275+
"foo/Cargo.toml",
276+
r#"
277+
[project]
278+
279+
name = "foo"
280+
version = "0.5.0"
281+
authors = ["wycats@example.com"]
282+
build = "build.rs"
283+
"#,
284+
)
285+
.file("foo/src/lib.rs", "")
286+
.file(
287+
"foo/build.rs",
288+
r#"
289+
fn main() {
290+
println!("cargo:rustc-flags=-lnonexistinglib -L/dummy/path1 -L/dummy/path2");
291+
}
292+
"#,
293+
)
294+
.build();
295+
296+
p.cargo("build --verbose")
297+
.with_stderr(
298+
"\
299+
[COMPILING] foo [..]
300+
[RUNNING] `rustc --crate-name build_script_build foo/build.rs [..]
301+
[RUNNING] `[..]build-script-build`
302+
[RUNNING] `rustc --crate-name foo foo/src/lib.rs [..]\
303+
-L dependency=[CWD]/target/debug/deps \
304+
-L /dummy/path1 -L /dummy/path2 -l nonexistinglib`
305+
[COMPILING] bar [..]
306+
[RUNNING] `rustc --crate-name bar src/main.rs [..]\
307+
-L dependency=[CWD]/target/debug/deps \
308+
--extern foo=[..]libfoo-[..] \
309+
-L /dummy/path1 -L /dummy/path2`
310+
[FINISHED] dev [..]
311+
",
312+
)
313+
.run();
314+
}
315+
257316
#[cargo_test]
258317
fn links_no_build_cmd() {
259318
let p = project()

0 commit comments

Comments
 (0)