1
1
//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
2
2
//! standalone executable.
3
3
4
+ use std:: fs:: File ;
4
5
use std:: path:: PathBuf ;
5
6
use std:: sync:: Arc ;
6
7
@@ -81,11 +82,10 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
81
82
ObjectModule :: new ( builder)
82
83
}
83
84
84
- fn emit_module (
85
+ fn emit_cgu (
85
86
tcx : TyCtxt < ' _ > ,
86
87
backend_config : & BackendConfig ,
87
88
name : String ,
88
- kind : ModuleKind ,
89
89
module : ObjectModule ,
90
90
debug : Option < DebugContext < ' _ > > ,
91
91
unwind_context : UnwindContext ,
@@ -99,42 +99,29 @@ fn emit_module(
99
99
100
100
unwind_context. emit ( & mut product) ;
101
101
102
- let tmp_file = tcx. output_filenames ( ( ) ) . temp_path ( OutputType :: Object , Some ( & name) ) ;
103
- let obj = product. object . write ( ) . unwrap ( ) ;
104
-
105
- tcx. sess . prof . artifact_size ( "object_file" , name. clone ( ) , obj. len ( ) . try_into ( ) . unwrap ( ) ) ;
106
-
107
- if let Err ( err) = std:: fs:: write ( & tmp_file, obj) {
108
- tcx. sess . fatal ( & format ! ( "error writing object file: {}" , err) ) ;
109
- }
102
+ let module_regular = emit_module ( tcx, product. object , ModuleKind :: Regular , name. clone ( ) ) ;
110
103
111
104
let work_product = if backend_config. disable_incr_cache {
112
105
None
113
106
} else if let Some ( global_asm_object_file) = & global_asm_object_file {
114
107
rustc_incremental:: copy_cgu_workproduct_to_incr_comp_cache_dir (
115
108
tcx. sess ,
116
109
& name,
117
- & [ ( "o" , & tmp_file ) , ( "asm.o" , global_asm_object_file) ] ,
110
+ & [ ( "o" , & module_regular . object . as_ref ( ) . unwrap ( ) ) , ( "asm.o" , global_asm_object_file) ] ,
118
111
)
119
112
} else {
120
113
rustc_incremental:: copy_cgu_workproduct_to_incr_comp_cache_dir (
121
114
tcx. sess ,
122
115
& name,
123
- & [ ( "o" , & tmp_file ) ] ,
116
+ & [ ( "o" , & module_regular . object . as_ref ( ) . unwrap ( ) ) ] ,
124
117
)
125
118
} ;
126
119
127
120
ModuleCodegenResult {
128
- module_regular : CompiledModule {
129
- name : name. clone ( ) ,
130
- kind,
131
- object : Some ( tmp_file) ,
132
- dwarf_object : None ,
133
- bytecode : None ,
134
- } ,
121
+ module_regular,
135
122
module_global_asm : global_asm_object_file. map ( |global_asm_object_file| CompiledModule {
136
123
name : format ! ( "{name}.asm" ) ,
137
- kind,
124
+ kind : ModuleKind :: Regular ,
138
125
object : Some ( global_asm_object_file) ,
139
126
dwarf_object : None ,
140
127
bytecode : None ,
@@ -143,6 +130,27 @@ fn emit_module(
143
130
}
144
131
}
145
132
133
+ fn emit_module (
134
+ tcx : TyCtxt < ' _ > ,
135
+ object : cranelift_object:: object:: write:: Object < ' _ > ,
136
+ kind : ModuleKind ,
137
+ name : String ,
138
+ ) -> CompiledModule {
139
+ let tmp_file = tcx. output_filenames ( ( ) ) . temp_path ( OutputType :: Object , Some ( & name) ) ;
140
+ let mut file = match File :: create ( & tmp_file) {
141
+ Ok ( file) => file,
142
+ Err ( err) => tcx. sess . fatal ( & format ! ( "error creating object file: {}" , err) ) ,
143
+ } ;
144
+
145
+ if let Err ( err) = object. write_stream ( & mut file) {
146
+ tcx. sess . fatal ( & format ! ( "error writing object file: {}" , err) ) ;
147
+ }
148
+
149
+ tcx. sess . prof . artifact_size ( "object_file" , & * name, file. metadata ( ) . unwrap ( ) . len ( ) ) ;
150
+
151
+ CompiledModule { name, kind, object : Some ( tmp_file) , dwarf_object : None , bytecode : None }
152
+ }
153
+
146
154
fn reuse_workproduct_for_cgu ( tcx : TyCtxt < ' _ > , cgu : & CodegenUnit < ' _ > ) -> ModuleCodegenResult {
147
155
let work_product = cgu. previous_work_product ( tcx) ;
148
156
let obj_out_regular =
@@ -261,11 +269,10 @@ fn module_codegen(
261
269
let debug_context = cx. debug_context ;
262
270
let unwind_context = cx. unwind_context ;
263
271
let codegen_result = tcx. sess . time ( "write object file" , || {
264
- emit_module (
272
+ emit_cgu (
265
273
tcx,
266
274
& backend_config,
267
275
cgu. name ( ) . as_str ( ) . to_string ( ) ,
268
- ModuleKind :: Regular ,
269
276
module,
270
277
debug_context,
271
278
unwind_context,
@@ -336,27 +343,10 @@ pub(crate) fn run_aot(
336
343
crate :: allocator:: codegen ( tcx, & mut allocator_module, & mut allocator_unwind_context) ;
337
344
338
345
let allocator_module = if created_alloc_shim {
339
- let name = "allocator_shim" . to_owned ( ) ;
340
-
341
346
let mut product = allocator_module. finish ( ) ;
342
347
allocator_unwind_context. emit ( & mut product) ;
343
348
344
- let tmp_file = tcx. output_filenames ( ( ) ) . temp_path ( OutputType :: Object , Some ( & name) ) ;
345
- let obj = product. object . write ( ) . unwrap ( ) ;
346
-
347
- tcx. sess . prof . artifact_size ( "object_file" , & * name, obj. len ( ) . try_into ( ) . unwrap ( ) ) ;
348
-
349
- if let Err ( err) = std:: fs:: write ( & tmp_file, obj) {
350
- tcx. sess . fatal ( & format ! ( "error writing object file: {}" , err) ) ;
351
- }
352
-
353
- Some ( CompiledModule {
354
- name,
355
- kind : ModuleKind :: Allocator ,
356
- object : Some ( tmp_file) ,
357
- dwarf_object : None ,
358
- bytecode : None ,
359
- } )
349
+ Some ( emit_module ( tcx, product. object , ModuleKind :: Allocator , "allocator_shim" . to_owned ( ) ) )
360
350
} else {
361
351
None
362
352
} ;
0 commit comments