Add cargo-compile command
authorCarl Lerche <me@carllerche.com>
Tue, 18 Mar 2014 00:48:47 +0000 (17:48 -0700)
committerCarl Lerche <me@carllerche.com>
Tue, 18 Mar 2014 00:48:47 +0000 (17:48 -0700)
Makefile
src/bin/cargo-compile.rs [new file with mode: 0644]
src/bin/cargo-rustc.rs

index ff74ed696a66e06933261fa3a7389d4ad06f5cac..af30c5eb0d89424eed4e3b5dba37c9b71193c967 100644 (file)
--- 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 (file)
index 0000000..39e885b
--- /dev/null
@@ -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<Options>, c: FlagConfiguration) -> FlagConfiguration { c }
+}
+
+fn main() {
+    match compile() {
+        Err(io_error) => fail!("{}", io_error),
+        Ok(_) => return
+    }
+}
+
+fn compile() -> IoResult<()> {
+    let options = try!(flags::<Options>());
+    let manifest_bytes = try!(read_manifest(options.manifest_path));
+
+    call_rustc(~BufReader::new(manifest_bytes.as_slice()))
+}
+
+fn flags<T: FlagConfig + Decodable<FlagDecoder>>() -> IoResult<T> {
+    let mut decoder = FlagDecoder::new::<T>(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<ProcessOutput> {
+    Ok((try!(exec(program, args, input, |_| {}))).wait_with_output())
+}
+
+fn exec_tty(program: &str, args: &[~str], input: Option<&mut Reader>) -> IoResult<ProcessExit> {
+    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<Process> {
+    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)
+}
index b6e516345c03863f69396efedb436ec083fdf609..70c62a86bc1332fcae670280cea4a989de06c46c 100644 (file)
@@ -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);