Skip to content

Commit 41ed455

Browse files
committed
rustc: Repurpose the --crate-name CLI flag
In a cargo-driven world the primary location for the name of a crate will be in its manifest, not in the source file itself. The purpose of this flag is to reduce required duplication for new cargo projects. This is a breaking change because the existing --crate-name flag actually printed the crate name. This flag was renamed to --print-crate-name, and to maintain consistence, the --crate-file-name flag was renamed to --print-file-name. To maintain backwards compatibility, the --crate-file-name flag is still recognized, but it is deprecated. [breaking-change]
1 parent 812637e commit 41ed455

File tree

6 files changed

+54
-10
lines changed

6 files changed

+54
-10
lines changed

src/librustc/back/link.rs

+10
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,16 @@ pub fn find_crate_name(sess: Option<&Session>,
556556
s
557557
};
558558

559+
match sess {
560+
Some(sess) => {
561+
match sess.opts.crate_name {
562+
Some(ref s) => return validate(s.clone(), None),
563+
None => {}
564+
}
565+
}
566+
None => {}
567+
}
568+
559569
let crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
560570
.and_then(|at| at.value_str().map(|s| (at, s)));
561571
match crate_name {

src/librustc/driver/config.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Contains infrastructure for configuring the compiler, including parsing
1212
//! command line options.
1313
14-
use driver::early_error;
14+
use driver::{early_error, early_warn};
1515
use driver::driver;
1616
use driver::session::Session;
1717

@@ -96,6 +96,7 @@ pub struct Options {
9696
pub cg: CodegenOptions,
9797
pub color: ColorConfig,
9898
pub externs: HashMap<String, Vec<String>>,
99+
pub crate_name: Option<String>,
99100
}
100101

101102
/// Some reasonable defaults
@@ -122,6 +123,7 @@ pub fn basic_options() -> Options {
122123
cg: basic_codegen_options(),
123124
color: Auto,
124125
externs: HashMap::new(),
126+
crate_name: None,
125127
}
126128
}
127129

@@ -511,9 +513,12 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
511513
"[bin|lib|rlib|dylib|staticlib]"),
512514
optmulti("", "emit", "Comma separated list of types of output for the compiler to emit",
513515
"[asm|bc|ir|obj|link]"),
514-
optflag("", "crate-name", "Output the crate name and exit"),
515-
optflag("", "crate-file-name", "Output the file(s) that would be written if compilation \
516+
optopt("", "crate-name", "Specify the name of the crate being built",
517+
"NAME"),
518+
optflag("", "print-crate-name", "Output the crate name and exit"),
519+
optflag("", "print-file-name", "Output the file(s) that would be written if compilation \
516520
continued and exit"),
521+
optflag("", "crate-file-name", "deprecated in favor of --print-file-name"),
517522
optflag("g", "", "Equivalent to --debuginfo=2"),
518523
optopt("", "debuginfo", "Emit DWARF debug info to the objects created:
519524
0 = no debug info,
@@ -716,8 +721,13 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
716721
matches.opt_str("dep-info")
717722
.map(|p| Path::new(p)));
718723

719-
let print_metas = (matches.opt_present("crate-name"),
724+
let print_metas = (matches.opt_present("print-crate-name"),
725+
matches.opt_present("print-file-name") ||
720726
matches.opt_present("crate-file-name"));
727+
if matches.opt_present("crate-file-name") {
728+
early_warn("the --crate-file-name argument has been renamed to \
729+
--print-file-name");
730+
}
721731
let cg = build_codegen_options(matches);
722732

723733
let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
@@ -749,6 +759,8 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
749759
locs.push(location.to_string());
750760
}
751761

762+
let crate_name = matches.opt_str("crate-name");
763+
752764
Options {
753765
crate_types: crate_types,
754766
gc: gc,
@@ -771,6 +783,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
771783
cg: cg,
772784
color: color,
773785
externs: externs,
786+
crate_name: crate_name,
774787
}
775788
}
776789

src/librustc/driver/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ pub fn early_error(msg: &str) -> ! {
389389
fail!(diagnostic::FatalError);
390390
}
391391

392+
pub fn early_warn(msg: &str) {
393+
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto);
394+
emitter.emit(None, msg, diagnostic::Warning);
395+
}
396+
392397
pub fn list_metadata(sess: &Session, path: &Path,
393398
out: &mut io::Writer) -> io::IoResult<()> {
394399
metadata::loader::list_file_metadata(sess.targ_cfg.os, path, out)
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-include ../tools.mk
22

33
all:
4-
[ `$(RUSTC) --crate-name crate.rs` = "foo" ]
5-
[ `$(RUSTC) --crate-file-name crate.rs` = "foo" ]
6-
[ `$(RUSTC) --crate-file-name --crate-type=lib --test crate.rs` = "foo" ]
7-
[ `$(RUSTC) --crate-file-name --test lib.rs` = "mylib" ]
8-
$(RUSTC) --crate-file-name lib.rs
9-
$(RUSTC) --crate-file-name rlib.rs
4+
[ `$(RUSTC) --print-crate-name crate.rs` = "foo" ]
5+
[ `$(RUSTC) --print-file-name crate.rs` = "foo" ]
6+
[ `$(RUSTC) --print-file-name --crate-type=lib --test crate.rs` = "foo" ]
7+
[ `$(RUSTC) --print-file-name --test lib.rs` = "mylib" ]
8+
$(RUSTC) --print-file-name lib.rs
9+
$(RUSTC) --print-file-name rlib.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --crate-name foo bar.rs
5+
rm $(TMPDIR)/libfoo.rlib
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]

0 commit comments

Comments
 (0)