@@ -6,6 +6,8 @@ use rustc_codegen_ssa::back::archive::{
6
6
} ;
7
7
use rustc_session:: Session ;
8
8
9
+ struct UnsupportedTargetForRawDyLib ;
10
+
9
11
pub ( crate ) struct ArArchiveBuilderBuilder ;
10
12
11
13
impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
@@ -15,18 +17,26 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
15
17
16
18
fn create_dll_import_lib (
17
19
& self ,
18
- _sess : & Session ,
20
+ sess : & Session ,
19
21
lib_name : & str ,
20
22
dll_imports : & [ rustc_session:: cstore:: DllImport ] ,
21
23
tmpdir : & Path ,
22
24
_is_direct_dependency : bool ,
23
25
) -> 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
+ ) ;
25
37
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 {
30
40
symbol_name : import. name . to_string ( ) ,
31
41
ordinal_or_hint : import. ordinal ( ) ,
32
42
name_type : match import. import_name_type {
@@ -44,13 +54,32 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
44
54
}
45
55
} ,
46
56
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
+ ) ) ;
49
65
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
+ } ;
51
75
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
+ }
54
83
55
84
lib_path
56
85
}
0 commit comments