Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build only lib from package #839

Merged
merged 1 commit into from
Nov 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
};

Expand Down
3 changes: 3 additions & 0 deletions src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "
Expand All @@ -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
Expand Down Expand Up @@ -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| {
Expand Down
1 change: 1 addition & 0 deletions src/bin/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
};

Expand Down
1 change: 1 addition & 0 deletions src/bin/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
};

Expand Down
10 changes: 8 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(' ')
Expand Down Expand Up @@ -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()));
Copy link

@ryoqun ryoqun Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

let ret = {
let _p = profile::start("compiling");
Expand Down
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
57 changes: 57 additions & 0 deletions tests/test_cargo_build_lib.rs
Original file line number Diff line number Diff line change
@@ -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"));
})
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ macro_rules! test(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this file turned into an executable, could you flip it back?

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;
Expand Down