10
10
#include " BinaryHolder.h"
11
11
#include " DebugMap.h"
12
12
#include " LinkUtils.h"
13
+ #include " llvm/ADT/SmallString.h"
13
14
#include " llvm/CodeGen/NonRelocatableStringpool.h"
14
15
#include " llvm/MC/MCAsmLayout.h"
15
16
#include " llvm/MC/MCAssembler.h"
@@ -29,24 +30,30 @@ namespace dsymutil {
29
30
namespace MachOUtils {
30
31
31
32
llvm::Error ArchAndFile::createTempFile () {
32
- llvm::SmallString<128 > TmpModel;
33
- llvm::sys::path::system_temp_directory (true , TmpModel);
34
- llvm::sys::path::append (TmpModel, " dsym.tmp%%%%%.dwarf" );
35
- Expected<sys::fs::TempFile> T = sys::fs::TempFile::create (TmpModel);
33
+ SmallString<256 > SS;
34
+ std::error_code EC = sys::fs::createTemporaryFile (" dsym" , " dwarf" , FD, SS);
36
35
37
- if (!T)
38
- return T.takeError ();
36
+ if (EC)
37
+ return errorCodeToError (EC);
38
+
39
+ Path = SS.str ();
39
40
40
- File = std::make_unique<sys::fs::TempFile>(std::move (*T));
41
41
return Error::success ();
42
42
}
43
43
44
- llvm::StringRef ArchAndFile::path () const { return File->TmpName ; }
44
+ llvm::StringRef ArchAndFile::getPath () const {
45
+ assert (!Path.empty () && " path called before createTempFile" );
46
+ return Path;
47
+ }
48
+
49
+ int ArchAndFile::getFD () const {
50
+ assert ((FD != -1 ) && " path called before createTempFile" );
51
+ return FD;
52
+ }
45
53
46
54
ArchAndFile::~ArchAndFile () {
47
- if (File)
48
- if (auto E = File->discard ())
49
- llvm::consumeError (std::move (E));
55
+ if (!Path.empty ())
56
+ sys::fs::remove (Path);
50
57
}
51
58
52
59
std::string getArchName (StringRef Arch) {
@@ -82,11 +89,17 @@ bool generateUniversalBinary(SmallVectorImpl<ArchAndFile> &ArchFiles,
82
89
bool Fat64) {
83
90
// No need to merge one file into a universal fat binary.
84
91
if (ArchFiles.size () == 1 ) {
85
- if (auto E = ArchFiles.front ().File ->keep (OutputFileName)) {
86
- WithColor::error () << " while keeping " << ArchFiles.front ().path ()
87
- << " as " << OutputFileName << " : "
88
- << toString (std::move (E)) << " \n " ;
89
- return false ;
92
+ llvm::StringRef TmpPath = ArchFiles.front ().getPath ();
93
+ if (auto EC = sys::fs::rename (TmpPath, OutputFileName)) {
94
+ // If we can't rename, try to copy to work around cross-device link
95
+ // issues.
96
+ EC = sys::fs::copy_file (TmpPath, OutputFileName);
97
+ if (EC) {
98
+ WithColor::error () << " while keeping " << TmpPath << " as "
99
+ << OutputFileName << " : " << EC.message () << " \n " ;
100
+ return false ;
101
+ }
102
+ sys::fs::remove (TmpPath);
90
103
}
91
104
return true ;
92
105
}
@@ -96,7 +109,7 @@ bool generateUniversalBinary(SmallVectorImpl<ArchAndFile> &ArchFiles,
96
109
Args.push_back (" -create" );
97
110
98
111
for (auto &Thin : ArchFiles)
99
- Args.push_back (Thin.path ());
112
+ Args.push_back (Thin.getPath ());
100
113
101
114
// Align segments to match dsymutil-classic alignment.
102
115
for (auto &Thin : ArchFiles) {
0 commit comments