11//! The AOT driver uses [`cranelift_object`] to write object files suitable for linking into a
22//! standalone executable.
33
4+ use std:: fs:: File ;
45use std:: path:: PathBuf ;
56use std:: sync:: Arc ;
67
@@ -81,11 +82,10 @@ fn make_module(sess: &Session, backend_config: &BackendConfig, name: String) ->
8182 ObjectModule :: new ( builder)
8283}
8384
84- fn emit_module (
85+ fn emit_cgu (
8586 tcx : TyCtxt < ' _ > ,
8687 backend_config : & BackendConfig ,
8788 name : String ,
88- kind : ModuleKind ,
8989 module : ObjectModule ,
9090 debug : Option < DebugContext < ' _ > > ,
9191 unwind_context : UnwindContext ,
@@ -99,42 +99,29 @@ fn emit_module(
9999
100100 unwind_context. emit ( & mut product) ;
101101
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 ( ) ) ;
110103
111104 let work_product = if backend_config. disable_incr_cache {
112105 None
113106 } else if let Some ( global_asm_object_file) = & global_asm_object_file {
114107 rustc_incremental:: copy_cgu_workproduct_to_incr_comp_cache_dir (
115108 tcx. sess ,
116109 & 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) ] ,
118111 )
119112 } else {
120113 rustc_incremental:: copy_cgu_workproduct_to_incr_comp_cache_dir (
121114 tcx. sess ,
122115 & name,
123- & [ ( "o" , & tmp_file ) ] ,
116+ & [ ( "o" , & module_regular . object . as_ref ( ) . unwrap ( ) ) ] ,
124117 )
125118 } ;
126119
127120 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,
135122 module_global_asm : global_asm_object_file. map ( |global_asm_object_file| CompiledModule {
136123 name : format ! ( "{name}.asm" ) ,
137- kind,
124+ kind : ModuleKind :: Regular ,
138125 object : Some ( global_asm_object_file) ,
139126 dwarf_object : None ,
140127 bytecode : None ,
@@ -143,6 +130,27 @@ fn emit_module(
143130 }
144131}
145132
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+
146154fn reuse_workproduct_for_cgu ( tcx : TyCtxt < ' _ > , cgu : & CodegenUnit < ' _ > ) -> ModuleCodegenResult {
147155 let work_product = cgu. previous_work_product ( tcx) ;
148156 let obj_out_regular =
@@ -261,11 +269,10 @@ fn module_codegen(
261269 let debug_context = cx. debug_context ;
262270 let unwind_context = cx. unwind_context ;
263271 let codegen_result = tcx. sess . time ( "write object file" , || {
264- emit_module (
272+ emit_cgu (
265273 tcx,
266274 & backend_config,
267275 cgu. name ( ) . as_str ( ) . to_string ( ) ,
268- ModuleKind :: Regular ,
269276 module,
270277 debug_context,
271278 unwind_context,
@@ -336,27 +343,10 @@ pub(crate) fn run_aot(
336343 crate :: allocator:: codegen ( tcx, & mut allocator_module, & mut allocator_unwind_context) ;
337344
338345 let allocator_module = if created_alloc_shim {
339- let name = "allocator_shim" . to_owned ( ) ;
340-
341346 let mut product = allocator_module. finish ( ) ;
342347 allocator_unwind_context. emit ( & mut product) ;
343348
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 ( ) ) )
360350 } else {
361351 None
362352 } ;
0 commit comments