From 41ed455db8cbde3cfff0d72e0ae383c20721b27a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 1 Jul 2014 17:07:06 -0700 Subject: [PATCH] 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] --- src/librustc/back/link.rs | 10 ++++++++++ src/librustc/driver/config.rs | 21 ++++++++++++++++---- src/librustc/driver/mod.rs | 5 +++++ src/test/run-make/crate-data-smoke/Makefile | 12 +++++------ src/test/run-make/manual-crate-name/Makefile | 5 +++++ src/test/run-make/manual-crate-name/bar.rs | 11 ++++++++++ 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 src/test/run-make/manual-crate-name/Makefile create mode 100644 src/test/run-make/manual-crate-name/bar.rs diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 497fcab44e1..c306a184ca1 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -556,6 +556,16 @@ pub fn find_crate_name(sess: Option<&Session>, s }; + match sess { + Some(sess) => { + match sess.opts.crate_name { + Some(ref s) => return validate(s.clone(), None), + None => {} + } + } + None => {} + } + let crate_name = attrs.iter().find(|at| at.check_name("crate_name")) .and_then(|at| at.value_str().map(|s| (at, s))); match crate_name { diff --git a/src/librustc/driver/config.rs b/src/librustc/driver/config.rs index 72875735864..e91dfd9587b 100644 --- a/src/librustc/driver/config.rs +++ b/src/librustc/driver/config.rs @@ -11,7 +11,7 @@ //! Contains infrastructure for configuring the compiler, including parsing //! command line options. -use driver::early_error; +use driver::{early_error, early_warn}; use driver::driver; use driver::session::Session; @@ -96,6 +96,7 @@ pub struct Options { pub cg: CodegenOptions, pub color: ColorConfig, pub externs: HashMap>, + pub crate_name: Option, } /// Some reasonable defaults @@ -122,6 +123,7 @@ pub fn basic_options() -> Options { cg: basic_codegen_options(), color: Auto, externs: HashMap::new(), + crate_name: None, } } @@ -511,9 +513,12 @@ pub fn optgroups() -> Vec { "[bin|lib|rlib|dylib|staticlib]"), optmulti("", "emit", "Comma separated list of types of output for the compiler to emit", "[asm|bc|ir|obj|link]"), - optflag("", "crate-name", "Output the crate name and exit"), - optflag("", "crate-file-name", "Output the file(s) that would be written if compilation \ + optopt("", "crate-name", "Specify the name of the crate being built", + "NAME"), + optflag("", "print-crate-name", "Output the crate name and exit"), + optflag("", "print-file-name", "Output the file(s) that would be written if compilation \ continued and exit"), + optflag("", "crate-file-name", "deprecated in favor of --print-file-name"), optflag("g", "", "Equivalent to --debuginfo=2"), optopt("", "debuginfo", "Emit DWARF debug info to the objects created: 0 = no debug info, @@ -716,8 +721,13 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { matches.opt_str("dep-info") .map(|p| Path::new(p))); - let print_metas = (matches.opt_present("crate-name"), + let print_metas = (matches.opt_present("print-crate-name"), + matches.opt_present("print-file-name") || matches.opt_present("crate-file-name")); + if matches.opt_present("crate-file-name") { + early_warn("the --crate-file-name argument has been renamed to \ + --print-file-name"); + } let cg = build_codegen_options(matches); 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 { locs.push(location.to_string()); } + let crate_name = matches.opt_str("crate-name"); + Options { crate_types: crate_types, gc: gc, @@ -771,6 +783,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { cg: cg, color: color, externs: externs, + crate_name: crate_name, } } diff --git a/src/librustc/driver/mod.rs b/src/librustc/driver/mod.rs index dc6b8fa5af3..cbbc8ebd8c8 100644 --- a/src/librustc/driver/mod.rs +++ b/src/librustc/driver/mod.rs @@ -389,6 +389,11 @@ pub fn early_error(msg: &str) -> ! { fail!(diagnostic::FatalError); } +pub fn early_warn(msg: &str) { + let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto); + emitter.emit(None, msg, diagnostic::Warning); +} + pub fn list_metadata(sess: &Session, path: &Path, out: &mut io::Writer) -> io::IoResult<()> { metadata::loader::list_file_metadata(sess.targ_cfg.os, path, out) diff --git a/src/test/run-make/crate-data-smoke/Makefile b/src/test/run-make/crate-data-smoke/Makefile index a709320c0f9..23d155fe23d 100644 --- a/src/test/run-make/crate-data-smoke/Makefile +++ b/src/test/run-make/crate-data-smoke/Makefile @@ -1,9 +1,9 @@ -include ../tools.mk all: - [ `$(RUSTC) --crate-name crate.rs` = "foo" ] - [ `$(RUSTC) --crate-file-name crate.rs` = "foo" ] - [ `$(RUSTC) --crate-file-name --crate-type=lib --test crate.rs` = "foo" ] - [ `$(RUSTC) --crate-file-name --test lib.rs` = "mylib" ] - $(RUSTC) --crate-file-name lib.rs - $(RUSTC) --crate-file-name rlib.rs + [ `$(RUSTC) --print-crate-name crate.rs` = "foo" ] + [ `$(RUSTC) --print-file-name crate.rs` = "foo" ] + [ `$(RUSTC) --print-file-name --crate-type=lib --test crate.rs` = "foo" ] + [ `$(RUSTC) --print-file-name --test lib.rs` = "mylib" ] + $(RUSTC) --print-file-name lib.rs + $(RUSTC) --print-file-name rlib.rs diff --git a/src/test/run-make/manual-crate-name/Makefile b/src/test/run-make/manual-crate-name/Makefile new file mode 100644 index 00000000000..1d1419997a2 --- /dev/null +++ b/src/test/run-make/manual-crate-name/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTC) --crate-name foo bar.rs + rm $(TMPDIR)/libfoo.rlib diff --git a/src/test/run-make/manual-crate-name/bar.rs b/src/test/run-make/manual-crate-name/bar.rs new file mode 100644 index 00000000000..04d3ae67207 --- /dev/null +++ b/src/test/run-make/manual-crate-name/bar.rs @@ -0,0 +1,11 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_type = "rlib"]