From: Carl Lerche Date: Tue, 18 Mar 2014 00:48:47 +0000 (-0700) Subject: Add cargo-compile command X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~1131 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=4e236c8558f88a8107f90ef068e6e1a90cedc538;p=cargo.git Add cargo-compile command --- diff --git a/Makefile b/Makefile index ff74ed696..af30c5eb0 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,10 @@ RUSTC ?= rustc RUSTC_FLAGS ?= # Link flags to pull in dependencies -BINS = cargo-read-manifest \ - cargo-rustc \ - cargo-verify-project +BINS = cargo-compile \ + cargo-read-manifest \ + cargo-rustc \ + cargo-verify-project SRC = $(wildcard src/*.rs) DEPS = -L libs/hammer.rs/target -L libs/rust-toml/lib diff --git a/src/bin/cargo-compile.rs b/src/bin/cargo-compile.rs new file mode 100644 index 000000000..39e885b23 --- /dev/null +++ b/src/bin/cargo-compile.rs @@ -0,0 +1,81 @@ +#[crate_id="cargo-compile"]; + +extern crate serialize; +extern crate hammer; +// extern crate cargo; + +use serialize::{Decodable}; +use hammer::{FlagDecoder,FlagConfig,FlagConfiguration}; +use std::io; +use io::{IoResult,IoError,OtherIoError,BufReader}; +use io::process::{Process,ProcessExit,ProcessOutput,InheritFd,ProcessConfig}; + +#[deriving(Decodable)] +struct Options { + manifest_path: ~str +} + +impl FlagConfig for Options { + fn config(_: Option, c: FlagConfiguration) -> FlagConfiguration { c } +} + +fn main() { + match compile() { + Err(io_error) => fail!("{}", io_error), + Ok(_) => return + } +} + +fn compile() -> IoResult<()> { + let options = try!(flags::()); + let manifest_bytes = try!(read_manifest(options.manifest_path)); + + call_rustc(~BufReader::new(manifest_bytes.as_slice())) +} + +fn flags>() -> IoResult { + let mut decoder = FlagDecoder::new::(std::os::args().tail()); + let flags: T = Decodable::decode(&mut decoder); + + if decoder.error.is_some() { + Err(IoError{ kind: OtherIoError, desc: "could not decode flags", detail: Some(decoder.error.unwrap()) }) + } else { + Ok(flags) + } +} + +fn read_manifest(manifest_path: &str) -> IoResult<~[u8]> { + Ok((try!(exec_with_output("cargo-read-manifest", [~"--manifest-path", manifest_path.to_owned()], None))).output) +} + +fn call_rustc(mut manifest_data: ~Reader:) -> IoResult<()> { + let data: &mut Reader = manifest_data; + try!(exec_tty("cargo-rustc", [], Some(data))); + Ok(()) +} + +fn exec_with_output(program: &str, args: &[~str], input: Option<&mut Reader>) -> IoResult { + Ok((try!(exec(program, args, input, |_| {}))).wait_with_output()) +} + +fn exec_tty(program: &str, args: &[~str], input: Option<&mut Reader>) -> IoResult { + Ok((try!(exec(program, args, input, |config| { + config.stdout = InheritFd(1); + config.stderr = InheritFd(2); + }))).wait()) +} + +fn exec(program: &str, args: &[~str], input: Option<&mut Reader>, configurator: |&mut ProcessConfig|) -> IoResult { + let mut config = ProcessConfig::new(); + config.program = program; + config.args = args; + configurator(&mut config); + + println!("Executing {} {}", program, args); + + let mut process = try!(Process::configure(config)); + + input.map(|mut reader| io::util::copy(&mut reader, process.stdin.get_mut_ref())); + + Ok(process) +} diff --git a/src/bin/cargo-rustc.rs b/src/bin/cargo-rustc.rs index b6e516345..70c62a86b 100644 --- a/src/bin/cargo-rustc.rs +++ b/src/bin/cargo-rustc.rs @@ -43,7 +43,7 @@ fn main() { Ok(val) => val } - println!("Executing {}", args.as_slice()); + println!("Executing rustc {}", args.as_slice()); let mut config = ProcessConfig::new(); config.stdout = InheritFd(1);