Skip to content

Commit

Permalink
Rollup merge of #132820 - bjorn3:default_backend_link_impl, r=jieyouxu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr authored Nov 11, 2024
2 parents 517e8be + e8b1029 commit 35225d6
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 72 deletions.
12 changes: 0 additions & 12 deletions compiler/rustc_codegen_cranelift/src/archive.rs

This file was deleted.

13 changes: 0 additions & 13 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::back::versioned_llvm_target;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_errors::ErrorGuaranteed;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_session::Session;
Expand All @@ -56,7 +55,6 @@ use crate::prelude::*;
mod abi;
mod allocator;
mod analyze;
mod archive;
mod base;
mod cast;
mod codegen_i128;
Expand Down Expand Up @@ -249,17 +247,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
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 {
Expand Down
25 changes: 0 additions & 25 deletions compiler/rustc_codegen_gcc/src/archive.rs

This file was deleted.

14 changes: 1 addition & 13 deletions compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ extern crate rustc_driver;

mod abi;
mod allocator;
mod archive;
mod asm;
mod attributes;
mod back;
Expand Down Expand Up @@ -103,7 +102,7 @@ use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBacken
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::IntoDynSyncSend;
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
use rustc_errors::DiagCtxtHandle;
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -261,17 +260,6 @@ impl CodegenBackend for GccCodegenBackend {
.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> {
target_features(sess, allow_unstable, &self.target_info)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl CodegenBackend for LlvmCodegenBackend {

// Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library.
link_binary(sess, &LlvmArchiveBuilderBuilder, &codegen_results, outputs)
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, outputs)
}
}

Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ pub trait ArchiveBuilder {
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"]
pub struct ArArchiveBuilder<'a> {
sess: &'a Session,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn ensure_removed(dcx: DiagCtxtHandle<'_>, path: &Path) {
pub fn link_binary(
sess: &Session,
archive_builder_builder: &dyn ArchiveBuilderBuilder,
codegen_results: &CodegenResults,
codegen_results: CodegenResults,
outputs: &OutputFilenames,
) -> Result<(), ErrorGuaranteed> {
let _timer = sess.timer("link_binary");
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn link_binary(
link_rlib(
sess,
archive_builder_builder,
codegen_results,
&codegen_results,
RlibFlavor::Normal,
&path,
)?
Expand All @@ -126,7 +126,7 @@ pub fn link_binary(
link_staticlib(
sess,
archive_builder_builder,
codegen_results,
&codegen_results,
&out_filename,
&path,
)?;
Expand All @@ -137,7 +137,7 @@ pub fn link_binary(
archive_builder_builder,
crate_type,
&out_filename,
codegen_results,
&codegen_results,
path.as_ref(),
)?;
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_codegen_ssa/src/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use rustc_span::symbol::Symbol;

use super::CodegenObject;
use super::write::WriteBackendMethods;
use crate::back::archive::ArArchiveBuilderBuilder;
use crate::back::link::link_binary;
use crate::back::write::TargetMachineFactoryFn;
use crate::{CodegenResults, ModuleCodegen};

Expand Down Expand Up @@ -87,7 +89,9 @@ pub trait CodegenBackend {
sess: &Session,
codegen_results: CodegenResults,
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.
///
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ top_level_options!(
pub struct Options {
/// The crate config requested for the session, which may be combined
/// 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],
optimize: OptLevel [TRACKED],
/// Include the `debug_assertions` flag in dependency tracking, since it
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/internal-lints/bad_opt_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn access_bad_option(sess: Session) {
//~^ ERROR use `Session::split_debuginfo` instead of this field

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;
// okay!
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/internal-lints/bad_opt_access.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ note: the lint level is defined here
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
|
LL | let _ = sess.opts.crate_types;
Expand Down

0 comments on commit 35225d6

Please sign in to comment.