Skip to content

Commit de44e81

Browse files
committed
refactor: Merge collate_raw_dylibs_windows and collate_raw_dylibs_elf
1 parent 68baa87 commit de44e81

File tree

1 file changed

+13
-44
lines changed

1 file changed

+13
-44
lines changed

compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{NativeLib, common, errors};
2323
/// then the CodegenResults value contains one NativeLib instance for each block. However, the
2424
/// linker appears to expect only a single import library for each library used, so we need to
2525
/// collate the symbols together by library name before generating the import libraries.
26-
fn collate_raw_dylibs_windows<'a>(
26+
fn collate_raw_dylibs<'a>(
2727
sess: &Session,
2828
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
2929
) -> Vec<(String, Vec<DllImport>)> {
@@ -32,14 +32,21 @@ fn collate_raw_dylibs_windows<'a>(
3232

3333
for lib in used_libraries {
3434
if lib.kind == NativeLibKind::RawDylib {
35-
let ext = if lib.verbatim { "" } else { ".dll" };
36-
let name = format!("{}{}", lib.name, ext);
35+
let name = if lib.verbatim {
36+
lib.name.as_str().to_owned()
37+
} else {
38+
let prefix = sess.target.dll_prefix.as_ref();
39+
let suffix = sess.target.dll_suffix.as_ref();
40+
format!("{prefix}{}{suffix}", lib.name)
41+
};
3742
let imports = dylib_table.entry(name.clone()).or_default();
3843
for import in &lib.dll_imports {
3944
if let Some(old_import) = imports.insert(import.name, import) {
4045
// FIXME: when we add support for ordinals, figure out if we need to do anything
4146
// if we have two DllImport values with the same name but different ordinals.
42-
if import.calling_convention != old_import.calling_convention {
47+
if sess.target.is_like_windows
48+
&& import.calling_convention != old_import.calling_convention
49+
{
4350
sess.dcx().emit_err(errors::MultipleExternalFuncDecl {
4451
span: import.span,
4552
function: import.name,
@@ -66,7 +73,7 @@ pub(super) fn create_raw_dylib_dll_import_libs<'a>(
6673
tmpdir: &Path,
6774
is_direct_dependency: bool,
6875
) -> Vec<PathBuf> {
69-
collate_raw_dylibs_windows(sess, used_libraries)
76+
collate_raw_dylibs(sess, used_libraries)
7077
.into_iter()
7178
.map(|(raw_dylib_name, raw_dylib_imports)| {
7279
let name_suffix = if is_direct_dependency { "_imports" } else { "_imports_indirect" };
@@ -119,50 +126,12 @@ pub(super) fn create_raw_dylib_dll_import_libs<'a>(
119126
.collect()
120127
}
121128

122-
/// Extract all symbols defined in raw-dylib libraries, collated by library name.
123-
///
124-
/// If we have multiple extern blocks that specify symbols defined in the same raw-dylib library,
125-
/// then the CodegenResults value contains one NativeLib instance for each block. However, the
126-
/// linker appears to expect only a single import library for each library used, so we need to
127-
/// collate the symbols together by library name before generating the import libraries.
128-
fn collate_raw_dylibs_elf<'a>(
129-
sess: &Session,
130-
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
131-
) -> Vec<(String, Vec<DllImport>)> {
132-
// Use index maps to preserve original order of imports and libraries.
133-
let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
134-
135-
for lib in used_libraries {
136-
if lib.kind == NativeLibKind::RawDylib {
137-
let filename = if lib.verbatim {
138-
lib.name.as_str().to_owned()
139-
} else {
140-
let ext = sess.target.dll_suffix.as_ref();
141-
let prefix = sess.target.dll_prefix.as_ref();
142-
format!("{prefix}{}{ext}", lib.name)
143-
};
144-
145-
let imports = dylib_table.entry(filename.clone()).or_default();
146-
for import in &lib.dll_imports {
147-
imports.insert(import.name, import);
148-
}
149-
}
150-
}
151-
sess.dcx().abort_if_errors();
152-
dylib_table
153-
.into_iter()
154-
.map(|(name, imports)| {
155-
(name, imports.into_iter().map(|(_, import)| import.clone()).collect())
156-
})
157-
.collect()
158-
}
159-
160129
pub(super) fn create_raw_dylib_elf_stub_shared_objects<'a>(
161130
sess: &Session,
162131
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
163132
raw_dylib_so_dir: &Path,
164133
) -> Vec<String> {
165-
collate_raw_dylibs_elf(sess, used_libraries)
134+
collate_raw_dylibs(sess, used_libraries)
166135
.into_iter()
167136
.map(|(load_filename, raw_dylib_imports)| {
168137
use std::hash::Hash;

0 commit comments

Comments
 (0)