From: Valerii Hiora Date: Tue, 11 Nov 2014 12:59:06 +0000 (+0200) Subject: Build only lib from package X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=9c1c3633b761f377ab5e7783fac6f1c03690b040;p=cargo.git Build only lib from package Fixes #724 --- diff --git a/src/bin/bench.rs b/src/bin/bench.rs index 612c37b15..97073e1e0 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -63,6 +63,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult 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 }, }; diff --git a/src/bin/build.rs b/src/bin/build.rs index 07ce41ee3..da0c1d4ee 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -16,6 +16,7 @@ struct Options { flag_manifest_path: Option, 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 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| { diff --git a/src/bin/doc.rs b/src/bin/doc.rs index 9381a30a8..b94ec30e3 100644 --- a/src/bin/doc.rs +++ b/src/bin/doc.rs @@ -51,6 +51,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult features: options.flag_features.as_slice(), no_default_features: options.flag_no_default_features, spec: None, + lib_only: false }, }; diff --git a/src/bin/run.rs b/src/bin/run.rs index efdf78fd2..72e118537 100644 --- a/src/bin/run.rs +++ b/src/bin/run.rs @@ -67,6 +67,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult 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) { diff --git a/src/bin/test.rs b/src/bin/test.rs index d54100b6e..27f5a3a7c 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -65,6 +65,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult 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 }, }; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 1de7a9e50..7fb32ffcf 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -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 { 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::>(); + }).filter(|target| !lib_only || target.is_lib()).collect::>(); + + 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"); diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 807e0166b..6e0bfe863 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -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 index 000000000..2b3c1b302 --- /dev/null +++ b/tests/test_cargo_build_lib.rs @@ -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")); +}) diff --git a/tests/tests.rs b/tests/tests.rs index 0c0a47851..b205bd0e2 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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;