1
- use rustc:: middle:: cstore:: MetadataLoader ;
1
+ use std:: fs:: File ;
2
+ use std:: path:: Path ;
3
+
4
+ use rustc:: session:: config;
5
+ use rustc:: ty:: TyCtxt ;
6
+ use rustc:: middle:: cstore:: { EncodedMetadata , MetadataLoader } ;
2
7
use rustc_codegen_ssa:: METADATA_FILENAME ;
3
8
use rustc_data_structures:: owning_ref:: { self , OwningRef } ;
4
9
use rustc_data_structures:: rustc_erase_owner;
5
- use std:: fs:: File ;
6
- use std:: path:: Path ;
10
+ use rustc_target:: spec:: Target ;
7
11
8
12
pub struct CraneliftMetadataLoader ;
9
13
10
14
impl MetadataLoader for CraneliftMetadataLoader {
11
15
fn get_rlib_metadata (
12
16
& self ,
13
- _target : & crate :: rustc_target :: spec :: Target ,
17
+ _target : & Target ,
14
18
path : & Path ,
15
19
) -> Result < owning_ref:: ErasedBoxRef < [ u8 ] > , String > {
16
20
let mut archive = ar:: Archive :: new ( File :: open ( path) . map_err ( |e| format ! ( "{:?}" , e) ) ?) ;
@@ -31,9 +35,62 @@ impl MetadataLoader for CraneliftMetadataLoader {
31
35
32
36
fn get_dylib_metadata (
33
37
& self ,
34
- _target : & crate :: rustc_target :: spec :: Target ,
35
- _path : & Path ,
38
+ _target : & Target ,
39
+ path : & Path ,
36
40
) -> Result < owning_ref:: ErasedBoxRef < [ u8 ] > , String > {
37
- Err ( "dylib metadata loading is not yet supported" . to_string ( ) )
41
+ use object:: Object ;
42
+ let file = std:: fs:: read ( path) . map_err ( |e| format ! ( "read:{:?}" , e) ) ?;
43
+ let file = object:: File :: parse ( & file) . map_err ( |e| format ! ( "parse: {:?}" , e) ) ?;
44
+ let buf = file. section_data_by_name ( ".rustc" ) . ok_or ( "no .rustc section" ) ?. into_owned ( ) ;
45
+ let buf: OwningRef < Vec < u8 > , [ u8 ] > = OwningRef :: new ( buf) . into ( ) ;
46
+ Ok ( rustc_erase_owner ! ( buf. map_owner_box( ) ) )
38
47
}
39
48
}
49
+
50
+ // Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
51
+ pub fn write_metadata < ' a , ' gcx > (
52
+ tcx : TyCtxt < ' a , ' gcx , ' gcx > ,
53
+ artifact : & mut faerie:: Artifact
54
+ ) -> EncodedMetadata {
55
+ use std:: io:: Write ;
56
+ use flate2:: Compression ;
57
+ use flate2:: write:: DeflateEncoder ;
58
+
59
+ #[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
60
+ enum MetadataKind {
61
+ None ,
62
+ Uncompressed ,
63
+ Compressed
64
+ }
65
+
66
+ let kind = tcx. sess . crate_types . borrow ( ) . iter ( ) . map ( |ty| {
67
+ match * ty {
68
+ config:: CrateType :: Executable |
69
+ config:: CrateType :: Staticlib |
70
+ config:: CrateType :: Cdylib => MetadataKind :: None ,
71
+
72
+ config:: CrateType :: Rlib => MetadataKind :: Uncompressed ,
73
+
74
+ config:: CrateType :: Dylib |
75
+ config:: CrateType :: ProcMacro => MetadataKind :: Compressed ,
76
+ }
77
+ } ) . max ( ) . unwrap_or ( MetadataKind :: None ) ;
78
+
79
+ if kind == MetadataKind :: None {
80
+ return EncodedMetadata :: new ( ) ;
81
+ }
82
+
83
+ let metadata = tcx. encode_metadata ( ) ;
84
+ if kind == MetadataKind :: Uncompressed {
85
+ return metadata;
86
+ }
87
+
88
+ assert ! ( kind == MetadataKind :: Compressed ) ;
89
+ let mut compressed = tcx. metadata_encoding_version ( ) ;
90
+ DeflateEncoder :: new ( & mut compressed, Compression :: fast ( ) )
91
+ . write_all ( & metadata. raw_data ) . unwrap ( ) ;
92
+
93
+ artifact. declare_with ( ".rustc" , faerie:: Decl :: debug_section ( ) , compressed) . unwrap ( ) ;
94
+
95
+ metadata
96
+ }
0 commit comments