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

https://gitlab.com/jianglu/rust · Rust · 82 lines · 61 code · 11 blank · 10 comment · 4 complexity · 88549f9de46fe6c0e40b9f1960953ab8 MD5 · raw file

  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. #![feature(rustc_private)]
  11. extern crate syntax;
  12. extern crate rustc;
  13. extern crate rustc_codegen_utils;
  14. use std::any::Any;
  15. use std::sync::mpsc;
  16. use syntax::symbol::Symbol;
  17. use rustc::session::{Session, CompileIncomplete};
  18. use rustc::session::config::OutputFilenames;
  19. use rustc::ty::TyCtxt;
  20. use rustc::ty::maps::Providers;
  21. use rustc::middle::cstore::MetadataLoader;
  22. use rustc::dep_graph::DepGraph;
  23. use rustc_codegen_utils::codegen_backend::{CodegenBackend, MetadataOnlyCodegenBackend};
  24. struct TheBackend(Box<CodegenBackend>);
  25. impl CodegenBackend for TheBackend {
  26. fn metadata_loader(&self) -> Box<MetadataLoader + Sync> {
  27. self.0.metadata_loader()
  28. }
  29. fn provide(&self, providers: &mut Providers) {
  30. self.0.provide(providers);
  31. }
  32. fn provide_extern(&self, providers: &mut Providers) {
  33. self.0.provide_extern(providers);
  34. }
  35. fn codegen_crate<'a, 'tcx>(
  36. &self,
  37. tcx: TyCtxt<'a, 'tcx, 'tcx>,
  38. _rx: mpsc::Receiver<Box<Any + Send>>
  39. ) -> Box<Any> {
  40. use rustc::hir::def_id::LOCAL_CRATE;
  41. Box::new(tcx.crate_name(LOCAL_CRATE) as Symbol)
  42. }
  43. fn join_codegen_and_link(
  44. &self,
  45. ongoing_codegen: Box<Any>,
  46. sess: &Session,
  47. _dep_graph: &DepGraph,
  48. outputs: &OutputFilenames,
  49. ) -> Result<(), CompileIncomplete> {
  50. use std::io::Write;
  51. use rustc::session::config::CrateType;
  52. use rustc_codegen_utils::link::out_filename;
  53. let crate_name = ongoing_codegen.downcast::<Symbol>()
  54. .expect("in join_codegen_and_link: ongoing_codegen is not a Symbol");
  55. for &crate_type in sess.opts.crate_types.iter() {
  56. if crate_type != CrateType::CrateTypeExecutable {
  57. sess.fatal(&format!("Crate type is {:?}", crate_type));
  58. }
  59. let output_name =
  60. out_filename(sess, crate_type, &outputs, &*crate_name.as_str());
  61. let mut out_file = ::std::fs::File::create(output_name).unwrap();
  62. write!(out_file, "This has been \"compiled\" successfully.").unwrap();
  63. }
  64. Ok(())
  65. }
  66. }
  67. /// This is the entrypoint for a hot plugged rustc_codegen_llvm
  68. #[no_mangle]
  69. pub fn __rustc_codegen_backend() -> Box<CodegenBackend> {
  70. Box::new(TheBackend(MetadataOnlyCodegenBackend::new()))
  71. }