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