Skip to content

Commit 37ca367

Browse files
committed
Reorganise driver code.
The goal of this refactoring is to make the rustc driver code easier to understand and use. Since this is as close to an API as we have, I think it is important that it is nice. On getting stuck in, I found that there wasn't as much to change as I'd hoped to make the stage... fns easier to use by tools. This patch only moves code around - mostly just moving code to different files, but a few extracted method refactorings too. To summarise the changes: I added driver::config which handles everything about configuring the compiler. driver::session now just defines and builds session objects. I moved driver code from librustc/lib.rs to librustc/driver/mod.rs so all the code is one place. I extracted methods to make emulating the compiler without being the compiler a little easier. Within the driver directory, I moved code around to more logically fit in the modules.
1 parent 11571cd commit 37ca367

27 files changed

+1444
-1374
lines changed

src/librustc/back/arm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use back::target_strs;
12-
use driver::session::sess_os_to_meta_os;
12+
use driver::config::cfg_os_to_meta_os;
1313
use metadata::loader::meta_section_name;
1414
use syntax::abi;
1515

@@ -22,7 +22,7 @@ pub fn get_target_strs(target_triple: ~str, target_os: abi::Os) -> target_strs::
2222
return target_strs::t {
2323
module_asm: "".to_owned(),
2424

25-
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)).to_owned(),
25+
meta_sect_name: meta_section_name(cfg_os_to_meta_os(target_os)).to_owned(),
2626

2727
data_layout: match target_os {
2828
abi::OsMacos => {

src/librustc/back/link.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use back::archive::{Archive, METADATA_FILENAME};
1212
use back::rpath;
1313
use back::svh::Svh;
1414
use driver::driver::{CrateTranslation, OutputFilenames};
15-
use driver::session::{NoDebugInfo, Session};
16-
use driver::session;
15+
use driver::config::NoDebugInfo;
16+
use driver::session::Session;
17+
use driver::config;
1718
use lib::llvm::llvm;
1819
use lib::llvm::ModuleRef;
1920
use lib;
@@ -92,8 +93,9 @@ pub mod write {
9293
use back::link::{OutputTypeExe, OutputTypeLlvmAssembly};
9394
use back::link::{OutputTypeObject};
9495
use driver::driver::{CrateTranslation, OutputFilenames};
95-
use driver::session::{NoDebugInfo, Session};
96-
use driver::session;
96+
use driver::config::NoDebugInfo;
97+
use driver::session::Session;
98+
use driver::config;
9799
use lib::llvm::llvm;
98100
use lib::llvm::{ModuleRef, TargetMachineRef, PassManagerRef};
99101
use lib;
@@ -139,10 +141,10 @@ pub mod write {
139141
}
140142

141143
let opt_level = match sess.opts.optimize {
142-
session::No => lib::llvm::CodeGenLevelNone,
143-
session::Less => lib::llvm::CodeGenLevelLess,
144-
session::Default => lib::llvm::CodeGenLevelDefault,
145-
session::Aggressive => lib::llvm::CodeGenLevelAggressive,
144+
config::No => lib::llvm::CodeGenLevelNone,
145+
config::Less => lib::llvm::CodeGenLevelLess,
146+
config::Default => lib::llvm::CodeGenLevelDefault,
147+
config::Aggressive => lib::llvm::CodeGenLevelAggressive,
146148
};
147149
let use_softfp = sess.opts.cg.soft_float;
148150

@@ -231,7 +233,7 @@ pub mod write {
231233
// emitting an rlib. Whenever an rlib is created, the bytecode is
232234
// inserted into the archive in order to allow LTO against it.
233235
if sess.opts.cg.save_temps ||
234-
(sess.crate_types.borrow().contains(&session::CrateTypeRlib) &&
236+
(sess.crate_types.borrow().contains(&config::CrateTypeRlib) &&
235237
sess.opts.output_types.contains(&OutputTypeExe)) {
236238
output.temp_path(OutputTypeBitcode).with_c_str(|buf| {
237239
llvm::LLVMWriteBitcodeToFile(llmod, buf);
@@ -378,10 +380,10 @@ pub mod write {
378380
// Copy what clang does by turning on loop vectorization at O2 and
379381
// slp vectorization at O3
380382
let vectorize_loop = !sess.opts.cg.no_vectorize_loops &&
381-
(sess.opts.optimize == session::Default ||
382-
sess.opts.optimize == session::Aggressive);
383+
(sess.opts.optimize == config::Default ||
384+
sess.opts.optimize == config::Aggressive);
383385
let vectorize_slp = !sess.opts.cg.no_vectorize_slp &&
384-
sess.opts.optimize == session::Aggressive;
386+
sess.opts.optimize == config::Aggressive;
385387

386388
let mut llvm_c_strs = Vec::new();
387389
let mut llvm_args = Vec::new();
@@ -823,14 +825,14 @@ fn is_writeable(p: &Path) -> bool {
823825
}
824826
}
825827

826-
pub fn filename_for_input(sess: &Session, crate_type: session::CrateType,
828+
pub fn filename_for_input(sess: &Session, crate_type: config::CrateType,
827829
id: &CrateId, out_filename: &Path) -> Path {
828830
let libname = output_lib_filename(id);
829831
match crate_type {
830-
session::CrateTypeRlib => {
832+
config::CrateTypeRlib => {
831833
out_filename.with_filename(format!("lib{}.rlib", libname))
832834
}
833-
session::CrateTypeDylib => {
835+
config::CrateTypeDylib => {
834836
let (prefix, suffix) = match sess.targ_cfg.os {
835837
abi::OsWin32 => (loader::WIN32_DLL_PREFIX, loader::WIN32_DLL_SUFFIX),
836838
abi::OsMacos => (loader::MACOS_DLL_PREFIX, loader::MACOS_DLL_SUFFIX),
@@ -840,16 +842,16 @@ pub fn filename_for_input(sess: &Session, crate_type: session::CrateType,
840842
};
841843
out_filename.with_filename(format!("{}{}{}", prefix, libname, suffix))
842844
}
843-
session::CrateTypeStaticlib => {
845+
config::CrateTypeStaticlib => {
844846
out_filename.with_filename(format!("lib{}.a", libname))
845847
}
846-
session::CrateTypeExecutable => out_filename.clone(),
848+
config::CrateTypeExecutable => out_filename.clone(),
847849
}
848850
}
849851

850852
fn link_binary_output(sess: &Session,
851853
trans: &CrateTranslation,
852-
crate_type: session::CrateType,
854+
crate_type: config::CrateType,
853855
outputs: &OutputFilenames,
854856
id: &CrateId) -> Path {
855857
let obj_filename = outputs.temp_path(OutputTypeObject);
@@ -877,16 +879,16 @@ fn link_binary_output(sess: &Session,
877879
}
878880

879881
match crate_type {
880-
session::CrateTypeRlib => {
882+
config::CrateTypeRlib => {
881883
link_rlib(sess, Some(trans), &obj_filename, &out_filename);
882884
}
883-
session::CrateTypeStaticlib => {
885+
config::CrateTypeStaticlib => {
884886
link_staticlib(sess, &obj_filename, &out_filename);
885887
}
886-
session::CrateTypeExecutable => {
888+
config::CrateTypeExecutable => {
887889
link_natively(sess, trans, false, &obj_filename, &out_filename);
888890
}
889-
session::CrateTypeDylib => {
891+
config::CrateTypeDylib => {
890892
link_natively(sess, trans, true, &obj_filename, &out_filename);
891893
}
892894
}
@@ -1045,7 +1047,7 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
10451047
let mut cc_args = sess.targ_cfg.target_strs.cc_args.clone();
10461048
cc_args.push_all_move(link_args(sess, dylib, tmpdir.path(), trans,
10471049
obj_filename, out_filename));
1048-
if (sess.opts.debugging_opts & session::PRINT_LINK_ARGS) != 0 {
1050+
if (sess.opts.debugging_opts & config::PRINT_LINK_ARGS) != 0 {
10491051
println!("{} link args: '{}'", cc_prog, cc_args.connect("' '"));
10501052
}
10511053

@@ -1161,8 +1163,8 @@ fn link_args(sess: &Session,
11611163

11621164
// GNU-style linkers support optimization with -O. GNU ld doesn't need a
11631165
// numeric argument, but other linkers do.
1164-
if sess.opts.optimize == session::Default ||
1165-
sess.opts.optimize == session::Aggressive {
1166+
if sess.opts.optimize == config::Default ||
1167+
sess.opts.optimize == config::Aggressive {
11661168
args.push("-Wl,-O1".to_owned());
11671169
}
11681170
} else if sess.targ_cfg.os == abi::OsMacos {
@@ -1373,9 +1375,9 @@ fn add_upstream_rust_crates(args: &mut Vec<~str>, sess: &Session,
13731375
// involves just passing the right -l flag.
13741376

13751377
let data = if dylib {
1376-
trans.crate_formats.get(&session::CrateTypeDylib)
1378+
trans.crate_formats.get(&config::CrateTypeDylib)
13771379
} else {
1378-
trans.crate_formats.get(&session::CrateTypeExecutable)
1380+
trans.crate_formats.get(&config::CrateTypeExecutable)
13791381
};
13801382

13811383
// Invoke get_used_crates to ensure that we get a topological sorting of
@@ -1403,7 +1405,7 @@ fn add_upstream_rust_crates(args: &mut Vec<~str>, sess: &Session,
14031405
}
14041406

14051407
// Converts a library file-stem into a cc -l argument
1406-
fn unlib(config: &session::Config, stem: &str) -> ~str {
1408+
fn unlib(config: &config::Config, stem: &str) -> ~str {
14071409
if stem.starts_with("lib") && config.os != abi::OsWin32 {
14081410
stem.slice(3, stem.len()).to_owned()
14091411
} else {

src/librustc/back/lto.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use back::archive::ArchiveRO;
1212
use back::link;
1313
use driver::session;
14+
use driver::config;
1415
use lib::llvm::{ModuleRef, TargetMachineRef, llvm, True, False};
1516
use metadata::cstore;
1617
use util::common::time;
@@ -29,7 +30,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
2930
// Make sure we actually can run LTO
3031
for crate_type in sess.crate_types.borrow().iter() {
3132
match *crate_type {
32-
session::CrateTypeExecutable | session::CrateTypeStaticlib => {}
33+
config::CrateTypeExecutable | config::CrateTypeStaticlib => {}
3334
_ => {
3435
sess.fatal("lto can only be run for executables and \
3536
static library outputs");

src/librustc/back/mips.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
// except according to those terms.
1010

1111
use back::target_strs;
12-
use driver::session::sess_os_to_meta_os;
12+
use driver::config::cfg_os_to_meta_os;
1313
use metadata::loader::meta_section_name;
1414
use syntax::abi;
1515

1616
pub fn get_target_strs(target_triple: ~str, target_os: abi::Os) -> target_strs::t {
1717
return target_strs::t {
1818
module_asm: "".to_owned(),
1919

20-
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)).to_owned(),
20+
meta_sect_name: meta_section_name(cfg_os_to_meta_os(target_os)).to_owned(),
2121

2222
data_layout: match target_os {
2323
abi::OsMacos => {

src/librustc/back/x86.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111

1212
use back::target_strs;
13-
use driver::session::sess_os_to_meta_os;
13+
use driver::config::cfg_os_to_meta_os;
1414
use metadata::loader::meta_section_name;
1515
use syntax::abi;
1616

1717
pub fn get_target_strs(target_triple: ~str, target_os: abi::Os) -> target_strs::t {
1818
return target_strs::t {
1919
module_asm: "".to_owned(),
2020

21-
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)).to_owned(),
21+
meta_sect_name: meta_section_name(cfg_os_to_meta_os(target_os)).to_owned(),
2222

2323
data_layout: match target_os {
2424
abi::OsMacos => {

src/librustc/back/x86_64.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111

1212
use back::target_strs;
13-
use driver::session::sess_os_to_meta_os;
13+
use driver::config::cfg_os_to_meta_os;
1414
use metadata::loader::meta_section_name;
1515
use syntax::abi;
1616

1717
pub fn get_target_strs(target_triple: ~str, target_os: abi::Os) -> target_strs::t {
1818
return target_strs::t {
1919
module_asm: "".to_owned(),
2020

21-
meta_sect_name: meta_section_name(sess_os_to_meta_os(target_os)).to_owned(),
21+
meta_sect_name: meta_section_name(cfg_os_to_meta_os(target_os)).to_owned(),
2222

2323
data_layout: match target_os {
2424
abi::OsMacos => {

0 commit comments

Comments
 (0)