]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/hotplug_codegen_backend/the_backend.rs
rustc: Load the `rustc_trans` crate at runtime
[rust.git] / src / test / run-make / hotplug_codegen_backend / the_backend.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(rustc_private)]
12
13 extern crate syntax;
14 extern crate rustc;
15 extern crate rustc_trans_utils;
16
17 use std::any::Any;
18 use std::sync::mpsc;
19 use syntax::symbol::Symbol;
20 use rustc::session::{Session, CompileIncomplete};
21 use rustc::session::config::OutputFilenames;
22 use rustc::ty::TyCtxt;
23 use rustc::ty::maps::Providers;
24 use rustc::middle::cstore::MetadataLoader;
25 use rustc::dep_graph::DepGraph;
26 use rustc_trans_utils::trans_crate::{TransCrate, MetadataOnlyTransCrate};
27
28 struct TheBackend(Box<TransCrate>);
29
30 impl TransCrate for TheBackend {
31     fn metadata_loader(&self) -> Box<MetadataLoader> {
32         self.0.metadata_loader()
33     }
34
35     fn provide(&self, providers: &mut Providers) {
36         self.0.provide(providers);
37     }
38
39     fn provide_extern(&self, providers: &mut Providers) {
40         self.0.provide_extern(providers);
41     }
42
43     fn trans_crate<'a, 'tcx>(
44         &self,
45         tcx: TyCtxt<'a, 'tcx, 'tcx>,
46         _rx: mpsc::Receiver<Box<Any + Send>>
47     ) -> Box<Any> {
48         use rustc::hir::def_id::LOCAL_CRATE;
49
50         Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
51     }
52
53     fn join_trans_and_link(
54         &self,
55         trans: Box<Any>,
56         sess: &Session,
57         _dep_graph: &DepGraph,
58         outputs: &OutputFilenames,
59     ) -> Result<(), CompileIncomplete> {
60         use std::io::Write;
61         use rustc::session::config::CrateType;
62         use rustc_trans_utils::link::out_filename;
63         let crate_name = trans.downcast::<Symbol>()
64             .expect("in join_trans_and_link: trans is not a Symbol");
65         for &crate_type in sess.opts.crate_types.iter() {
66             if crate_type != CrateType::CrateTypeExecutable {
67                 sess.fatal(&format!("Crate type is {:?}", crate_type));
68             }
69             let output_name =
70                 out_filename(sess, crate_type, &outputs, &*crate_name.as_str());
71             let mut out_file = ::std::fs::File::create(output_name).unwrap();
72             write!(out_file, "This has been \"compiled\" succesfully.").unwrap();
73         }
74         Ok(())
75     }
76 }
77
78 /// This is the entrypoint for a hot plugged rustc_trans
79 #[no_mangle]
80 pub fn __rustc_codegen_backend() -> Box<TransCrate> {
81     Box::new(TheBackend(MetadataOnlyTransCrate::new()))
82 }