Use fixed type for CodegenResults

This also moves the -Zno-link implementation to rustc_interface
This commit is contained in:
bjorn3 2020-10-10 16:18:36 +02:00
parent f141acf067
commit 69f26b7761
4 changed files with 23 additions and 27 deletions

View File

@ -28,14 +28,12 @@ use rustc_errors::{ErrorReported, FatalError, Handler};
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
use rustc_middle::ty::{self, TyCtxt};
use rustc_serialize::json;
use rustc_session::config::{self, OptLevel, OutputFilenames, PrintRequest};
use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest};
use rustc_session::Session;
use rustc_span::symbol::Symbol;
use std::any::Any;
use std::ffi::CStr;
use std::fs;
use std::sync::Arc;
mod back {
@ -275,7 +273,7 @@ impl CodegenBackend for LlvmCodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
let (codegen_results, work_products) = ongoing_codegen
.downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
.expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
@ -284,31 +282,15 @@ impl CodegenBackend for LlvmCodegenBackend {
rustc_codegen_ssa::back::write::dump_incremental_data(&codegen_results);
}
Ok((Box::new(codegen_results), work_products))
Ok((codegen_results, work_products))
}
fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported> {
let codegen_results = codegen_results
.downcast::<CodegenResults>()
.expect("Expected CodegenResults, found Box<Any>");
if sess.opts.debugging_opts.no_link {
// FIXME: use a binary format to encode the `.rlink` file
let rlink_data = json::encode(&codegen_results).map_err(|err| {
sess.fatal(&format!("failed to encode rlink: {}", err));
})?;
let rlink_file = outputs.with_extension(config::RLINK_EXT);
fs::write(&rlink_file, rlink_data).map_err(|err| {
sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
})?;
return Ok(());
}
// Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library.
sess.time("link_crate", || {

View File

@ -1,6 +1,6 @@
use super::write::WriteBackendMethods;
use super::CodegenObject;
use crate::ModuleCodegen;
use crate::{CodegenResults, ModuleCodegen};
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_data_structures::fx::FxHashMap;
@ -81,7 +81,7 @@ pub trait CodegenBackend {
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
) -> Result<(Box<dyn Any>, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported>;
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
///
@ -91,7 +91,7 @@ pub trait CodegenBackend {
fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported>;
}

View File

@ -599,7 +599,7 @@ impl RustcDefaultCalls {
let codegen_results: CodegenResults = json::decode(&rlink_data).unwrap_or_else(|err| {
sess.fatal(&format!("failed to decode rlink: {}", err));
});
compiler.codegen_backend().link(&sess, Box::new(codegen_results), &outputs)
compiler.codegen_backend().link(&sess, codegen_results, &outputs)
} else {
sess.fatal("rlink must be a file")
}

View File

@ -14,7 +14,8 @@ use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::steal::Steal;
use rustc_middle::ty::{GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_session::config::{OutputFilenames, OutputType};
use rustc_serialize::json;
use rustc_session::config::{self, OutputFilenames, OutputType};
use rustc_session::{output::find_crate_name, Session};
use rustc_span::symbol::sym;
use std::any::Any;
@ -387,6 +388,19 @@ impl Linker {
{
return Ok(());
}
if sess.opts.debugging_opts.no_link {
// FIXME: use a binary format to encode the `.rlink` file
let rlink_data = json::encode(&codegen_results).map_err(|err| {
sess.fatal(&format!("failed to encode rlink: {}", err));
})?;
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
std::fs::write(&rlink_file, rlink_data).map_err(|err| {
sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
})?;
return Ok(());
}
self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
}
}