]> git.proxmox.com Git - cargo.git/commitdiff
Use cargo conventions for cargo itself
authorCarl Lerche <me@carllerche.com>
Wed, 12 Mar 2014 22:32:47 +0000 (15:32 -0700)
committerCarl Lerche <me@carllerche.com>
Wed, 12 Mar 2014 22:32:47 +0000 (15:32 -0700)
Makefile
commands/cargo-read-manifest/main.rs [deleted file]
commands/cargo-rustc/main.rs [deleted file]
commands/cargo-verify-project/main.rs [deleted file]
commands/cargo/main.rs [deleted file]
libcargo/Makefile [deleted file]
libcargo/cargo.rs [deleted file]
src/bin/cargo-read-manifest.rs [new file with mode: 0644]
src/bin/cargo-rustc.rs [new file with mode: 0644]
src/bin/cargo-verify-project.rs [new file with mode: 0644]
src/cargo.rs [new file with mode: 0644]

index 68b1517c98d5622ba4cca16cbe56fe1a30165a31..88b614745f84ad9e505eace3a7e12b79103b0178 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,41 +1,46 @@
-RUSTC_TARGET = target
-RUSTC_FLAGS ?= --out-dir $(RUSTC_TARGET) -L $(RUSTC_TARGET)/libs
+RUSTC ?= rustc
+RUSTC_FLAGS ?= --out-dir $(RUSTC_TARGET) -L $(RUSTC_TARGET)/libs
 
 TOML_LIB := $(shell rustc --crate-file-name libs/rust-toml/src/toml/lib.rs)
 HAMMER_LIB := $(shell rustc --crate-file-name libs/hammer.rs/src/hammer.rs)
-LIBCARGO_LIB := $(shell rustc --crate-file-name libcargo/cargo.rs)
 
-default: dependencies commands
+# Link flags to pull in dependencies
+DEPS = -L libs/hammer.rs/target -L libs/rust-toml/lib
+SRC = $(wildcard src/*.rs)
+BINS = cargo-read-manifest \
+                        cargo-rustc \
+                        cargo-verify-project
 
-dependencies: target/libs/$(TOML_LIB) target/libs/$(HAMMER_LIB) target/libs/$(LIBCARGO_LIB)
+BIN_TARGETS = $(patsubst %,target/%,$(BINS))
 
-commands: target/cargo-rustc target/cargo-verify-project target/cargo-read-manifest
+all: $(BIN_TARGETS)
 
-clean:
-       rm -rf target
+# Builds the hammer dependency
+hammer:
+       cd libs/hammer.rs && make
 
-target/cargo-rustc: target dependencies target/libs/$(TOML_LIB) commands/cargo-rustc/main.rs
-       rustc commands/cargo-rustc/main.rs $(RUSTC_FLAGS)
+toml:
+       cd libs/rust-toml && make
 
-target/cargo-verify-project: target dependencies target/libs/$(TOML_LIB) commands/cargo-verify-project/main.rs
-       rustc commands/cargo-verify-project/main.rs $(RUSTC_FLAGS)
+# === Cargo
 
-target/cargo-read-manifest: target dependencies target/libs/$(TOML_LIB) target/libs/$(HAMMER_LIB) commands/cargo-read-manifest/main.rs
-       rustc commands/cargo-read-manifest/main.rs $(RUSTC_FLAGS)
+target:
+       mkdir -p target
 
-target/libs/$(TOML_LIB): target libs/rust-toml/src/toml/lib.rs
-       cd libs/rust-toml && make
-       cp libs/rust-toml/lib/*.rlib target/libs
+libcargo: target $(SRC)
+       $(RUSTC) --out-dir target src/cargo.rs
 
-target/libs/$(HAMMER_LIB): target libs/hammer.rs/src/hammer.rs
-       cd libs/hammer.rs && make
-       cp libs/hammer.rs/target/*.rlib target/libs
+# === Commands
 
-target/libs/$(LIBCARGO_LIB): target libcargo/cargo.rs
-       cd libcargo && make
-       cp libcargo/target/*.rlib target/libs/
+$(BIN_TARGETS): target/%: src/bin/%.rs hammer toml libcargo
+       $(RUSTC) $(DEPS) -Ltarget --out-dir target $<
 
-target:
-       mkdir -p $(RUSTC_TARGET)/libs
+clean:
+       rm -rf target
+
+distclean: clean
+       cd libs/hamcrest-rust && make clean
+       cd libs/hammer.rs && make clean
+       cd libs/rust-toml && make clean
 
-.PHONY: default clean dependencies commands
+.PHONY: all clean distclean test hammer libcargo
diff --git a/commands/cargo-read-manifest/main.rs b/commands/cargo-read-manifest/main.rs
deleted file mode 100644 (file)
index c4f12cd..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-#[crate_id="cargo-read-manifest"];
-
-extern crate cargo;
-extern crate hammer;
-extern crate serialize;
-extern crate toml;
-extern crate semver;
-
-use hammer::{FlagDecoder,FlagConfig,FlagConfiguration};
-use serialize::{Decoder,Decodable};
-use serialize::json::Encoder;
-use toml::from_toml;
-use semver::Version;
-use cargo::{Manifest,LibTarget,ExecTarget,Project};
-use std::path::Path;
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-struct SerializedManifest {
-  project: ~Project,
-  lib: Option<~[SerializedLibTarget]>,
-  bin: Option<~[SerializedExecTarget]>
-}
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-pub struct SerializedTarget {
-  name: ~str,
-  path: Option<~str>
-}
-
-pub type SerializedLibTarget = SerializedTarget;
-pub type SerializedExecTarget = SerializedTarget;
-
-
-#[deriving(Decodable,Eq,Clone,Ord)]
-struct ReadManifestFlags {
-  manifest_path: ~str
-}
-
-impl FlagConfig for ReadManifestFlags {
-  fn config(_: Option<ReadManifestFlags>, c: FlagConfiguration) -> FlagConfiguration {
-    c
-  }
-}
-
-fn main() {
-  let mut decoder = FlagDecoder::new::<ReadManifestFlags>(std::os::args().tail());
-  let flags: ReadManifestFlags = Decodable::decode(&mut decoder);
-
-  if decoder.error.is_some() {
-    fail!("Error: {}", decoder.error.unwrap());
-  }
-
-  let root = toml::parse_from_file(flags.manifest_path).unwrap();
-
-  let toml_manifest = from_toml::<SerializedManifest>(root.clone());
-
-  let (lib, bin) = normalize(&toml_manifest.lib, &toml_manifest.bin);
-
-  let manifest = Manifest{
-    root: Path::new(flags.manifest_path).dirname_str().unwrap().to_owned(),
-    project: toml_manifest.project,
-    lib: lib,
-    bin: bin
-  };
-
-  let encoded: ~str = Encoder::str_encode(&manifest);
-
-  println!("{}", encoded);
-}
-
-fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExecTarget]>) -> (~[LibTarget], ~[ExecTarget]) {
-  if lib.is_some() && bin.is_some() {
-    let l = lib.clone().unwrap()[0];
-    let mut path = l.path.clone();
-
-    if path.is_none() {
-      path = Some(format!("src/{}.rs", l.name));
-    }
-
-    let b = bin.get_ref().map(|b_ref| {
-      let mut b = b_ref.clone();
-      let mut path = b.path.clone();
-      if path.is_none() {
-        path = Some(format!("src/bin/{}.rs", b.name.clone()));
-      }
-      ExecTarget{ path: path.unwrap(), name: b.name }
-    });
-    (~[LibTarget{ path: path.unwrap(), name: l.name }], b)
-  } else if lib.is_some() {
-    let l = lib.clone().unwrap()[0];
-    let mut path = l.path.clone();
-
-    if path.is_none() {
-      path = Some(format!("src/{}.rs", l.name));
-    }
-
-    (~[LibTarget{ path: path.unwrap(), name: l.name }], ~[])
-  } else if bin.is_some() {
-    let b = bin.get_ref().map(|b_ref| {
-      let mut b = b_ref.clone();
-      let mut path = b.path.clone();
-      if path.is_none() {
-        path = Some(format!("src/bin/{}.rs", b.name.clone()));
-      }
-      ExecTarget{ path: path.unwrap(), name: b.name }
-    });
-    (~[], b)
-  } else {
-    (~[], ~[])
-  }
-}
diff --git a/commands/cargo-rustc/main.rs b/commands/cargo-rustc/main.rs
deleted file mode 100644 (file)
index 5b48a98..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-#[crate_id="cargo-rustc"];
-
-extern crate toml;
-extern crate serialize;
-extern crate cargo;
-
-use std::os::args;
-use std::io;
-use std::io::process::Process;
-use serialize::json;
-use serialize::{Decoder,Decodable};
-use std::path::Path;
-use cargo::{Manifest,LibTarget,ExecTarget,Project};
-
-/**
-  cargo-rustc -- ...args
-
-  Delegate ...args to actual rustc command
-*/
-
-fn main() {
-  let mut reader = io::stdin();
-  let input = reader.read_to_str().unwrap();
-
-  let json = json::from_str(input).unwrap();
-  let mut decoder = json::Decoder::new(json);
-  let manifest: Manifest = Decodable::decode(&mut decoder);
-
-  //let mut arguments = args();
-  //arguments.shift();
-
-  //if arguments[0] != ~"--" {
-    //fail!("LOL");
-  //} else {
-    //arguments.shift();
-  //}
-
-  let Manifest{ root, lib, .. } = manifest;
-
-  let root = Path::new(root);
-  let out_dir = lib[0].path;
-  let target = join(&root, ~"target");
-
-  let args = ~[
-    join(&root, out_dir),
-    ~"--out-dir", target,
-    ~"--crate-type", ~"lib"
-  ];
-
-  io::fs::mkdir_recursive(&root.join("target"), io::UserRWX);
-
-  println!("Executing {}", args);
-
-  let mut p = Process::new("rustc", args).unwrap();
-  let o = p.wait_with_output();
-
-  if o.status == std::io::process::ExitStatus(0) {
-    println!("output: {:s}", std::str::from_utf8(o.output).unwrap());
-  } else {
-    fail!("Failed to execute")
-  }
-}
-
-fn join(path: &Path, part: ~str) -> ~str {
-  path.join(part).as_str().unwrap().to_owned()
-}
diff --git a/commands/cargo-verify-project/main.rs b/commands/cargo-verify-project/main.rs
deleted file mode 100644 (file)
index afc6d00..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-#[crate_id="cargo-verify-project"];
-
-extern crate toml;
-extern crate getopts;
-
-use std::os::{args,set_exit_status};
-use std::io::process::Process;
-use getopts::{reqopt,getopts,OptGroup};
-
-/**
-  cargo-verify-project --manifest=LOCATION
-*/
-
-fn main() {
-  let arguments = args();
-
-  let opts = ~[
-    reqopt("m", "manifest", "the location of the manifest", "MANIFEST")
-  ];
-
-  let matches = match getopts(arguments.tail(), opts) {
-    Ok(m) => m,
-    Err(err) => {
-      fail("missing-argument", "manifest");
-      return;
-    }
-  };
-
-  if !matches.opt_present("m") {
-    fail("missing-argument", "manifest");
-    return;
-  }
-
-  let manifest = matches.opt_str("m").unwrap();
-  let file = Path::new(manifest);
-
-  if !file.exists() {
-    fail("invalid", "not-found");
-    return;
-  }
-
-  let root = match toml::parse_from_file(file.as_str().unwrap()) {
-    Err(e) => {
-      fail("invalid", "invalid-format");
-      return;
-    },
-    Ok(r) => r
-  };
-
-  println!("{}", "{ \"success\": \"true\" }");
-}
-
-fn fail(reason: &str, value: &str) {
-  println!(r#"\{ "{:s}", "{:s}" \}"#, reason, value);
-  set_exit_status(1);
-}
diff --git a/commands/cargo/main.rs b/commands/cargo/main.rs
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/libcargo/Makefile b/libcargo/Makefile
deleted file mode 100644 (file)
index 0354663..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-LIBCARGO_LIB := $(shell rustc --crate-file-name cargo.rs)
-
-default: target/$(LIBCARGO_LIB)
-
-target:
-       mkdir -p target
-
-clean:
-       rm -rf target
-
-target/$(LIBCARGO_LIB): target cargo.rs
-       rustc cargo.rs --out-dir target
-
-.PHONY: default clean
diff --git a/libcargo/cargo.rs b/libcargo/cargo.rs
deleted file mode 100644 (file)
index 7c94661..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#[crate_type="rlib"];
-
-extern crate serialize;
-use serialize::{Decoder};
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-pub struct Manifest {
-  project: ~Project,
-  root: ~str,
-  lib: ~[LibTarget],
-  bin: ~[ExecTarget]
-}
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-pub struct ExecTarget {
-  name: ~str,
-  path: ~str
-}
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-pub struct LibTarget {
-  name: ~str,
-  path: ~str
-}
-
-//pub type LibTarget = Target;
-//pub type ExecTarget = Target;
-
-#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
-pub struct Project {
-  name: ~str,
-  version: ~str,
-  authors: ~[~str]
-}
-
diff --git a/src/bin/cargo-read-manifest.rs b/src/bin/cargo-read-manifest.rs
new file mode 100644 (file)
index 0000000..c4f12cd
--- /dev/null
@@ -0,0 +1,111 @@
+#[crate_id="cargo-read-manifest"];
+
+extern crate cargo;
+extern crate hammer;
+extern crate serialize;
+extern crate toml;
+extern crate semver;
+
+use hammer::{FlagDecoder,FlagConfig,FlagConfiguration};
+use serialize::{Decoder,Decodable};
+use serialize::json::Encoder;
+use toml::from_toml;
+use semver::Version;
+use cargo::{Manifest,LibTarget,ExecTarget,Project};
+use std::path::Path;
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+struct SerializedManifest {
+  project: ~Project,
+  lib: Option<~[SerializedLibTarget]>,
+  bin: Option<~[SerializedExecTarget]>
+}
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+pub struct SerializedTarget {
+  name: ~str,
+  path: Option<~str>
+}
+
+pub type SerializedLibTarget = SerializedTarget;
+pub type SerializedExecTarget = SerializedTarget;
+
+
+#[deriving(Decodable,Eq,Clone,Ord)]
+struct ReadManifestFlags {
+  manifest_path: ~str
+}
+
+impl FlagConfig for ReadManifestFlags {
+  fn config(_: Option<ReadManifestFlags>, c: FlagConfiguration) -> FlagConfiguration {
+    c
+  }
+}
+
+fn main() {
+  let mut decoder = FlagDecoder::new::<ReadManifestFlags>(std::os::args().tail());
+  let flags: ReadManifestFlags = Decodable::decode(&mut decoder);
+
+  if decoder.error.is_some() {
+    fail!("Error: {}", decoder.error.unwrap());
+  }
+
+  let root = toml::parse_from_file(flags.manifest_path).unwrap();
+
+  let toml_manifest = from_toml::<SerializedManifest>(root.clone());
+
+  let (lib, bin) = normalize(&toml_manifest.lib, &toml_manifest.bin);
+
+  let manifest = Manifest{
+    root: Path::new(flags.manifest_path).dirname_str().unwrap().to_owned(),
+    project: toml_manifest.project,
+    lib: lib,
+    bin: bin
+  };
+
+  let encoded: ~str = Encoder::str_encode(&manifest);
+
+  println!("{}", encoded);
+}
+
+fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExecTarget]>) -> (~[LibTarget], ~[ExecTarget]) {
+  if lib.is_some() && bin.is_some() {
+    let l = lib.clone().unwrap()[0];
+    let mut path = l.path.clone();
+
+    if path.is_none() {
+      path = Some(format!("src/{}.rs", l.name));
+    }
+
+    let b = bin.get_ref().map(|b_ref| {
+      let mut b = b_ref.clone();
+      let mut path = b.path.clone();
+      if path.is_none() {
+        path = Some(format!("src/bin/{}.rs", b.name.clone()));
+      }
+      ExecTarget{ path: path.unwrap(), name: b.name }
+    });
+    (~[LibTarget{ path: path.unwrap(), name: l.name }], b)
+  } else if lib.is_some() {
+    let l = lib.clone().unwrap()[0];
+    let mut path = l.path.clone();
+
+    if path.is_none() {
+      path = Some(format!("src/{}.rs", l.name));
+    }
+
+    (~[LibTarget{ path: path.unwrap(), name: l.name }], ~[])
+  } else if bin.is_some() {
+    let b = bin.get_ref().map(|b_ref| {
+      let mut b = b_ref.clone();
+      let mut path = b.path.clone();
+      if path.is_none() {
+        path = Some(format!("src/bin/{}.rs", b.name.clone()));
+      }
+      ExecTarget{ path: path.unwrap(), name: b.name }
+    });
+    (~[], b)
+  } else {
+    (~[], ~[])
+  }
+}
diff --git a/src/bin/cargo-rustc.rs b/src/bin/cargo-rustc.rs
new file mode 100644 (file)
index 0000000..5b48a98
--- /dev/null
@@ -0,0 +1,66 @@
+#[crate_id="cargo-rustc"];
+
+extern crate toml;
+extern crate serialize;
+extern crate cargo;
+
+use std::os::args;
+use std::io;
+use std::io::process::Process;
+use serialize::json;
+use serialize::{Decoder,Decodable};
+use std::path::Path;
+use cargo::{Manifest,LibTarget,ExecTarget,Project};
+
+/**
+  cargo-rustc -- ...args
+
+  Delegate ...args to actual rustc command
+*/
+
+fn main() {
+  let mut reader = io::stdin();
+  let input = reader.read_to_str().unwrap();
+
+  let json = json::from_str(input).unwrap();
+  let mut decoder = json::Decoder::new(json);
+  let manifest: Manifest = Decodable::decode(&mut decoder);
+
+  //let mut arguments = args();
+  //arguments.shift();
+
+  //if arguments[0] != ~"--" {
+    //fail!("LOL");
+  //} else {
+    //arguments.shift();
+  //}
+
+  let Manifest{ root, lib, .. } = manifest;
+
+  let root = Path::new(root);
+  let out_dir = lib[0].path;
+  let target = join(&root, ~"target");
+
+  let args = ~[
+    join(&root, out_dir),
+    ~"--out-dir", target,
+    ~"--crate-type", ~"lib"
+  ];
+
+  io::fs::mkdir_recursive(&root.join("target"), io::UserRWX);
+
+  println!("Executing {}", args);
+
+  let mut p = Process::new("rustc", args).unwrap();
+  let o = p.wait_with_output();
+
+  if o.status == std::io::process::ExitStatus(0) {
+    println!("output: {:s}", std::str::from_utf8(o.output).unwrap());
+  } else {
+    fail!("Failed to execute")
+  }
+}
+
+fn join(path: &Path, part: ~str) -> ~str {
+  path.join(part).as_str().unwrap().to_owned()
+}
diff --git a/src/bin/cargo-verify-project.rs b/src/bin/cargo-verify-project.rs
new file mode 100644 (file)
index 0000000..afc6d00
--- /dev/null
@@ -0,0 +1,56 @@
+#[crate_id="cargo-verify-project"];
+
+extern crate toml;
+extern crate getopts;
+
+use std::os::{args,set_exit_status};
+use std::io::process::Process;
+use getopts::{reqopt,getopts,OptGroup};
+
+/**
+  cargo-verify-project --manifest=LOCATION
+*/
+
+fn main() {
+  let arguments = args();
+
+  let opts = ~[
+    reqopt("m", "manifest", "the location of the manifest", "MANIFEST")
+  ];
+
+  let matches = match getopts(arguments.tail(), opts) {
+    Ok(m) => m,
+    Err(err) => {
+      fail("missing-argument", "manifest");
+      return;
+    }
+  };
+
+  if !matches.opt_present("m") {
+    fail("missing-argument", "manifest");
+    return;
+  }
+
+  let manifest = matches.opt_str("m").unwrap();
+  let file = Path::new(manifest);
+
+  if !file.exists() {
+    fail("invalid", "not-found");
+    return;
+  }
+
+  let root = match toml::parse_from_file(file.as_str().unwrap()) {
+    Err(e) => {
+      fail("invalid", "invalid-format");
+      return;
+    },
+    Ok(r) => r
+  };
+
+  println!("{}", "{ \"success\": \"true\" }");
+}
+
+fn fail(reason: &str, value: &str) {
+  println!(r#"\{ "{:s}", "{:s}" \}"#, reason, value);
+  set_exit_status(1);
+}
diff --git a/src/cargo.rs b/src/cargo.rs
new file mode 100644 (file)
index 0000000..fb08956
--- /dev/null
@@ -0,0 +1,34 @@
+#[crate_type="rlib"];
+
+extern crate serialize;
+use serialize::{Decoder};
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+pub struct Manifest {
+  project: ~Project,
+  root: ~str,
+  lib: ~[LibTarget],
+  bin: ~[ExecTarget]
+}
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+pub struct ExecTarget {
+  name: ~str,
+  path: ~str
+}
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+pub struct LibTarget {
+  name: ~str,
+  path: ~str
+}
+
+//pub type LibTarget = Target;
+//pub type ExecTarget = Target;
+
+#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
+pub struct Project {
+  name: ~str,
+  version: ~str,
+  authors: ~[~str]
+}