]> git.proxmox.com Git - cargo.git/commitdiff
Build only lib from package
authorValerii Hiora <valerii.hiora@gmail.com>
Tue, 11 Nov 2014 12:59:06 +0000 (14:59 +0200)
committerValerii Hiora <valerii.hiora@gmail.com>
Fri, 14 Nov 2014 21:09:15 +0000 (23:09 +0200)
Fixes #724

src/bin/bench.rs
src/bin/build.rs
src/bin/doc.rs
src/bin/run.rs
src/bin/test.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_package.rs
tests/test_cargo_build_lib.rs [new file with mode: 0644]
tests/tests.rs

index 612c37b1534092bf2f432f3eb15ead83e836ee56..97073e1e07a8047b14691d5dc4e43234f1964760 100644 (file)
@@ -63,6 +63,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
             features: options.flag_features.as_slice(),
             no_default_features: options.flag_no_default_features,
             spec: options.flag_package.as_ref().map(|s| s.as_slice()),
+            lib_only: false
         },
     };
 
index 07ce41ee329b2d0dd5241ec8507315cbdff18f35..da0c1d4ee7921e8f47ccc05f5189b3ed344d129b 100644 (file)
@@ -16,6 +16,7 @@ struct Options {
     flag_manifest_path: Option<String>,
     flag_verbose: bool,
     flag_release: bool,
+    flag_lib: bool
 }
 
 pub const USAGE: &'static str = "
@@ -28,6 +29,7 @@ Options:
     -h, --help               Print this message
     -p SPEC, --package SPEC  Package to build
     -j N, --jobs N           The number of jobs to run in parallel
+    --lib                    Build only lib (if present in package)
     --release                Build artifacts in release mode, with optimizations
     --features FEATURES      Space-separated list of features to also build
     --no-default-features    Do not build the `default` feature
@@ -62,6 +64,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
         features: options.flag_features.as_slice(),
         no_default_features: options.flag_no_default_features,
         spec: options.flag_package.as_ref().map(|s| s.as_slice()),
+        lib_only: options.flag_lib
     };
 
     ops::compile(&root, &mut opts).map(|_| None).map_err(|err| {
index 9381a30a8d2ddc3fb1ed3edc6e92b78bcdccd28d..b94ec30e362740432f5d22e69eb6c45fd06653ab 100644 (file)
@@ -51,6 +51,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
             features: options.flag_features.as_slice(),
             no_default_features: options.flag_no_default_features,
             spec: None,
+            lib_only: false
         },
     };
 
index efdf78fd299b28a72d834b0c10626eb69be79531..72e118537654e5244103e08b4d25d287b6e3d5db 100644 (file)
@@ -67,6 +67,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
         features: options.flag_features.as_slice(),
         no_default_features: options.flag_no_default_features,
         spec: None,
+        lib_only: false
     };
 
     let (target_kind, name) = match (options.flag_name, options.flag_example) {
index d54100b6e651abb4603f186ae53ed7c6204e1587..27f5a3a7cf6769ba30bd67f39b3ae0cfde89dd55 100644 (file)
@@ -65,6 +65,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>
             features: options.flag_features.as_slice(),
             no_default_features: options.flag_no_default_features,
             spec: options.flag_package.as_ref().map(|s| s.as_slice()),
+            lib_only: false
         },
     };
 
index 1de7a9e50cbac8a1d1f0d65a5aeff7249b41e9ab..7fb32ffcf13b8dfd72a3686b40a9721e2fc42090 100644 (file)
@@ -47,6 +47,7 @@ pub struct CompileOptions<'a> {
     pub features: &'a [String],
     pub no_default_features: bool,
     pub spec: Option<&'a str>,
+    pub lib_only: bool
 }
 
 pub fn compile(manifest_path: &Path,
@@ -70,7 +71,8 @@ pub fn compile(manifest_path: &Path,
 pub fn compile_pkg(package: &Package, options: &mut CompileOptions)
                    -> CargoResult<ops::Compilation> {
     let CompileOptions { env, ref mut shell, jobs, target, spec,
-                         dev_deps, features, no_default_features } = *options;
+                         dev_deps, features, no_default_features,
+                         lib_only } = *options;
     let target = target.map(|s| s.to_string());
     let features = features.iter().flat_map(|s| {
         s.as_slice().split(' ')
@@ -135,7 +137,11 @@ pub fn compile_pkg(package: &Package, options: &mut CompileOptions)
             "doc" | "doc-all" => target.get_profile().get_env() == "doc",
             env => target.get_profile().get_env() == env,
         }
-    }).collect::<Vec<&Target>>();
+    }).filter(|target| !lib_only || target.is_lib()).collect::<Vec<&Target>>();
+
+    if lib_only && targets.len() == 0 {
+        return Err(human("There is no lib to build, remove `--lib` flag".to_string()));
+    }
 
     let ret = {
         let _p = profile::start("compiling");
index 807e0166bd810d7fb41da3424b9a55900131c022..6e0bfe863c55afc85f0ae286d2f30d8a29d0696d 100644 (file)
@@ -126,6 +126,7 @@ fn run_verify(pkg: &Package, shell: &mut MultiShell, tar: &Path)
         features: [],
         no_default_features: false,
         spec: None,
+        lib_only: false,
     }));
 
     Ok(())
diff --git a/tests/test_cargo_build_lib.rs b/tests/test_cargo_build_lib.rs
new file mode 100644 (file)
index 0000000..2b3c1b3
--- /dev/null
@@ -0,0 +1,57 @@
+use std::path;
+use support::{basic_bin_manifest, execs, project, ProjectBuilder};
+use support::{COMPILING, RUNNING};
+use hamcrest::{assert_that};
+
+fn setup() {
+}
+
+fn verbose_output_for_lib(p: &ProjectBuilder) -> String {
+    format!("\
+{compiling} {name} v{version} ({url})
+{running} `rustc {dir}{sep}src{sep}lib.rs --crate-name {name} --crate-type lib -g \
+        -C metadata=[..] \
+        -C extra-filename=-[..] \
+        --out-dir {dir}{sep}target \
+        --dep-info [..] \
+        -L {dir}{sep}target \
+        -L {dir}{sep}target{sep}deps`
+",
+            running = RUNNING, compiling = COMPILING, sep = path::SEP,
+            dir = p.root().display(), url = p.url(),
+            name = "foo", version = "0.0.1")
+}
+
+test!(build_lib_only {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+
+            name = "foo"
+            version = "0.0.1"
+            authors = ["wycats@example.com"]
+        "#)
+        .file("src/main.rs", r#"
+            fn main() {}
+        "#)
+        .file("src/lib.rs", r#" "#);
+
+    assert_that(p.cargo_process("build").arg("--lib").arg("-v"),
+                execs()
+                .with_status(0)
+                .with_stdout(verbose_output_for_lib(&p)));
+})
+
+
+test!(build_with_no_lib {
+    let p = project("foo")
+        .file("Cargo.toml", basic_bin_manifest("foo"))
+        .file("src/main.rs", r#"
+            fn main() {}
+        "#);
+
+    assert_that(p.cargo_process("build").arg("--lib"),
+                execs()
+                .with_status(101)
+                .with_stderr("There is no lib to build, remove `--lib` flag"));
+})
index 0c0a47851cf5748099950d8366b6a85608a1b444..b205bd0e2956c8f7f3b45b80460580bda306be6c 100644 (file)
@@ -27,6 +27,7 @@ macro_rules! test(
 
 mod test_cargo;
 mod test_cargo_bench;
+mod test_cargo_build_lib;
 mod test_cargo_clean;
 mod test_cargo_compile;
 mod test_cargo_compile_custom_build;