]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_utils/codegen_backend.rs
submodule: update rls from c9d25b667a to f331ff7
[rust.git] / src / librustc_codegen_utils / codegen_backend.rs
1 //! The Rust compiler.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6
7 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
8       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
9       html_root_url = "https://doc.rust-lang.org/nightly/")]
10 #![deny(warnings)]
11
12 #![feature(box_syntax)]
13
14 use std::any::Any;
15 use std::io::Write;
16 use std::fs;
17 use std::path::Path;
18 use std::sync::{mpsc, Arc};
19
20 use rustc_data_structures::owning_ref::OwningRef;
21 use flate2::Compression;
22 use flate2::write::DeflateEncoder;
23
24 use syntax::symbol::Symbol;
25 use rustc::hir::def_id::LOCAL_CRATE;
26 use rustc::session::{Session, CompileIncomplete};
27 use rustc::session::config::{CrateType, OutputFilenames, PrintRequest};
28 use rustc::ty::TyCtxt;
29 use rustc::ty::query::Providers;
30 use rustc::middle::cstore::EncodedMetadata;
31 use rustc::middle::cstore::MetadataLoader;
32 use rustc::dep_graph::DepGraph;
33 use rustc_target::spec::Target;
34 use link::out_filename;
35
36 pub use rustc_data_structures::sync::MetadataRef;
37
38 pub trait CodegenBackend {
39     fn init(&self, _sess: &Session) {}
40     fn print(&self, _req: PrintRequest, _sess: &Session) {}
41     fn target_features(&self, _sess: &Session) -> Vec<Symbol> { vec![] }
42     fn print_passes(&self) {}
43     fn print_version(&self) {}
44     fn diagnostics(&self) -> &[(&'static str, &'static str)] { &[] }
45
46     fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync>;
47     fn provide(&self, _providers: &mut Providers);
48     fn provide_extern(&self, _providers: &mut Providers);
49     fn codegen_crate<'a, 'tcx>(
50         &self,
51         tcx: TyCtxt<'a, 'tcx, 'tcx>,
52         rx: mpsc::Receiver<Box<dyn Any + Send>>
53     ) -> Box<dyn Any>;
54
55     /// This is called on the returned `Box<dyn Any>` from `codegen_backend`
56     ///
57     /// # Panics
58     ///
59     /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
60     fn join_codegen_and_link(
61         &self,
62         ongoing_codegen: Box<dyn Any>,
63         sess: &Session,
64         dep_graph: &DepGraph,
65         outputs: &OutputFilenames,
66     ) -> Result<(), CompileIncomplete>;
67 }
68
69 pub struct NoLlvmMetadataLoader;
70
71 impl MetadataLoader for NoLlvmMetadataLoader {
72     fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
73         let buf = fs::read(filename).map_err(|e| format!("metadata file open err: {:?}", e))?;
74         let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf);
75         Ok(rustc_erase_owner!(buf.map_owner_box()))
76     }
77
78     fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String> {
79         self.get_rlib_metadata(target, filename)
80     }
81 }
82
83 pub struct MetadataOnlyCodegenBackend(());
84 pub struct OngoingCodegen {
85     metadata: EncodedMetadata,
86     metadata_version: Vec<u8>,
87     crate_name: Symbol,
88 }
89
90 impl MetadataOnlyCodegenBackend {
91     pub fn boxed() -> Box<dyn CodegenBackend> {
92         box MetadataOnlyCodegenBackend(())
93     }
94 }
95
96 impl CodegenBackend for MetadataOnlyCodegenBackend {
97     fn init(&self, sess: &Session) {
98         for cty in sess.opts.crate_types.iter() {
99             match *cty {
100                 CrateType::Rlib | CrateType::Dylib | CrateType::Executable => {},
101                 _ => {
102                     sess.diagnostic().warn(
103                         &format!("LLVM unsupported, so output type {} is not supported", cty)
104                     );
105                 },
106             }
107         }
108     }
109
110     fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
111         box NoLlvmMetadataLoader
112     }
113
114     fn provide(&self, providers: &mut Providers) {
115         ::symbol_names::provide(providers);
116
117         providers.target_features_whitelist = |_tcx, _cnum| {
118             Default::default() // Just a dummy
119         };
120         providers.is_reachable_non_generic = |_tcx, _defid| true;
121         providers.exported_symbols = |_tcx, _crate| Arc::new(Vec::new());
122     }
123     fn provide_extern(&self, providers: &mut Providers) {
124         providers.is_reachable_non_generic = |_tcx, _defid| true;
125     }
126
127     fn codegen_crate<'a, 'tcx>(
128         &self,
129         tcx: TyCtxt<'a, 'tcx, 'tcx>,
130         _rx: mpsc::Receiver<Box<dyn Any + Send>>
131     ) -> Box<dyn Any> {
132         use rustc_mir::monomorphize::item::MonoItem;
133
134         ::check_for_rustc_errors_attr(tcx);
135         ::symbol_names_test::report_symbol_names(tcx);
136         ::rustc_incremental::assert_dep_graph(tcx);
137         ::rustc_incremental::assert_module_sources::assert_module_sources(tcx);
138         // FIXME: Fix this
139         // ::rustc::middle::dependency_format::calculate(tcx);
140         let _ = tcx.link_args(LOCAL_CRATE);
141         let _ = tcx.native_libraries(LOCAL_CRATE);
142         let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
143         for (mono_item, _) in cgus.iter().flat_map(|cgu| cgu.items().iter()) {
144             if let MonoItem::Fn(inst) = mono_item {
145                 let def_id = inst.def_id();
146                 if def_id.is_local() {
147                     let _ = tcx.codegen_fn_attrs(def_id);
148                 }
149             }
150         }
151         tcx.sess.abort_if_errors();
152
153         let metadata = tcx.encode_metadata();
154
155         box OngoingCodegen {
156             metadata,
157             metadata_version: tcx.metadata_encoding_version().to_vec(),
158             crate_name: tcx.crate_name(LOCAL_CRATE),
159         }
160     }
161
162     fn join_codegen_and_link(
163         &self,
164         ongoing_codegen: Box<dyn Any>,
165         sess: &Session,
166         _dep_graph: &DepGraph,
167         outputs: &OutputFilenames,
168     ) -> Result<(), CompileIncomplete> {
169         let ongoing_codegen = ongoing_codegen.downcast::<OngoingCodegen>()
170             .expect("Expected MetadataOnlyCodegenBackend's OngoingCodegen, found Box<dyn Any>");
171         for &crate_type in sess.opts.crate_types.iter() {
172             if crate_type != CrateType::Rlib &&
173                crate_type != CrateType::Dylib {
174                 continue;
175             }
176             let output_name =
177                 out_filename(sess, crate_type, &outputs, &ongoing_codegen.crate_name.as_str());
178             let mut compressed = ongoing_codegen.metadata_version.clone();
179             let metadata = if crate_type == CrateType::Dylib {
180                 DeflateEncoder::new(&mut compressed, Compression::fast())
181                     .write_all(&ongoing_codegen.metadata.raw_data)
182                     .unwrap();
183                 &compressed
184             } else {
185                 &ongoing_codegen.metadata.raw_data
186             };
187             fs::write(&output_name, metadata).unwrap();
188         }
189
190         sess.abort_if_errors();
191         if !sess.opts.crate_types.contains(&CrateType::Rlib)
192             && !sess.opts.crate_types.contains(&CrateType::Dylib)
193         {
194             sess.fatal("Executables are not supported by the metadata-only backend.");
195         }
196         Ok(())
197     }
198 }