Rollup merge of #148177 - bjorn3:codegen_backend_crate_types, r=WaffleLapkin
Allow codegen backends to indicate which crate types they support This way cargo will drop the unsupported crate types for crates that specify multiple crate types. This is a prerequisite for https://github.com/rust-lang/miri/pull/4648.
This commit is contained in:
@@ -10,7 +10,7 @@ use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_middle::util::Providers;
|
||||
use rustc_session::Session;
|
||||
use rustc_session::config::{self, OutputFilenames, PrintRequest};
|
||||
use rustc_session::config::{self, CrateType, OutputFilenames, PrintRequest};
|
||||
use rustc_span::Symbol;
|
||||
|
||||
use super::CodegenObject;
|
||||
@@ -62,6 +62,18 @@ pub trait CodegenBackend {
|
||||
}
|
||||
}
|
||||
|
||||
fn supported_crate_types(&self, _sess: &Session) -> Vec<CrateType> {
|
||||
vec![
|
||||
CrateType::Executable,
|
||||
CrateType::Dylib,
|
||||
CrateType::Rlib,
|
||||
CrateType::Staticlib,
|
||||
CrateType::Cdylib,
|
||||
CrateType::ProcMacro,
|
||||
CrateType::Sdylib,
|
||||
]
|
||||
}
|
||||
|
||||
fn print_passes(&self) {}
|
||||
|
||||
fn print_version(&self) {}
|
||||
|
||||
@@ -686,7 +686,8 @@ fn print_crate_info(
|
||||
};
|
||||
let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
|
||||
let crate_name = passes::get_crate_name(sess, attrs);
|
||||
let crate_types = collect_crate_types(sess, attrs);
|
||||
let crate_types =
|
||||
collect_crate_types(sess, &codegen_backend.supported_crate_types(sess), attrs);
|
||||
for &style in &crate_types {
|
||||
let fname = rustc_session::output::filename_for_input(
|
||||
sess, style, crate_name, &t_outputs,
|
||||
|
||||
@@ -116,8 +116,6 @@ use rustc_data_structures::unord::{UnordMap, UnordSet};
|
||||
use rustc_data_structures::{base_n, flock};
|
||||
use rustc_fs_util::{LinkOrCopy, link_or_copy, try_canonicalize};
|
||||
use rustc_middle::bug;
|
||||
use rustc_session::config::CrateType;
|
||||
use rustc_session::output::collect_crate_types;
|
||||
use rustc_session::{Session, StableCrateId};
|
||||
use rustc_span::Symbol;
|
||||
use tracing::debug;
|
||||
@@ -212,7 +210,11 @@ pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBu
|
||||
/// The garbage collection will take care of it.
|
||||
///
|
||||
/// [`rustc_interface::queries::dep_graph`]: ../../rustc_interface/struct.Queries.html#structfield.dep_graph
|
||||
pub(crate) fn prepare_session_directory(sess: &Session, crate_name: Symbol) {
|
||||
pub(crate) fn prepare_session_directory(
|
||||
sess: &Session,
|
||||
crate_name: Symbol,
|
||||
stable_crate_id: StableCrateId,
|
||||
) {
|
||||
if sess.opts.incremental.is_none() {
|
||||
return;
|
||||
}
|
||||
@@ -222,7 +224,7 @@ pub(crate) fn prepare_session_directory(sess: &Session, crate_name: Symbol) {
|
||||
debug!("prepare_session_directory");
|
||||
|
||||
// {incr-comp-dir}/{crate-name-and-disambiguator}
|
||||
let crate_dir = crate_path(sess, crate_name);
|
||||
let crate_dir = crate_path(sess, crate_name, stable_crate_id);
|
||||
debug!("crate-dir: {}", crate_dir.display());
|
||||
create_dir(sess, &crate_dir, "crate");
|
||||
|
||||
@@ -595,17 +597,9 @@ fn string_to_timestamp(s: &str) -> Result<SystemTime, &'static str> {
|
||||
Ok(UNIX_EPOCH + duration)
|
||||
}
|
||||
|
||||
fn crate_path(sess: &Session, crate_name: Symbol) -> PathBuf {
|
||||
fn crate_path(sess: &Session, crate_name: Symbol, stable_crate_id: StableCrateId) -> PathBuf {
|
||||
let incr_dir = sess.opts.incremental.as_ref().unwrap().clone();
|
||||
|
||||
let crate_types = collect_crate_types(sess, &[]);
|
||||
let stable_crate_id = StableCrateId::new(
|
||||
crate_name,
|
||||
crate_types.contains(&CrateType::Executable),
|
||||
sess.opts.cg.metadata.clone(),
|
||||
sess.cfg_version,
|
||||
);
|
||||
|
||||
let crate_name =
|
||||
format!("{crate_name}-{}", stable_crate_id.as_u64().to_base_fixed_len(CASE_INSENSITIVE));
|
||||
incr_dir.join(crate_name)
|
||||
|
||||
@@ -10,8 +10,8 @@ use rustc_middle::dep_graph::{DepGraph, DepsType, SerializedDepGraph, WorkProduc
|
||||
use rustc_middle::query::on_disk_cache::OnDiskCache;
|
||||
use rustc_serialize::Decodable;
|
||||
use rustc_serialize::opaque::MemDecoder;
|
||||
use rustc_session::Session;
|
||||
use rustc_session::config::IncrementalStateAssertion;
|
||||
use rustc_session::{Session, StableCrateId};
|
||||
use rustc_span::Symbol;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
@@ -208,9 +208,14 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache> {
|
||||
|
||||
/// Setups the dependency graph by loading an existing graph from disk and set up streaming of a
|
||||
/// new graph to an incremental session directory.
|
||||
pub fn setup_dep_graph(sess: &Session, crate_name: Symbol, deps: &DepsType) -> DepGraph {
|
||||
pub fn setup_dep_graph(
|
||||
sess: &Session,
|
||||
crate_name: Symbol,
|
||||
stable_crate_id: StableCrateId,
|
||||
deps: &DepsType,
|
||||
) -> DepGraph {
|
||||
// `load_dep_graph` can only be called after `prepare_session_directory`.
|
||||
prepare_session_directory(sess, crate_name);
|
||||
prepare_session_directory(sess, crate_name, stable_crate_id);
|
||||
|
||||
let res = sess.opts.build_dep_graph().then(|| load_dep_graph(sess, deps));
|
||||
|
||||
|
||||
@@ -926,7 +926,11 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
|
||||
let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
|
||||
|
||||
let crate_name = get_crate_name(sess, &pre_configured_attrs);
|
||||
let crate_types = collect_crate_types(sess, &pre_configured_attrs);
|
||||
let crate_types = collect_crate_types(
|
||||
sess,
|
||||
&compiler.codegen_backend.supported_crate_types(sess),
|
||||
&pre_configured_attrs,
|
||||
);
|
||||
let stable_crate_id = StableCrateId::new(
|
||||
crate_name,
|
||||
crate_types.contains(&CrateType::Executable),
|
||||
@@ -937,7 +941,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
|
||||
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
|
||||
|
||||
let dep_type = DepsType { dep_names: rustc_query_impl::dep_kind_names() };
|
||||
let dep_graph = setup_dep_graph(sess, crate_name, &dep_type);
|
||||
let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id, &dep_type);
|
||||
|
||||
let cstore =
|
||||
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
|
||||
|
||||
@@ -354,6 +354,14 @@ impl CodegenBackend for DummyCodegenBackend {
|
||||
"dummy"
|
||||
}
|
||||
|
||||
fn supported_crate_types(&self, _sess: &Session) -> Vec<CrateType> {
|
||||
// This includes bin despite failing on the link step to ensure that you
|
||||
// can still get the frontend handling for binaries. For all library
|
||||
// like crate types cargo will fallback to rlib unless you specifically
|
||||
// say that only a different crate type must be used.
|
||||
vec![CrateType::Rlib, CrateType::Executable]
|
||||
}
|
||||
|
||||
fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
|
||||
Box::new(CodegenResults {
|
||||
modules: vec![],
|
||||
@@ -380,12 +388,16 @@ impl CodegenBackend for DummyCodegenBackend {
|
||||
) {
|
||||
// JUSTIFICATION: TyCtxt no longer available here
|
||||
#[allow(rustc::bad_opt_access)]
|
||||
if sess.opts.crate_types.iter().any(|&crate_type| crate_type != CrateType::Rlib) {
|
||||
if let Some(&crate_type) = codegen_results
|
||||
.crate_info
|
||||
.crate_types
|
||||
.iter()
|
||||
.find(|&&crate_type| crate_type != CrateType::Rlib)
|
||||
{
|
||||
#[allow(rustc::untranslatable_diagnostic)]
|
||||
#[allow(rustc::diagnostic_outside_of_impl)]
|
||||
sess.dcx().fatal(format!(
|
||||
"crate type {} not supported by the dummy codegen backend",
|
||||
sess.opts.crate_types[0],
|
||||
"crate type {crate_type} not supported by the dummy codegen backend"
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,11 @@ pub fn categorize_crate_type(s: Symbol) -> Option<CrateType> {
|
||||
Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1)
|
||||
}
|
||||
|
||||
pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<CrateType> {
|
||||
pub fn collect_crate_types(
|
||||
session: &Session,
|
||||
backend_crate_types: &[CrateType],
|
||||
attrs: &[ast::Attribute],
|
||||
) -> Vec<CrateType> {
|
||||
// If we're generating a test executable, then ignore all other output
|
||||
// styles at all other locations
|
||||
if session.opts.test {
|
||||
@@ -219,7 +223,12 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<C
|
||||
}
|
||||
|
||||
base.retain(|crate_type| {
|
||||
if invalid_output_for_target(session, *crate_type) {
|
||||
if invalid_output_for_target(session, *crate_type)
|
||||
|| !backend_crate_types.contains(crate_type)
|
||||
{
|
||||
// FIXME provide a better warning for the case where the codegen
|
||||
// backend doesn't support it once cargo doesn't hard code this
|
||||
// warning message.
|
||||
session.dcx().emit_warn(errors::UnsupportedCrateTypeForTarget {
|
||||
crate_type: *crate_type,
|
||||
target_triple: &session.opts.target_triple,
|
||||
|
||||
Reference in New Issue
Block a user