@@ -163,18 +163,6 @@ class FileHandler {
163
163
// / Read the current bundle and write the result into the stream \a OS.
164
164
virtual Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) = 0;
165
165
166
- // / Read the current bundle and write the result into the file \a FileName.
167
- // / The meaning of \a FileName depends on unbundling type - in some
168
- // / cases (type="oo") it will contain a list of actual outputs.
169
- virtual Error ReadBundle (StringRef FileName, MemoryBuffer &Input) {
170
- std::error_code EC;
171
- raw_fd_ostream OS (FileName, EC);
172
-
173
- if (EC)
174
- return createFileError (FileName, EC);
175
- return ReadBundle (OS, Input);
176
- }
177
-
178
166
// / Write the header of the bundled file to \a OS based on the information
179
167
// / gathered from \a Inputs.
180
168
virtual Error WriteHeader (raw_fd_ostream &OS,
@@ -354,8 +342,6 @@ class BinaryFileHandler final : public FileHandler {
354
342
return Error::success ();
355
343
}
356
344
357
- using FileHandler::ReadBundle; // to avoid hiding via the overload below
358
-
359
345
Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) final {
360
346
assert (CurBundleInfo != BundlesInfo.end () && " Invalid reader info!" );
361
347
StringRef FC = Input.getBuffer ();
@@ -638,23 +624,14 @@ class ObjectFileHandler final : public FileHandler {
638
624
639
625
Error ReadBundleEnd (MemoryBuffer &Input) final { return Error::success (); }
640
626
641
- Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) {
642
- llvm_unreachable (" must not be called for the ObjectFileHandler" );
643
- }
644
-
645
- Error ReadBundle (StringRef OutName, MemoryBuffer &Input) final {
627
+ Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) final {
646
628
assert (CurBundle != TripleToBundleInfo.end () &&
647
629
" all bundles have been read already" );
648
630
649
631
// TODO: temporary workaround to copy fat object to the host output until
650
632
// driver is fixed to correctly handle list file for the host bundle in
651
633
// 'oo' mode.
652
634
if (FilesType == " oo" && hasHostKind (CurBundle->getKey ())) {
653
- std::error_code EC;
654
- raw_fd_ostream OS (OutName, EC);
655
-
656
- if (EC)
657
- return createFileError (OutName, EC);
658
635
OS.write (Input.getBufferStart (), Input.getBufferSize ());
659
636
return Error::success ();
660
637
}
@@ -676,35 +653,13 @@ class ObjectFileHandler final : public FileHandler {
676
653
errc::invalid_argument,
677
654
" 'o' file type is requested, but the fat object contains multiple "
678
655
" device objects; use 'oo' instead" );
679
- std::string FileList;
680
656
681
657
// Iterate through individual objects and extract them
682
658
for (size_t I = 0 ; I < NumObjects; ++I) {
683
659
uint64_t ObjSize = SizeVec[I];
684
- // Flag for the special case used to "unbundle" host target object.
685
- bool HostTriple = ObjSize == 1 ;
686
-
687
- StringRef ObjFileName = OutName;
688
- SmallString<128 > Path;
689
-
690
- // If not in file list mode there is no need in a temporary file - output
691
- // goes directly to what was specified in -outputs. The same is true for
692
- // the host triple.
693
- if (FileListMode && !HostTriple) {
694
- std::error_code EC =
695
- sys::fs::createTemporaryFile (TempFileNameBase, " devo" , Path);
696
- ObjFileName = Path.data ();
697
-
698
- if (EC)
699
- return createFileError (ObjFileName, EC);
700
- }
701
- std::error_code EC;
702
- raw_fd_ostream OS (ObjFileName, EC);
703
-
704
- if (EC)
705
- return createFileError (ObjFileName, EC);
706
660
707
- if (HostTriple) {
661
+ // Flag for the special case used to "unbundle" host target object.
662
+ if (ObjSize == 1 ) {
708
663
// Copy fat object contents to the output when extracting host bundle.
709
664
710
665
// In the partially linked fat object multiple dummy host bundles were
@@ -716,26 +671,31 @@ class ObjectFileHandler final : public FileHandler {
716
671
OS.write (Input.getBufferStart (), Input.getBufferSize ());
717
672
break ;
718
673
}
719
- OS.write (ObjData, ObjSize);
720
674
721
675
if (FileListMode) {
676
+ SmallString<128 > ObjFileName;
677
+ std::error_code EC =
678
+ sys::fs::createTemporaryFile (TempFileNameBase, " devo" , ObjFileName);
679
+ if (EC)
680
+ return createFileError (ObjFileName, EC);
681
+
682
+ raw_fd_ostream ObjOS (ObjFileName, EC);
683
+ if (EC)
684
+ return createFileError (ObjFileName, EC);
685
+
686
+ ObjOS.write (ObjData, ObjSize);
687
+ if (ObjOS.has_error ())
688
+ return createFileError (ObjFileName, ObjOS.error ());
689
+
722
690
// add the written file name to the output list of files
723
- FileList = (Twine (FileList) + Twine (ObjFileName) + Twine (" \n " )).str ();
724
- }
691
+ OS << ObjFileName << " \n " ;
692
+ } else
693
+ OS.write (ObjData, ObjSize);
694
+
725
695
// Move "object data" pointer to the next object within the concatenated
726
696
// bundle.
727
697
ObjData += ObjSize;
728
698
}
729
- if (FileListMode) {
730
- // dump the list of files into the file list specified in -outputs for the
731
- // current target
732
- std::error_code EC;
733
- raw_fd_ostream OS1 (OutName, EC);
734
-
735
- if (EC)
736
- return createFileError (OutName, EC);
737
- OS1.write (FileList.data (), FileList.size ());
738
- }
739
699
return Error::success ();
740
700
}
741
701
@@ -939,8 +899,6 @@ class TextFileHandler final : public FileHandler {
939
899
return Error::success ();
940
900
}
941
901
942
- using FileHandler::ReadBundle; // to avoid hiding via the overload below
943
-
944
902
Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) final {
945
903
StringRef FC = Input.getBuffer ();
946
904
size_t BundleStart = ReadChars;
@@ -1056,7 +1014,7 @@ class ArchiveFileHandler final : public FileHandler {
1056
1014
1057
1015
Error ReadBundleEnd (MemoryBuffer &Input) override { return Error::success (); }
1058
1016
1059
- Error ReadBundle (StringRef OutName , MemoryBuffer &Input) override {
1017
+ Error ReadBundle (raw_fd_ostream &OS , MemoryBuffer &Input) override {
1060
1018
assert (CurrBundle->second && " attempt to extract nonexistent bundle" );
1061
1019
1062
1020
bool FileListMode = FilesType == " aoo" ;
@@ -1068,11 +1026,6 @@ class ArchiveFileHandler final : public FileHandler {
1068
1026
" 'ao' file type is requested, but the archive contains multiple "
1069
1027
" device objects; use 'aoo' instead" );
1070
1028
1071
- // In file-list mode archive unbundling produces multiple files, so output
1072
- // file is a file list where we write the unbundled object names.
1073
- SmallVector<char , 0u > FileListBuf;
1074
- raw_svector_ostream FileList{FileListBuf};
1075
-
1076
1029
// Read all children.
1077
1030
Error Err = Error::success ();
1078
1031
for (auto &C : Ar->children (Err)) {
@@ -1102,25 +1055,30 @@ class ArchiveFileHandler final : public FileHandler {
1102
1055
// This is the bundle we are looking for. Create temporary file where
1103
1056
// the device part will be extracted if we are in the file-list mode,
1104
1057
// or write directly to the output file otherwise.
1105
- SmallString<128u > ChildFileName;
1106
1058
if (FileListMode) {
1059
+ SmallString<128u > ChildFileName;
1107
1060
auto EC = sys::fs::createTemporaryFile (TempFileNameBase, " o" ,
1108
1061
ChildFileName);
1109
1062
if (EC)
1110
1063
return createFileError (ChildFileName, EC);
1111
- } else
1112
- ChildFileName = OutName;
1113
1064
1114
- // And extract the bundle.
1115
- if (Error Err = OFH.ReadBundle (ChildFileName, *Buf))
1116
- return Err;
1117
- if (Error Err = OFH.ReadBundleEnd (*Buf))
1118
- return Err;
1065
+ raw_fd_ostream ChildOS (ChildFileName, EC);
1066
+ if (EC)
1067
+ return createFileError (ChildFileName, EC);
1068
+
1069
+ if (Error Err = OFH.ReadBundle (ChildOS, *Buf))
1070
+ return Err;
1071
+
1072
+ if (ChildOS.has_error ())
1073
+ return createFileError (ChildFileName, ChildOS.error ());
1119
1074
1120
- if (FileListMode)
1121
1075
// Add temporary file name with the device part to the output file
1122
1076
// list.
1123
- FileList << ChildFileName << " \n " ;
1077
+ OS << ChildFileName << " \n " ;
1078
+ } else if (Error Err = OFH.ReadBundle (OS, *Buf))
1079
+ return Err;
1080
+ if (Error Err = OFH.ReadBundleEnd (*Buf))
1081
+ return Err;
1124
1082
}
1125
1083
NameOrErr = OFH.ReadBundleStart (*Buf);
1126
1084
if (!NameOrErr)
@@ -1129,22 +1087,9 @@ class ArchiveFileHandler final : public FileHandler {
1129
1087
}
1130
1088
if (Err)
1131
1089
return Err;
1132
-
1133
- if (FileListMode) {
1134
- // Dump file list to the output file.
1135
- std::error_code EC;
1136
- raw_fd_ostream OS (OutName, EC);
1137
- if (EC)
1138
- return createFileError (OutName, EC);
1139
- OS << FileList.str ();
1140
- }
1141
1090
return Error::success ();
1142
1091
}
1143
1092
1144
- Error ReadBundle (raw_fd_ostream &OS, MemoryBuffer &Input) override {
1145
- llvm_unreachable (" must not be called for the ArchiveFileHandler" );
1146
- }
1147
-
1148
1093
Error WriteHeader (raw_fd_ostream &OS,
1149
1094
ArrayRef<std::unique_ptr<MemoryBuffer>> Inputs) override {
1150
1095
llvm_unreachable (" unsupported for the ArchiveFileHandler" );
@@ -1323,7 +1268,11 @@ static Error UnbundleFiles() {
1323
1268
continue ;
1324
1269
1325
1270
// Check if the output file can be opened and copy the bundle to it.
1326
- if (Error Err = FH->ReadBundle (Output->second , Input))
1271
+ std::error_code EC;
1272
+ raw_fd_ostream OutputFile (Output->second , EC, sys::fs::OF_None);
1273
+ if (EC)
1274
+ return createFileError (Output->second , EC);
1275
+ if (Error Err = FH->ReadBundle (OutputFile, Input))
1327
1276
return Err;
1328
1277
if (Error Err = FH->ReadBundleEnd (Input))
1329
1278
return Err;
0 commit comments