2024-06-21 20:16:42 +00:00
|
|
|
use std::any::Any;
|
2024-06-21 20:10:26 +00:00
|
|
|
use std::sync::Arc;
|
2019-03-26 18:07:13 +00:00
|
|
|
|
2022-04-02 15:26:39 +00:00
|
|
|
use rustc_codegen_ssa::CodegenResults;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
2020-10-10 13:20:35 +00:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2024-06-21 20:10:26 +00:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::dep_graph::DepGraph;
|
2024-10-31 15:46:11 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-11-20 02:26:09 +00:00
|
|
|
use rustc_session::Session;
|
2024-06-21 20:10:26 +00:00
|
|
|
use rustc_session::config::{self, OutputFilenames, OutputType};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
use crate::errors::FailedWritingFile;
|
2024-10-31 14:23:38 +00:00
|
|
|
use crate::passes;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2019-11-24 15:32:57 +00:00
|
|
|
pub struct Linker {
|
|
|
|
dep_graph: DepGraph,
|
2023-11-16 22:04:45 +00:00
|
|
|
output_filenames: Arc<OutputFilenames>,
|
2023-03-03 06:02:11 +00:00
|
|
|
// Only present when incr. comp. is enabled.
|
|
|
|
crate_hash: Option<Svh>,
|
2019-11-24 15:32:57 +00:00
|
|
|
ongoing_codegen: Box<dyn Any>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Linker {
|
2024-06-30 18:44:11 +00:00
|
|
|
pub fn codegen_and_build_linker(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
codegen_backend: &dyn CodegenBackend,
|
2024-11-03 16:45:22 +00:00
|
|
|
) -> Linker {
|
|
|
|
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx);
|
2024-06-30 18:44:11 +00:00
|
|
|
|
2024-11-03 16:45:22 +00:00
|
|
|
Linker {
|
2024-06-30 18:44:11 +00:00
|
|
|
dep_graph: tcx.dep_graph.clone(),
|
2024-10-07 19:22:51 +00:00
|
|
|
output_filenames: Arc::clone(tcx.output_filenames(())),
|
2024-06-30 18:44:11 +00:00
|
|
|
crate_hash: if tcx.needs_crate_hash() {
|
|
|
|
Some(tcx.crate_hash(LOCAL_CRATE))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
ongoing_codegen,
|
2024-11-03 16:45:22 +00:00
|
|
|
}
|
2024-06-30 18:44:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 16:45:22 +00:00
|
|
|
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) {
|
2024-11-09 17:58:29 +00:00
|
|
|
let (codegen_results, work_products) = sess.time("finish_ongoing_codegen", || {
|
|
|
|
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames)
|
|
|
|
});
|
2020-10-10 13:14:58 +00:00
|
|
|
|
2024-11-03 16:45:22 +00:00
|
|
|
sess.dcx().abort_if_errors();
|
2020-10-10 13:14:58 +00:00
|
|
|
|
2024-11-09 17:58:29 +00:00
|
|
|
let _timer = sess.timer("link");
|
|
|
|
|
2020-10-10 13:14:58 +00:00
|
|
|
sess.time("serialize_work_products", || {
|
2023-11-16 22:19:11 +00:00
|
|
|
rustc_incremental::save_work_product_index(sess, &self.dep_graph, work_products)
|
2020-10-10 13:14:58 +00:00
|
|
|
});
|
|
|
|
|
2023-11-16 21:15:36 +00:00
|
|
|
let prof = sess.prof.clone();
|
2023-11-16 22:19:11 +00:00
|
|
|
prof.generic_activity("drop_dep_graph").run(move || drop(self.dep_graph));
|
2020-01-28 13:16:14 +00:00
|
|
|
|
2020-10-10 13:20:35 +00:00
|
|
|
// Now that we won't touch anything in the incremental compilation directory
|
|
|
|
// any more, we can finalize it (which involves renaming it)
|
2023-11-16 21:15:36 +00:00
|
|
|
rustc_incremental::finalize_session_directory(sess, self.crate_hash);
|
2020-10-10 13:20:35 +00:00
|
|
|
|
2023-11-16 21:15:36 +00:00
|
|
|
if !sess
|
2020-01-28 13:16:14 +00:00
|
|
|
.opts
|
|
|
|
.output_types
|
|
|
|
.keys()
|
|
|
|
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
|
|
|
|
{
|
2024-11-03 16:45:22 +00:00
|
|
|
return;
|
2020-01-28 13:16:14 +00:00
|
|
|
}
|
2020-10-10 14:18:36 +00:00
|
|
|
|
2022-07-06 12:44:47 +00:00
|
|
|
if sess.opts.unstable_opts.no_link {
|
2023-11-16 22:04:45 +00:00
|
|
|
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
|
2023-11-04 15:49:57 +00:00
|
|
|
CodegenResults::serialize_rlink(
|
|
|
|
sess,
|
|
|
|
&rlink_file,
|
|
|
|
&codegen_results,
|
|
|
|
&*self.output_filenames,
|
|
|
|
)
|
2024-11-03 16:45:22 +00:00
|
|
|
.unwrap_or_else(|error| {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_fatal(FailedWritingFile { path: &rlink_file, error })
|
2024-11-03 16:45:22 +00:00
|
|
|
});
|
|
|
|
return;
|
2020-10-10 14:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 13:11:35 +00:00
|
|
|
let _timer = sess.prof.verbose_generic_activity("link_crate");
|
2023-11-16 22:04:45 +00:00
|
|
|
codegen_backend.link(sess, codegen_results, &self.output_filenames)
|
2019-11-24 15:32:57 +00:00
|
|
|
}
|
|
|
|
}
|