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