Rollup merge of #132820 - bjorn3:default_backend_link_impl, r=jieyouxu

Add a default implementation for CodegenBackend::link

As a side effect this should add raw-dylib support to cg_gcc as the default ArchiveBuilderBuilder that is used implements create_dll_import_lib. I haven't tested if the raw-dylib support actually works however.
This commit is contained in:
Matthias Krüger 2024-11-11 21:58:32 +01:00 committed by GitHub
commit 35225d61f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 22 additions and 72 deletions

View File

@ -1,12 +0,0 @@
use rustc_codegen_ssa::back::archive::{
ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder, DEFAULT_OBJECT_READER,
};
use rustc_session::Session;
pub(crate) struct ArArchiveBuilderBuilder;
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
Box::new(ArArchiveBuilder::new(sess, &DEFAULT_OBJECT_READER))
}
}

View File

@ -43,7 +43,6 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::back::versioned_llvm_target; use rustc_codegen_ssa::back::versioned_llvm_target;
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::ErrorGuaranteed;
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_session::Session; use rustc_session::Session;
@ -56,7 +55,6 @@ use crate::prelude::*;
mod abi; mod abi;
mod allocator; mod allocator;
mod analyze; mod analyze;
mod archive;
mod base; mod base;
mod cast; mod cast;
mod codegen_i128; mod codegen_i128;
@ -249,17 +247,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
self.config.borrow().as_ref().unwrap(), self.config.borrow().as_ref().unwrap(),
) )
} }
fn link(
&self,
sess: &Session,
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed> {
use rustc_codegen_ssa::back::link::link_binary;
link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
}
} }
fn target_triple(sess: &Session) -> target_lexicon::Triple { fn target_triple(sess: &Session) -> target_lexicon::Triple {

View File

@ -1,25 +0,0 @@
use std::path::Path;
use rustc_codegen_ssa::back::archive::{
ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder, DEFAULT_OBJECT_READER,
ImportLibraryItem,
};
use rustc_session::Session;
pub(crate) struct ArArchiveBuilderBuilder;
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
Box::new(ArArchiveBuilder::new(sess, &DEFAULT_OBJECT_READER))
}
fn create_dll_import_lib(
&self,
_sess: &Session,
_lib_name: &str,
_items: Vec<ImportLibraryItem>,
_output_path: &Path,
) {
unimplemented!("creating dll imports is not yet supported");
}
}

View File

@ -58,7 +58,6 @@ extern crate rustc_driver;
mod abi; mod abi;
mod allocator; mod allocator;
mod archive;
mod asm; mod asm;
mod attributes; mod attributes;
mod back; mod back;
@ -103,7 +102,7 @@ use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBacken
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen}; use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::IntoDynSyncSend; use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed}; use rustc_errors::DiagCtxtHandle;
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
@ -261,17 +260,6 @@ impl CodegenBackend for GccCodegenBackend {
.join(sess) .join(sess)
} }
fn link(
&self,
sess: &Session,
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed> {
use rustc_codegen_ssa::back::link::link_binary;
link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
}
fn target_features(&self, sess: &Session, allow_unstable: bool) -> Vec<Symbol> { fn target_features(&self, sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
target_features(sess, allow_unstable, &self.target_info) target_features(sess, allow_unstable, &self.target_info)
} }

View File

@ -382,7 +382,7 @@ impl CodegenBackend for LlvmCodegenBackend {
// Run the linker on any artifacts that resulted from the LLVM run. // Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library. // This should produce either a finished executable or library.
link_binary(sess, &LlvmArchiveBuilderBuilder, &codegen_results, outputs) link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, outputs)
} }
} }

View File

@ -304,6 +304,14 @@ pub trait ArchiveBuilder {
fn build(self: Box<Self>, output: &Path) -> bool; fn build(self: Box<Self>, output: &Path) -> bool;
} }
pub struct ArArchiveBuilderBuilder;
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
Box::new(ArArchiveBuilder::new(sess, &DEFAULT_OBJECT_READER))
}
}
#[must_use = "must call build() to finish building the archive"] #[must_use = "must call build() to finish building the archive"]
pub struct ArArchiveBuilder<'a> { pub struct ArArchiveBuilder<'a> {
sess: &'a Session, sess: &'a Session,

View File

@ -69,7 +69,7 @@ pub fn ensure_removed(dcx: DiagCtxtHandle<'_>, path: &Path) {
pub fn link_binary( pub fn link_binary(
sess: &Session, sess: &Session,
archive_builder_builder: &dyn ArchiveBuilderBuilder, archive_builder_builder: &dyn ArchiveBuilderBuilder,
codegen_results: &CodegenResults, codegen_results: CodegenResults,
outputs: &OutputFilenames, outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
let _timer = sess.timer("link_binary"); let _timer = sess.timer("link_binary");
@ -116,7 +116,7 @@ pub fn link_binary(
link_rlib( link_rlib(
sess, sess,
archive_builder_builder, archive_builder_builder,
codegen_results, &codegen_results,
RlibFlavor::Normal, RlibFlavor::Normal,
&path, &path,
)? )?
@ -126,7 +126,7 @@ pub fn link_binary(
link_staticlib( link_staticlib(
sess, sess,
archive_builder_builder, archive_builder_builder,
codegen_results, &codegen_results,
&out_filename, &out_filename,
&path, &path,
)?; )?;
@ -137,7 +137,7 @@ pub fn link_binary(
archive_builder_builder, archive_builder_builder,
crate_type, crate_type,
&out_filename, &out_filename,
codegen_results, &codegen_results,
path.as_ref(), path.as_ref(),
)?; )?;
} }

View File

@ -16,6 +16,8 @@ use rustc_span::symbol::Symbol;
use super::CodegenObject; use super::CodegenObject;
use super::write::WriteBackendMethods; use super::write::WriteBackendMethods;
use crate::back::archive::ArArchiveBuilderBuilder;
use crate::back::link::link_binary;
use crate::back::write::TargetMachineFactoryFn; use crate::back::write::TargetMachineFactoryFn;
use crate::{CodegenResults, ModuleCodegen}; use crate::{CodegenResults, ModuleCodegen};
@ -87,7 +89,9 @@ pub trait CodegenBackend {
sess: &Session, sess: &Session,
codegen_results: CodegenResults, codegen_results: CodegenResults,
outputs: &OutputFilenames, outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed>; ) -> Result<(), ErrorGuaranteed> {
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs)
}
/// Returns `true` if this backend can be safely called from multiple threads. /// Returns `true` if this backend can be safely called from multiple threads.
/// ///

View File

@ -130,7 +130,7 @@ top_level_options!(
pub struct Options { pub struct Options {
/// The crate config requested for the session, which may be combined /// The crate config requested for the session, which may be combined
/// with additional crate configurations during the compile process. /// with additional crate configurations during the compile process.
#[rustc_lint_opt_deny_field_access("use `Session::crate_types` instead of this field")] #[rustc_lint_opt_deny_field_access("use `TyCtxt::crate_types` instead of this field")]
crate_types: Vec<CrateType> [TRACKED], crate_types: Vec<CrateType> [TRACKED],
optimize: OptLevel [TRACKED], optimize: OptLevel [TRACKED],
/// Include the `debug_assertions` flag in dependency tracking, since it /// Include the `debug_assertions` flag in dependency tracking, since it

View File

@ -15,7 +15,7 @@ pub fn access_bad_option(sess: Session) {
//~^ ERROR use `Session::split_debuginfo` instead of this field //~^ ERROR use `Session::split_debuginfo` instead of this field
let _ = sess.opts.crate_types; let _ = sess.opts.crate_types;
//~^ ERROR use `Session::crate_types` instead of this field //~^ ERROR use `TyCtxt::crate_types` instead of this field
let _ = sess.opts.crate_name; let _ = sess.opts.crate_name;
// okay! // okay!

View File

@ -10,7 +10,7 @@ note: the lint level is defined here
LL | #![deny(rustc::bad_opt_access)] LL | #![deny(rustc::bad_opt_access)]
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
error: use `Session::crate_types` instead of this field error: use `TyCtxt::crate_types` instead of this field
--> $DIR/bad_opt_access.rs:17:13 --> $DIR/bad_opt_access.rs:17:13
| |
LL | let _ = sess.opts.crate_types; LL | let _ = sess.opts.crate_types;