Skip to content

Commit 2eadfe4

Browse files
committed
auto merge of #15511 : brson/rust/extract-rustc-back, r=alexcrichton
This was my weekend project, to start breaking up rustc. It first pulls out LLVM into `rustc_llvm`, then parts of `rustc::back` and `rustc::util` to `rustc_back`. The immediate intent is just to reduce the size of rustc, to reduce memory pressure when building rustc, but this is also a good starting point for further refactoring. The `rustc_back` crate is definitely misnamed (`rustc::back` was never a very cohesive module anyway) - it's mostly just somewhere to stuff parts of rustc that don't have many deps. Right now it's main dep is `syntax`; it has no dep on `rustc_llvm`. Some next steps might be to split `rustc_back` into `rustc_util` (with no `syntax` dep), and `rustc_syntax_util` (with a syntax dep); move the rest of `rustc::util` into `rustc_syntax_util`; move all of `rustc::front` to a new crate, `rustc_front`. At that point the refactoring necessary to keep extracting crates will get harder.
2 parents b1ae09e + a008fc8 commit 2eadfe4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2718
-2459
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ src/etc/dl
8686
.settings/
8787
/build
8888
i686-pc-mingw32/
89-
src/librustc/lib/llvmdeps.rs
89+
src/librustc_llvm/llvmdeps.rs
9090
*.pot

mk/crates.mk

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
5353
uuid serialize sync getopts collections num test time rand \
5454
url log regex graphviz core rlibc alloc debug rustrt \
5555
unicode
56-
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros
56+
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros \
57+
rustc_llvm rustc_back
5758
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
5859
TOOLS := compiletest rustdoc rustc
5960

@@ -70,8 +71,10 @@ DEPS_green := std native:context_switch
7071
DEPS_rustuv := std native:uv native:uv_support
7172
DEPS_native := std
7273
DEPS_syntax := std term serialize log fmt_macros debug
73-
DEPS_rustc := syntax native:rustllvm flate arena serialize getopts \
74-
time log graphviz debug
74+
DEPS_rustc := syntax flate arena serialize getopts \
75+
time log graphviz debug rustc_llvm rustc_back
76+
DEPS_rustc_llvm := native:rustllvm libc std
77+
DEPS_rustc_back := std syntax rustc_llvm flate log libc
7578
DEPS_rustdoc := rustc native:hoedown serialize getopts \
7679
test time debug
7780
DEPS_flate := std native:miniz

mk/llvm.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ $(foreach host,$(CFG_HOST), \
5757
$(foreach host,$(CFG_HOST), \
5858
$(eval LLVM_CONFIGS := $(LLVM_CONFIGS) $(LLVM_CONFIG_$(host))))
5959

60-
$(S)src/librustc/lib/llvmdeps.rs: \
60+
$(S)src/librustc_llvm/llvmdeps.rs: \
6161
$(LLVM_CONFIGS) \
6262
$(S)src/etc/mklldeps.py \
6363
$(MKFILE_DEPS)

mk/target.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ SNAPSHOT_RUSTC_POST_CLEANUP=$(HBIN0_H_$(CFG_BUILD))/rustc$(X_$(CFG_BUILD))
134134

135135
define TARGET_HOST_RULES
136136

137-
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc: $(S)src/librustc/lib/llvmdeps.rs
137+
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.rustc_llvm: $(S)src/librustc_llvm/llvmdeps.rs
138138

139139
$$(TBIN$(1)_T_$(2)_H_$(3))/:
140140
mkdir -p $$@

src/librustc/back/link.rs

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use back::archive::{Archive, METADATA_FILENAME};
12-
use back::rpath;
13-
use back::svh::Svh;
11+
use super::archive::{Archive, ArchiveConfig, METADATA_FILENAME};
12+
use super::rpath;
13+
use super::rpath::RPathConfig;
14+
use super::svh::Svh;
1415
use driver::driver::{CrateTranslation, OutputFilenames, Input, FileInput};
1516
use driver::config::NoDebugInfo;
1617
use driver::session::Session;
1718
use driver::config;
18-
use lib::llvm::llvm;
19-
use lib::llvm::ModuleRef;
20-
use lib;
19+
use llvm;
20+
use llvm::ModuleRef;
2121
use metadata::common::LinkMeta;
2222
use metadata::{encoder, cstore, filesearch, csearch, loader, creader};
2323
use middle::trans::context::CrateContext;
@@ -29,6 +29,7 @@ use util::sha2::{Digest, Sha256};
2929

3030
use std::c_str::{ToCStr, CString};
3131
use std::char;
32+
use std::collections::HashSet;
3233
use std::io::{fs, TempDir, Command};
3334
use std::io;
3435
use std::ptr;
@@ -70,11 +71,11 @@ pub fn llvm_err(sess: &Session, msg: String) -> ! {
7071

7172
pub fn write_output_file(
7273
sess: &Session,
73-
target: lib::llvm::TargetMachineRef,
74-
pm: lib::llvm::PassManagerRef,
74+
target: llvm::TargetMachineRef,
75+
pm: llvm::PassManagerRef,
7576
m: ModuleRef,
7677
output: &Path,
77-
file_type: lib::llvm::FileType) {
78+
file_type: llvm::FileType) {
7879
unsafe {
7980
output.with_c_str(|output| {
8081
let result = llvm::LLVMRustWriteOutputFile(
@@ -88,18 +89,17 @@ pub fn write_output_file(
8889

8990
pub mod write {
9091

91-
use back::lto;
92-
use back::link::{write_output_file, OutputType};
93-
use back::link::{OutputTypeAssembly, OutputTypeBitcode};
94-
use back::link::{OutputTypeExe, OutputTypeLlvmAssembly};
95-
use back::link::{OutputTypeObject};
92+
use super::super::lto;
93+
use super::{write_output_file, OutputType};
94+
use super::{OutputTypeAssembly, OutputTypeBitcode};
95+
use super::{OutputTypeExe, OutputTypeLlvmAssembly};
96+
use super::{OutputTypeObject};
9697
use driver::driver::{CrateTranslation, OutputFilenames};
9798
use driver::config::NoDebugInfo;
9899
use driver::session::Session;
99100
use driver::config;
100-
use lib::llvm::llvm;
101-
use lib::llvm::{ModuleRef, TargetMachineRef, PassManagerRef};
102-
use lib;
101+
use llvm;
102+
use llvm::{ModuleRef, TargetMachineRef, PassManagerRef};
103103
use util::common::time;
104104
use syntax::abi;
105105

@@ -152,10 +152,10 @@ pub mod write {
152152
}
153153

154154
let opt_level = match sess.opts.optimize {
155-
config::No => lib::llvm::CodeGenLevelNone,
156-
config::Less => lib::llvm::CodeGenLevelLess,
157-
config::Default => lib::llvm::CodeGenLevelDefault,
158-
config::Aggressive => lib::llvm::CodeGenLevelAggressive,
155+
config::No => llvm::CodeGenLevelNone,
156+
config::Less => llvm::CodeGenLevelLess,
157+
config::Default => llvm::CodeGenLevelDefault,
158+
config::Aggressive => llvm::CodeGenLevelAggressive,
159159
};
160160
let use_softfp = sess.opts.cg.soft_float;
161161

@@ -172,10 +172,10 @@ pub mod write {
172172
let fdata_sections = ffunction_sections;
173173

174174
let reloc_model = match sess.opts.cg.relocation_model.as_slice() {
175-
"pic" => lib::llvm::RelocPIC,
176-
"static" => lib::llvm::RelocStatic,
177-
"default" => lib::llvm::RelocDefault,
178-
"dynamic-no-pic" => lib::llvm::RelocDynamicNoPic,
175+
"pic" => llvm::RelocPIC,
176+
"static" => llvm::RelocStatic,
177+
"default" => llvm::RelocDefault,
178+
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
179179
_ => {
180180
sess.err(format!("{} is not a valid relocation mode",
181181
sess.opts
@@ -195,7 +195,7 @@ pub mod write {
195195
target_feature(sess).with_c_str(|features| {
196196
llvm::LLVMRustCreateTargetMachine(
197197
t, cpu, features,
198-
lib::llvm::CodeModelDefault,
198+
llvm::CodeModelDefault,
199199
reloc_model,
200200
opt_level,
201201
true /* EnableSegstk */,
@@ -320,7 +320,7 @@ pub mod write {
320320
};
321321
with_codegen(tm, llmod, trans.no_builtins, |cpm| {
322322
write_output_file(sess, tm, cpm, llmod, &path,
323-
lib::llvm::AssemblyFile);
323+
llvm::AssemblyFile);
324324
});
325325
}
326326
OutputTypeObject => {
@@ -338,7 +338,7 @@ pub mod write {
338338
Some(ref path) => {
339339
with_codegen(tm, llmod, trans.no_builtins, |cpm| {
340340
write_output_file(sess, tm, cpm, llmod, path,
341-
lib::llvm::ObjectFile);
341+
llvm::ObjectFile);
342342
});
343343
}
344344
None => {}
@@ -350,7 +350,7 @@ pub mod write {
350350
.with_extension("metadata.o");
351351
write_output_file(sess, tm, cpm,
352352
trans.metadata_module, &out,
353-
lib::llvm::ObjectFile);
353+
llvm::ObjectFile);
354354
})
355355
}
356356
});
@@ -455,29 +455,29 @@ pub mod write {
455455
});
456456
}
457457

458-
unsafe fn populate_llvm_passes(fpm: lib::llvm::PassManagerRef,
459-
mpm: lib::llvm::PassManagerRef,
458+
unsafe fn populate_llvm_passes(fpm: llvm::PassManagerRef,
459+
mpm: llvm::PassManagerRef,
460460
llmod: ModuleRef,
461-
opt: lib::llvm::CodeGenOptLevel,
461+
opt: llvm::CodeGenOptLevel,
462462
no_builtins: bool) {
463463
// Create the PassManagerBuilder for LLVM. We configure it with
464464
// reasonable defaults and prepare it to actually populate the pass
465465
// manager.
466466
let builder = llvm::LLVMPassManagerBuilderCreate();
467467
match opt {
468-
lib::llvm::CodeGenLevelNone => {
468+
llvm::CodeGenLevelNone => {
469469
// Don't add lifetime intrinsics at O0
470470
llvm::LLVMRustAddAlwaysInlinePass(builder, false);
471471
}
472-
lib::llvm::CodeGenLevelLess => {
472+
llvm::CodeGenLevelLess => {
473473
llvm::LLVMRustAddAlwaysInlinePass(builder, true);
474474
}
475475
// numeric values copied from clang
476-
lib::llvm::CodeGenLevelDefault => {
476+
llvm::CodeGenLevelDefault => {
477477
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder,
478478
225);
479479
}
480-
lib::llvm::CodeGenLevelAggressive => {
480+
llvm::CodeGenLevelAggressive => {
481481
llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder,
482482
275);
483483
}
@@ -611,7 +611,7 @@ pub fn build_link_meta(sess: &Session, krate: &ast::Crate,
611611
name: String) -> LinkMeta {
612612
let r = LinkMeta {
613613
crate_name: name,
614-
crate_hash: Svh::calculate(sess, krate),
614+
crate_hash: Svh::calculate(&sess.opts.cg.metadata, krate),
615615
};
616616
info!("{}", r);
617617
return r;
@@ -963,6 +963,17 @@ fn link_binary_output(sess: &Session,
963963
out_filename
964964
}
965965

966+
fn archive_search_paths(sess: &Session) -> Vec<Path> {
967+
let mut rustpath = filesearch::rust_path();
968+
rustpath.push(sess.target_filesearch().get_lib_path());
969+
// FIXME: Addl lib search paths are an unordered HashSet?
970+
// Shouldn't this search be done in some order?
971+
let addl_lib_paths: HashSet<Path> = sess.opts.addl_lib_search_paths.borrow().clone();
972+
let mut search: Vec<Path> = addl_lib_paths.move_iter().collect();
973+
search.push_all(rustpath.as_slice());
974+
return search;
975+
}
976+
966977
// Create an 'rlib'
967978
//
968979
// An rlib in its current incarnation is essentially a renamed .a file. The
@@ -973,7 +984,15 @@ fn link_rlib<'a>(sess: &'a Session,
973984
trans: Option<&CrateTranslation>, // None == no metadata/bytecode
974985
obj_filename: &Path,
975986
out_filename: &Path) -> Archive<'a> {
976-
let mut a = Archive::create(sess, out_filename, obj_filename);
987+
let handler = &sess.diagnostic().handler;
988+
let config = ArchiveConfig {
989+
handler: handler,
990+
dst: out_filename.clone(),
991+
lib_search_paths: archive_search_paths(sess),
992+
os: sess.targ_cfg.os,
993+
maybe_ar_prog: sess.opts.cg.ar.clone()
994+
};
995+
let mut a = Archive::create(config, obj_filename);
977996

978997
for &(ref l, kind) in sess.cstore.get_used_libraries().borrow().iter() {
979998
match kind {
@@ -1387,7 +1406,24 @@ fn link_args(cmd: &mut Command,
13871406
// where extern libraries might live, based on the
13881407
// addl_lib_search_paths
13891408
if sess.opts.cg.rpath {
1390-
cmd.args(rpath::get_rpath_flags(sess, out_filename).as_slice());
1409+
let sysroot = sess.sysroot();
1410+
let target_triple = sess.opts.target_triple.as_slice();
1411+
let get_install_prefix_lib_path = || {
1412+
let install_prefix = option_env!("CFG_PREFIX").expect("CFG_PREFIX");
1413+
let tlib = filesearch::relative_target_lib_path(sysroot, target_triple);
1414+
let mut path = Path::new(install_prefix);
1415+
path.push(&tlib);
1416+
1417+
path
1418+
};
1419+
let rpath_config = RPathConfig {
1420+
os: sess.targ_cfg.os,
1421+
used_crates: sess.cstore.get_used_crates(cstore::RequireDynamic),
1422+
out_filename: out_filename.clone(),
1423+
get_install_prefix_lib_path: get_install_prefix_lib_path,
1424+
realpath: ::util::fs::realpath
1425+
};
1426+
cmd.args(rpath::get_rpath_flags(rpath_config).as_slice());
13911427
}
13921428

13931429
// compiler-rt contains implementations of low-level LLVM helpers. This is
@@ -1545,7 +1581,15 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15451581
sess.abort_if_errors();
15461582
}
15471583
}
1548-
let mut archive = Archive::open(sess, dst.clone());
1584+
let handler = &sess.diagnostic().handler;
1585+
let config = ArchiveConfig {
1586+
handler: handler,
1587+
dst: dst.clone(),
1588+
lib_search_paths: archive_search_paths(sess),
1589+
os: sess.targ_cfg.os,
1590+
maybe_ar_prog: sess.opts.cg.ar.clone()
1591+
};
1592+
let mut archive = Archive::open(config);
15491593
archive.remove_file(format!("{}.o", name).as_slice());
15501594
let files = archive.files();
15511595
if files.iter().any(|s| s.as_slice().ends_with(".o")) {

src/librustc/back/lto.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use back::archive::ArchiveRO;
12-
use back::link;
11+
use super::link;
1312
use driver::session;
1413
use driver::config;
15-
use lib::llvm::{ModuleRef, TargetMachineRef, llvm, True, False};
14+
use llvm;
15+
use llvm::archive_ro::ArchiveRO;
16+
use llvm::{ModuleRef, TargetMachineRef, True, False};
1617
use metadata::cstore;
1718
use util::common::time;
1819

src/librustc/driver/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ use syntax::parse::token::InternedString;
3333
use std::collections::{HashSet, HashMap};
3434
use getopts::{optopt, optmulti, optflag, optflagopt};
3535
use getopts;
36-
use lib::llvm::llvm;
3736
use std::cell::{RefCell};
3837
use std::fmt;
3938

39+
use llvm;
4040

4141
pub struct Config {
4242
pub os: abi::Os,

src/librustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use driver::{config, PpMode};
1515
use driver::{PpmFlowGraph, PpmExpanded, PpmExpandedIdentified, PpmTyped};
1616
use driver::{PpmIdentified};
1717
use front;
18-
use lib::llvm::{ContextRef, ModuleRef};
1918
use lint;
19+
use llvm::{ContextRef, ModuleRef};
2020
use metadata::common::LinkMeta;
2121
use metadata::creader;
2222
use middle::cfg;

src/librustc/driver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
292292
}
293293

294294
if cg_flags.contains(&"passes=list".to_string()) {
295-
unsafe { ::lib::llvm::llvm::LLVMRustPrintPasses(); }
295+
unsafe { ::llvm::LLVMRustPrintPasses(); }
296296
return None;
297297
}
298298

0 commit comments

Comments
 (0)