Skip to content

Support both --bin and --lib together in cargo new #5433

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

Closed
wants to merge 2 commits into from
Closed
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
69 changes: 42 additions & 27 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ pub struct NewOptions {
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NewProjectKind {
Bin,
Lib,

pub struct NewProjectKind {
bin: bool,
lib: bool,
}

impl NewProjectKind {
fn is_bin(&self) -> bool {
*self == NewProjectKind::Bin
self.bin
}
}

impl fmt::Display for NewProjectKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
NewProjectKind::Bin => "binary (application)",
NewProjectKind::Lib => "library",
match (self.bin, self.lib) {
(true, false) => "binary (application)",
(false, true) => "library",
_ => "library with a binary (application)",
}.fmt(f)
}
}
Expand All @@ -64,7 +66,6 @@ struct MkOptions<'a> {
path: &'a Path,
name: &'a str,
source_files: Vec<SourceFileInformation>,
bin: bool,
}

impl NewOptions {
Expand All @@ -76,10 +77,9 @@ impl NewOptions {
name: Option<String>,
) -> CargoResult<NewOptions> {
let kind = match (bin, lib) {
(true, true) => bail!("can't specify both lib and binary outputs"),
(false, true) => NewProjectKind::Lib,
// default to bin
(_, false) => NewProjectKind::Bin,
(false, false) => NewProjectKind { bin: true, lib: false },
(bin, lib) => NewProjectKind { bin, lib },
};

let opts = NewOptions {
Expand Down Expand Up @@ -286,20 +286,23 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous",
Ok(())
}

fn plan_new_source_file(bin: bool, project_name: String) -> SourceFileInformation {
if bin {
SourceFileInformation {
fn plan_new_source_files(kind: NewProjectKind, project_name: &str) -> Vec<SourceFileInformation> {
let mut files = Vec::new();
if kind.bin {
files.push(SourceFileInformation {
relative_path: "src/main.rs".to_string(),
target_name: project_name,
target_name: project_name.to_owned(),
bin: true,
}
} else {
SourceFileInformation {
})
}
if kind.lib {
files.push(SourceFileInformation {
relative_path: "src/lib.rs".to_string(),
target_name: project_name,
target_name: project_name.to_owned(),
bin: false,
}
})
}
files
}

pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
Expand All @@ -319,8 +322,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
version_control: opts.version_control,
path,
name,
source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())],
bin: opts.kind.is_bin(),
source_files: plan_new_source_files(opts.kind, name),
};

mk(config, &mkopts).chain_err(|| {
Expand Down Expand Up @@ -348,7 +350,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
detect_source_paths_and_types(path, name, &mut src_paths_types)?;

if src_paths_types.is_empty() {
src_paths_types.push(plan_new_source_file(opts.kind.is_bin(), name.to_string()));
src_paths_types.extend(plan_new_source_files(opts.kind, name));
} else {
// --bin option may be ignored if lib.rs or src/lib.rs present
// Maybe when doing `cargo init --bin` inside a library project stub,
Expand Down Expand Up @@ -395,7 +397,6 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
version_control,
path,
name,
bin: src_paths_types.iter().any(|x| x.bin),
source_files: src_paths_types,
};

Expand All @@ -417,19 +418,21 @@ fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
let path = opts.path;
let name = opts.name;
let cfg = global_config(config)?;
let has_bin = opts.source_files.iter().any(|f| f.bin);
let has_lib = opts.source_files.iter().any(|f| !f.bin);
// Please ensure that ignore and hgignore are in sync.
let ignore = [
"/target\n",
"**/*.rs.bk\n",
if !opts.bin { "Cargo.lock\n" } else { "" },
if !has_bin { "Cargo.lock\n" } else { "" },
].concat();
// Mercurial glob ignores can't be rooted, so just sticking a 'syntax: glob' at the top of the
// file will exclude too much. Instead, use regexp-based ignores. See 'hg help ignore' for
// more.
let hgignore = [
"^target/\n",
"glob:*.rs.bk\n",
if !opts.bin { "glob:Cargo.lock\n" } else { "" },
if !has_bin { "glob:Cargo.lock\n" } else { "" },
].concat();

let vcs = opts.version_control.unwrap_or_else(|| {
Expand Down Expand Up @@ -554,12 +557,24 @@ authors = [{}]
fs::create_dir_all(src_dir)?;
}

let file_content_tmp;
let default_file_content: &[u8] = if i.bin {
b"\
if has_lib {
file_content_tmp = format!("\
extern crate {};

fn main() {{
println!(\"Hello, world!\");
}}
", name);
file_content_tmp.as_bytes()
} else {
b"\
fn main() {
println!(\"Hello, world!\");
}
"
}
} else {
b"\
#[cfg(test)]
Expand Down
23 changes: 17 additions & 6 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,28 @@ fn simple_bin() {
}

#[test]
fn both_lib_and_bin() {
let td = tempfile::Builder::new().prefix("cargo").tempdir().unwrap();
fn lib_and_bin() {
let path = paths::root().join("foo");
fs::create_dir(&path).unwrap();
assert_that(
cargo_process("init")
.arg("--lib")
.arg("--bin")
.cwd(td.path())
.env("USER", "foo"),
.env("USER", "foo")
.cwd(&path),
execs()
.with_status(101)
.with_stderr("[ERROR] can't specify both lib and binary outputs"),
.with_status(0)
.with_stderr("[CREATED] library with a binary (application) project"),
);

assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
assert_that(&paths::root().join("foo/src/lib.rs"), existing_file());

assert_that(cargo_process("build").cwd(&path), execs().with_status(0));
assert_that(
&paths::root().join(&format!("foo/target/debug/foo{}", env::consts::EXE_SUFFIX)),
existing_file(),
);
}

Expand Down
22 changes: 18 additions & 4 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,27 @@ fn simple_bin() {
fn both_lib_and_bin() {
assert_that(
cargo_process("new")
.arg("--lib")
.arg("--bin")
.arg("foo")
.arg("--lib")
.arg("fooboth")
.env("USER", "foo"),
execs()
.with_status(101)
.with_stderr("[ERROR] can't specify both lib and binary outputs"),
.with_status(0)
.with_stderr("[CREATED] library with a binary (application) `fooboth` project"),
);

assert_that(&paths::root().join("fooboth"), existing_dir());
assert_that(&paths::root().join("fooboth/Cargo.toml"), existing_file());
assert_that(&paths::root().join("fooboth/src/main.rs"), existing_file());
assert_that(&paths::root().join("fooboth/src/lib.rs"), existing_file());

assert_that(
cargo_process("build").cwd(&paths::root().join("fooboth")),
execs().with_status(0),
);
assert_that(
&paths::root().join(&format!("fooboth/target/debug/fooboth{}", env::consts::EXE_SUFFIX)),
existing_file(),
);
}

Expand Down