Skip to content

Commit b8d5c74

Browse files
committed
It now completely compiles without LLVM!!!
1 parent b7314c7 commit b8d5c74

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

src/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ crate-type = ["dylib"]
1212
arena = { path = "../libarena" }
1313
graphviz = { path = "../libgraphviz" }
1414
log = { version = "0.3", features = ["release_max_level_info"] }
15+
owning_ref = "0.3.3"
1516
env_logger = { version = "0.4", default-features = false }
1617
rustc = { path = "../librustc" }
1718
rustc_allocator = { path = "../librustc_allocator" }

src/librustc_driver/driver.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc::session::CompileIncomplete;
1818
use rustc::session::config::{self, Input, OutputFilenames, OutputType};
1919
use rustc::session::search_paths::PathKind;
2020
use rustc::lint;
21-
use rustc::middle::{self, dependency_format, stability, reachable};
21+
use rustc::middle::{self, stability, reachable};
22+
#[cfg(feature="llvm")]
23+
use rustc::middle::dependency_format;
2224
use rustc::middle::privacy::AccessLevels;
2325
use rustc::mir::transform::{MIR_CONST, MIR_VALIDATED, MIR_OPTIMIZED, Passes};
2426
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
@@ -71,6 +73,11 @@ pub fn compile_input(sess: &Session,
7173
output: &Option<PathBuf>,
7274
addl_plugins: Option<Vec<String>>,
7375
control: &CompileController) -> CompileResult {
76+
#[cfg(feature="llvm")]
77+
use rustc_trans::back::write::OngoingCrateTranslation;
78+
#[cfg(not(feature="llvm"))]
79+
type OngoingCrateTranslation = ();
80+
7481
macro_rules! controller_entry_point {
7582
($point: ident, $tsess: expr, $make_state: expr, $phase_result: expr) => {{
7683
let state = &mut $make_state;
@@ -90,7 +97,7 @@ pub fn compile_input(sess: &Session,
9097
// We need nested scopes here, because the intermediate results can keep
9198
// large chunks of memory alive and we want to free them as soon as
9299
// possible to keep the peak memory usage low
93-
let (outputs, trans) = {
100+
let (outputs, trans): (OutputFilenames, OngoingCrateTranslation) = {
94101
let krate = match phase_1_parse_input(control, sess, input) {
95102
Ok(krate) => krate,
96103
Err(mut parse_error) => {
@@ -213,8 +220,6 @@ pub fn compile_input(sess: &Session,
213220
#[cfg(feature="llvm")]
214221
let trans = phase_4_translate_to_llvm(tcx, analysis, incremental_hashes_map,
215222
&outputs);
216-
#[cfg(not(feature="llvm"))]
217-
let trans = { panic!("LLVM not supported by this rustc."); () };
218223

219224
if log_enabled!(::log::LogLevel::Info) {
220225
println!("Post-trans");
@@ -228,12 +233,25 @@ pub fn compile_input(sess: &Session,
228233
}
229234
}
230235

236+
#[cfg(not(feature="llvm"))]
237+
{
238+
let _ = incremental_hashes_map;
239+
sess.err(&format!("LLVM is not supported by this rustc"));
240+
sess.abort_if_errors();
241+
unreachable!();
242+
}
243+
244+
#[cfg(feature="llvm")]
231245
Ok((outputs, trans))
232246
})??
233247
};
234248

235249
#[cfg(not(feature="llvm"))]
236-
unreachable!();
250+
{
251+
let _ = outputs;
252+
let _ = trans;
253+
unreachable!();
254+
}
237255

238256
#[cfg(feature="llvm")]
239257
{
@@ -505,6 +523,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
505523
}
506524
}
507525

526+
#[cfg(feature="llvm")]
508527
fn state_when_compilation_done(input: &'a Input,
509528
session: &'tcx Session,
510529
out_dir: &'a Option<PathBuf>,
@@ -1145,7 +1164,12 @@ fn write_out_deps(sess: &Session, outputs: &OutputFilenames, crate_name: &str) {
11451164
match *output_type {
11461165
OutputType::Exe => {
11471166
for output in sess.crate_types.borrow().iter() {
1148-
let p = ::rustc_trans_utils::link::filename_for_input(sess, *output, crate_name, outputs);
1167+
let p = ::rustc_trans_utils::link::filename_for_input(
1168+
sess,
1169+
*output,
1170+
crate_name,
1171+
outputs
1172+
);
11491173
out_filenames.push(p);
11501174
}
11511175
}
@@ -1263,7 +1287,7 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
12631287

12641288
base.into_iter()
12651289
.filter(|crate_type| {
1266-
let res = !rustc_trans_utils::link::invalid_output_for_target(session, *crate_type);
1290+
let res = !::rustc_trans_utils::link::invalid_output_for_target(session, *crate_type);
12671291

12681292
if !res {
12691293
session.warn(&format!("dropping unsupported crate type `{}` for target `{}`",

src/librustc_driver/lib.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern crate arena;
3535
extern crate getopts;
3636
extern crate graphviz;
3737
extern crate env_logger;
38+
#[cfg(not(feature="llvm"))]
39+
extern crate owning_ref;
3840
extern crate libc;
3941
extern crate rustc;
4042
extern crate rustc_allocator;
@@ -70,8 +72,6 @@ use rustc_resolve as resolve;
7072
use rustc_save_analysis as save;
7173
use rustc_save_analysis::DumpHandler;
7274
#[cfg(feature="llvm")]
73-
use rustc_trans::back::link;
74-
#[cfg(feature="llvm")]
7575
use rustc_trans::back::write::{RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS};
7676
use rustc::dep_graph::DepGraph;
7777
use rustc::session::{self, config, Session, build_session, CompileResult};
@@ -82,7 +82,7 @@ use rustc::session::{early_error, early_warn};
8282
use rustc::lint::Lint;
8383
use rustc::lint;
8484
#[cfg(not(feature="llvm"))]
85-
use rustc::middle::cstore::MetadataLoader;
85+
use rustc::middle::cstore::MetadataLoader as MetadataLoaderTrait;
8686
use rustc_metadata::locator;
8787
use rustc_metadata::cstore::CStore;
8888
use rustc::util::common::{time, ErrorReported};
@@ -114,6 +114,9 @@ use syntax::feature_gate::{GatedCfg, UnstableFeatures};
114114
use syntax::parse::{self, PResult};
115115
use syntax_pos::{DUMMY_SP, MultiSpan};
116116

117+
#[cfg(not(feature="llvm"))]
118+
use owning_ref::{OwningRef, ErasedBoxRef};
119+
117120
#[cfg(test)]
118121
pub mod test;
119122

@@ -174,7 +177,7 @@ pub use NoLLvmMetadataLoader as MetadataLoader;
174177
pub use rustc_trans::LlvmMetadataLoader as MetadataLoader;
175178

176179
#[cfg(not(feature="llvm"))]
177-
impl MetadataLoader for NoLLvmMetadataLoader {
180+
impl MetadataLoaderTrait for NoLLvmMetadataLoader {
178181
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<ErasedBoxRef<[u8]>, String> {
179182
use std::fs::File;
180183
use std::io;
@@ -185,20 +188,20 @@ impl MetadataLoader for NoLLvmMetadataLoader {
185188

186189
while let Some(entry_result) = archive.next_entry() {
187190
let mut entry = entry_result.map_err(|e|format!("metadata section read err: {:?}", e))?;
188-
if entry.header().identifier() == METADATA_FILENAME {
191+
if entry.header().identifier() == "rust.metadata.bin" {
189192
let mut buf = Vec::new();
190193
io::copy(&mut entry, &mut buf).unwrap();
191194
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
192-
return Ok(buf.erase_owner());
195+
return Ok(buf.map_owner_box().erase_owner());
193196
}
194197
}
195198

196199
Err("Couldnt find metadata section".to_string())
197200
}
198201

199202
fn get_dylib_metadata(&self,
200-
target: &Target,
201-
filename: &Path)
203+
_target: &Target,
204+
_filename: &Path)
202205
-> Result<ErasedBoxRef<[u8]>, String> {
203206
panic!("Dylib metadata loading not supported without LLVM")
204207
}
@@ -207,6 +210,7 @@ impl MetadataLoader for NoLLvmMetadataLoader {
207210
// Parse args and run the compiler. This is the primary entry point for rustc.
208211
// See comments on CompilerCalls below for details about the callbacks argument.
209212
// The FileLoader provides a way to load files from sources other than the file system.
213+
#[cfg_attr(not(feature="llvm"), allow(unused_mut))]
210214
pub fn run_compiler<'a>(args: &[String],
211215
callbacks: &mut CompilerCalls<'a>,
212216
file_loader: Option<Box<FileLoader + 'static>>,
@@ -516,6 +520,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
516520
Compilation::Continue
517521
}
518522

523+
#[cfg_attr(not(feature="llvm"), allow(unused_mut))]
519524
fn no_input(&mut self,
520525
matches: &getopts::Matches,
521526
sopts: &config::Options,
@@ -743,7 +748,12 @@ impl RustcDefaultCalls {
743748
}
744749
let crate_types = driver::collect_crate_types(sess, attrs);
745750
for &style in &crate_types {
746-
let fname = rustc_trans_utils::link::filename_for_input(sess, style, &id, &t_outputs);
751+
let fname = rustc_trans_utils::link::filename_for_input(
752+
sess,
753+
style,
754+
&id,
755+
&t_outputs
756+
);
747757
println!("{}",
748758
fname.file_name()
749759
.unwrap()
@@ -792,15 +802,15 @@ impl RustcDefaultCalls {
792802
}
793803
PrintRequest::RelocationModels => {
794804
println!("Available relocation models:");
795-
#[cfg(features="llvm")]
805+
#[cfg(feature="llvm")]
796806
for &(name, _) in RELOC_MODEL_ARGS.iter() {
797807
println!(" {}", name);
798808
}
799809
println!("");
800810
}
801811
PrintRequest::CodeModels => {
802812
println!("Available code models:");
803-
#[cfg(features="llvm")]
813+
#[cfg(feature="llvm")]
804814
for &(name, _) in CODE_GEN_MODEL_ARGS.iter(){
805815
println!(" {}", name);
806816
}

src/librustc_trans/back/link.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ pub const RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET: usize =
8888
pub const RLIB_BYTECODE_OBJECT_V1_DATA_OFFSET: usize =
8989
RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET + 8;
9090

91-
pub use self::rustc_trans_utils::link::{find_crate_name, filename_for_input, default_output_for_target, invalid_output_for_target};
91+
pub use self::rustc_trans_utils::link::{find_crate_name, filename_for_input,
92+
default_output_for_target, invalid_output_for_target};
9293

9394
pub fn build_link_meta(incremental_hashes_map: &IncrementalHashesMap) -> LinkMeta {
9495
let krate_dep_node = &DepNode::new_no_params(DepKind::Krate);

0 commit comments

Comments
 (0)