rust/src/test/run-make-fulldeps/hotplug_codegen_backend/the_backend.rs

114 lines
3.7 KiB
Rust
Raw Normal View History

2018-01-04 11:15:40 -06:00
#![feature(rustc_private)]
extern crate rustc;
2018-05-08 08:10:16 -05:00
extern crate rustc_codegen_utils;
2019-03-02 06:46:10 -06:00
#[macro_use]
extern crate rustc_data_structures;
2020-01-04 22:35:51 -06:00
extern crate rustc_hir;
2019-03-02 06:46:10 -06:00
extern crate rustc_target;
extern crate rustc_driver;
2020-01-01 17:01:07 -06:00
extern crate rustc_span;
2018-01-04 11:15:40 -06:00
use std::any::Any;
use std::sync::Arc;
2019-03-02 06:46:10 -06:00
use std::path::Path;
2020-01-01 17:01:07 -06:00
use rustc_span::symbol::Symbol;
use rustc::session::Session;
2018-01-04 11:15:40 -06:00
use rustc::session::config::OutputFilenames;
use rustc::ty::TyCtxt;
2018-06-13 08:44:43 -05:00
use rustc::ty::query::Providers;
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader, MetadataLoaderDyn};
2018-01-04 11:15:40 -06:00
use rustc::dep_graph::DepGraph;
use rustc::util::common::ErrorReported;
2019-03-02 06:46:10 -06:00
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::sync::MetadataRef;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_target::spec::Target;
2018-01-04 11:15:40 -06:00
2019-03-02 06:46:10 -06:00
pub struct NoLlvmMetadataLoader;
impl MetadataLoader for NoLlvmMetadataLoader {
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
let buf = std::fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
Ok(rustc_erase_owner!(buf.map_owner_box()))
}
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String> {
self.get_rlib_metadata(target, filename)
}
}
struct TheBackend;
2018-01-04 11:15:40 -06:00
2018-05-08 08:10:16 -05:00
impl CodegenBackend for TheBackend {
fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
2019-03-02 06:46:10 -06:00
Box::new(NoLlvmMetadataLoader)
2018-01-04 11:15:40 -06:00
}
fn provide(&self, providers: &mut Providers) {
2019-03-02 06:46:10 -06:00
rustc_codegen_utils::symbol_names::provide(providers);
providers.target_features_whitelist = |tcx, _cnum| {
tcx.arena.alloc(Default::default()) // Just a dummy
2019-03-02 06:46:10 -06:00
};
providers.is_reachable_non_generic = |_tcx, _defid| true;
providers.exported_symbols = |_tcx, _crate| Arc::new(Vec::new());
2018-01-04 11:15:40 -06:00
}
fn provide_extern(&self, providers: &mut Providers) {
2019-03-02 06:46:10 -06:00
providers.is_reachable_non_generic = |_tcx, _defid| true;
2018-01-04 11:15:40 -06:00
}
2018-05-08 08:10:16 -05:00
fn codegen_crate<'a, 'tcx>(
2018-01-04 11:15:40 -06:00
&self,
2019-06-13 16:48:52 -05:00
tcx: TyCtxt<'tcx>,
_metadata: EncodedMetadata,
_need_metadata_module: bool,
) -> Box<dyn Any> {
2020-01-04 22:35:51 -06:00
use rustc_hir::def_id::LOCAL_CRATE;
2018-01-04 11:26:34 -06:00
2018-01-04 11:15:40 -06:00
Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
}
fn join_codegen(
2018-01-04 11:15:40 -06:00
&self,
ongoing_codegen: Box<dyn Any>,
_sess: &Session,
2018-01-04 11:15:40 -06:00
_dep_graph: &DepGraph,
) -> Result<Box<dyn Any>, ErrorReported> {
let crate_name = ongoing_codegen.downcast::<Symbol>()
.expect("in join_codegen: ongoing_codegen is not a Symbol");
Ok(crate_name)
}
fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
2018-01-04 11:15:40 -06:00
outputs: &OutputFilenames,
) -> Result<(), ErrorReported> {
2018-01-04 11:15:40 -06:00
use std::io::Write;
use rustc::session::config::CrateType;
2018-05-08 08:10:16 -05:00
use rustc_codegen_utils::link::out_filename;
let crate_name = codegen_results.downcast::<Symbol>()
.expect("in link: codegen_results is not a Symbol");
2018-01-04 11:15:40 -06:00
for &crate_type in sess.opts.crate_types.iter() {
if crate_type != CrateType::Rlib {
2018-01-04 11:15:40 -06:00
sess.fatal(&format!("Crate type is {:?}", crate_type));
}
let output_name =
out_filename(sess, crate_type, &outputs, &*crate_name.as_str());
let mut out_file = ::std::fs::File::create(output_name).unwrap();
2018-02-16 08:56:50 -06:00
write!(out_file, "This has been \"compiled\" successfully.").unwrap();
2018-01-04 11:15:40 -06:00
}
Ok(())
}
}
2018-05-08 08:10:16 -05:00
/// This is the entrypoint for a hot plugged rustc_codegen_llvm
2018-01-04 11:15:40 -06:00
#[no_mangle]
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
2019-03-02 06:46:10 -06:00
Box::new(TheBackend)
2018-01-04 11:15:40 -06:00
}