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