Skip to content

Commit

Permalink
Make rustc compile without LLVM
Browse files Browse the repository at this point in the history
Doesn't yet compile when `llvm-enabled = false` due to crate metadata loading needing LLVM
  • Loading branch information
bjorn3 committed Jun 21, 2017
1 parent ed78ac1 commit 2e2501b
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 162 deletions.
5 changes: 4 additions & 1 deletion src/librustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ rustc_plugin = { path = "../librustc_plugin" }
rustc_privacy = { path = "../librustc_privacy" }
rustc_resolve = { path = "../librustc_resolve" }
rustc_save_analysis = { path = "../librustc_save_analysis" }
rustc_trans = { path = "../librustc_trans" }
rustc_trans = { path = "../librustc_trans", optional=true }
rustc_typeck = { path = "../librustc_typeck" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_ext = { path = "../libsyntax_ext" }
syntax_pos = { path = "../libsyntax_pos" }

[features]
llvm = ["rustc_trans"]
90 changes: 59 additions & 31 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ use rustc_incremental::{self, IncrementalHashesMap};
use rustc_resolve::{MakeGlobMap, Resolver};
use rustc_metadata::creader::CrateLoader;
use rustc_metadata::cstore::{self, CStore};
#[cfg(feature="llvm")]
use rustc_trans::back::{link, write};
#[cfg(not(feature="llvm"))]
use ::link;
#[cfg(feature="llvm")]
use rustc_trans as trans;
use rustc_typeck as typeck;
use rustc_privacy;
Expand Down Expand Up @@ -111,7 +115,7 @@ pub fn compile_input(sess: &Session,
};

let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
let crate_name = link::find_crate_name(Some(sess), &krate.attrs, input);
let crate_name: String = link::find_crate_name(Some(sess), &krate.attrs, input);
let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
phase_2_configure_and_expand(
sess, &cstore, krate, registry, &crate_name, addl_plugins, control.make_glob_map,
Expand Down Expand Up @@ -204,55 +208,71 @@ pub fn compile_input(sess: &Session,
println!("Pre-trans");
tcx.print_debug_stats();
}
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
&outputs);

if log_enabled!(::log::LogLevel::Info) {
println!("Post-trans");
tcx.print_debug_stats();
}
#[cfg(feature="llvm")]
{
let trans = phase_4_translate_to_llvm(tcx, analysis, &incremental_hashes_map,
&outputs);

if log_enabled!(::log::LogLevel::Info) {
println!("Post-trans");
tcx.print_debug_stats();
}

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(e) = mir::transform::dump_mir::emit_mir(tcx, &outputs) {
sess.err(&format!("could not emit MIR: {}", e));
sess.abort_if_errors();
}
}

return Ok((outputs, trans))
}

Ok((outputs, trans))
#[cfg(not(feature="llvm"))]
panic!("Unreachable")
})??
};

if sess.opts.debugging_opts.print_type_sizes {
sess.code_stats.borrow().print_type_sizes();
}

let phase5_result = phase_5_run_llvm_passes(sess, &trans, &outputs);
#[cfg(feature="llvm")]
{
let phase5_result = phase_5_run_llvm_passes(sess, &trans, &outputs);

controller_entry_point!(after_llvm,
sess,
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
phase5_result);
phase5_result?;
controller_entry_point!(after_llvm,
sess,
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
phase5_result);
phase5_result?;

write::cleanup_llvm(&trans);
write::cleanup_llvm(&trans);

phase_6_link_output(sess, &trans, &outputs);
phase_6_link_output(sess, &trans, &outputs);

// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(sess, trans.link.crate_hash);
// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(sess, trans.link.crate_hash);

if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
}
if sess.opts.debugging_opts.perf_stats {
sess.print_perf_stats();
}

controller_entry_point!(compilation_done,
sess,
CompileState::state_when_compilation_done(input,
sess,
outdir,
output),
Ok(()));

controller_entry_point!(compilation_done,
sess,
CompileState::state_when_compilation_done(input, sess, outdir, output),
Ok(()));
return Ok(())
}

Ok(())
#[cfg(not(feature="llvm"))]
panic!("Unreachable")
}

fn keep_hygiene_data(sess: &Session) -> bool {
Expand Down Expand Up @@ -355,6 +375,7 @@ pub struct CompileState<'a, 'tcx: 'a> {
pub resolutions: Option<&'a Resolutions>,
pub analysis: Option<&'a ty::CrateAnalysis>,
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
#[cfg(feature="llvm")]
pub trans: Option<&'a trans::CrateTranslation>,
}

Expand All @@ -381,6 +402,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
resolutions: None,
analysis: None,
tcx: None,
#[cfg(feature="llvm")]
trans: None,
}
}
Expand Down Expand Up @@ -470,6 +492,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
}


#[cfg(feature="llvm")]
fn state_after_llvm(input: &'a Input,
session: &'tcx Session,
out_dir: &'a Option<PathBuf>,
Expand Down Expand Up @@ -893,6 +916,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
mir::provide(&mut local_providers);
reachable::provide(&mut local_providers);
rustc_privacy::provide(&mut local_providers);
#[cfg(feature="llvm")]
trans::provide(&mut local_providers);
typeck::provide(&mut local_providers);
ty::provide(&mut local_providers);
Expand All @@ -904,6 +928,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

let mut extern_providers = ty::maps::Providers::default();
cstore::provide(&mut extern_providers);
#[cfg(feature="llvm")]
trans::provide(&mut extern_providers);
ty::provide_extern(&mut extern_providers);
traits::provide_extern(&mut extern_providers);
Expand Down Expand Up @@ -1045,6 +1070,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,

/// Run the translation phase to LLVM, after which the AST and analysis can
/// be discarded.
#[cfg(feature="llvm")]
pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
analysis: ty::CrateAnalysis,
incremental_hashes_map: &IncrementalHashesMap,
Expand Down Expand Up @@ -1076,6 +1102,7 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

/// Run LLVM itself, producing a bitcode file, assembly file or object file
/// as a side effect.
#[cfg(feature="llvm")]
pub fn phase_5_run_llvm_passes(sess: &Session,
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) -> CompileResult {
Expand Down Expand Up @@ -1124,6 +1151,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session,

/// Run the linker on any artifacts that resulted from the LLVM run.
/// This should produce either a finished executable or library.
#[cfg(feature="llvm")]
pub fn phase_6_link_output(sess: &Session,
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) {
Expand Down
52 changes: 43 additions & 9 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern crate rustc_metadata;
extern crate rustc_mir;
extern crate rustc_resolve;
extern crate rustc_save_analysis;
#[cfg(feature="llvm")]
extern crate rustc_trans;
extern crate rustc_typeck;
extern crate serialize;
Expand All @@ -63,7 +64,9 @@ use pretty::{PpMode, UserIdentifiedItem};
use rustc_resolve as resolve;
use rustc_save_analysis as save;
use rustc_save_analysis::DumpHandler;
#[cfg(feature="llvm")]
use rustc_trans::back::link;
#[cfg(feature="llvm")]
use rustc_trans::back::write::{RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS};
use rustc::dep_graph::DepGraph;
use rustc::session::{self, config, Session, build_session, CompileResult};
Expand Down Expand Up @@ -175,6 +178,7 @@ pub fn run_compiler<'a>(args: &[String],
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);

if sopts.debugging_opts.debug_llvm {
#[cfg(feature="llvm")]
rustc_trans::enable_llvm_debug();
}

Expand Down Expand Up @@ -204,6 +208,7 @@ pub fn run_compiler<'a>(args: &[String],
let mut sess = session::build_session_with_codemap(
sopts, &dep_graph, input_file_path, descriptions, cstore.clone(), codemap, emitter_dest,
);
#[cfg(feature="llvm")]
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

Expand Down Expand Up @@ -409,6 +414,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
None,
descriptions.clone(),
cstore.clone());
#[cfg(feature="llvm")]
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let mut cfg = config::build_configuration(&sess, cfg.clone());
Expand Down Expand Up @@ -607,7 +613,7 @@ impl RustcDefaultCalls {
};
let attrs = attrs.as_ref().unwrap();
let t_outputs = driver::build_output_filenames(input, odir, ofile, attrs, sess);
let id = link::find_crate_name(Some(sess), attrs, input);
let id: String = link::find_crate_name(Some(sess), attrs, input);
if *req == PrintRequest::CrateName {
println!("{}", id);
continue;
Expand Down Expand Up @@ -662,20 +668,36 @@ impl RustcDefaultCalls {
}
}
PrintRequest::RelocationModels => {
println!("Available relocation models:");
for &(name, _) in RELOC_MODEL_ARGS.iter() {
println!(" {}", name);
#[cfg(not(feature="llvm"))]
panic!("LLVM is not enabled for this rustc version");

#[cfg(feature="llvm")]
{
println!("Available relocation models:");
for &(name, _) in RELOC_MODEL_ARGS.iter() {
println!(" {}", name);
}
println!("");
}
println!("");
}
PrintRequest::CodeModels => {
println!("Available code models:");
for &(name, _) in CODE_GEN_MODEL_ARGS.iter(){
println!(" {}", name);
#[cfg(not(feature="llvm"))]
panic!("LLVM is not enabled for this rustc version");

#[cfg(feature="llvm")]
{
println!("Available code models:");
for &(name, _) in CODE_GEN_MODEL_ARGS.iter(){
println!(" {}", name);
}
println!("");
}
println!("");
}
PrintRequest::TargetCPUs | PrintRequest::TargetFeatures => {
#[cfg(not(feature="llvm"))]
panic!("LLVM is not enabled for this rustc version");

#[cfg(feature="llvm")]
rustc_trans::print(*req, sess);
}
}
Expand Down Expand Up @@ -715,6 +737,7 @@ pub fn version(binary: &str, matches: &getopts::Matches) {
println!("commit-date: {}", unw(commit_date_str()));
println!("host: {}", config::host_triple());
println!("release: {}", unw(release_str()));
#[cfg(feature="llvm")]
rustc_trans::print_version();
}
}
Expand Down Expand Up @@ -1008,6 +1031,10 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
}

if cg_flags.contains(&"passes=list".to_string()) {
#[cfg(not(feature="llvm"))]
panic!("LLVM is not enabled for this rustc version");

#[cfg(feature="llvm")]
rustc_trans::print_passes();
return None;
}
Expand Down Expand Up @@ -1135,6 +1162,7 @@ pub fn diagnostics_registry() -> errors::registry::Registry {
all_errors.extend_from_slice(&rustc_borrowck::DIAGNOSTICS);
all_errors.extend_from_slice(&rustc_resolve::DIAGNOSTICS);
all_errors.extend_from_slice(&rustc_privacy::DIAGNOSTICS);
#[cfg(feature="llvm")]
all_errors.extend_from_slice(&rustc_trans::DIAGNOSTICS);
all_errors.extend_from_slice(&rustc_const_eval::DIAGNOSTICS);
all_errors.extend_from_slice(&rustc_metadata::DIAGNOSTICS);
Expand All @@ -1159,3 +1187,9 @@ pub fn main() {
None));
process::exit(result as i32);
}


#[cfg(not(feature="llvm"))]
mod link {
include!("../librustc_trans/back/no_llvm_link.rs");
}
2 changes: 2 additions & 0 deletions src/librustc_driver/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use syntax::ast;
use rustc::session::Session;
use syntax::symbol::Symbol;
#[cfg(feature="llvm")]
use rustc_trans;

/// Add `target_feature = "..."` cfgs for a variety of platform
Expand All @@ -21,6 +22,7 @@ use rustc_trans;
pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
let tf = Symbol::intern("target_feature");

#[cfg(feature="llvm")]
for feat in rustc_trans::target_features(sess) {
cfg.insert((tf, Some(feat)));
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use driver;
use rustc::dep_graph::DepGraph;
use rustc_lint;
use rustc_resolve::MakeGlobMap;
#[cfg(feature="llvm")]
use rustc_trans;
use rustc::middle::lang_items;
use rustc::middle::free_region::FreeRegionMap;
Expand Down Expand Up @@ -113,6 +114,7 @@ fn test_env<F>(source_string: &str,
diagnostic_handler,
Rc::new(CodeMap::new(FilePathMapping::empty())),
cstore.clone());
#[cfg(feature="llvm")]
rustc_trans::init(&sess);
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let input = config::Input::Str {
Expand Down
Loading

0 comments on commit 2e2501b

Please sign in to comment.