Skip to content

Commit b43c02b

Browse files
committed
Make librustc_driver work without librustc_trans
1 parent e152a16 commit b43c02b

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

src/Cargo.lock

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

src/librustc_driver/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ rustc_plugin = { path = "../librustc_plugin" }
2929
rustc_privacy = { path = "../librustc_privacy" }
3030
rustc_resolve = { path = "../librustc_resolve" }
3131
rustc_save_analysis = { path = "../librustc_save_analysis" }
32-
rustc_trans = { path = "../librustc_trans" }
32+
rustc_trans = { path = "../librustc_trans", optional = true }
3333
rustc_typeck = { path = "../librustc_typeck" }
3434
serialize = { path = "../libserialize" }
3535
syntax = { path = "../libsyntax" }
3636
syntax_ext = { path = "../libsyntax_ext" }
3737
syntax_pos = { path = "../libsyntax_pos" }
38+
39+
ar = "0.3.0"
40+
41+
[features]
42+
llvm = ["rustc_trans"]

src/librustc_driver/lib.rs

+47-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#![feature(rustc_diagnostic_macros)]
2929
#![feature(set_stdio)]
3030

31+
#[cfg(not(feature="llvm"))]
32+
extern crate ar;
33+
3134
extern crate arena;
3235
extern crate getopts;
3336
extern crate graphviz;
@@ -49,6 +52,7 @@ extern crate rustc_metadata;
4952
extern crate rustc_mir;
5053
extern crate rustc_resolve;
5154
extern crate rustc_save_analysis;
55+
#[cfg(feature="llvm")]
5256
extern crate rustc_trans;
5357
extern crate rustc_typeck;
5458
extern crate serialize;
@@ -74,6 +78,8 @@ use rustc::session::config::nightly_options;
7478
use rustc::session::{early_error, early_warn};
7579
use rustc::lint::Lint;
7680
use rustc::lint;
81+
#[cfg(not(feature="llvm"))]
82+
use rustc::middle::cstore::MetadataLoader;
7783
use rustc_metadata::locator;
7884
use rustc_metadata::cstore::CStore;
7985
use rustc::util::common::{time, ErrorReported};
@@ -151,6 +157,45 @@ pub fn run<F>(run_compiler: F) -> isize
151157
0
152158
}
153159

160+
#[cfg(not(feature="llvm"))]
161+
pub struct NoLLvmMetadataLoader;
162+
163+
#[cfg(not(feature="llvm"))]
164+
pub use NoLLvmMetadataLoader as MetadataLoader;
165+
#[cfg(feature="llvm")]
166+
pub use rustc_trans::LlvmMetadataLoader as MetadataLoader;
167+
168+
#[cfg(not(feature="llvm"))]
169+
impl MetadataLoader for NoLLvmMetadataLoader {
170+
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<ErasedBoxRef<[u8]>, String> {
171+
use std::fs::File;
172+
use std::io;
173+
use self::ar::Archive;
174+
175+
let file = File::open(filename).map_err(|e|format!("metadata file open err: {:?}", e))?;
176+
let mut archive = Archive::new(file);
177+
178+
while let Some(entry_result) = archive.next_entry() {
179+
let mut entry = entry_result.map_err(|e|format!("metadata section read err: {:?}", e))?;
180+
if entry.header().identifier() == METADATA_FILENAME {
181+
let mut buf = Vec::new();
182+
io::copy(&mut entry, &mut buf).unwrap();
183+
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
184+
return Ok(buf.erase_owner());
185+
}
186+
}
187+
188+
Err("Couldnt find metadata section".to_string())
189+
}
190+
191+
fn get_dylib_metadata(&self,
192+
target: &Target,
193+
filename: &Path)
194+
-> Result<ErasedBoxRef<[u8]>, String> {
195+
panic!("Dylib metadata loading not supported without LLVM")
196+
}
197+
}
198+
154199
// Parse args and run the compiler. This is the primary entry point for rustc.
155200
// See comments on CompilerCalls below for details about the callbacks argument.
156201
// The FileLoader provides a way to load files from sources other than the file system.
@@ -197,7 +242,7 @@ pub fn run_compiler<'a>(args: &[String],
197242
};
198243

199244
let dep_graph = DepGraph::new(sopts.build_dep_graph());
200-
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
245+
let cstore = Rc::new(CStore::new(&dep_graph, box ::MetadataLoader));
201246

202247
let loader = file_loader.unwrap_or(box RealFileLoader);
203248
let codemap = Rc::new(CodeMap::with_file_loader(loader, sopts.file_path_mapping()));
@@ -477,7 +522,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
477522
return None;
478523
}
479524
let dep_graph = DepGraph::new(sopts.build_dep_graph());
480-
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
525+
let cstore = Rc::new(CStore::new(&dep_graph, box ::MetadataLoader));
481526
let mut sess = build_session(sopts.clone(),
482527
&dep_graph,
483528
None,

src/librustc_driver/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn test_env<F>(source_string: &str,
106106

107107
let dep_graph = DepGraph::new(false);
108108
let _ignore = dep_graph.in_ignore();
109-
let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader));
109+
let cstore = Rc::new(CStore::new(&dep_graph, box ::MetadataLoader));
110110
let sess = session::build_session_(options,
111111
&dep_graph,
112112
None,

src/rustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ rustc_driver = { path = "../librustc_driver" }
1515

1616
[features]
1717
jemalloc = ["rustc_back/jemalloc"]
18+
llvm = ["rustc_driver/llvm"]

0 commit comments

Comments
 (0)