Chris Lattner | 97f752f | 2003-12-30 07:45:46 +0000 | [diff] [blame] | 1 | //===-- llvm-ar.cpp - LLVM archive librarian utility ----------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 10 | // Builds up (relatively) standard unix archive files (.a) containing LLVM |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 11 | // bitcode or other files. |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 4fa9fd3 | 2003-10-10 18:47:08 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 17 | #include "llvm/Object/Archive.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
Michael J. Spencer | 54453f2 | 2011-01-10 02:34:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 22 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Signals.h" |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/ToolOutputFile.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 28 | #include <cstdlib> |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include <memory> |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 31 | |
| 32 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 33 | #include <unistd.h> |
| 34 | #else |
| 35 | #include <io.h> |
| 36 | #endif |
| 37 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 40 | // The name this program was invoked as. |
| 41 | static StringRef ToolName; |
| 42 | |
| 43 | static const char *TemporaryOutput; |
| 44 | |
| 45 | // fail - Show the error message and exit. |
| 46 | LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) { |
| 47 | outs() << ToolName << ": " << Error << ".\n"; |
| 48 | if (TemporaryOutput) |
| 49 | sys::fs::remove(TemporaryOutput); |
| 50 | exit(1); |
| 51 | } |
| 52 | |
| 53 | static void failIfError(error_code EC, Twine Context = "") { |
| 54 | if (!EC) |
| 55 | return; |
| 56 | |
| 57 | std::string ContextStr = Context.str(); |
| 58 | if (ContextStr == "") |
| 59 | fail(EC.message()); |
| 60 | fail(Context + ": " + EC.message()); |
| 61 | } |
| 62 | |
Dan Gohman | 82f209e | 2007-10-15 21:10:03 +0000 | [diff] [blame] | 63 | // Option for compatibility with AIX, not used but must allow it to be present. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | X32Option ("X32_64", cl::Hidden, |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 66 | cl::desc("Ignored option for compatibility with AIX")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 67 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 68 | // llvm-ar operation code and modifier flags. This must come first. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 69 | static cl::opt<std::string> |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 70 | Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 71 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 72 | // llvm-ar remaining positional arguments. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 73 | static cl::list<std::string> |
| 74 | RestOfArgs(cl::Positional, cl::OneOrMore, |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 75 | cl::desc("[relpos] [count] <archive-file> [members]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 76 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 77 | // MoreHelp - Provide additional help output explaining the operations and |
| 78 | // modifiers of llvm-ar. This object instructs the CommandLine library |
| 79 | // to print the text of the constructor when the --help option is given. |
| 80 | static cl::extrahelp MoreHelp( |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 81 | "\nOPERATIONS:\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 82 | " d[NsS] - delete file(s) from the archive\n" |
| 83 | " m[abiSs] - move file(s) in the archive\n" |
| 84 | " p[kN] - print file(s) found in the archive\n" |
| 85 | " q[ufsS] - quick append file(s) to the archive\n" |
Rafael Espindola | 94bc246 | 2012-08-10 01:57:52 +0000 | [diff] [blame] | 86 | " r[abfiuRsS] - replace or insert file(s) into the archive\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 87 | " t - display contents of archive\n" |
| 88 | " x[No] - extract file(s) from the archive\n" |
| 89 | "\nMODIFIERS (operation specific):\n" |
| 90 | " [a] - put file(s) after [relpos]\n" |
| 91 | " [b] - put file(s) before [relpos] (same as [i])\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 92 | " [i] - put file(s) before [relpos] (same as [b])\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 93 | " [N] - use instance [count] of name\n" |
| 94 | " [o] - preserve original dates\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 95 | " [s] - create an archive index (cf. ranlib)\n" |
| 96 | " [S] - do not build a symbol table\n" |
| 97 | " [u] - update only files newer than archive contents\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 98 | "\nMODIFIERS (generic):\n" |
| 99 | " [c] - do not warn if the library had to be created\n" |
| 100 | " [v] - be verbose about actions taken\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 101 | ); |
| 102 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 103 | // This enumeration delineates the kinds of operations on an archive |
| 104 | // that are permitted. |
| 105 | enum ArchiveOperation { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 106 | Print, ///< Print the contents of the archive |
| 107 | Delete, ///< Delete the specified members |
| 108 | Move, ///< Move members to end or as given by {a,b,i} modifiers |
| 109 | QuickAppend, ///< Quickly append to end of archive |
| 110 | ReplaceOrInsert, ///< Replace or Insert members |
| 111 | DisplayTable, ///< Display the table of contents |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 112 | Extract ///< Extract files back to file system |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 113 | }; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 114 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 115 | // Modifiers to follow operation to vary behavior |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 116 | static bool AddAfter = false; ///< 'a' modifier |
| 117 | static bool AddBefore = false; ///< 'b' modifier |
| 118 | static bool Create = false; ///< 'c' modifier |
| 119 | static bool OriginalDates = false; ///< 'o' modifier |
| 120 | static bool OnlyUpdate = false; ///< 'u' modifier |
| 121 | static bool Verbose = false; ///< 'v' modifier |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 122 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 123 | // Relative Positional Argument (for insert/move). This variable holds |
| 124 | // the name of the archive member to which the 'a', 'b' or 'i' modifier |
| 125 | // refers. Only one of 'a', 'b' or 'i' can be specified so we only need |
| 126 | // one variable. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 127 | static std::string RelPos; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 128 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 129 | // This variable holds the name of the archive file as given on the |
| 130 | // command line. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 131 | static std::string ArchiveName; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 132 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 133 | // This variable holds the list of member files to proecess, as given |
| 134 | // on the command line. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 135 | static std::vector<std::string> Members; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 136 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 137 | // show_help - Show the error message, the help message and exit. |
| 138 | LLVM_ATTRIBUTE_NORETURN static void |
| 139 | show_help(const std::string &msg) { |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 140 | errs() << ToolName << ": " << msg << "\n\n"; |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 141 | cl::PrintHelpMessage(); |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 142 | std::exit(1); |
| 143 | } |
| 144 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 145 | // getRelPos - Extract the member filename from the command line for |
| 146 | // the [relpos] argument associated with a, b, and i modifiers |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 147 | static void getRelPos() { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 148 | if(RestOfArgs.size() == 0) |
| 149 | show_help("Expected [relpos] for a, b, or i modifier"); |
| 150 | RelPos = RestOfArgs[0]; |
| 151 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 154 | // getArchive - Get the archive file name from the command line |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 155 | static void getArchive() { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 156 | if(RestOfArgs.size() == 0) |
| 157 | show_help("An archive name must be specified"); |
| 158 | ArchiveName = RestOfArgs[0]; |
| 159 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 162 | // getMembers - Copy over remaining items in RestOfArgs to our Members vector |
| 163 | // This is just for clarity. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 164 | static void getMembers() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 165 | if(RestOfArgs.size() > 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 166 | Members = std::vector<std::string>(RestOfArgs); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 169 | // parseCommandLine - Parse the command line options as presented and return the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 170 | // operation specified. Process all modifiers and check to make sure that |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 171 | // constraints on modifier/operation pairs have not been violated. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 172 | static ArchiveOperation parseCommandLine() { |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 173 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 174 | // Keep track of number of operations. We can only specify one |
| 175 | // per execution. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 176 | unsigned NumOperations = 0; |
| 177 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 178 | // Keep track of the number of positional modifiers (a,b,i). Only |
| 179 | // one can be specified. |
| 180 | unsigned NumPositional = 0; |
| 181 | |
| 182 | // Keep track of which operation was requested |
Rafael Espindola | 87b8a7f9 | 2013-07-05 12:12:43 +0000 | [diff] [blame] | 183 | ArchiveOperation Operation; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 184 | |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 185 | for(unsigned i=0; i<Options.size(); ++i) { |
| 186 | switch(Options[i]) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 187 | case 'd': ++NumOperations; Operation = Delete; break; |
| 188 | case 'm': ++NumOperations; Operation = Move ; break; |
| 189 | case 'p': ++NumOperations; Operation = Print; break; |
Seo Sanghyeon | dc32d19 | 2007-12-25 13:53:47 +0000 | [diff] [blame] | 190 | case 'q': ++NumOperations; Operation = QuickAppend; break; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 191 | case 'r': ++NumOperations; Operation = ReplaceOrInsert; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 192 | case 't': ++NumOperations; Operation = DisplayTable; break; |
| 193 | case 'x': ++NumOperations; Operation = Extract; break; |
| 194 | case 'c': Create = true; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 195 | case 'l': /* accepted but unused */ break; |
| 196 | case 'o': OriginalDates = true; break; |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 197 | case 's': break; // Ignore for now. |
| 198 | case 'S': break; // Ignore for now. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 199 | case 'u': OnlyUpdate = true; break; |
| 200 | case 'v': Verbose = true; break; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 201 | case 'a': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 202 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 203 | AddAfter = true; |
| 204 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 205 | break; |
| 206 | case 'b': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 207 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 208 | AddBefore = true; |
| 209 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 210 | break; |
| 211 | case 'i': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 212 | getRelPos(); |
Rafael Espindola | 1b3e3ee | 2013-07-11 15:54:53 +0000 | [diff] [blame] | 213 | AddBefore = true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 214 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 215 | break; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 216 | default: |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 217 | cl::PrintHelpMessage(); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 221 | // At this point, the next thing on the command line must be |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 222 | // the archive name. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 223 | getArchive(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 224 | |
| 225 | // Everything on the command line at this point is a member. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 226 | getMembers(); |
| 227 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 228 | // Perform various checks on the operation/modifier specification |
| 229 | // to make sure we are dealing with a legal request. |
| 230 | if (NumOperations == 0) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 231 | show_help("You must specify at least one of the operations"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 232 | if (NumOperations > 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 233 | show_help("Only one operation may be specified"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 234 | if (NumPositional > 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 235 | show_help("You may only specify one of a, b, and i modifiers"); |
Rafael Espindola | 1b3e3ee | 2013-07-11 15:54:53 +0000 | [diff] [blame] | 236 | if (AddAfter || AddBefore) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 237 | if (Operation != Move && Operation != ReplaceOrInsert) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 238 | show_help("The 'a', 'b' and 'i' modifiers can only be specified with " |
| 239 | "the 'm' or 'r' operations"); |
| 240 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 241 | if (OriginalDates && Operation != Extract) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 242 | show_help("The 'o' modifier is only applicable to the 'x' operation"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 243 | if (OnlyUpdate && Operation != ReplaceOrInsert) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 244 | show_help("The 'u' modifier is only applicable to the 'r' operation"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 245 | |
| 246 | // Return the parsed operation to the caller |
| 247 | return Operation; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 248 | } |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 249 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 250 | // Implements the 'p' operation. This function traverses the archive |
| 251 | // looking for members that match the path list. |
| 252 | static void doPrint(StringRef Name, object::Archive::child_iterator I) { |
| 253 | if (Verbose) |
| 254 | outs() << "Printing " << Name << "\n"; |
Rafael Espindola | 8fe960e | 2013-06-19 14:58:16 +0000 | [diff] [blame] | 255 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 256 | StringRef Data = I->getBuffer(); |
| 257 | outs().write(Data.data(), Data.size()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // putMode - utility function for printing out the file mode when the 't' |
| 261 | // operation is in verbose mode. |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 262 | static void printMode(unsigned mode) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 263 | if (mode & 004) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 264 | outs() << "r"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 265 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 266 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 267 | if (mode & 002) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 268 | outs() << "w"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 269 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 270 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 271 | if (mode & 001) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 272 | outs() << "x"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 273 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 274 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 277 | // Implement the 't' operation. This function prints out just |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 278 | // the file names of each of the members. However, if verbose mode is requested |
| 279 | // ('v' modifier) then the file type, permission mode, user, group, size, and |
| 280 | // modification time are also printed. |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 281 | static void doDisplayTable(StringRef Name, object::Archive::child_iterator I) { |
| 282 | if (Verbose) { |
| 283 | sys::fs::perms Mode = I->getAccessMode(); |
| 284 | printMode((Mode >> 6) & 007); |
| 285 | printMode((Mode >> 3) & 007); |
| 286 | printMode(Mode & 007); |
| 287 | outs() << ' ' << I->getUID(); |
| 288 | outs() << '/' << I->getGID(); |
| 289 | outs() << ' ' << format("%6llu", I->getSize()); |
| 290 | outs() << ' ' << I->getLastModified().str(); |
| 291 | outs() << ' '; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 292 | } |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 293 | outs() << Name << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 296 | // Implement the 'x' operation. This function extracts files back to the file |
| 297 | // system. |
| 298 | static void doExtract(StringRef Name, object::Archive::child_iterator I) { |
| 299 | // Open up a file stream for writing |
| 300 | // FIXME: we should abstract this, O_BINARY in particular. |
| 301 | int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT; |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 302 | #ifdef O_BINARY |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 303 | OpenFlags |= O_BINARY; |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 304 | #endif |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 305 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 306 | // Retain the original mode. |
| 307 | sys::fs::perms Mode = I->getAccessMode(); |
Rafael Espindola | 35637fc | 2013-07-08 16:16:51 +0000 | [diff] [blame] | 308 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 309 | int FD = open(Name.str().c_str(), OpenFlags, Mode); |
| 310 | if (FD < 0) |
| 311 | fail("Could not open output file"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 312 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 313 | { |
| 314 | raw_fd_ostream file(FD, false); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 315 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 316 | // Get the data and its length |
| 317 | StringRef Data = I->getBuffer(); |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 318 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 319 | // Write the data. |
| 320 | file.write(Data.data(), Data.size()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 323 | // If we're supposed to retain the original modification times, etc. do so |
| 324 | // now. |
| 325 | if (OriginalDates) |
| 326 | failIfError( |
| 327 | sys::fs::setLastModificationAndAccessTime(FD, I->getLastModified())); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 328 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 329 | if (close(FD)) |
| 330 | fail("Could not close the file"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Rafael Espindola | 2494dfc | 2013-07-12 16:29:27 +0000 | [diff] [blame] | 333 | static bool shouldCreateArchive(ArchiveOperation Op) { |
Rafael Espindola | 61de142 | 2013-07-05 13:03:07 +0000 | [diff] [blame] | 334 | switch (Op) { |
| 335 | case Print: |
| 336 | case Delete: |
| 337 | case Move: |
| 338 | case DisplayTable: |
| 339 | case Extract: |
| 340 | return false; |
| 341 | |
| 342 | case QuickAppend: |
| 343 | case ReplaceOrInsert: |
| 344 | return true; |
| 345 | } |
Michael Gottesman | 2be430d | 2013-07-06 02:39:51 +0000 | [diff] [blame] | 346 | |
| 347 | llvm_unreachable("Missing entry in covered switch."); |
Rafael Espindola | 61de142 | 2013-07-05 13:03:07 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 350 | static void performReadOperation(ArchiveOperation Operation, |
| 351 | object::Archive *OldArchive) { |
| 352 | for (object::Archive::child_iterator I = OldArchive->begin_children(), |
| 353 | E = OldArchive->end_children(); |
| 354 | I != E; ++I) { |
| 355 | StringRef Name; |
| 356 | failIfError(I->getName(Name)); |
| 357 | |
| 358 | if (!Members.empty() && |
| 359 | std::find(Members.begin(), Members.end(), Name) == Members.end()) |
| 360 | continue; |
| 361 | |
| 362 | switch (Operation) { |
| 363 | default: |
| 364 | llvm_unreachable("Not a read operation"); |
| 365 | case Print: |
| 366 | doPrint(Name, I); |
| 367 | break; |
| 368 | case DisplayTable: |
| 369 | doDisplayTable(Name, I); |
| 370 | break; |
| 371 | case Extract: |
| 372 | doExtract(Name, I); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | namespace { |
| 379 | class NewArchiveIterator { |
| 380 | bool IsNewMember; |
| 381 | SmallString<16> MemberName; |
| 382 | union { |
| 383 | object::Archive::child_iterator OldI; |
| 384 | std::vector<std::string>::const_iterator NewI; |
| 385 | }; |
| 386 | |
| 387 | public: |
| 388 | NewArchiveIterator(object::Archive::child_iterator I, Twine Name); |
| 389 | NewArchiveIterator(std::vector<std::string>::const_iterator I, Twine Name); |
| 390 | bool isNewMember() const; |
| 391 | object::Archive::child_iterator getOld() const; |
| 392 | StringRef getNew() const; |
| 393 | StringRef getMemberName() const { return MemberName; } |
| 394 | }; |
| 395 | } |
| 396 | |
| 397 | NewArchiveIterator::NewArchiveIterator(object::Archive::child_iterator I, |
| 398 | Twine Name) |
| 399 | : IsNewMember(false), OldI(I) { |
| 400 | Name.toVector(MemberName); |
| 401 | } |
| 402 | |
| 403 | NewArchiveIterator::NewArchiveIterator( |
| 404 | std::vector<std::string>::const_iterator I, Twine Name) |
| 405 | : IsNewMember(true), NewI(I) { |
| 406 | Name.toVector(MemberName); |
| 407 | } |
| 408 | |
| 409 | bool NewArchiveIterator::isNewMember() const { return IsNewMember; } |
| 410 | |
| 411 | object::Archive::child_iterator NewArchiveIterator::getOld() const { |
| 412 | assert(!IsNewMember); |
| 413 | return OldI; |
| 414 | } |
| 415 | |
| 416 | StringRef NewArchiveIterator::getNew() const { |
| 417 | assert(IsNewMember); |
| 418 | return *NewI; |
| 419 | } |
| 420 | |
| 421 | template <typename T> |
| 422 | void addMember(std::vector<NewArchiveIterator> &Members, |
| 423 | std::string &StringTable, T I, StringRef Name) { |
| 424 | if (Name.size() < 15) { |
| 425 | NewArchiveIterator NI(I, Twine(Name) + "/"); |
| 426 | Members.push_back(NI); |
| 427 | } else { |
| 428 | int MapIndex = StringTable.size(); |
| 429 | NewArchiveIterator NI(I, Twine("/") + Twine(MapIndex)); |
| 430 | Members.push_back(NI); |
| 431 | StringTable += Name; |
| 432 | StringTable += "/\n"; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | namespace { |
| 437 | class HasName { |
| 438 | StringRef Name; |
| 439 | |
| 440 | public: |
| 441 | HasName(StringRef Name) : Name(Name) {} |
| 442 | bool operator()(StringRef Path) { return Name == sys::path::filename(Path); } |
| 443 | }; |
| 444 | } |
| 445 | |
| 446 | // We have to walk this twice and computing it is not trivial, so creating an |
| 447 | // explicit std::vector is actually fairly efficient. |
| 448 | static std::vector<NewArchiveIterator> |
| 449 | computeNewArchiveMembers(ArchiveOperation Operation, |
| 450 | object::Archive *OldArchive, |
| 451 | std::string &StringTable) { |
| 452 | std::vector<NewArchiveIterator> Ret; |
| 453 | std::vector<NewArchiveIterator> Moved; |
| 454 | int InsertPos = -1; |
| 455 | StringRef PosName = sys::path::filename(RelPos); |
| 456 | if (OldArchive) { |
| 457 | int Pos = 0; |
| 458 | for (object::Archive::child_iterator I = OldArchive->begin_children(), |
| 459 | E = OldArchive->end_children(); |
| 460 | I != E; ++I, ++Pos) { |
| 461 | StringRef Name; |
| 462 | failIfError(I->getName(Name)); |
| 463 | if (Name == PosName) { |
| 464 | assert(AddAfter || AddBefore); |
| 465 | if (AddBefore) |
| 466 | InsertPos = Pos; |
| 467 | else |
| 468 | InsertPos = Pos + 1; |
| 469 | } |
| 470 | if (Operation != QuickAppend && !Members.empty()) { |
| 471 | std::vector<std::string>::iterator MI = |
| 472 | std::find_if(Members.begin(), Members.end(), HasName(Name)); |
| 473 | if (MI != Members.end()) { |
| 474 | if (Operation == Move) { |
| 475 | addMember(Moved, StringTable, I, Name); |
| 476 | continue; |
| 477 | } |
| 478 | if (Operation != ReplaceOrInsert || !OnlyUpdate) |
| 479 | continue; |
| 480 | // Ignore if the file if it is older than the member. |
| 481 | sys::fs::file_status Status; |
| 482 | failIfError(sys::fs::status(*MI, Status)); |
| 483 | if (Status.getLastModificationTime() < I->getLastModified()) |
| 484 | Members.erase(MI); |
| 485 | else |
| 486 | continue; |
| 487 | } |
| 488 | } |
| 489 | addMember(Ret, StringTable, I, Name); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (Operation == Delete) |
| 494 | return Ret; |
| 495 | |
| 496 | if (Operation == Move) { |
| 497 | if (RelPos.empty()) { |
| 498 | Ret.insert(Ret.end(), Moved.begin(), Moved.end()); |
| 499 | return Ret; |
| 500 | } |
| 501 | if (InsertPos == -1) |
| 502 | fail("Insertion point not found"); |
| 503 | assert(unsigned(InsertPos) <= Ret.size()); |
| 504 | Ret.insert(Ret.begin() + InsertPos, Moved.begin(), Moved.end()); |
| 505 | return Ret; |
| 506 | } |
| 507 | |
| 508 | for (std::vector<std::string>::iterator I = Members.begin(), |
| 509 | E = Members.end(); |
| 510 | I != E; ++I) { |
| 511 | StringRef Name = sys::path::filename(*I); |
| 512 | addMember(Ret, StringTable, I, Name); |
| 513 | } |
| 514 | |
| 515 | return Ret; |
| 516 | } |
| 517 | |
| 518 | template <typename T> |
| 519 | static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) { |
| 520 | uint64_t OldPos = OS.tell(); |
| 521 | OS << Data; |
| 522 | unsigned SizeSoFar = OS.tell() - OldPos; |
| 523 | assert(Size >= SizeSoFar && "Data doesn't fit in Size"); |
| 524 | unsigned Remaining = Size - SizeSoFar; |
| 525 | for (unsigned I = 0; I < Remaining; ++I) |
| 526 | OS << ' '; |
| 527 | } |
| 528 | |
| 529 | static void performWriteOperation(ArchiveOperation Operation, |
| 530 | object::Archive *OldArchive) { |
| 531 | int TmpArchiveFD; |
| 532 | SmallString<128> TmpArchive; |
| 533 | failIfError(sys::fs::createUniqueFile(ArchiveName + ".temp-archive-%%%%%%%.a", |
| 534 | TmpArchiveFD, TmpArchive)); |
| 535 | |
| 536 | TemporaryOutput = TmpArchive.c_str(); |
| 537 | tool_output_file Output(TemporaryOutput, TmpArchiveFD); |
| 538 | raw_fd_ostream &Out = Output.os(); |
| 539 | Out << "!<arch>\n"; |
| 540 | |
| 541 | std::string StringTable; |
| 542 | std::vector<NewArchiveIterator> NewMembers = |
| 543 | computeNewArchiveMembers(Operation, OldArchive, StringTable); |
| 544 | if (!StringTable.empty()) { |
| 545 | if (StringTable.size() % 2) |
| 546 | StringTable += '\n'; |
| 547 | printWithSpacePadding(Out, "//", 48); |
| 548 | printWithSpacePadding(Out, StringTable.size(), 10); |
| 549 | Out << "`\n"; |
| 550 | Out << StringTable; |
| 551 | } |
| 552 | |
| 553 | for (std::vector<NewArchiveIterator>::iterator I = NewMembers.begin(), |
| 554 | E = NewMembers.end(); |
| 555 | I != E; ++I) { |
| 556 | StringRef Name = I->getMemberName(); |
| 557 | printWithSpacePadding(Out, Name, 16); |
| 558 | |
| 559 | if (I->isNewMember()) { |
| 560 | // FIXME: we do a stat + open. We should do a open + fstat. |
| 561 | StringRef FileName = I->getNew(); |
| 562 | sys::fs::file_status Status; |
| 563 | failIfError(sys::fs::status(FileName, Status), FileName); |
| 564 | |
| 565 | uint64_t secondsSinceEpoch = |
| 566 | Status.getLastModificationTime().toEpochTime(); |
| 567 | printWithSpacePadding(Out, secondsSinceEpoch, 12); |
| 568 | |
| 569 | printWithSpacePadding(Out, Status.getUser(), 6); |
| 570 | printWithSpacePadding(Out, Status.getGroup(), 6); |
| 571 | printWithSpacePadding(Out, format("%o", Status.permissions()), 8); |
| 572 | printWithSpacePadding(Out, Status.getSize(), 10); |
| 573 | Out << "`\n"; |
| 574 | |
| 575 | OwningPtr<MemoryBuffer> File; |
| 576 | failIfError(MemoryBuffer::getFile(FileName, File), FileName); |
| 577 | Out << File->getBuffer(); |
| 578 | } else { |
| 579 | object::Archive::child_iterator OldMember = I->getOld(); |
| 580 | |
| 581 | uint64_t secondsSinceEpoch = OldMember->getLastModified().toEpochTime(); |
| 582 | printWithSpacePadding(Out, secondsSinceEpoch, 12); |
| 583 | |
| 584 | printWithSpacePadding(Out, OldMember->getUID(), 6); |
| 585 | printWithSpacePadding(Out, OldMember->getGID(), 6); |
| 586 | printWithSpacePadding(Out, format("%o", OldMember->getAccessMode()), 8); |
| 587 | printWithSpacePadding(Out, OldMember->getSize(), 10); |
| 588 | Out << "`\n"; |
| 589 | |
| 590 | Out << OldMember->getBuffer(); |
| 591 | } |
| 592 | |
| 593 | if (Out.tell() % 2) |
| 594 | Out << '\n'; |
| 595 | } |
| 596 | Output.keep(); |
| 597 | Out.close(); |
| 598 | sys::fs::rename(TemporaryOutput, ArchiveName); |
| 599 | TemporaryOutput = NULL; |
| 600 | } |
| 601 | |
| 602 | static void performOperation(ArchiveOperation Operation, |
| 603 | object::Archive *OldArchive) { |
| 604 | switch (Operation) { |
| 605 | case Print: |
| 606 | case DisplayTable: |
| 607 | case Extract: |
| 608 | performReadOperation(Operation, OldArchive); |
| 609 | return; |
| 610 | |
| 611 | case Delete: |
| 612 | case Move: |
| 613 | case QuickAppend: |
| 614 | case ReplaceOrInsert: |
| 615 | performWriteOperation(Operation, OldArchive); |
| 616 | return; |
| 617 | } |
| 618 | llvm_unreachable("Unknown operation."); |
| 619 | } |
| 620 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 621 | // main - main program for llvm-ar .. see comments in the code |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 622 | int main(int argc, char **argv) { |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 623 | ToolName = argv[0]; |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 624 | // Print a stack trace if we signal out. |
| 625 | sys::PrintStackTraceOnErrorSignal(); |
| 626 | PrettyStackTraceProgram X(argc, argv); |
| 627 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 628 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 629 | // Have the command line options parsed and handle things |
| 630 | // like --help and --version. |
| 631 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 632 | "LLVM Archiver (llvm-ar)\n\n" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 633 | " This program archives bitcode files into single libraries\n" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 634 | ); |
| 635 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 636 | // Do our own parsing of the command line because the CommandLine utility |
| 637 | // can't handle the grouped positional parameters without a dash. |
| 638 | ArchiveOperation Operation = parseCommandLine(); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 639 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 640 | // Create or open the archive object. |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 641 | OwningPtr<MemoryBuffer> Buf; |
| 642 | error_code EC = MemoryBuffer::getFile(ArchiveName, Buf, -1, false); |
| 643 | if (EC && EC != llvm::errc::no_such_file_or_directory) { |
| 644 | errs() << argv[0] << ": error opening '" << ArchiveName |
| 645 | << "': " << EC.message() << "!\n"; |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 646 | return 1; |
| 647 | } |
| 648 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 649 | if (!EC) { |
| 650 | object::Archive Archive(Buf.take(), EC); |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 651 | |
Rafael Espindola | 34ac52d | 2013-07-12 20:21:39 +0000 | [diff] [blame^] | 652 | if (EC) { |
| 653 | errs() << argv[0] << ": error loading '" << ArchiveName |
| 654 | << "': " << EC.message() << "!\n"; |
| 655 | return 1; |
| 656 | } |
| 657 | performOperation(Operation, &Archive); |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | assert(EC == llvm::errc::no_such_file_or_directory); |
| 662 | |
| 663 | if (!shouldCreateArchive(Operation)) { |
| 664 | failIfError(EC, Twine("error loading '") + ArchiveName + "'"); |
| 665 | } else { |
| 666 | if (!Create) { |
| 667 | // Produce a warning if we should and we're creating the archive |
| 668 | errs() << argv[0] << ": creating " << ArchiveName << "\n"; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | performOperation(Operation, NULL); |
| 673 | return 0; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 674 | } |