]> git.lizzy.rs Git - rust.git/blob - src/test/run-make-fulldeps/hotplug_codegen_backend/the_backend.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / test / run-make-fulldeps / hotplug_codegen_backend / the_backend.rs
1 #![feature(rustc_private)]
2
3 extern crate rustc;
4 extern crate rustc_codegen_utils;
5 #[macro_use]
6 extern crate rustc_data_structures;
7 extern crate rustc_hir;
8 extern crate rustc_target;
9 extern crate rustc_driver;
10 extern crate rustc_session;
11 extern crate rustc_span;
12
13 use std::any::Any;
14 use std::sync::Arc;
15 use std::path::Path;
16 use rustc::ty::TyCtxt;
17 use rustc::ty::query::Providers;
18 use rustc::middle::cstore::{EncodedMetadata, MetadataLoader, MetadataLoaderDyn};
19 use rustc::dep_graph::DepGraph;
20 use rustc::util::common::ErrorReported;
21 use rustc_codegen_utils::codegen_backend::CodegenBackend;
22 use rustc_data_structures::sync::MetadataRef;
23 use rustc_data_structures::owning_ref::OwningRef;
24 use rustc_session::Session;
25 use rustc_session::config::OutputFilenames;
26 use rustc_span::symbol::Symbol;
27 use rustc_target::spec::Target;
28
29 pub struct NoLlvmMetadataLoader;
30
31 impl MetadataLoader for NoLlvmMetadataLoader {
32     fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
33         let buf = std::fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?;
34         let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
35         Ok(rustc_erase_owner!(buf.map_owner_box()))
36     }
37
38     fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String> {
39         self.get_rlib_metadata(target, filename)
40     }
41 }
42
43 struct TheBackend;
44
45 impl CodegenBackend for TheBackend {
46     fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
47         Box::new(NoLlvmMetadataLoader)
48     }
49
50     fn provide(&self, providers: &mut Providers) {
51         rustc_codegen_utils::symbol_names::provide(providers);
52
53         providers.target_features_whitelist = |tcx, _cnum| {
54             tcx.arena.alloc(Default::default()) // Just a dummy
55         };
56         providers.is_reachable_non_generic = |_tcx, _defid| true;
57         providers.exported_symbols = |_tcx, _crate| Arc::new(Vec::new());
58     }
59
60     fn provide_extern(&self, providers: &mut Providers) {
61         providers.is_reachable_non_generic = |_tcx, _defid| true;
62     }
63
64     fn codegen_crate<'a, 'tcx>(
65         &self,
66         tcx: TyCtxt<'tcx>,
67         _metadata: EncodedMetadata,
68         _need_metadata_module: bool,
69     ) -> Box<dyn Any> {
70         use rustc_hir::def_id::LOCAL_CRATE;
71
72         Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
73     }
74
75     fn join_codegen(
76         &self,
77         ongoing_codegen: Box<dyn Any>,
78         _sess: &Session,
79         _dep_graph: &DepGraph,
80     ) -> Result<Box<dyn Any>, ErrorReported> {
81         let crate_name = ongoing_codegen.downcast::<Symbol>()
82             .expect("in join_codegen: ongoing_codegen is not a Symbol");
83         Ok(crate_name)
84     }
85
86     fn link(
87         &self,
88         sess: &Session,
89         codegen_results: Box<dyn Any>,
90         outputs: &OutputFilenames,
91     ) -> Result<(), ErrorReported> {
92         use std::io::Write;
93         use rustc_session::config::CrateType;
94         use rustc_codegen_utils::link::out_filename;
95         let crate_name = codegen_results.downcast::<Symbol>()
96             .expect("in link: codegen_results is not a Symbol");
97         for &crate_type in sess.opts.crate_types.iter() {
98             if crate_type != CrateType::Rlib {
99                 sess.fatal(&format!("Crate type is {:?}", crate_type));
100             }
101             let output_name =
102                 out_filename(sess, crate_type, &outputs, &*crate_name.as_str());
103             let mut out_file = ::std::fs::File::create(output_name).unwrap();
104             write!(out_file, "This has been \"compiled\" successfully.").unwrap();
105         }
106         Ok(())
107     }
108 }
109
110 /// This is the entrypoint for a hot plugged rustc_codegen_llvm
111 #[no_mangle]
112 pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
113     Box::new(TheBackend)
114 }