@@ -820,6 +820,10 @@ pub(crate) unsafe fn codegen(
820820 let bc_out = cgcx. output_filenames . temp_path ( OutputType :: Bitcode , module_name) ;
821821 let obj_out = cgcx. output_filenames . temp_path ( OutputType :: Object , module_name) ;
822822
823+ // Ensure we don't overwrite any hard links from previous compilation sessions.
824+ ensure_removed ( dcx, & bc_out) ;
825+ ensure_removed ( dcx, & obj_out) ;
826+
823827 if config. bitcode_needed ( ) {
824828 if config. emit_bc || config. emit_obj == EmitObj :: Bitcode {
825829 let thin = {
@@ -860,8 +864,11 @@ pub(crate) unsafe fn codegen(
860864 if config. emit_ir {
861865 let _timer =
862866 cgcx. prof . generic_activity_with_arg ( "LLVM_module_codegen_emit_ir" , & * module. name ) ;
863- let out = cgcx. output_filenames . temp_path ( OutputType :: LlvmAssembly , module_name) ;
864- let out_c = path_to_c_string ( & out) ;
867+ let ir_out = cgcx. output_filenames . temp_path ( OutputType :: LlvmAssembly , module_name) ;
868+ // Ensure we don't overwrite any hard links from previous compilation sessions.
869+ ensure_removed ( dcx, & ir_out) ;
870+
871+ let out_c = path_to_c_string ( & ir_out) ;
865872
866873 extern "C" fn demangle_callback (
867874 input_ptr : * const c_char ,
@@ -893,16 +900,20 @@ pub(crate) unsafe fn codegen(
893900 unsafe { llvm:: LLVMRustPrintModule ( llmod, out_c. as_ptr ( ) , demangle_callback) } ;
894901
895902 if result == llvm:: LLVMRustResult :: Success {
896- record_artifact_size ( & cgcx. prof , "llvm_ir" , & out ) ;
903+ record_artifact_size ( & cgcx. prof , "llvm_ir" , & ir_out ) ;
897904 }
898905
899- result. into_result ( ) . map_err ( |( ) | llvm_err ( dcx, LlvmError :: WriteIr { path : & out } ) ) ?;
906+ result
907+ . into_result ( )
908+ . map_err ( |( ) | llvm_err ( dcx, LlvmError :: WriteIr { path : & ir_out } ) ) ?;
900909 }
901910
902911 if config. emit_asm {
903912 let _timer =
904913 cgcx. prof . generic_activity_with_arg ( "LLVM_module_codegen_emit_asm" , & * module. name ) ;
905- let path = cgcx. output_filenames . temp_path ( OutputType :: Assembly , module_name) ;
914+ let asm_out = cgcx. output_filenames . temp_path ( OutputType :: Assembly , module_name) ;
915+ // Ensure we don't overwrite any hard links from previous compilation sessions.
916+ ensure_removed ( dcx, & asm_out) ;
906917
907918 // We can't use the same module for asm and object code output,
908919 // because that triggers various errors like invalid IR or broken
@@ -918,7 +929,7 @@ pub(crate) unsafe fn codegen(
918929 tm. raw ( ) ,
919930 config. no_builtins ,
920931 llmod,
921- & path ,
932+ & asm_out ,
922933 None ,
923934 llvm:: FileType :: AssemblyFile ,
924935 & cgcx. prof ,
@@ -933,6 +944,9 @@ pub(crate) unsafe fn codegen(
933944 . generic_activity_with_arg ( "LLVM_module_codegen_emit_obj" , & * module. name ) ;
934945
935946 let dwo_out = cgcx. output_filenames . temp_path_dwo ( module_name) ;
947+ // Ensure we don't overwrite any hard links from previous compilation sessions.
948+ ensure_removed ( dcx, & dwo_out) ;
949+
936950 let dwo_out = match ( cgcx. split_debuginfo , cgcx. split_dwarf_kind ) {
937951 // Don't change how DWARF is emitted when disabled.
938952 ( SplitDebuginfo :: Off , _) => None ,
0 commit comments