Skip to content

Commit b0977b1

Browse files
committed
auto merge of #13905 : alexcrichton/rust/issue-13337, r=thestinger
This has long since not been too relevant since the introduction of many crate type outputs. This commit removes the flag entirely, adjusting all logic to do the most reasonable thing when building both a library and an executable. Closes #13337
2 parents 002f791 + 825f6ac commit b0977b1

File tree

10 files changed

+73
-68
lines changed

10 files changed

+73
-68
lines changed

src/librustc/driver/driver.rs

-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ pub fn phase_2_configure_and_expand(sess: &Session,
210210
-> (ast::Crate, syntax::ast_map::Map) {
211211
let time_passes = sess.time_passes();
212212

213-
sess.building_library.set(session::building_library(&sess.opts, &krate));
214213
*sess.crate_types.borrow_mut() = session::collect_crate_types(sess, krate.attrs.as_slice());
215214

216215
time(time_passes, "gated feature checking", (), |_|
@@ -1046,7 +1045,6 @@ pub fn build_session_(sopts: session::Options,
10461045
entry_type: Cell::new(None),
10471046
macro_registrar_fn: Cell::new(None),
10481047
default_sysroot: default_sysroot,
1049-
building_library: Cell::new(false),
10501048
local_crate_source_file: local_crate_source_file,
10511049
working_dir: os::getcwd(),
10521050
lints: RefCell::new(NodeMap::new()),

src/librustc/driver/session.rs

-22
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use syntax::codemap::Span;
2525
use syntax::diagnostic;
2626
use syntax::parse::ParseSess;
2727
use syntax::{abi, ast, codemap};
28-
use syntax;
2928

3029
use std::cell::{Cell, RefCell};
3130
use collections::HashSet;
@@ -185,7 +184,6 @@ pub struct Session {
185184
pub entry_type: Cell<Option<EntryFnType>>,
186185
pub macro_registrar_fn: Cell<Option<ast::NodeId>>,
187186
pub default_sysroot: Option<Path>,
188-
pub building_library: Cell<bool>,
189187
// The name of the root source file of the crate, in the local file system. The path is always
190188
// expected to be absolute. `None` means that there is no source file.
191189
pub local_crate_source_file: Option<Path>,
@@ -477,26 +475,6 @@ pub fn expect<T:Clone>(sess: &Session, opt: Option<T>, msg: || -> ~str) -> T {
477475
diagnostic::expect(sess.diagnostic(), opt, msg)
478476
}
479477

480-
pub fn building_library(options: &Options, krate: &ast::Crate) -> bool {
481-
if options.test { return false }
482-
for output in options.crate_types.iter() {
483-
match *output {
484-
CrateTypeExecutable => {}
485-
CrateTypeStaticlib | CrateTypeDylib | CrateTypeRlib => return true
486-
}
487-
}
488-
match syntax::attr::first_attr_value_str_by_name(krate.attrs.as_slice(),
489-
"crate_type") {
490-
Some(s) => {
491-
s.equiv(&("lib")) ||
492-
s.equiv(&("rlib")) ||
493-
s.equiv(&("dylib")) ||
494-
s.equiv(&("staticlib"))
495-
}
496-
_ => false
497-
}
498-
}
499-
500478
pub fn default_lib_output() -> CrateType {
501479
CrateTypeRlib
502480
}

src/librustc/front/std_inject.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
11+
use driver::session;
1212
use driver::session::Session;
1313

1414
use syntax::ast;
@@ -86,7 +86,10 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
8686
span: DUMMY_SP
8787
});
8888

89-
if use_start(&krate) && !self.sess.building_library.get() {
89+
let any_exe = self.sess.crate_types.borrow().iter().any(|ty| {
90+
*ty == session::CrateTypeExecutable
91+
});
92+
if use_start(&krate) && any_exe {
9093
vis.push(ast::ViewItem {
9194
node: ast::ViewItemExternCrate(token::str_to_ident("native"),
9295
with_version("native"),

src/librustc/front/test.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,23 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
125125
// Remove any #[main] from the AST so it doesn't clash with
126126
// the one we're going to add. Only if compiling an executable.
127127

128-
fn nomain(cx: &TestCtxt, item: @ast::Item) -> @ast::Item {
129-
if !cx.sess.building_library.get() {
130-
@ast::Item {
131-
attrs: item.attrs.iter().filter_map(|attr| {
132-
if !attr.name().equiv(&("main")) {
133-
Some(*attr)
134-
} else {
135-
None
136-
}
137-
}).collect(),
138-
.. (*item).clone()
139-
}
140-
} else {
141-
item
128+
fn nomain(item: @ast::Item) -> @ast::Item {
129+
@ast::Item {
130+
attrs: item.attrs.iter().filter_map(|attr| {
131+
if !attr.name().equiv(&("main")) {
132+
Some(*attr)
133+
} else {
134+
None
135+
}
136+
}).collect(),
137+
.. (*item).clone()
142138
}
143139
}
144140

145141
let mod_nomain = ast::Mod {
146142
inner: m.inner,
147143
view_items: m.view_items.clone(),
148-
items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(),
144+
items: m.items.iter().map(|i| nomain(*i)).collect(),
149145
};
150146

151147
fold::noop_fold_mod(&mod_nomain, self)

src/librustc/middle/entry.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ impl<'a> Visitor<()> for EntryContext<'a> {
4848
}
4949

5050
pub fn find_entry_point(session: &Session, krate: &Crate, ast_map: &ast_map::Map) {
51-
if session.building_library.get() {
51+
let any_exe = session.crate_types.borrow().iter().any(|ty| {
52+
*ty == session::CrateTypeExecutable
53+
});
54+
if !any_exe {
5255
// No need to find a main function
53-
return;
56+
return
5457
}
5558

5659
// If the user wants no main function at all, then stop here.
@@ -132,18 +135,16 @@ fn configure_main(this: &mut EntryContext) {
132135
*this.session.entry_fn.borrow_mut() = this.main_fn;
133136
this.session.entry_type.set(Some(session::EntryMain));
134137
} else {
135-
if !this.session.building_library.get() {
136-
// No main function
137-
this.session.err("main function not found");
138-
if !this.non_main_fns.is_empty() {
139-
// There were some functions named 'main' though. Try to give the user a hint.
140-
this.session.note("the main function must be defined at the crate level \
141-
but you have one or more functions named 'main' that are not \
142-
defined at the crate level. Either move the definition or \
143-
attach the `#[main]` attribute to override this behavior.");
144-
for &(_, span) in this.non_main_fns.iter() {
145-
this.session.span_note(span, "here is a function named 'main'");
146-
}
138+
// No main function
139+
this.session.err("main function not found");
140+
if !this.non_main_fns.is_empty() {
141+
// There were some functions named 'main' though. Try to give the user a hint.
142+
this.session.note("the main function must be defined at the crate level \
143+
but you have one or more functions named 'main' that are not \
144+
defined at the crate level. Either move the definition or \
145+
attach the `#[main]` attribute to override this behavior.");
146+
for &(_, span) in this.non_main_fns.iter() {
147+
this.session.span_note(span, "here is a function named 'main'");
147148
}
148149
this.session.abort_if_errors();
149150
}

src/librustc/middle/reachable.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// makes all other generics or inline functions that it references
1616
// reachable as well.
1717

18+
use driver::session;
1819
use middle::ty;
1920
use middle::typeck;
2021
use middle::privacy;
@@ -89,6 +90,8 @@ struct ReachableContext<'a> {
8990
// A worklist of item IDs. Each item ID in this worklist will be inlined
9091
// and will be scanned for further references.
9192
worklist: Vec<ast::NodeId>,
93+
// Whether any output of this compilation is a library
94+
any_library: bool,
9295
}
9396

9497
impl<'a> Visitor<()> for ReachableContext<'a> {
@@ -157,10 +160,14 @@ impl<'a> Visitor<()> for ReachableContext<'a> {
157160
impl<'a> ReachableContext<'a> {
158161
// Creates a new reachability computation context.
159162
fn new(tcx: &'a ty::ctxt) -> ReachableContext<'a> {
163+
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
164+
*ty != session::CrateTypeExecutable
165+
});
160166
ReachableContext {
161167
tcx: tcx,
162168
reachable_symbols: NodeSet::new(),
163169
worklist: Vec::new(),
170+
any_library: any_library,
164171
}
165172
}
166173

@@ -234,7 +241,7 @@ impl<'a> ReachableContext<'a> {
234241

235242
fn propagate_node(&mut self, node: &ast_map::Node,
236243
search_item: ast::NodeId) {
237-
if !self.tcx.sess.building_library.get() {
244+
if !self.any_library {
238245
// If we are building an executable, then there's no need to flag
239246
// anything as external except for `extern fn` types. These
240247
// functions may still participate in some form of native interface,

src/librustc/middle/trans/base.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ fn finish_register_fn(ccx: &CrateContext, sp: Span, sym: ~str, node_id: ast::Nod
17011701
lib::llvm::SetLinkage(llfn, lib::llvm::InternalLinkage);
17021702
}
17031703

1704-
if is_entry_fn(ccx.sess(), node_id) && !ccx.sess().building_library.get() {
1704+
if is_entry_fn(ccx.sess(), node_id) {
17051705
create_entry_wrapper(ccx, sp, llfn);
17061706
}
17071707
}
@@ -2100,7 +2100,10 @@ pub fn crate_ctxt_to_encode_parms<'r>(cx: &'r CrateContext, ie: encoder::EncodeI
21002100
pub fn write_metadata(cx: &CrateContext, krate: &ast::Crate) -> Vec<u8> {
21012101
use flate;
21022102

2103-
if !cx.sess().building_library.get() {
2103+
let any_library = cx.sess().crate_types.borrow().iter().any(|ty| {
2104+
*ty != session::CrateTypeExecutable
2105+
});
2106+
if !any_library {
21042107
return Vec::new()
21052108
}
21062109

src/librustc/middle/typeck/mod.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,14 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
409409

410410
fn check_for_entry_fn(ccx: &CrateCtxt) {
411411
let tcx = ccx.tcx;
412-
if !tcx.sess.building_library.get() {
413-
match *tcx.sess.entry_fn.borrow() {
414-
Some((id, sp)) => match tcx.sess.entry_type.get() {
415-
Some(session::EntryMain) => check_main_fn_ty(ccx, id, sp),
416-
Some(session::EntryStart) => check_start_fn_ty(ccx, id, sp),
417-
Some(session::EntryNone) => {}
418-
None => tcx.sess.bug("entry function without a type")
419-
},
420-
None => {}
421-
}
412+
match *tcx.sess.entry_fn.borrow() {
413+
Some((id, sp)) => match tcx.sess.entry_type.get() {
414+
Some(session::EntryMain) => check_main_fn_ty(ccx, id, sp),
415+
Some(session::EntryStart) => check_start_fn_ty(ccx, id, sp),
416+
Some(session::EntryNone) => {}
417+
None => tcx.sess.bug("entry function without a type")
418+
},
419+
None => {}
422420
}
423421
}
424422

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) foo.rs
5+
$(call RUN,foo)
6+
rm $(TMPDIR)/$(call DYLIB_GLOB,foo)
7+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 = "dylib"]
12+
#![crate_type = "bin"]
13+
14+
fn main() {}

0 commit comments

Comments
 (0)