Skip to content

Commit f888b4b

Browse files
tomakaalexcrichton
authored andcommitted
Flags from custom build script are now used
1 parent 34ace11 commit f888b4b

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::collections::HashSet;
22
use std::dynamic_lib::DynamicLibrary;
3-
use std::io::{fs, BufReader, USER_RWX};
4-
use std::io::fs::PathExtensions;
3+
use std::io::{fs, BufferedReader, BufReader, USER_RWX};
4+
use std::io::fs::{File, PathExtensions};
5+
use std::os;
56

67
use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve};
78
use util::{mod, CargoResult, ProcessBuilder, CargoError, human, caused_human};
@@ -512,12 +513,60 @@ fn rustc(package: &Package, target: &Target,
512513
let show_warnings = package.get_package_id() == cx.resolve.root() ||
513514
is_path_source;
514515
let rustc = if show_warnings {rustc} else {rustc.arg("-Awarnings")};
516+
let build_cmd_layout = cx.layout(package, KindForHost);
517+
518+
// building the possible `build/$pkg/output` file for this local package
519+
let command_output_file = build_cmd_layout.build(package).join("output");
520+
521+
// building the list of all possible `build/$pkg/output` files
522+
// whether they exist or not will be checked during the work
523+
let command_output_files = cx.dep_targets(package).iter().map(|&(pkg, _)| {
524+
build_cmd_layout.build(pkg).join("output")
525+
}).collect::<Vec<_>>();
515526

516527
(proc() {
528+
let mut rustc = rustc;
529+
530+
let mut additional_library_paths = Vec::new();
531+
532+
// list of `-l` flags to pass to rustc coming from custom build scripts
533+
let additional_library_links = match File::open(&command_output_file) {
534+
Ok(f) => {
535+
let flags = try!(CustomBuildCommandOutput::parse(
536+
BufferedReader::new(f), name.as_slice()));
537+
538+
additional_library_paths.extend(flags.library_paths.iter().map(|p| p.clone()));
539+
flags.library_links.clone()
540+
},
541+
Err(_) => Vec::new()
542+
};
543+
544+
// loading each possible custom build output file to fill `additional_library_paths`
545+
for flags_file in command_output_files.into_iter() {
546+
let flags = match File::open(&flags_file) {
547+
Ok(f) => f,
548+
Err(_) => continue // the file doesn't exist, probably means that this pkg
549+
// doesn't have a build command
550+
};
551+
552+
let flags = try!(CustomBuildCommandOutput::parse(
553+
BufferedReader::new(flags), name.as_slice()));
554+
additional_library_paths.extend(flags.library_paths.iter().map(|p| p.clone()));
555+
}
556+
557+
for p in additional_library_paths.into_iter() {
558+
rustc = rustc.arg("-L").arg(p);
559+
}
560+
for lib in additional_library_links.into_iter() {
561+
rustc = rustc.arg("-l").arg(lib);
562+
}
563+
517564
try!(rustc.exec().chain_error(|| {
518565
human(format!("Could not compile `{}`.", name))
519566
}));
567+
520568
Ok(())
569+
521570
}, kind, desc)
522571
}).collect())
523572
}

tests/test_cargo_compile_custom_build.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use std::path;
2+
13
use support::{project, execs};
4+
use support::{COMPILING, RUNNING};
25
use hamcrest::{assert_that};
36

47
fn setup() {
@@ -151,3 +154,55 @@ Only `-l` and `-L` flags are allowed in build script of `foo v0.5.0 (file://{})`
151154
`",
152155
p.root().display())));
153156
})
157+
158+
/*
159+
test!(custom_build_script_rustc_flags {
160+
let p = project("foo")
161+
.file("Cargo.toml", r#"
162+
[project]
163+
164+
name = "bar"
165+
version = "0.5.0"
166+
authors = ["wycats@example.com"]
167+
168+
[dependencies.foo]
169+
path = "foo"
170+
"#)
171+
.file("src/main.rs", r#"
172+
fn main() {}
173+
"#)
174+
.file("foo/Cargo.toml", r#"
175+
[project]
176+
177+
name = "foo"
178+
version = "0.5.0"
179+
authors = ["wycats@example.com"]
180+
build = "build.rs"
181+
"#)
182+
.file("foo/src/lib.rs", r#"
183+
"#)
184+
.file("foo/build.rs", r#"
185+
fn main() {
186+
println!("cargo:rustc-flags=-l nonexistinglib -L /dummy/path1 -L /dummy/path2");
187+
}
188+
"#);
189+
190+
// TODO: TEST FAILS BECAUSE OF WRONG STDOUT (but otherwise, the build works)
191+
assert_that(p.cargo_process("build").arg("--verbose"),
192+
execs().with_status(101)
193+
.with_stdout(format!("\
194+
{compiling} bar v0.5.0 ({url})
195+
{running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib -g \
196+
-C metadata=[..] \
197+
-C extra-filename=-[..] \
198+
--out-dir {dir}{sep}target \
199+
--dep-info [..] \
200+
-L {dir}{sep}target \
201+
-L {dir}{sep}target{sep}deps`
202+
",
203+
running = RUNNING, compiling = COMPILING, sep = path::SEP,
204+
dir = p.root().display(),
205+
url = p.url(),
206+
)));
207+
})
208+
*/

0 commit comments

Comments
 (0)