diff --git a/Cargo.lock b/Cargo.lock index 5fcd9346354..bbce8baaace 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3875,6 +3875,7 @@ dependencies = [ "rustc_macros", "rustc_serialize", "rustc_span", + "rustc_structures", ] [[package]] diff --git a/compiler/rustc_attr_ir/src/data_structures.rs b/compiler/rustc_attr_ir/src/data_structures.rs index cd95d5ce0c8..d15f21d1cc4 100644 --- a/compiler/rustc_attr_ir/src/data_structures.rs +++ b/compiler/rustc_attr_ir/src/data_structures.rs @@ -16,7 +16,7 @@ use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash}; use rustc_span::def_id::DefId; use rustc_span::hygiene::Transparency; use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol}; -use rustc_structures::{CrateType, Limit, SanitizerSet}; +use rustc_structures::{CrateType, Limit, NativeLibKind, SanitizerSet}; use thin_vec::ThinVec; pub use crate::canonical_symbols::{CanonicalSymbol, CanonicalSymbols}; @@ -371,76 +371,6 @@ pub enum PeImportNameType { Undecorated, } -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[derive(Encodable, Decodable, PrintAttribute)] -#[derive(StableHash)] -pub enum NativeLibKind { - /// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC) - Static { - /// Whether to bundle objects from static library into produced rlib - bundle: Option, - /// Whether to link static library without throwing any object files away - whole_archive: Option, - /// Whether to export c static library symbols - export_symbols: Option, - }, - /// Dynamic library (e.g. `libfoo.so` on Linux) - /// or an import library corresponding to a dynamic library (e.g. `foo.lib` on Windows/MSVC). - Dylib { - /// Whether the dynamic library will be linked only if it satisfies some undefined symbols - as_needed: Option, - }, - /// Dynamic library (e.g. `foo.dll` on Windows) without a corresponding import library. - /// On Linux, it refers to a generated shared library stub. - RawDylib { - /// Whether the dynamic library will be linked only if it satisfies some undefined symbols - as_needed: Option, - }, - /// A macOS-specific kind of dynamic libraries. - Framework { - /// Whether the framework will be linked only if it satisfies some undefined symbols - as_needed: Option, - }, - /// Argument which is passed to linker, relative order with libraries and other arguments - /// is preserved - LinkArg, - - /// Module imported from WebAssembly - WasmImportModule, - - /// The library kind wasn't specified, `Dylib` is currently used as a default. - Unspecified, -} - -impl NativeLibKind { - pub fn has_modifiers(&self) -> bool { - match self { - NativeLibKind::Static { bundle, whole_archive, export_symbols } => { - bundle.is_some() || whole_archive.is_some() || export_symbols.is_some() - } - NativeLibKind::Dylib { as_needed } - | NativeLibKind::Framework { as_needed } - | NativeLibKind::RawDylib { as_needed } => as_needed.is_some(), - NativeLibKind::Unspecified - | NativeLibKind::LinkArg - | NativeLibKind::WasmImportModule => false, - } - } - - pub fn is_statically_included(&self) -> bool { - matches!(self, NativeLibKind::Static { .. }) - } - - pub fn is_dllimport(&self) -> bool { - matches!( - self, - NativeLibKind::Dylib { .. } - | NativeLibKind::RawDylib { .. } - | NativeLibKind::Unspecified - ) - } -} - #[derive(Debug, Encodable, Decodable, Clone, StableHash, PrintAttribute)] pub struct LinkEntry { pub span: Span, diff --git a/compiler/rustc_attr_ir/src/pretty_printing.rs b/compiler/rustc_attr_ir/src/pretty_printing.rs index 7abcd4c23e3..7a0e9180ec7 100644 --- a/compiler/rustc_attr_ir/src/pretty_printing.rs +++ b/compiler/rustc_attr_ir/src/pretty_printing.rs @@ -14,7 +14,7 @@ use rustc_data_structures::fx::FxIndexMap; use rustc_span::def_id::DefId; use rustc_span::hygiene::Transparency; use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol}; -use rustc_structures::{CrateType, Limit, SanitizerSet}; +use rustc_structures::{CrateType, Limit, NativeLibKind, SanitizerSet}; use thin_vec::ThinVec; /// This trait is used to print attributes in `rustc_hir_pretty`. @@ -208,4 +208,5 @@ print_debug!( DiffActivity, DiffMode, CrateType, + NativeLibKind, ); diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index 6d25e7fb001..d674149c225 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -7,6 +7,7 @@ use rustc_session::Session; use rustc_session::diagnostics::feature_err; use rustc_span::edition::Edition::Edition2024; use rustc_span::kw; +use rustc_structures::NativeLibKind; use rustc_target::spec::{Arch, BinaryFormat}; use super::prelude::*; diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index a5288d339cf..25003e071be 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -21,7 +21,6 @@ use rustc_data_structures::memmap::Mmap; use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_errors::DiagCtxtHandle; use rustc_fs_util::{TempDirBuilder, fix_windows_verbatim_for_gcc, try_canonicalize}; -use rustc_hir::attrs::NativeLibKind; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_lint_defs::builtin::{LINKER_INFO, LINKER_MESSAGES}; use rustc_macros::Diagnostic; @@ -43,7 +42,7 @@ use rustc_session::search_paths::PathKind; /// need out of the shared crate context before we get rid of it. use rustc_session::{Session, filesearch}; use rustc_span::Symbol; -use rustc_structures::CrateType; +use rustc_structures::{CrateType, NativeLibKind}; use rustc_target::spec::crt_objects::CrtObjects; use rustc_target::spec::{ Arch, BinaryFormat, Cc, CfgAbi, Env, LinkOutputKind, LinkSelfContainedComponents, diff --git a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs index 2857c4f28b2..13478f41723 100644 --- a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs +++ b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs @@ -8,9 +8,9 @@ use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::stable_hash::StableHasher; use rustc_hashes::Hash128; -use rustc_hir::attrs::NativeLibKind; use rustc_session::Session; use rustc_span::Symbol; +use rustc_structures::NativeLibKind; use rustc_target::spec::Arch; use crate::back::archive::ImportLibraryItem; diff --git a/compiler/rustc_codegen_ssa/src/back/rmeta_link.rs b/compiler/rustc_codegen_ssa/src/back/rmeta_link.rs index 69ec3788ac6..59139214146 100644 --- a/compiler/rustc_codegen_ssa/src/back/rmeta_link.rs +++ b/compiler/rustc_codegen_ssa/src/back/rmeta_link.rs @@ -8,11 +8,11 @@ use std::path::{Path, PathBuf}; use object::read::archive::ArchiveFile; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::memmap::Mmap; -use rustc_hir::attrs::NativeLibKind; use rustc_serialize::opaque::mem_encoder::MemEncoder; use rustc_serialize::opaque::{MAGIC_END_BYTES, MemDecoder}; use rustc_serialize::{Decodable, Encodable}; use rustc_span::Symbol; +use rustc_structures::NativeLibKind; use rustc_target::spec::Target; use tracing::debug; diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index 23cb46d5a66..bf2ce74e38d 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -22,7 +22,7 @@ use rustc_crate_store::{self as cstore, CrateSource}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_data_structures::unord::{UnordMap, UnordSet}; use rustc_hir::CRATE_HIR_ID; -use rustc_hir::attrs::{CfgEntry, NativeLibKind, WindowsSubsystemKind}; +use rustc_hir::attrs::{CfgEntry, WindowsSubsystemKind}; use rustc_hir::def_id::CrateNum; use rustc_lint_defs::builtin::{LINKER_INFO, LINKER_MESSAGES}; use rustc_macros::{Decodable, Encodable}; @@ -39,7 +39,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_session::Session; use rustc_session::config::{OutputFilenames, OutputType}; use rustc_span::{Span, Symbol}; -use rustc_structures::CrateType; +use rustc_structures::{CrateType, NativeLibKind}; pub mod assert_module_sources; pub mod back; diff --git a/compiler/rustc_crate_store/Cargo.toml b/compiler/rustc_crate_store/Cargo.toml index 7c84c26afe2..1857ae8130c 100644 --- a/compiler/rustc_crate_store/Cargo.toml +++ b/compiler/rustc_crate_store/Cargo.toml @@ -12,4 +12,5 @@ rustc_hir_id = { path = "../rustc_hir_id" } rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } +rustc_structures = { path = "../rustc_structures" } # tidy-alphabetical-end diff --git a/compiler/rustc_crate_store/src/cstore.rs b/compiler/rustc_crate_store/src/cstore.rs index e3b339a5ce8..fba2fc288f5 100644 --- a/compiler/rustc_crate_store/src/cstore.rs +++ b/compiler/rustc_crate_store/src/cstore.rs @@ -6,7 +6,7 @@ use std::any::Any; use std::path::PathBuf; use rustc_abi::ExternAbi; -use rustc_attr_ir::{CfgEntry, NativeLibKind, PeImportNameType}; +use rustc_attr_ir::{CfgEntry, PeImportNameType}; use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock}; use rustc_hir_id::definitions::{DefKey, DefPath, Definitions}; use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash}; @@ -14,6 +14,7 @@ use rustc_span::def_id::{ CrateNum, DefId, DefPathHash, LOCAL_CRATE, LocalDefId, StableCrateId, StableCrateIdMap, }; use rustc_span::{Span, Symbol}; +use rustc_structures::NativeLibKind; /// Where a crate came from on the local filesystem. One of these three options /// must be non-None. diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 138bb558472..87e0902b1f7 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -4,7 +4,7 @@ use rustc_crate_store::{ DllCallingConvention, DllImport, DllImportSymbolType, ForeignModule, NativeLib, }; use rustc_data_structures::fx::FxHashSet; -use rustc_hir::attrs::{NativeLibKind, PeImportNameType}; +use rustc_hir::attrs::PeImportNameType; use rustc_hir::def::DefKind; use rustc_hir::find_attr; use rustc_middle::bug; @@ -14,6 +14,7 @@ use rustc_middle::ty::{self, List, Ty, TyCtxt}; use rustc_session::Session; use rustc_span::Symbol; use rustc_span::def_id::{DefId, LOCAL_CRATE}; +use rustc_structures::NativeLibKind; use rustc_target::spec::{Arch, BinaryFormat, CfgAbi}; use crate::diagnostics; diff --git a/compiler/rustc_session/src/config/native_libs.rs b/compiler/rustc_session/src/config/native_libs.rs index d8c41fbae4c..0733bd3ca4b 100644 --- a/compiler/rustc_session/src/config/native_libs.rs +++ b/compiler/rustc_session/src/config/native_libs.rs @@ -4,8 +4,8 @@ //! (There is also a similar but separate syntax for `#[link]` attributes, //! which have their own parser in `rustc_metadata`.) -use rustc_attr_ir::NativeLibKind; use rustc_feature::UnstableFeatures; +use rustc_structures::NativeLibKind; use crate::EarlyDiagCtxt; use crate::config::UnstableOptions; diff --git a/compiler/rustc_session/src/utils.rs b/compiler/rustc_session/src/utils.rs index d1d0f18b2c4..42300d540ed 100644 --- a/compiler/rustc_session/src/utils.rs +++ b/compiler/rustc_session/src/utils.rs @@ -1,10 +1,10 @@ use std::path::PathBuf; use std::sync::OnceLock; -use rustc_attr_ir::NativeLibKind; use rustc_data_structures::profiling::VerboseTimingGuard; use rustc_fs_util::try_canonicalize; use rustc_macros::{Decodable, Encodable, StableHash}; +use rustc_structures::NativeLibKind; use crate::session::Session; diff --git a/compiler/rustc_structures/src/lib.rs b/compiler/rustc_structures/src/lib.rs index a769a28edff..f8adc211fa0 100644 --- a/compiler/rustc_structures/src/lib.rs +++ b/compiler/rustc_structures/src/lib.rs @@ -2,8 +2,10 @@ mod crate_type; mod limit; +mod native_lib_kind; mod sanitizer_set; pub use crate_type::CrateType; pub use limit::Limit; +pub use native_lib_kind::NativeLibKind; pub use sanitizer_set::SanitizerSet; diff --git a/compiler/rustc_structures/src/native_lib_kind.rs b/compiler/rustc_structures/src/native_lib_kind.rs new file mode 100644 index 00000000000..a232d7a3145 --- /dev/null +++ b/compiler/rustc_structures/src/native_lib_kind.rs @@ -0,0 +1,71 @@ +#[cfg(feature = "nightly")] +use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "nightly", derive(StableHash, Encodable_NoContext, Decodable_NoContext))] +pub enum NativeLibKind { + /// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC) + Static { + /// Whether to bundle objects from static library into produced rlib + bundle: Option, + /// Whether to link static library without throwing any object files away + whole_archive: Option, + /// Whether to export c static library symbols + export_symbols: Option, + }, + /// Dynamic library (e.g. `libfoo.so` on Linux) + /// or an import library corresponding to a dynamic library (e.g. `foo.lib` on Windows/MSVC). + Dylib { + /// Whether the dynamic library will be linked only if it satisfies some undefined symbols + as_needed: Option, + }, + /// Dynamic library (e.g. `foo.dll` on Windows) without a corresponding import library. + /// On Linux, it refers to a generated shared library stub. + RawDylib { + /// Whether the dynamic library will be linked only if it satisfies some undefined symbols + as_needed: Option, + }, + /// A macOS-specific kind of dynamic libraries. + Framework { + /// Whether the framework will be linked only if it satisfies some undefined symbols + as_needed: Option, + }, + /// Argument which is passed to linker, relative order with libraries and other arguments + /// is preserved + LinkArg, + + /// Module imported from WebAssembly + WasmImportModule, + + /// The library kind wasn't specified, `Dylib` is currently used as a default. + Unspecified, +} + +impl NativeLibKind { + pub fn has_modifiers(&self) -> bool { + match self { + NativeLibKind::Static { bundle, whole_archive, export_symbols } => { + bundle.is_some() || whole_archive.is_some() || export_symbols.is_some() + } + NativeLibKind::Dylib { as_needed } + | NativeLibKind::Framework { as_needed } + | NativeLibKind::RawDylib { as_needed } => as_needed.is_some(), + NativeLibKind::Unspecified + | NativeLibKind::LinkArg + | NativeLibKind::WasmImportModule => false, + } + } + + pub fn is_statically_included(&self) -> bool { + matches!(self, NativeLibKind::Static { .. }) + } + + pub fn is_dllimport(&self) -> bool { + matches!( + self, + NativeLibKind::Dylib { .. } + | NativeLibKind::RawDylib { .. } + | NativeLibKind::Unspecified + ) + } +}