Skip to content

Commit 326067e

Browse files
committed
Some restructuring to lift the important bits up to archive.rs.
- Adds explicit checks for suported target - Pass in architecture/machine - Use a builder rather than a single function API for the library. - Use session errors rather than panicking at the top level.
1 parent 58325ad commit 326067e

File tree

3 files changed

+255
-189
lines changed

3 files changed

+255
-189
lines changed

src/archive.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_codegen_ssa::back::archive::{
66
};
77
use rustc_session::Session;
88

9+
struct UnsupportedTargetForRawDyLib;
10+
911
pub(crate) struct ArArchiveBuilderBuilder;
1012

1113
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
@@ -15,18 +17,26 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
1517

1618
fn create_dll_import_lib(
1719
&self,
18-
_sess: &Session,
20+
sess: &Session,
1921
lib_name: &str,
2022
dll_imports: &[rustc_session::cstore::DllImport],
2123
tmpdir: &Path,
2224
_is_direct_dependency: bool,
2325
) -> PathBuf {
24-
let lib_path = tmpdir.join(format!("{lib_name}_import.lib"));
26+
if sess.target.arch != "x86_64" || !sess.target.is_like_msvc {
27+
sess.span_fatal(
28+
dll_imports.iter().map(|import| import.span).collect::<Vec<_>>(),
29+
"cranelift codegen currently only supports raw_dylib on x86_64 msvc targets.",
30+
)
31+
}
32+
33+
let mut import_lib = crate::dll_import_lib::ImportLibraryBuilder::new(
34+
lib_name,
35+
crate::dll_import_lib::Machine::X86_64,
36+
);
2537

26-
// todo: use the same DllImport type?
27-
let import_lib_imports = dll_imports
28-
.into_iter()
29-
.map(|import| crate::dll_import_lib::Import {
38+
for import in dll_imports {
39+
import_lib.add_import(crate::dll_import_lib::Import {
3040
symbol_name: import.name.to_string(),
3141
ordinal_or_hint: import.ordinal(),
3242
name_type: match import.import_name_type {
@@ -44,13 +54,32 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
4454
}
4555
},
4656
import_type: crate::dll_import_lib::ImportType::Code,
47-
})
48-
.collect::<Vec<_>>();
57+
});
58+
}
59+
60+
let lib_path = tmpdir.join(format!(
61+
"{prefix}{lib_name}_import{suffix}",
62+
prefix = sess.target.staticlib_prefix,
63+
suffix = sess.target.staticlib_suffix,
64+
));
4965

50-
let import_lib = crate::dll_import_lib::generate(lib_name, &import_lib_imports);
66+
let mut file = match fs::OpenOptions::new().write(true).create_new(true).open(&lib_path) {
67+
Ok(file) => file,
68+
Err(error) => {
69+
sess.fatal(format!(
70+
"failed to create import library file `{path}`: {error}",
71+
path = lib_path.display(),
72+
));
73+
}
74+
};
5175

52-
// todo: emit session error instead of expects
53-
fs::write(&lib_path, import_lib).expect("failed to write import library");
76+
// import_lib.write() internally uses BufWriter, so we don't need anything here.
77+
if let Err(error) = import_lib.write(&mut file) {
78+
sess.fatal(format!(
79+
"failed to write import library `{path}`: {error}",
80+
path = lib_path.display(),
81+
));
82+
}
5483

5584
lib_path
5685
}

0 commit comments

Comments
 (0)