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 | |
Rafael Espindola | 8496fae | 2013-06-17 15:47:20 +0000 | [diff] [blame] | 15 | #include "Archive.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Module.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" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/PrettyStackTrace.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Signals.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 28 | #include <memory> |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 29 | |
| 30 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 31 | #include <unistd.h> |
| 32 | #else |
| 33 | #include <io.h> |
| 34 | #endif |
| 35 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Dan Gohman | 82f209e | 2007-10-15 21:10:03 +0000 | [diff] [blame] | 38 | // 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] | 39 | static cl::opt<bool> |
| 40 | X32Option ("X32_64", cl::Hidden, |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 41 | cl::desc("Ignored option for compatibility with AIX")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 42 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 43 | // llvm-ar operation code and modifier flags. This must come first. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 44 | static cl::opt<std::string> |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 45 | Options(cl::Positional, cl::Required, cl::desc("{operation}[modifiers]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 46 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 47 | // llvm-ar remaining positional arguments. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 48 | static cl::list<std::string> |
| 49 | RestOfArgs(cl::Positional, cl::OneOrMore, |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 50 | cl::desc("[relpos] [count] <archive-file> [members]...")); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 51 | |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 52 | // MoreHelp - Provide additional help output explaining the operations and |
| 53 | // modifiers of llvm-ar. This object instructs the CommandLine library |
| 54 | // to print the text of the constructor when the --help option is given. |
| 55 | static cl::extrahelp MoreHelp( |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 56 | "\nOPERATIONS:\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 57 | " d[NsS] - delete file(s) from the archive\n" |
| 58 | " m[abiSs] - move file(s) in the archive\n" |
| 59 | " p[kN] - print file(s) found in the archive\n" |
| 60 | " q[ufsS] - quick append file(s) to the archive\n" |
Rafael Espindola | 94bc246 | 2012-08-10 01:57:52 +0000 | [diff] [blame] | 61 | " r[abfiuRsS] - replace or insert file(s) into the archive\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 62 | " t - display contents of archive\n" |
| 63 | " x[No] - extract file(s) from the archive\n" |
| 64 | "\nMODIFIERS (operation specific):\n" |
| 65 | " [a] - put file(s) after [relpos]\n" |
| 66 | " [b] - put file(s) before [relpos] (same as [i])\n" |
| 67 | " [f] - truncate inserted file names\n" |
| 68 | " [i] - put file(s) before [relpos] (same as [b])\n" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 69 | " [k] - always print bitcode files (default is to skip them)\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 70 | " [N] - use instance [count] of name\n" |
| 71 | " [o] - preserve original dates\n" |
| 72 | " [P] - use full path names when matching\n" |
| 73 | " [R] - recurse through directories when inserting\n" |
| 74 | " [s] - create an archive index (cf. ranlib)\n" |
| 75 | " [S] - do not build a symbol table\n" |
| 76 | " [u] - update only files newer than archive contents\n" |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 77 | "\nMODIFIERS (generic):\n" |
| 78 | " [c] - do not warn if the library had to be created\n" |
| 79 | " [v] - be verbose about actions taken\n" |
| 80 | " [V] - be *really* verbose about actions taken\n" |
| 81 | ); |
| 82 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 83 | // This enumeration delineates the kinds of operations on an archive |
| 84 | // that are permitted. |
| 85 | enum ArchiveOperation { |
| 86 | NoOperation, ///< An operation hasn't been specified |
| 87 | Print, ///< Print the contents of the archive |
| 88 | Delete, ///< Delete the specified members |
| 89 | Move, ///< Move members to end or as given by {a,b,i} modifiers |
| 90 | QuickAppend, ///< Quickly append to end of archive |
| 91 | ReplaceOrInsert, ///< Replace or Insert members |
| 92 | DisplayTable, ///< Display the table of contents |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 93 | Extract ///< Extract files back to file system |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 94 | }; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 95 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 96 | // Modifiers to follow operation to vary behavior |
| 97 | bool AddAfter = false; ///< 'a' modifier |
| 98 | bool AddBefore = false; ///< 'b' modifier |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 99 | bool Create = false; ///< 'c' modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 100 | bool TruncateNames = false; ///< 'f' modifier |
| 101 | bool InsertBefore = false; ///< 'i' modifier |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 102 | bool DontSkipBitcode = false; ///< 'k' modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 103 | bool UseCount = false; ///< 'N' modifier |
| 104 | bool OriginalDates = false; ///< 'o' modifier |
| 105 | bool FullPath = false; ///< 'P' modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 106 | bool SymTable = true; ///< 's' & 'S' modifiers |
| 107 | bool OnlyUpdate = false; ///< 'u' modifier |
| 108 | bool Verbose = false; ///< 'v' modifier |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 109 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 110 | // Relative Positional Argument (for insert/move). This variable holds |
| 111 | // the name of the archive member to which the 'a', 'b' or 'i' modifier |
| 112 | // refers. Only one of 'a', 'b' or 'i' can be specified so we only need |
| 113 | // one variable. |
| 114 | std::string RelPos; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 115 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 116 | // Select which of multiple entries in the archive with the same name should be |
| 117 | // used (specified with -N) for the delete and extract operations. |
| 118 | int Count = 1; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 119 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 120 | // This variable holds the name of the archive file as given on the |
| 121 | // command line. |
| 122 | std::string ArchiveName; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 123 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 124 | // This variable holds the list of member files to proecess, as given |
| 125 | // on the command line. |
| 126 | std::vector<std::string> Members; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 127 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 128 | // This variable holds the (possibly expanded) list of path objects that |
| 129 | // correspond to files we will |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 130 | std::set<std::string> Paths; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 131 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 132 | // The Archive object to which all the editing operations will be sent. |
| 133 | Archive* TheArchive = 0; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 134 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 135 | // The name this program was invoked as. |
| 136 | static const char *program_name; |
| 137 | |
| 138 | // show_help - Show the error message, the help message and exit. |
| 139 | LLVM_ATTRIBUTE_NORETURN static void |
| 140 | show_help(const std::string &msg) { |
| 141 | errs() << program_name << ": " << msg << "\n\n"; |
| 142 | cl::PrintHelpMessage(); |
| 143 | if (TheArchive) |
| 144 | delete TheArchive; |
| 145 | std::exit(1); |
| 146 | } |
| 147 | |
| 148 | // fail - Show the error message and exit. |
| 149 | LLVM_ATTRIBUTE_NORETURN static void |
| 150 | fail(const std::string &msg) { |
| 151 | errs() << program_name << ": " << msg << "\n\n"; |
| 152 | if (TheArchive) |
| 153 | delete TheArchive; |
| 154 | std::exit(1); |
| 155 | } |
| 156 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 157 | // getRelPos - Extract the member filename from the command line for |
| 158 | // the [relpos] argument associated with a, b, and i modifiers |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 159 | void getRelPos() { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 160 | if(RestOfArgs.size() == 0) |
| 161 | show_help("Expected [relpos] for a, b, or i modifier"); |
| 162 | RelPos = RestOfArgs[0]; |
| 163 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 166 | // getCount - Extract the [count] argument associated with the N modifier |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 167 | // from the command line and check its value. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 168 | void getCount() { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 169 | if(RestOfArgs.size() == 0) |
| 170 | show_help("Expected [count] value with N modifier"); |
| 171 | |
| 172 | Count = atoi(RestOfArgs[0].c_str()); |
| 173 | RestOfArgs.erase(RestOfArgs.begin()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 174 | |
| 175 | // Non-positive counts are not allowed |
| 176 | if (Count < 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 177 | show_help("Invalid [count] value (not a positive integer)"); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 180 | // getArchive - Get the archive file name from the command line |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 181 | void getArchive() { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 182 | if(RestOfArgs.size() == 0) |
| 183 | show_help("An archive name must be specified"); |
| 184 | ArchiveName = RestOfArgs[0]; |
| 185 | RestOfArgs.erase(RestOfArgs.begin()); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 188 | // getMembers - Copy over remaining items in RestOfArgs to our Members vector |
| 189 | // This is just for clarity. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 190 | void getMembers() { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 191 | if(RestOfArgs.size() > 0) |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 192 | Members = std::vector<std::string>(RestOfArgs); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 195 | // parseCommandLine - Parse the command line options as presented and return the |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 196 | // operation specified. Process all modifiers and check to make sure that |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 197 | // constraints on modifier/operation pairs have not been violated. |
| 198 | ArchiveOperation parseCommandLine() { |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 199 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 200 | // Keep track of number of operations. We can only specify one |
| 201 | // per execution. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 202 | unsigned NumOperations = 0; |
| 203 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 204 | // Keep track of the number of positional modifiers (a,b,i). Only |
| 205 | // one can be specified. |
| 206 | unsigned NumPositional = 0; |
| 207 | |
| 208 | // Keep track of which operation was requested |
| 209 | ArchiveOperation Operation = NoOperation; |
| 210 | |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 211 | for(unsigned i=0; i<Options.size(); ++i) { |
| 212 | switch(Options[i]) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 213 | case 'd': ++NumOperations; Operation = Delete; break; |
| 214 | case 'm': ++NumOperations; Operation = Move ; break; |
| 215 | case 'p': ++NumOperations; Operation = Print; break; |
Seo Sanghyeon | dc32d19 | 2007-12-25 13:53:47 +0000 | [diff] [blame] | 216 | case 'q': ++NumOperations; Operation = QuickAppend; break; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 217 | case 'r': ++NumOperations; Operation = ReplaceOrInsert; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 218 | case 't': ++NumOperations; Operation = DisplayTable; break; |
| 219 | case 'x': ++NumOperations; Operation = Extract; break; |
| 220 | case 'c': Create = true; break; |
| 221 | case 'f': TruncateNames = true; break; |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 222 | case 'k': DontSkipBitcode = true; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 223 | case 'l': /* accepted but unused */ break; |
| 224 | case 'o': OriginalDates = true; break; |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 225 | case 's': break; // Ignore for now. |
| 226 | case 'S': break; // Ignore for now. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 227 | case 'P': FullPath = true; break; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 228 | case 'u': OnlyUpdate = true; break; |
| 229 | case 'v': Verbose = true; break; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 230 | case 'a': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 231 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 232 | AddAfter = true; |
| 233 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 234 | break; |
| 235 | case 'b': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 236 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 237 | AddBefore = true; |
| 238 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 239 | break; |
| 240 | case 'i': |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 241 | getRelPos(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 242 | InsertBefore = true; |
| 243 | NumPositional++; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 244 | break; |
| 245 | case 'N': |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 246 | getCount(); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 247 | UseCount = true; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 248 | break; |
| 249 | default: |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 250 | cl::PrintHelpMessage(); |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 254 | // At this point, the next thing on the command line must be |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 255 | // the archive name. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 256 | getArchive(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 257 | |
| 258 | // Everything on the command line at this point is a member. |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 259 | getMembers(); |
| 260 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 261 | // Perform various checks on the operation/modifier specification |
| 262 | // to make sure we are dealing with a legal request. |
| 263 | if (NumOperations == 0) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 264 | show_help("You must specify at least one of the operations"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 265 | if (NumOperations > 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 266 | show_help("Only one operation may be specified"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 267 | if (NumPositional > 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 268 | show_help("You may only specify one of a, b, and i modifiers"); |
| 269 | if (AddAfter || AddBefore || InsertBefore) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 270 | if (Operation != Move && Operation != ReplaceOrInsert) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 271 | show_help("The 'a', 'b' and 'i' modifiers can only be specified with " |
| 272 | "the 'm' or 'r' operations"); |
| 273 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 274 | if (OriginalDates && Operation != Extract) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 275 | show_help("The 'o' modifier is only applicable to the 'x' operation"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 276 | if (TruncateNames && Operation!=QuickAppend && Operation!=ReplaceOrInsert) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 277 | show_help("The 'f' modifier is only applicable to the 'q' and 'r' " |
| 278 | "operations"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 279 | if (OnlyUpdate && Operation != ReplaceOrInsert) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 280 | show_help("The 'u' modifier is only applicable to the 'r' operation"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 281 | if (Count > 1 && Members.size() > 1) |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 282 | show_help("Only one member name may be specified with the 'N' modifier"); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 283 | |
| 284 | // Return the parsed operation to the caller |
| 285 | return Operation; |
Tanya Lattner | 57bd796 | 2003-12-06 23:01:25 +0000 | [diff] [blame] | 286 | } |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 288 | // buildPaths - Convert the strings in the Members vector to sys::Path objects |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 289 | // and make sure they are valid and exist exist. This check is only needed for |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 290 | // the operations that add/replace files to the archive ('q' and 'r') |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 291 | bool buildPaths(bool checkExistence, std::string* ErrMsg) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 292 | for (unsigned i = 0; i < Members.size(); i++) { |
Rafael Espindola | 436cd0c | 2013-06-20 11:59:19 +0000 | [diff] [blame] | 293 | std::string aPath = Members[i]; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 294 | if (checkExistence) { |
Rafael Espindola | 436cd0c | 2013-06-20 11:59:19 +0000 | [diff] [blame] | 295 | bool IsDirectory; |
| 296 | error_code EC = sys::fs::is_directory(aPath, IsDirectory); |
| 297 | if (EC) |
| 298 | fail(aPath + ": " + EC.message()); |
| 299 | if (IsDirectory) |
| 300 | fail(aPath + " Is a directory"); |
Rafael Espindola | 8fe960e | 2013-06-19 14:58:16 +0000 | [diff] [blame] | 301 | |
Rafael Espindola | 436cd0c | 2013-06-20 11:59:19 +0000 | [diff] [blame] | 302 | Paths.insert(aPath); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 303 | } else { |
Rafael Espindola | 436cd0c | 2013-06-20 11:59:19 +0000 | [diff] [blame] | 304 | Paths.insert(aPath); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 307 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // doPrint - Implements the 'p' operation. This function traverses the archive |
| 311 | // looking for members that match the path list. It is careful to uncompress |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 312 | // things that should be and to skip bitcode files unless the 'k' modifier was |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 313 | // given. |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 314 | bool doPrint(std::string* ErrMsg) { |
| 315 | if (buildPaths(false, ErrMsg)) |
| 316 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 317 | unsigned countDown = Count; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 318 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 319 | I != E; ++I ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 320 | if (Paths.empty() || |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 321 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 322 | if (countDown == 1) { |
| 323 | const char* data = reinterpret_cast<const char*>(I->getData()); |
| 324 | |
| 325 | // Skip things that don't make sense to print |
Rafael Espindola | 250bfb1 | 2013-06-14 23:25:53 +0000 | [diff] [blame] | 326 | if (I->isSVR4SymbolTable() || |
Gabor Greif | e75ca3d | 2007-07-06 13:38:17 +0000 | [diff] [blame] | 327 | I->isBSD4SymbolTable() || (!DontSkipBitcode && I->isBitcode())) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 328 | continue; |
| 329 | |
| 330 | if (Verbose) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 331 | outs() << "Printing " << I->getPath().str() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 333 | unsigned len = I->getSize(); |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 334 | outs().write(data, len); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 335 | } else { |
| 336 | countDown--; |
| 337 | } |
| 338 | } |
| 339 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 340 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // putMode - utility function for printing out the file mode when the 't' |
| 344 | // operation is in verbose mode. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 345 | void |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 346 | printMode(unsigned mode) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 347 | if (mode & 004) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 348 | outs() << "r"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 349 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 350 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 351 | if (mode & 002) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 352 | outs() << "w"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 353 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 354 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 355 | if (mode & 001) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 356 | outs() << "x"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 357 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 358 | outs() << "-"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // doDisplayTable - Implement the 't' operation. This function prints out just |
| 362 | // the file names of each of the members. However, if verbose mode is requested |
| 363 | // ('v' modifier) then the file type, permission mode, user, group, size, and |
| 364 | // modification time are also printed. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 365 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 366 | doDisplayTable(std::string* ErrMsg) { |
| 367 | if (buildPaths(false, ErrMsg)) |
| 368 | return true; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 369 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 370 | I != E; ++I ) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 371 | if (Paths.empty() || |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 372 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 373 | if (Verbose) { |
| 374 | // FIXME: Output should be this format: |
| 375 | // Zrw-r--r-- 500/ 500 525 Nov 8 17:42 2004 Makefile |
Gabor Greif | e75ca3d | 2007-07-06 13:38:17 +0000 | [diff] [blame] | 376 | if (I->isBitcode()) |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 377 | outs() << "b"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 378 | else |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 379 | outs() << " "; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 380 | unsigned mode = I->getMode(); |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 381 | printMode((mode >> 6) & 007); |
| 382 | printMode((mode >> 3) & 007); |
| 383 | printMode(mode & 007); |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 384 | outs() << " " << format("%4u", I->getUser()); |
| 385 | outs() << "/" << format("%4u", I->getGroup()); |
| 386 | outs() << " " << format("%8u", I->getSize()); |
| 387 | outs() << " " << format("%20s", I->getModTime().str().substr(4).c_str()); |
| 388 | outs() << " " << I->getPath().str() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 389 | } else { |
Chris Lattner | 424a04e | 2010-11-29 23:02:20 +0000 | [diff] [blame] | 390 | outs() << I->getPath().str() << "\n"; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 394 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // doExtract - Implement the 'x' operation. This function extracts files back to |
Rafael Espindola | 94bc246 | 2012-08-10 01:57:52 +0000 | [diff] [blame] | 398 | // the file system. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 399 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 400 | doExtract(std::string* ErrMsg) { |
| 401 | if (buildPaths(false, ErrMsg)) |
| 402 | return true; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 403 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 404 | I != E; ++I ) { |
| 405 | if (Paths.empty() || |
| 406 | (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end())) { |
| 407 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 408 | // Open up a file stream for writing |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 409 | int OpenFlags = O_TRUNC | O_WRONLY | O_CREAT; |
| 410 | #ifdef O_BINARY |
| 411 | OpenFlags |= O_BINARY; |
| 412 | #endif |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 413 | |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 414 | int FD = open(I->getPath().str().c_str(), OpenFlags, 0664); |
| 415 | if (FD < 0) |
| 416 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 417 | |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 418 | { |
| 419 | raw_fd_ostream file(FD, false); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 420 | |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 421 | // Get the data and its length |
| 422 | const char* data = reinterpret_cast<const char*>(I->getData()); |
| 423 | unsigned len = I->getSize(); |
| 424 | |
| 425 | // Write the data. |
| 426 | file.write(data, len); |
| 427 | } |
Rafael Espindola | 27ff1f3 | 2013-06-19 19:17:15 +0000 | [diff] [blame] | 428 | |
| 429 | // Retain the original mode. |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 430 | sys::fs::perms Mode = sys::fs::perms(I->getMode()); |
| 431 | error_code EC = sys::fs::permissions(I->getPath(), Mode); |
| 432 | if (EC) |
| 433 | fail(EC.message()); |
Rafael Espindola | 27ff1f3 | 2013-06-19 19:17:15 +0000 | [diff] [blame] | 434 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 435 | // If we're supposed to retain the original modification times, etc. do so |
| 436 | // now. |
Rafael Espindola | 11ca2e5 | 2013-06-20 20:56:14 +0000 | [diff] [blame] | 437 | if (OriginalDates) { |
| 438 | EC = sys::fs::setLastModificationAndAccessTime(FD, I->getModTime()); |
| 439 | if (EC) |
| 440 | fail(EC.message()); |
| 441 | } |
| 442 | if (close(FD)) |
| 443 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Reid Spencer | e5c9cb5 | 2006-08-23 00:39:35 +0000 | [diff] [blame] | 446 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // doDelete - Implement the delete operation. This function deletes zero or more |
| 450 | // members from the archive. Note that if the count is specified, there should |
| 451 | // be no more than one path in the Paths list or else this algorithm breaks. |
| 452 | // That check is enforced in parseCommandLine (above). |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 453 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 454 | doDelete(std::string* ErrMsg) { |
| 455 | if (buildPaths(false, ErrMsg)) |
| 456 | return true; |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 457 | if (Paths.empty()) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 458 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 459 | unsigned countDown = Count; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 460 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 461 | I != E; ) { |
| 462 | if (std::find(Paths.begin(), Paths.end(), I->getPath()) != Paths.end()) { |
| 463 | if (countDown == 1) { |
| 464 | Archive::iterator J = I; |
| 465 | ++I; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 466 | TheArchive->erase(J); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 467 | } else |
| 468 | countDown--; |
| 469 | } else { |
| 470 | ++I; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // We're done editting, reconstruct the archive. |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 475 | if (TheArchive->writeToDisk(TruncateNames,ErrMsg)) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 476 | return true; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 477 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // doMore - Implement the move operation. This function re-arranges just the |
| 481 | // order of the archive members so that when the archive is written the move |
| 482 | // of the members is accomplished. Note the use of the RelPos variable to |
| 483 | // determine where the items should be moved to. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 484 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 485 | doMove(std::string* ErrMsg) { |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 486 | if (buildPaths(false, ErrMsg)) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 487 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 488 | |
| 489 | // By default and convention the place to move members to is the end of the |
| 490 | // archive. |
| 491 | Archive::iterator moveto_spot = TheArchive->end(); |
| 492 | |
| 493 | // However, if the relative positioning modifiers were used, we need to scan |
| 494 | // the archive to find the member in question. If we don't find it, its no |
| 495 | // crime, we just move to the end. |
| 496 | if (AddBefore || InsertBefore || AddAfter) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 497 | for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 498 | I != E; ++I ) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 499 | if (RelPos == I->getPath().str()) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 500 | if (AddAfter) { |
| 501 | moveto_spot = I; |
| 502 | moveto_spot++; |
| 503 | } else { |
| 504 | moveto_spot = I; |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Keep a list of the paths remaining to be moved |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 512 | std::set<std::string> remaining(Paths); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 513 | |
| 514 | // Scan the archive again, this time looking for the members to move to the |
| 515 | // moveto_spot. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 516 | for (Archive::iterator I = TheArchive->begin(), E= TheArchive->end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 517 | I != E && !remaining.empty(); ++I ) { |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 518 | std::set<std::string>::iterator found = |
| 519 | std::find(remaining.begin(),remaining.end(), I->getPath()); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 520 | if (found != remaining.end()) { |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 521 | if (I != moveto_spot) |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 522 | TheArchive->splice(moveto_spot,*TheArchive,I); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 523 | remaining.erase(found); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // We're done editting, reconstruct the archive. |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 528 | if (TheArchive->writeToDisk(TruncateNames,ErrMsg)) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 529 | return true; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 530 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // doQuickAppend - Implements the 'q' operation. This function just |
| 534 | // indiscriminantly adds the members to the archive and rebuilds it. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 535 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 536 | doQuickAppend(std::string* ErrMsg) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 537 | // Get the list of paths to append. |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 538 | if (buildPaths(true, ErrMsg)) |
| 539 | return true; |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 540 | if (Paths.empty()) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 541 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 542 | |
| 543 | // Append them quickly. |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 544 | for (std::set<std::string>::iterator PI = Paths.begin(), PE = Paths.end(); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 545 | PI != PE; ++PI) { |
Rafael Espindola | 13f4fd7 | 2013-06-19 17:49:07 +0000 | [diff] [blame] | 546 | if (TheArchive->addFileBefore(*PI, TheArchive->end(), ErrMsg)) |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 547 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // We're done editting, reconstruct the archive. |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 551 | if (TheArchive->writeToDisk(TruncateNames,ErrMsg)) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 552 | return true; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 553 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // doReplaceOrInsert - Implements the 'r' operation. This function will replace |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 557 | // any existing files or insert new ones into the archive. |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 558 | bool |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 559 | doReplaceOrInsert(std::string* ErrMsg) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 560 | |
| 561 | // Build the list of files to be added/replaced. |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 562 | if (buildPaths(true, ErrMsg)) |
| 563 | return true; |
Michael J. Spencer | 83a113b | 2011-01-10 02:34:40 +0000 | [diff] [blame] | 564 | if (Paths.empty()) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 565 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 566 | |
| 567 | // Keep track of the paths that remain to be inserted. |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 568 | std::set<std::string> remaining(Paths); |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 569 | |
| 570 | // Default the insertion spot to the end of the archive |
| 571 | Archive::iterator insert_spot = TheArchive->end(); |
| 572 | |
| 573 | // Iterate over the archive contents |
| 574 | for (Archive::iterator I = TheArchive->begin(), E = TheArchive->end(); |
| 575 | I != E && !remaining.empty(); ++I ) { |
| 576 | |
| 577 | // Determine if this archive member matches one of the paths we're trying |
| 578 | // to replace. |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 579 | |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 580 | std::set<std::string>::iterator found = remaining.end(); |
| 581 | for (std::set<std::string>::iterator RI = remaining.begin(), |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 582 | RE = remaining.end(); RI != RE; ++RI ) { |
Rafael Espindola | eb729e0 | 2013-06-20 18:30:37 +0000 | [diff] [blame] | 583 | std::string compare(sys::path::filename(*RI)); |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 584 | if (TruncateNames && compare.length() > 15) { |
| 585 | const char* nm = compare.c_str(); |
| 586 | unsigned len = compare.length(); |
| 587 | size_t slashpos = compare.rfind('/'); |
| 588 | if (slashpos != std::string::npos) { |
| 589 | nm += slashpos + 1; |
| 590 | len -= slashpos +1; |
| 591 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 592 | if (len > 15) |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 593 | len = 15; |
| 594 | compare.assign(nm,len); |
| 595 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 596 | if (compare == I->getPath().str()) { |
Reid Spencer | 0de66b5 | 2004-12-02 09:21:55 +0000 | [diff] [blame] | 597 | found = RI; |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 602 | if (found != remaining.end()) { |
Rafael Espindola | 29c17db | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 603 | sys::fs::file_status Status; |
| 604 | error_code EC = sys::fs::status(*found, Status); |
| 605 | if (EC) |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 606 | return true; |
Rafael Espindola | 29c17db | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 607 | if (!sys::fs::is_directory(Status)) { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 608 | if (OnlyUpdate) { |
| 609 | // Replace the item only if it is newer. |
Rafael Espindola | 29c17db | 2013-06-20 18:42:04 +0000 | [diff] [blame] | 610 | if (Status.getLastModificationTime() > I->getModTime()) |
Rafael Espindola | 13f4fd7 | 2013-06-19 17:49:07 +0000 | [diff] [blame] | 611 | if (I->replaceWith(*found, ErrMsg)) |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 612 | return true; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 613 | } else { |
| 614 | // Replace the item regardless of time stamp |
Rafael Espindola | 13f4fd7 | 2013-06-19 17:49:07 +0000 | [diff] [blame] | 615 | if (I->replaceWith(*found, ErrMsg)) |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 616 | return true; |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 617 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 618 | } else { |
Reid Spencer | bede583 | 2004-11-16 06:41:09 +0000 | [diff] [blame] | 619 | // We purposefully ignore directories. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | // Remove it from our "to do" list |
| 623 | remaining.erase(found); |
| 624 | } |
| 625 | |
| 626 | // Determine if this is the place where we should insert |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 627 | if ((AddBefore || InsertBefore) && RelPos == I->getPath().str()) |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 628 | insert_spot = I; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 629 | else if (AddAfter && RelPos == I->getPath().str()) { |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 630 | insert_spot = I; |
| 631 | insert_spot++; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // If we didn't replace all the members, some will remain and need to be |
| 636 | // inserted at the previously computed insert-spot. |
| 637 | if (!remaining.empty()) { |
Rafael Espindola | 4d07abb | 2013-06-19 15:45:37 +0000 | [diff] [blame] | 638 | for (std::set<std::string>::iterator PI = remaining.begin(), |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 639 | PE = remaining.end(); PI != PE; ++PI) { |
Rafael Espindola | 13f4fd7 | 2013-06-19 17:49:07 +0000 | [diff] [blame] | 640 | if (TheArchive->addFileBefore(*PI, insert_spot, ErrMsg)) |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 641 | return true; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | // We're done editting, reconstruct the archive. |
Rafael Espindola | 6f2c88a | 2013-06-20 13:00:30 +0000 | [diff] [blame] | 646 | if (TheArchive->writeToDisk(TruncateNames,ErrMsg)) |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 647 | return true; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 648 | return false; |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // main - main program for llvm-ar .. see comments in the code |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 652 | int main(int argc, char **argv) { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 653 | program_name = argv[0]; |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 654 | // Print a stack trace if we signal out. |
| 655 | sys::PrintStackTraceOnErrorSignal(); |
| 656 | PrettyStackTraceProgram X(argc, argv); |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 657 | LLVMContext &Context = getGlobalContext(); |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 658 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 659 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 660 | // Have the command line options parsed and handle things |
| 661 | // like --help and --version. |
| 662 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 663 | "LLVM Archiver (llvm-ar)\n\n" |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 664 | " This program archives bitcode files into single libraries\n" |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 665 | ); |
| 666 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 667 | int exitCode = 0; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 668 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 669 | // Do our own parsing of the command line because the CommandLine utility |
| 670 | // can't handle the grouped positional parameters without a dash. |
| 671 | ArchiveOperation Operation = parseCommandLine(); |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 672 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 673 | // Create or open the archive object. |
| 674 | bool Exists; |
Rafael Espindola | 201c66c | 2013-06-20 18:55:44 +0000 | [diff] [blame] | 675 | if (llvm::sys::fs::exists(ArchiveName, Exists) || !Exists) { |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 676 | // Produce a warning if we should and we're creating the archive |
| 677 | if (!Create) |
Rafael Espindola | 201c66c | 2013-06-20 18:55:44 +0000 | [diff] [blame] | 678 | errs() << argv[0] << ": creating " << ArchiveName << "\n"; |
| 679 | TheArchive = Archive::CreateEmpty(ArchiveName, Context); |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 680 | TheArchive->writeToDisk(); |
| 681 | } else { |
| 682 | std::string Error; |
Rafael Espindola | 201c66c | 2013-06-20 18:55:44 +0000 | [diff] [blame] | 683 | TheArchive = Archive::OpenAndLoad(ArchiveName, Context, &Error); |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 684 | if (TheArchive == 0) { |
Rafael Espindola | 201c66c | 2013-06-20 18:55:44 +0000 | [diff] [blame] | 685 | errs() << argv[0] << ": error loading '" << ArchiveName << "': " |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 686 | << Error << "!\n"; |
Reid Spencer | 142ca8e | 2006-08-23 06:56:27 +0000 | [diff] [blame] | 687 | return 1; |
| 688 | } |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Joerg Sonnenberger | 975bc07 | 2012-10-26 10:49:15 +0000 | [diff] [blame] | 691 | // Make sure we're not fooling ourselves. |
| 692 | assert(TheArchive && "Unable to instantiate the archive"); |
| 693 | |
| 694 | // Perform the operation |
| 695 | std::string ErrMsg; |
| 696 | bool haveError = false; |
| 697 | switch (Operation) { |
| 698 | case Print: haveError = doPrint(&ErrMsg); break; |
| 699 | case Delete: haveError = doDelete(&ErrMsg); break; |
| 700 | case Move: haveError = doMove(&ErrMsg); break; |
| 701 | case QuickAppend: haveError = doQuickAppend(&ErrMsg); break; |
| 702 | case ReplaceOrInsert: haveError = doReplaceOrInsert(&ErrMsg); break; |
| 703 | case DisplayTable: haveError = doDisplayTable(&ErrMsg); break; |
| 704 | case Extract: haveError = doExtract(&ErrMsg); break; |
| 705 | case NoOperation: |
| 706 | errs() << argv[0] << ": No operation was selected.\n"; |
| 707 | break; |
| 708 | } |
| 709 | if (haveError) { |
| 710 | errs() << argv[0] << ": " << ErrMsg << "\n"; |
| 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | delete TheArchive; |
| 715 | TheArchive = 0; |
| 716 | |
Reid Spencer | 3a1582b | 2004-11-14 22:20:07 +0000 | [diff] [blame] | 717 | // Return result code back to operating system. |
| 718 | return exitCode; |
Tanya Lattner | 14baebf | 2003-08-28 15:22:38 +0000 | [diff] [blame] | 719 | } |