Skip to content

Commit 189584e

Browse files
committed
auto merge of rust-lang#13489 : JustAPerson/rust/crate-file-name, r=alexcrichton
Before, the `--crate-file-name` flag only checked crate attributes for possible crate types. Now, if any type is specified by one or more `--crate-type` flags, only the filenames for those types will be emitted, and any types specified by crate attributes will be ignored.
2 parents 8a4ffbf + 0162f8e commit 189584e

File tree

4 files changed

+74
-35
lines changed

4 files changed

+74
-35
lines changed

src/doc/rust.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -3920,7 +3920,9 @@ link Rust crates together, and more information about native libraries can be
39203920
found in the [ffi tutorial][ffi].
39213921

39223922
In one session of compilation, the compiler can generate multiple artifacts
3923-
through the usage of command line flags and the `crate_type` attribute.
3923+
through the usage of either command line flags or the `crate_type` attribute.
3924+
If one or more command line flag is specified, all `crate_type` attributes will
3925+
be ignored in favor of only building the artifacts specified by command line.
39243926

39253927
* `--crate-type=bin`, `#[crate_type = "bin"]` - A runnable executable will be
39263928
produced. This requires that there is a `main` function in the crate which
@@ -3963,7 +3965,10 @@ through the usage of command line flags and the `crate_type` attribute.
39633965

39643966
Note that these outputs are stackable in the sense that if multiple are
39653967
specified, then the compiler will produce each form of output at once without
3966-
having to recompile.
3968+
having to recompile. However, this only applies for outputs specified by the same
3969+
method. If only `crate_type` attributes are specified, then they will all be
3970+
built, but if one or more `--crate-type` command line flag is specified,
3971+
then only those outputs will be built.
39673972

39683973
With all these different kinds of outputs, if crate A depends on crate B, then
39693974
the compiler could find B in various different forms throughout the system. The

src/librustc/driver/session.rs

+41-33
Original file line numberDiff line numberDiff line change
@@ -498,43 +498,51 @@ pub fn collect_crate_types(session: &Session,
498498
if session.opts.test {
499499
return vec!(CrateTypeExecutable)
500500
}
501+
502+
// Only check command line flags if present. If no types are specified by
503+
// command line, then reuse the empty `base` Vec to hold the types that
504+
// will be found in crate attributes.
501505
let mut base = session.opts.crate_types.clone();
502-
let iter = attrs.iter().filter_map(|a| {
503-
if a.name().equiv(&("crate_type")) {
504-
match a.value_str() {
505-
Some(ref n) if n.equiv(&("rlib")) => Some(CrateTypeRlib),
506-
Some(ref n) if n.equiv(&("dylib")) => Some(CrateTypeDylib),
507-
Some(ref n) if n.equiv(&("lib")) => {
508-
Some(default_lib_output())
509-
}
510-
Some(ref n) if n.equiv(&("staticlib")) => {
511-
Some(CrateTypeStaticlib)
512-
}
513-
Some(ref n) if n.equiv(&("bin")) => Some(CrateTypeExecutable),
514-
Some(_) => {
515-
session.add_lint(lint::UnknownCrateType,
516-
ast::CRATE_NODE_ID,
517-
a.span,
518-
~"invalid `crate_type` value");
519-
None
520-
}
521-
_ => {
522-
session.add_lint(lint::UnknownCrateType, ast::CRATE_NODE_ID,
523-
a.span, ~"`crate_type` requires a value");
524-
None
506+
if base.len() > 0 {
507+
return base
508+
} else {
509+
let iter = attrs.iter().filter_map(|a| {
510+
if a.name().equiv(&("crate_type")) {
511+
match a.value_str() {
512+
Some(ref n) if n.equiv(&("rlib")) => Some(CrateTypeRlib),
513+
Some(ref n) if n.equiv(&("dylib")) => Some(CrateTypeDylib),
514+
Some(ref n) if n.equiv(&("lib")) => {
515+
Some(default_lib_output())
516+
}
517+
Some(ref n) if n.equiv(&("staticlib")) => {
518+
Some(CrateTypeStaticlib)
519+
}
520+
Some(ref n) if n.equiv(&("bin")) => Some(CrateTypeExecutable),
521+
Some(_) => {
522+
session.add_lint(lint::UnknownCrateType,
523+
ast::CRATE_NODE_ID,
524+
a.span,
525+
~"invalid `crate_type` value");
526+
None
527+
}
528+
_ => {
529+
session.add_lint(lint::UnknownCrateType, ast::CRATE_NODE_ID,
530+
a.span, ~"`crate_type` requires a value");
531+
None
532+
}
525533
}
534+
} else {
535+
None
526536
}
527-
} else {
528-
None
537+
});
538+
base.extend(iter);
539+
if base.len() == 0 {
540+
base.push(CrateTypeExecutable);
529541
}
530-
});
531-
base.extend(iter);
532-
if base.len() == 0 {
533-
base.push(CrateTypeExecutable);
534-
}
535-
base.as_mut_slice().sort();
536-
base.dedup();
537-
return base;
542+
base.as_mut_slice().sort();
543+
base.dedup();
544+
return base;
545+
}
538546
}
539547

540548
pub fn sess_os_to_meta_os(os: abi::Os) -> metadata::loader::Os {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-include ../tools.mk
2+
3+
# check that rustc builds all crate_type attributes
4+
# delete rlib
5+
# delete whatever dylib is made for this system
6+
# check that rustc only builds --crate-type flags, ignoring attributes
7+
# fail if an rlib was built
8+
all:
9+
$(RUSTC) test.rs
10+
rm $(TMPDIR)/libtest*.rlib
11+
rm $(TMPDIR)/libtest*
12+
$(RUSTC) --crate-type dylib test.rs
13+
rm $(TMPDIR)/libtest*.rlib && exit 1 || exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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"]
12+
#![crate_type = "dylib"]
13+

0 commit comments

Comments
 (0)