2017-09-16 17:27:29 +02:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! The Rust compiler.
|
|
|
|
//!
|
|
|
|
//! # Note
|
|
|
|
//!
|
|
|
|
//! This API is completely unstable and subject to change.
|
|
|
|
|
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
|
|
|
#![deny(warnings)]
|
|
|
|
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2017-09-23 14:46:15 +02:00
|
|
|
use std::any::Any;
|
2017-09-18 17:37:57 +02:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::{self, Cursor};
|
|
|
|
use std::fs::File;
|
2017-09-16 17:27:29 +02:00
|
|
|
use std::path::Path;
|
2017-09-23 14:46:15 +02:00
|
|
|
use std::sync::mpsc;
|
2017-09-16 17:27:29 +02:00
|
|
|
|
2018-03-03 06:17:06 +01:00
|
|
|
use rustc_data_structures::owning_ref::OwningRef;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2017-09-18 17:37:57 +02:00
|
|
|
use ar::{Archive, Builder, Header};
|
|
|
|
use flate2::Compression;
|
|
|
|
use flate2::write::DeflateEncoder;
|
|
|
|
|
|
|
|
use syntax::symbol::Symbol;
|
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2017-10-30 18:42:21 +01:00
|
|
|
use rustc::session::{Session, CompileIncomplete};
|
|
|
|
use rustc::session::config::{CrateType, OutputFilenames, PrintRequest};
|
2017-09-23 14:46:15 +02:00
|
|
|
use rustc::ty::TyCtxt;
|
2017-09-16 17:27:29 +02:00
|
|
|
use rustc::ty::maps::Providers;
|
2017-09-18 17:37:57 +02:00
|
|
|
use rustc::middle::cstore::EncodedMetadata;
|
2018-01-04 12:40:11 +01:00
|
|
|
use rustc::middle::cstore::MetadataLoader;
|
2017-12-19 18:01:19 +01:00
|
|
|
use rustc::dep_graph::DepGraph;
|
2017-12-08 21:18:21 +02:00
|
|
|
use rustc_target::spec::Target;
|
2018-04-05 08:02:11 -07:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2017-10-30 18:42:21 +01:00
|
|
|
use rustc_mir::monomorphize::collector;
|
2017-09-18 18:12:07 +02:00
|
|
|
use link::{build_link_meta, out_filename};
|
2017-09-16 17:27:29 +02:00
|
|
|
|
2018-03-03 06:17:06 +01:00
|
|
|
pub use rustc_data_structures::sync::MetadataRef;
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub trait CodegenBackend {
|
2018-01-22 07:29:24 -08:00
|
|
|
fn init(&self, _sess: &Session) {}
|
2017-10-30 18:42:21 +01:00
|
|
|
fn print(&self, _req: PrintRequest, _sess: &Session) {}
|
|
|
|
fn target_features(&self, _sess: &Session) -> Vec<Symbol> { vec![] }
|
2018-01-22 07:29:24 -08:00
|
|
|
fn print_passes(&self) {}
|
|
|
|
fn print_version(&self) {}
|
|
|
|
fn diagnostics(&self) -> &[(&'static str, &'static str)] { &[] }
|
2017-09-16 17:27:29 +02:00
|
|
|
|
2018-03-03 06:26:48 +01:00
|
|
|
fn metadata_loader(&self) -> Box<MetadataLoader + Sync>;
|
2017-10-30 18:42:21 +01:00
|
|
|
fn provide(&self, _providers: &mut Providers);
|
|
|
|
fn provide_extern(&self, _providers: &mut Providers);
|
2018-05-08 16:10:16 +03:00
|
|
|
fn codegen_crate<'a, 'tcx>(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2017-09-16 17:27:29 +02:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-09-23 14:46:15 +02:00
|
|
|
rx: mpsc::Receiver<Box<Any + Send>>
|
2017-10-30 18:42:21 +01:00
|
|
|
) -> Box<Any>;
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
/// This is called on the returned `Box<Any>` from `codegen_backend`
|
2017-10-30 18:42:21 +01:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2018-05-08 16:10:16 +03:00
|
|
|
/// Panics when the passed `Box<Any>` was not returned by `codegen_backend`.
|
|
|
|
fn join_codegen_and_link(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2018-05-08 16:10:16 +03:00
|
|
|
ongoing_codegen: Box<Any>,
|
2017-09-16 17:27:29 +02:00
|
|
|
sess: &Session,
|
2017-10-30 18:42:21 +01:00
|
|
|
dep_graph: &DepGraph,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> Result<(), CompileIncomplete>;
|
2017-09-16 17:27:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub struct DummyCodegenBackend;
|
2017-09-16 17:27:29 +02:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
impl CodegenBackend for DummyCodegenBackend {
|
2018-03-03 06:26:48 +01:00
|
|
|
fn metadata_loader(&self) -> Box<MetadataLoader + Sync> {
|
2017-09-16 17:27:29 +02:00
|
|
|
box DummyMetadataLoader(())
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
fn provide(&self, _providers: &mut Providers) {
|
2018-05-08 16:10:16 +03:00
|
|
|
bug!("DummyCodegenBackend::provide");
|
2017-09-23 14:46:15 +02:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
fn provide_extern(&self, _providers: &mut Providers) {
|
2018-05-08 16:10:16 +03:00
|
|
|
bug!("DummyCodegenBackend::provide_extern");
|
2017-09-16 17:27:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn codegen_crate<'a, 'tcx>(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2017-09-16 17:27:29 +02:00
|
|
|
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-09-23 14:46:15 +02:00
|
|
|
_rx: mpsc::Receiver<Box<Any + Send>>
|
2017-10-30 18:42:21 +01:00
|
|
|
) -> Box<Any> {
|
2018-05-08 16:10:16 +03:00
|
|
|
bug!("DummyCodegenBackend::codegen_backend");
|
2017-09-16 17:27:29 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn join_codegen_and_link(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2018-05-08 16:10:16 +03:00
|
|
|
_ongoing_codegen: Box<Any>,
|
2017-09-16 17:27:29 +02:00
|
|
|
_sess: &Session,
|
2017-10-30 18:42:21 +01:00
|
|
|
_dep_graph: &DepGraph,
|
|
|
|
_outputs: &OutputFilenames,
|
|
|
|
) -> Result<(), CompileIncomplete> {
|
2018-05-08 16:10:16 +03:00
|
|
|
bug!("DummyCodegenBackend::join_codegen_and_link");
|
2017-09-16 17:27:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DummyMetadataLoader(());
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
impl MetadataLoader for DummyMetadataLoader {
|
2017-09-23 15:23:40 +02:00
|
|
|
fn get_rlib_metadata(
|
|
|
|
&self,
|
|
|
|
_target: &Target,
|
|
|
|
_filename: &Path
|
2018-03-03 06:17:06 +01:00
|
|
|
) -> Result<MetadataRef, String> {
|
2017-09-16 17:27:29 +02:00
|
|
|
bug!("DummyMetadataLoader::get_rlib_metadata");
|
|
|
|
}
|
|
|
|
|
2017-09-23 15:23:40 +02:00
|
|
|
fn get_dylib_metadata(
|
|
|
|
&self,
|
|
|
|
_target: &Target,
|
|
|
|
_filename: &Path
|
2018-03-03 06:17:06 +01:00
|
|
|
) -> Result<MetadataRef, String> {
|
2017-09-16 17:27:29 +02:00
|
|
|
bug!("DummyMetadataLoader::get_dylib_metadata");
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 17:37:57 +02:00
|
|
|
|
|
|
|
pub struct NoLlvmMetadataLoader;
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
impl MetadataLoader for NoLlvmMetadataLoader {
|
2018-03-03 06:17:06 +01:00
|
|
|
fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
|
2017-09-18 17:37:57 +02:00
|
|
|
let file = File::open(filename)
|
|
|
|
.map_err(|e| format!("metadata file open err: {:?}", e))?;
|
|
|
|
let mut archive = Archive::new(file);
|
|
|
|
|
|
|
|
while let Some(entry_result) = archive.next_entry() {
|
|
|
|
let mut entry = entry_result
|
|
|
|
.map_err(|e| format!("metadata section read err: {:?}", e))?;
|
|
|
|
if entry.header().identifier() == "rust.metadata.bin" {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
io::copy(&mut entry, &mut buf).unwrap();
|
|
|
|
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
|
2018-03-03 06:17:06 +01:00
|
|
|
return Ok(rustc_erase_owner!(buf.map_owner_box()));
|
2017-09-18 17:37:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 15:56:50 +01:00
|
|
|
Err("Couldn't find metadata section".to_string())
|
2017-09-18 17:37:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_dylib_metadata(
|
|
|
|
&self,
|
|
|
|
_target: &Target,
|
|
|
|
_filename: &Path,
|
2018-03-03 06:17:06 +01:00
|
|
|
) -> Result<MetadataRef, String> {
|
2017-09-18 17:37:57 +02:00
|
|
|
// FIXME: Support reading dylibs from llvm enabled rustc
|
|
|
|
self.get_rlib_metadata(_target, _filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub struct MetadataOnlyCodegenBackend(());
|
|
|
|
pub struct OngoingCodegen {
|
2017-09-18 17:37:57 +02:00
|
|
|
metadata: EncodedMetadata,
|
|
|
|
metadata_version: Vec<u8>,
|
|
|
|
crate_name: Symbol,
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
impl MetadataOnlyCodegenBackend {
|
|
|
|
pub fn new() -> Box<CodegenBackend> {
|
|
|
|
box MetadataOnlyCodegenBackend(())
|
2018-01-22 07:29:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
impl CodegenBackend for MetadataOnlyCodegenBackend {
|
2018-01-22 07:29:24 -08:00
|
|
|
fn init(&self, sess: &Session) {
|
2017-10-30 18:42:21 +01:00
|
|
|
for cty in sess.opts.crate_types.iter() {
|
|
|
|
match *cty {
|
|
|
|
CrateType::CrateTypeRlib | CrateType::CrateTypeDylib |
|
|
|
|
CrateType::CrateTypeExecutable => {},
|
|
|
|
_ => {
|
|
|
|
sess.parse_sess.span_diagnostic.warn(
|
|
|
|
&format!("LLVM unsupported, so output type {} is not supported", cty)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 17:37:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-03 06:26:48 +01:00
|
|
|
fn metadata_loader(&self) -> Box<MetadataLoader + Sync> {
|
2017-09-18 17:37:57 +02:00
|
|
|
box NoLlvmMetadataLoader
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
fn provide(&self, providers: &mut Providers) {
|
|
|
|
::symbol_names::provide(providers);
|
2018-02-27 20:12:32 -05:00
|
|
|
|
|
|
|
providers.target_features_whitelist = |_tcx, _cnum| {
|
2018-04-05 08:02:11 -07:00
|
|
|
Lrc::new(FxHashMap()) // Just a dummy
|
2018-01-20 11:55:55 +01:00
|
|
|
};
|
2017-10-30 18:42:21 +01:00
|
|
|
}
|
|
|
|
fn provide_extern(&self, _providers: &mut Providers) {}
|
2017-09-18 17:37:57 +02:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn codegen_crate<'a, 'tcx>(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2017-09-18 17:37:57 +02:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-09-23 14:46:15 +02:00
|
|
|
_rx: mpsc::Receiver<Box<Any + Send>>
|
2017-10-30 18:42:21 +01:00
|
|
|
) -> Box<Any> {
|
|
|
|
use rustc_mir::monomorphize::item::MonoItem;
|
|
|
|
|
2017-09-23 18:15:58 +02:00
|
|
|
::check_for_rustc_errors_attr(tcx);
|
2017-10-30 18:42:21 +01:00
|
|
|
::symbol_names_test::report_symbol_names(tcx);
|
|
|
|
::rustc_incremental::assert_dep_graph(tcx);
|
|
|
|
::rustc_incremental::assert_module_sources::assert_module_sources(tcx);
|
|
|
|
::rustc_mir::monomorphize::assert_symbols_are_distinct(tcx,
|
|
|
|
collector::collect_crate_mono_items(
|
|
|
|
tcx,
|
|
|
|
collector::MonoItemCollectionMode::Eager
|
|
|
|
).0.iter()
|
|
|
|
);
|
|
|
|
::rustc::middle::dependency_format::calculate(tcx);
|
2017-09-23 18:15:58 +02:00
|
|
|
let _ = tcx.link_args(LOCAL_CRATE);
|
|
|
|
let _ = tcx.native_libraries(LOCAL_CRATE);
|
2018-05-08 16:10:16 +03:00
|
|
|
for mono_item in
|
2017-10-30 18:42:21 +01:00
|
|
|
collector::collect_crate_mono_items(
|
|
|
|
tcx,
|
|
|
|
collector::MonoItemCollectionMode::Eager
|
|
|
|
).0 {
|
2018-05-08 16:10:16 +03:00
|
|
|
match mono_item {
|
2017-10-30 18:42:21 +01:00
|
|
|
MonoItem::Fn(inst) => {
|
|
|
|
let def_id = inst.def_id();
|
|
|
|
if def_id.is_local() {
|
|
|
|
let _ = inst.def.is_inline(tcx);
|
2018-05-08 16:10:16 +03:00
|
|
|
let _ = tcx.codegen_fn_attrs(def_id);
|
2017-10-30 18:42:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 18:15:58 +02:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2017-12-19 18:01:19 +01:00
|
|
|
let link_meta = build_link_meta(tcx.crate_hash(LOCAL_CRATE));
|
2018-02-23 16:25:03 +01:00
|
|
|
let metadata = tcx.encode_metadata(&link_meta);
|
2017-09-18 17:37:57 +02:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
box OngoingCodegen {
|
2017-09-18 17:37:57 +02:00
|
|
|
metadata: metadata,
|
|
|
|
metadata_version: tcx.metadata_encoding_version().to_vec(),
|
|
|
|
crate_name: tcx.crate_name(LOCAL_CRATE),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn join_codegen_and_link(
|
2017-10-30 18:42:21 +01:00
|
|
|
&self,
|
2018-05-08 16:10:16 +03:00
|
|
|
ongoing_codegen: Box<Any>,
|
2017-10-30 18:42:21 +01:00
|
|
|
sess: &Session,
|
2017-09-18 17:37:57 +02:00
|
|
|
_dep_graph: &DepGraph,
|
2017-10-30 18:42:21 +01:00
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> Result<(), CompileIncomplete> {
|
2018-05-08 16:10:16 +03:00
|
|
|
let ongoing_codegen = ongoing_codegen.downcast::<OngoingCodegen>()
|
|
|
|
.expect("Expected MetadataOnlyCodegenBackend's OngoingCodegen, found Box<Any>");
|
2017-09-18 17:37:57 +02:00
|
|
|
for &crate_type in sess.opts.crate_types.iter() {
|
|
|
|
if crate_type != CrateType::CrateTypeRlib && crate_type != CrateType::CrateTypeDylib {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let output_name =
|
2018-05-08 16:10:16 +03:00
|
|
|
out_filename(sess, crate_type, &outputs, &ongoing_codegen.crate_name.as_str());
|
|
|
|
let mut compressed = ongoing_codegen.metadata_version.clone();
|
2017-09-18 17:37:57 +02:00
|
|
|
let metadata = if crate_type == CrateType::CrateTypeDylib {
|
2017-12-29 11:24:38 +01:00
|
|
|
DeflateEncoder::new(&mut compressed, Compression::fast())
|
2018-05-08 16:10:16 +03:00
|
|
|
.write_all(&ongoing_codegen.metadata.raw_data)
|
2017-09-18 17:37:57 +02:00
|
|
|
.unwrap();
|
|
|
|
&compressed
|
|
|
|
} else {
|
2018-05-08 16:10:16 +03:00
|
|
|
&ongoing_codegen.metadata.raw_data
|
2017-09-18 17:37:57 +02:00
|
|
|
};
|
|
|
|
let mut builder = Builder::new(File::create(&output_name).unwrap());
|
|
|
|
let header = Header::new("rust.metadata.bin".to_string(), metadata.len() as u64);
|
|
|
|
builder.append(&header, Cursor::new(metadata)).unwrap();
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:42:21 +01:00
|
|
|
sess.abort_if_errors();
|
2017-09-18 17:37:57 +02:00
|
|
|
if !sess.opts.crate_types.contains(&CrateType::CrateTypeRlib)
|
2017-10-30 18:42:21 +01:00
|
|
|
&& !sess.opts.crate_types.contains(&CrateType::CrateTypeDylib)
|
|
|
|
{
|
2017-09-18 17:37:57 +02:00
|
|
|
sess.fatal("Executables are not supported by the metadata-only backend.");
|
|
|
|
}
|
2017-10-30 18:42:21 +01:00
|
|
|
Ok(())
|
2017-09-18 17:37:57 +02:00
|
|
|
}
|
|
|
|
}
|