command_line_interface.cc 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/command_line_interface.h>
  34. #include <google/protobuf/stubs/platform_macros.h>
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #ifdef major
  38. #undef major
  39. #endif
  40. #ifdef minor
  41. #undef minor
  42. #endif
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #ifndef _MSC_VER
  46. #include <unistd.h>
  47. #endif
  48. #include <errno.h>
  49. #include <fstream>
  50. #include <iostream>
  51. #include <ctype.h>
  52. #include <limits.h> //For PATH_MAX
  53. #include <memory>
  54. #ifdef __APPLE__
  55. #include <mach-o/dyld.h>
  56. #endif
  57. #include <google/protobuf/stubs/common.h>
  58. #include <google/protobuf/stubs/logging.h>
  59. #include <google/protobuf/stubs/stringprintf.h>
  60. #include <google/protobuf/compiler/subprocess.h>
  61. #include <google/protobuf/compiler/zip_writer.h>
  62. #include <google/protobuf/compiler/plugin.pb.h>
  63. #include <google/protobuf/compiler/code_generator.h>
  64. #include <google/protobuf/compiler/importer.h>
  65. #include <google/protobuf/io/coded_stream.h>
  66. #include <google/protobuf/io/printer.h>
  67. #include <google/protobuf/io/zero_copy_stream_impl.h>
  68. #include <google/protobuf/descriptor.h>
  69. #include <google/protobuf/dynamic_message.h>
  70. #include <google/protobuf/text_format.h>
  71. #include <google/protobuf/stubs/strutil.h>
  72. #include <google/protobuf/stubs/substitute.h>
  73. #include <google/protobuf/stubs/map_util.h>
  74. #include <google/protobuf/stubs/stl_util.h>
  75. #include <google/protobuf/stubs/io_win32.h>
  76. namespace google {
  77. namespace protobuf {
  78. namespace compiler {
  79. #ifndef O_BINARY
  80. #ifdef _O_BINARY
  81. #define O_BINARY _O_BINARY
  82. #else
  83. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  84. #endif
  85. #endif
  86. namespace {
  87. #if defined(_WIN32)
  88. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  89. // them like we do below.
  90. using google::protobuf::internal::win32::access;
  91. using google::protobuf::internal::win32::close;
  92. using google::protobuf::internal::win32::mkdir;
  93. using google::protobuf::internal::win32::open;
  94. using google::protobuf::internal::win32::setmode;
  95. using google::protobuf::internal::win32::write;
  96. #endif
  97. static const char* kDefaultDirectDependenciesViolationMsg =
  98. "File is imported but not declared in --direct_dependencies: %s";
  99. // Returns true if the text looks like a Windows-style absolute path, starting
  100. // with a drive letter. Example: "C:\foo". TODO(kenton): Share this with
  101. // copy in importer.cc?
  102. static bool IsWindowsAbsolutePath(const string& text) {
  103. #if defined(_WIN32) || defined(__CYGWIN__)
  104. return text.size() >= 3 && text[1] == ':' &&
  105. isalpha(text[0]) &&
  106. (text[2] == '/' || text[2] == '\\') &&
  107. text.find_last_of(':') == 1;
  108. #else
  109. return false;
  110. #endif
  111. }
  112. void SetFdToTextMode(int fd) {
  113. #ifdef _WIN32
  114. if (setmode(fd, _O_TEXT) == -1) {
  115. // This should never happen, I think.
  116. GOOGLE_LOG(WARNING) << "setmode(" << fd << ", _O_TEXT): " << strerror(errno);
  117. }
  118. #endif
  119. // (Text and binary are the same on non-Windows platforms.)
  120. }
  121. void SetFdToBinaryMode(int fd) {
  122. #ifdef _WIN32
  123. if (setmode(fd, _O_BINARY) == -1) {
  124. // This should never happen, I think.
  125. GOOGLE_LOG(WARNING) << "setmode(" << fd << ", _O_BINARY): " << strerror(errno);
  126. }
  127. #endif
  128. // (Text and binary are the same on non-Windows platforms.)
  129. }
  130. void AddTrailingSlash(string* path) {
  131. if (!path->empty() && path->at(path->size() - 1) != '/') {
  132. path->push_back('/');
  133. }
  134. }
  135. bool VerifyDirectoryExists(const string& path) {
  136. if (path.empty()) return true;
  137. if (access(path.c_str(), F_OK) == -1) {
  138. std::cerr << path << ": " << strerror(errno) << std::endl;
  139. return false;
  140. } else {
  141. return true;
  142. }
  143. }
  144. // Try to create the parent directory of the given file, creating the parent's
  145. // parent if necessary, and so on. The full file name is actually
  146. // (prefix + filename), but we assume |prefix| already exists and only create
  147. // directories listed in |filename|.
  148. bool TryCreateParentDirectory(const string& prefix, const string& filename) {
  149. // Recursively create parent directories to the output file.
  150. std::vector<string> parts = Split(filename, "/", true);
  151. string path_so_far = prefix;
  152. for (int i = 0; i < parts.size() - 1; i++) {
  153. path_so_far += parts[i];
  154. if (mkdir(path_so_far.c_str(), 0777) != 0) {
  155. if (errno != EEXIST) {
  156. std::cerr << filename << ": while trying to create directory "
  157. << path_so_far << ": " << strerror(errno) << std::endl;
  158. return false;
  159. }
  160. }
  161. path_so_far += '/';
  162. }
  163. return true;
  164. }
  165. // Get the absolute path of this protoc binary.
  166. bool GetProtocAbsolutePath(string* path) {
  167. #ifdef _WIN32
  168. char buffer[MAX_PATH];
  169. int len = GetModuleFileNameA(NULL, buffer, MAX_PATH);
  170. #elif __APPLE__
  171. char buffer[PATH_MAX];
  172. int len = 0;
  173. char dirtybuffer[PATH_MAX];
  174. uint32_t size = sizeof(dirtybuffer);
  175. if (_NSGetExecutablePath(dirtybuffer, &size) == 0) {
  176. realpath(dirtybuffer, buffer);
  177. len = strlen(buffer);
  178. }
  179. #else
  180. char buffer[PATH_MAX];
  181. int len = readlink("/proc/self/exe", buffer, PATH_MAX);
  182. #endif
  183. if (len > 0) {
  184. path->assign(buffer, len);
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. // Whether a path is where google/protobuf/descriptor.proto and other well-known
  191. // type protos are installed.
  192. bool IsInstalledProtoPath(const string& path) {
  193. // Checking the descriptor.proto file should be good enough.
  194. string file_path = path + "/google/protobuf/descriptor.proto";
  195. return access(file_path.c_str(), F_OK) != -1;
  196. }
  197. // Add the paths where google/protobuf/descriptor.proto and other well-known
  198. // type protos are installed.
  199. void AddDefaultProtoPaths(std::vector<std::pair<string, string> >* paths) {
  200. // TODO(xiaofeng): The code currently only checks relative paths of where
  201. // the protoc binary is installed. We probably should make it handle more
  202. // cases than that.
  203. string path;
  204. if (!GetProtocAbsolutePath(&path)) {
  205. return;
  206. }
  207. // Strip the binary name.
  208. size_t pos = path.find_last_of("/\\");
  209. if (pos == string::npos || pos == 0) {
  210. return;
  211. }
  212. path = path.substr(0, pos);
  213. // Check the binary's directory.
  214. if (IsInstalledProtoPath(path)) {
  215. paths->push_back(std::pair<string, string>("", path));
  216. return;
  217. }
  218. // Check if there is an include subdirectory.
  219. if (IsInstalledProtoPath(path + "/include")) {
  220. paths->push_back(std::pair<string, string>("", path + "/include"));
  221. return;
  222. }
  223. // Check if the upper level directory has an "include" subdirectory.
  224. pos = path.find_last_of("/\\");
  225. if (pos == string::npos || pos == 0) {
  226. return;
  227. }
  228. path = path.substr(0, pos);
  229. if (IsInstalledProtoPath(path + "/include")) {
  230. paths->push_back(std::pair<string, string>("", path + "/include"));
  231. return;
  232. }
  233. }
  234. string PluginName(const string& plugin_prefix, const string& directive) {
  235. // Assuming the directive starts with "--" and ends with "_out" or "_opt",
  236. // strip the "--" and "_out/_opt" and add the plugin prefix.
  237. return plugin_prefix + "gen-" + directive.substr(2, directive.size() - 6);
  238. }
  239. } // namespace
  240. // A MultiFileErrorCollector that prints errors to stderr.
  241. class CommandLineInterface::ErrorPrinter
  242. : public MultiFileErrorCollector,
  243. public io::ErrorCollector,
  244. public DescriptorPool::ErrorCollector {
  245. public:
  246. ErrorPrinter(ErrorFormat format, DiskSourceTree *tree = NULL)
  247. : format_(format), tree_(tree), found_errors_(false) {}
  248. ~ErrorPrinter() {}
  249. // implements MultiFileErrorCollector ------------------------------
  250. void AddError(const string& filename, int line, int column,
  251. const string& message) {
  252. found_errors_ = true;
  253. AddErrorOrWarning(filename, line, column, message, "error", std::cerr);
  254. }
  255. void AddWarning(const string& filename, int line, int column,
  256. const string& message) {
  257. AddErrorOrWarning(filename, line, column, message, "warning", std::clog);
  258. }
  259. // implements io::ErrorCollector -----------------------------------
  260. void AddError(int line, int column, const string& message) {
  261. AddError("input", line, column, message);
  262. }
  263. void AddWarning(int line, int column, const string& message) {
  264. AddErrorOrWarning("input", line, column, message, "warning", std::clog);
  265. }
  266. // implements DescriptorPool::ErrorCollector-------------------------
  267. void AddError(
  268. const string& filename,
  269. const string& element_name,
  270. const Message* descriptor,
  271. ErrorLocation location,
  272. const string& message) {
  273. AddErrorOrWarning(filename, -1, -1, message, "error", std::cerr);
  274. }
  275. void AddWarning(
  276. const string& filename,
  277. const string& element_name,
  278. const Message* descriptor,
  279. ErrorLocation location,
  280. const string& message) {
  281. AddErrorOrWarning(filename, -1, -1, message, "warning", std::clog);
  282. }
  283. bool FoundErrors() const { return found_errors_; }
  284. private:
  285. void AddErrorOrWarning(const string& filename, int line, int column,
  286. const string& message, const string& type,
  287. std::ostream& out) {
  288. // Print full path when running under MSVS
  289. string dfile;
  290. if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS &&
  291. tree_ != NULL &&
  292. tree_->VirtualFileToDiskFile(filename, &dfile)) {
  293. out << dfile;
  294. } else {
  295. out << filename;
  296. }
  297. // Users typically expect 1-based line/column numbers, so we add 1
  298. // to each here.
  299. if (line != -1) {
  300. // Allow for both GCC- and Visual-Studio-compatible output.
  301. switch (format_) {
  302. case CommandLineInterface::ERROR_FORMAT_GCC:
  303. out << ":" << (line + 1) << ":" << (column + 1);
  304. break;
  305. case CommandLineInterface::ERROR_FORMAT_MSVS:
  306. out << "(" << (line + 1) << ") : "
  307. << type << " in column=" << (column + 1);
  308. break;
  309. }
  310. }
  311. if (type == "warning") {
  312. out << ": warning: " << message << std::endl;
  313. } else {
  314. out << ": " << message << std::endl;
  315. }
  316. }
  317. const ErrorFormat format_;
  318. DiskSourceTree *tree_;
  319. bool found_errors_;
  320. };
  321. // -------------------------------------------------------------------
  322. // A GeneratorContext implementation that buffers files in memory, then dumps
  323. // them all to disk on demand.
  324. class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
  325. public:
  326. GeneratorContextImpl(const std::vector<const FileDescriptor*>& parsed_files);
  327. ~GeneratorContextImpl();
  328. // Write all files in the directory to disk at the given output location,
  329. // which must end in a '/'.
  330. bool WriteAllToDisk(const string& prefix);
  331. // Write the contents of this directory to a ZIP-format archive with the
  332. // given name.
  333. bool WriteAllToZip(const string& filename);
  334. // Add a boilerplate META-INF/MANIFEST.MF file as required by the Java JAR
  335. // format, unless one has already been written.
  336. void AddJarManifest();
  337. // Get name of all output files.
  338. void GetOutputFilenames(std::vector<string>* output_filenames);
  339. // implements GeneratorContext --------------------------------------
  340. io::ZeroCopyOutputStream* Open(const string& filename);
  341. io::ZeroCopyOutputStream* OpenForAppend(const string& filename);
  342. io::ZeroCopyOutputStream* OpenForInsert(
  343. const string& filename, const string& insertion_point);
  344. void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
  345. *output = parsed_files_;
  346. }
  347. private:
  348. friend class MemoryOutputStream;
  349. // map instead of hash_map so that files are written in order (good when
  350. // writing zips).
  351. std::map<string, string*> files_;
  352. const std::vector<const FileDescriptor*>& parsed_files_;
  353. bool had_error_;
  354. };
  355. class CommandLineInterface::MemoryOutputStream
  356. : public io::ZeroCopyOutputStream {
  357. public:
  358. MemoryOutputStream(GeneratorContextImpl* directory, const string& filename,
  359. bool append_mode);
  360. MemoryOutputStream(GeneratorContextImpl* directory, const string& filename,
  361. const string& insertion_point);
  362. virtual ~MemoryOutputStream();
  363. // implements ZeroCopyOutputStream ---------------------------------
  364. virtual bool Next(void** data, int* size) { return inner_->Next(data, size); }
  365. virtual void BackUp(int count) { inner_->BackUp(count); }
  366. virtual int64 ByteCount() const { return inner_->ByteCount(); }
  367. private:
  368. // Checks to see if "filename_.meta" exists in directory_; if so, fixes the
  369. // offsets in that GeneratedCodeInfo record to reflect bytes inserted in
  370. // filename_ at original offset insertion_offset with length insertion_length.
  371. // We assume that insertions will not occur within any given annotated span
  372. // of text.
  373. void UpdateMetadata(size_t insertion_offset, size_t insertion_length);
  374. // Where to insert the string when it's done.
  375. GeneratorContextImpl* directory_;
  376. string filename_;
  377. string insertion_point_;
  378. // The string we're building.
  379. string data_;
  380. // Whether we should append the output stream to the existing file.
  381. bool append_mode_;
  382. // StringOutputStream writing to data_.
  383. std::unique_ptr<io::StringOutputStream> inner_;
  384. };
  385. // -------------------------------------------------------------------
  386. CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl(
  387. const std::vector<const FileDescriptor*>& parsed_files)
  388. : parsed_files_(parsed_files),
  389. had_error_(false) {}
  390. CommandLineInterface::GeneratorContextImpl::~GeneratorContextImpl() {
  391. STLDeleteValues(&files_);
  392. }
  393. bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk(
  394. const string& prefix) {
  395. if (had_error_) {
  396. return false;
  397. }
  398. if (!VerifyDirectoryExists(prefix)) {
  399. return false;
  400. }
  401. for (std::map<string, string*>::const_iterator iter = files_.begin();
  402. iter != files_.end(); ++iter) {
  403. const string& relative_filename = iter->first;
  404. const char* data = iter->second->data();
  405. int size = iter->second->size();
  406. if (!TryCreateParentDirectory(prefix, relative_filename)) {
  407. return false;
  408. }
  409. string filename = prefix + relative_filename;
  410. // Create the output file.
  411. int file_descriptor;
  412. do {
  413. file_descriptor =
  414. open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  415. } while (file_descriptor < 0 && errno == EINTR);
  416. if (file_descriptor < 0) {
  417. int error = errno;
  418. std::cerr << filename << ": " << strerror(error);
  419. return false;
  420. }
  421. // Write the file.
  422. while (size > 0) {
  423. int write_result;
  424. do {
  425. write_result = write(file_descriptor, data, size);
  426. } while (write_result < 0 && errno == EINTR);
  427. if (write_result <= 0) {
  428. // Write error.
  429. // FIXME(kenton): According to the man page, if write() returns zero,
  430. // there was no error; write() simply did not write anything. It's
  431. // unclear under what circumstances this might happen, but presumably
  432. // errno won't be set in this case. I am confused as to how such an
  433. // event should be handled. For now I'm treating it as an error,
  434. // since retrying seems like it could lead to an infinite loop. I
  435. // suspect this never actually happens anyway.
  436. if (write_result < 0) {
  437. int error = errno;
  438. std::cerr << filename << ": write: " << strerror(error);
  439. } else {
  440. std::cerr << filename << ": write() returned zero?" << std::endl;
  441. }
  442. return false;
  443. }
  444. data += write_result;
  445. size -= write_result;
  446. }
  447. if (close(file_descriptor) != 0) {
  448. int error = errno;
  449. std::cerr << filename << ": close: " << strerror(error);
  450. return false;
  451. }
  452. }
  453. return true;
  454. }
  455. bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip(
  456. const string& filename) {
  457. if (had_error_) {
  458. return false;
  459. }
  460. // Create the output file.
  461. int file_descriptor;
  462. do {
  463. file_descriptor =
  464. open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  465. } while (file_descriptor < 0 && errno == EINTR);
  466. if (file_descriptor < 0) {
  467. int error = errno;
  468. std::cerr << filename << ": " << strerror(error);
  469. return false;
  470. }
  471. // Create the ZipWriter
  472. io::FileOutputStream stream(file_descriptor);
  473. ZipWriter zip_writer(&stream);
  474. for (std::map<string, string*>::const_iterator iter = files_.begin();
  475. iter != files_.end(); ++iter) {
  476. zip_writer.Write(iter->first, *iter->second);
  477. }
  478. zip_writer.WriteDirectory();
  479. if (stream.GetErrno() != 0) {
  480. std::cerr << filename << ": " << strerror(stream.GetErrno()) << std::endl;
  481. }
  482. if (!stream.Close()) {
  483. std::cerr << filename << ": " << strerror(stream.GetErrno()) << std::endl;
  484. }
  485. return true;
  486. }
  487. void CommandLineInterface::GeneratorContextImpl::AddJarManifest() {
  488. string** map_slot = &files_["META-INF/MANIFEST.MF"];
  489. if (*map_slot == NULL) {
  490. *map_slot = new string(
  491. "Manifest-Version: 1.0\n"
  492. "Created-By: 1.6.0 (protoc)\n"
  493. "\n");
  494. }
  495. }
  496. void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames(
  497. std::vector<string>* output_filenames) {
  498. for (std::map<string, string*>::iterator iter = files_.begin();
  499. iter != files_.end(); ++iter) {
  500. output_filenames->push_back(iter->first);
  501. }
  502. }
  503. io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open(
  504. const string& filename) {
  505. return new MemoryOutputStream(this, filename, false);
  506. }
  507. io::ZeroCopyOutputStream*
  508. CommandLineInterface::GeneratorContextImpl::OpenForAppend(
  509. const string& filename) {
  510. return new MemoryOutputStream(this, filename, true);
  511. }
  512. io::ZeroCopyOutputStream*
  513. CommandLineInterface::GeneratorContextImpl::OpenForInsert(
  514. const string& filename, const string& insertion_point) {
  515. return new MemoryOutputStream(this, filename, insertion_point);
  516. }
  517. // -------------------------------------------------------------------
  518. CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
  519. GeneratorContextImpl* directory, const string& filename, bool append_mode)
  520. : directory_(directory),
  521. filename_(filename),
  522. append_mode_(append_mode),
  523. inner_(new io::StringOutputStream(&data_)) {
  524. }
  525. CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
  526. GeneratorContextImpl* directory, const string& filename,
  527. const string& insertion_point)
  528. : directory_(directory),
  529. filename_(filename),
  530. insertion_point_(insertion_point),
  531. inner_(new io::StringOutputStream(&data_)) {
  532. }
  533. void CommandLineInterface::MemoryOutputStream::UpdateMetadata(
  534. size_t insertion_offset, size_t insertion_length) {
  535. std::map<string, string*>::iterator meta_file =
  536. directory_->files_.find(filename_ + ".meta");
  537. if (meta_file == directory_->files_.end() || !meta_file->second) {
  538. // No metadata was recorded for this file.
  539. return;
  540. }
  541. string* encoded_data = meta_file->second;
  542. GeneratedCodeInfo metadata;
  543. bool is_text_format = false;
  544. if (!metadata.ParseFromString(*encoded_data)) {
  545. if (!TextFormat::ParseFromString(*encoded_data, &metadata)) {
  546. // The metadata is invalid.
  547. std::cerr << filename_
  548. << ".meta: Could not parse metadata as wire or text format."
  549. << std::endl;
  550. return;
  551. }
  552. // Generators that use the public plugin interface emit text-format
  553. // metadata (because in the public plugin protocol, file content must be
  554. // UTF8-encoded strings).
  555. is_text_format = true;
  556. }
  557. for (int i = 0; i < metadata.annotation_size(); ++i) {
  558. GeneratedCodeInfo::Annotation* annotation = metadata.mutable_annotation(i);
  559. if (annotation->begin() >= insertion_offset) {
  560. annotation->set_begin(annotation->begin() + insertion_length);
  561. annotation->set_end(annotation->end() + insertion_length);
  562. }
  563. }
  564. if (is_text_format) {
  565. TextFormat::PrintToString(metadata, encoded_data);
  566. } else {
  567. metadata.SerializeToString(encoded_data);
  568. }
  569. }
  570. CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() {
  571. // Make sure all data has been written.
  572. inner_.reset();
  573. // Insert into the directory.
  574. string** map_slot = &directory_->files_[filename_];
  575. if (insertion_point_.empty()) {
  576. // This was just a regular Open().
  577. if (*map_slot != NULL) {
  578. if (append_mode_) {
  579. (*map_slot)->append(data_);
  580. } else {
  581. std::cerr << filename_ << ": Tried to write the same file twice."
  582. << std::endl;
  583. directory_->had_error_ = true;
  584. }
  585. return;
  586. }
  587. *map_slot = new string;
  588. (*map_slot)->swap(data_);
  589. } else {
  590. // This was an OpenForInsert().
  591. // If the data doesn't end with a clean line break, add one.
  592. if (!data_.empty() && data_[data_.size() - 1] != '\n') {
  593. data_.push_back('\n');
  594. }
  595. // Find the file we are going to insert into.
  596. if (*map_slot == NULL) {
  597. std::cerr << filename_
  598. << ": Tried to insert into file that doesn't exist."
  599. << std::endl;
  600. directory_->had_error_ = true;
  601. return;
  602. }
  603. string* target = *map_slot;
  604. // Find the insertion point.
  605. string magic_string = strings::Substitute(
  606. "@@protoc_insertion_point($0)", insertion_point_);
  607. string::size_type pos = target->find(magic_string);
  608. if (pos == string::npos) {
  609. std::cerr << filename_ << ": insertion point \"" << insertion_point_
  610. << "\" not found." << std::endl;
  611. directory_->had_error_ = true;
  612. return;
  613. }
  614. if ((pos > 3) && (target->substr(pos - 3, 2) == "/*")) {
  615. // Support for inline "/* @@protoc_insertion_point() */"
  616. pos = pos - 3;
  617. } else {
  618. // Seek backwards to the beginning of the line, which is where we will
  619. // insert the data. Note that this has the effect of pushing the
  620. // insertion point down, so the data is inserted before it. This is
  621. // intentional because it means that multiple insertions at the same point
  622. // will end up in the expected order in the final output.
  623. pos = target->find_last_of('\n', pos);
  624. if (pos == string::npos) {
  625. // Insertion point is on the first line.
  626. pos = 0;
  627. } else {
  628. // Advance to character after '\n'.
  629. ++pos;
  630. }
  631. }
  632. // Extract indent.
  633. string indent_(*target, pos, target->find_first_not_of(" \t", pos) - pos);
  634. if (indent_.empty()) {
  635. // No indent. This makes things easier.
  636. target->insert(pos, data_);
  637. UpdateMetadata(pos, data_.size());
  638. } else {
  639. // Calculate how much space we need.
  640. int indent_size = 0;
  641. for (int i = 0; i < data_.size(); i++) {
  642. if (data_[i] == '\n') indent_size += indent_.size();
  643. }
  644. // Make a hole for it.
  645. target->insert(pos, data_.size() + indent_size, '\0');
  646. UpdateMetadata(pos, data_.size() + indent_size);
  647. // Now copy in the data.
  648. string::size_type data_pos = 0;
  649. char* target_ptr = string_as_array(target) + pos;
  650. while (data_pos < data_.size()) {
  651. // Copy indent.
  652. memcpy(target_ptr, indent_.data(), indent_.size());
  653. target_ptr += indent_.size();
  654. // Copy line from data_.
  655. // We already guaranteed that data_ ends with a newline (above), so this
  656. // search can't fail.
  657. string::size_type line_length =
  658. data_.find_first_of('\n', data_pos) + 1 - data_pos;
  659. memcpy(target_ptr, data_.data() + data_pos, line_length);
  660. target_ptr += line_length;
  661. data_pos += line_length;
  662. }
  663. GOOGLE_CHECK_EQ(target_ptr,
  664. string_as_array(target) + pos + data_.size() + indent_size);
  665. }
  666. }
  667. }
  668. // ===================================================================
  669. #if defined(_WIN32) && !defined(__CYGWIN__)
  670. const char* const CommandLineInterface::kPathSeparator = ";";
  671. #else
  672. const char* const CommandLineInterface::kPathSeparator = ":";
  673. #endif
  674. CommandLineInterface::CommandLineInterface()
  675. : mode_(MODE_COMPILE),
  676. print_mode_(PRINT_NONE),
  677. error_format_(ERROR_FORMAT_GCC),
  678. direct_dependencies_explicitly_set_(false),
  679. direct_dependencies_violation_msg_(
  680. kDefaultDirectDependenciesViolationMsg),
  681. imports_in_descriptor_set_(false),
  682. source_info_in_descriptor_set_(false),
  683. disallow_services_(false) {}
  684. CommandLineInterface::~CommandLineInterface() {}
  685. void CommandLineInterface::RegisterGenerator(const string& flag_name,
  686. CodeGenerator* generator,
  687. const string& help_text) {
  688. GeneratorInfo info;
  689. info.flag_name = flag_name;
  690. info.generator = generator;
  691. info.help_text = help_text;
  692. generators_by_flag_name_[flag_name] = info;
  693. }
  694. void CommandLineInterface::RegisterGenerator(const string& flag_name,
  695. const string& option_flag_name,
  696. CodeGenerator* generator,
  697. const string& help_text) {
  698. GeneratorInfo info;
  699. info.flag_name = flag_name;
  700. info.option_flag_name = option_flag_name;
  701. info.generator = generator;
  702. info.help_text = help_text;
  703. generators_by_flag_name_[flag_name] = info;
  704. generators_by_option_name_[option_flag_name] = info;
  705. }
  706. void CommandLineInterface::AllowPlugins(const string& exe_name_prefix) {
  707. plugin_prefix_ = exe_name_prefix;
  708. }
  709. int CommandLineInterface::Run(int argc, const char* const argv[]) {
  710. Clear();
  711. switch (ParseArguments(argc, argv)) {
  712. case PARSE_ARGUMENT_DONE_AND_EXIT:
  713. return 0;
  714. case PARSE_ARGUMENT_FAIL:
  715. return 1;
  716. case PARSE_ARGUMENT_DONE_AND_CONTINUE:
  717. break;
  718. }
  719. std::vector<const FileDescriptor*> parsed_files;
  720. // null unless descriptor_set_in_names_.empty()
  721. std::unique_ptr<DiskSourceTree> disk_source_tree;
  722. std::unique_ptr<ErrorPrinter> error_collector;
  723. std::unique_ptr<DescriptorPool> descriptor_pool;
  724. std::unique_ptr<DescriptorDatabase> descriptor_database;
  725. if (descriptor_set_in_names_.empty()) {
  726. disk_source_tree.reset(new DiskSourceTree());
  727. if (!InitializeDiskSourceTree(disk_source_tree.get())) {
  728. return 1;
  729. }
  730. error_collector.reset(
  731. new ErrorPrinter(error_format_, disk_source_tree.get()));
  732. SourceTreeDescriptorDatabase* database =
  733. new SourceTreeDescriptorDatabase(disk_source_tree.get());
  734. database->RecordErrorsTo(error_collector.get());
  735. descriptor_database.reset(database);
  736. descriptor_pool.reset(new DescriptorPool(
  737. descriptor_database.get(), database->GetValidationErrorCollector()));
  738. } else {
  739. error_collector.reset(new ErrorPrinter(error_format_));
  740. SimpleDescriptorDatabase* database = new SimpleDescriptorDatabase();
  741. descriptor_database.reset(database);
  742. if (!PopulateSimpleDescriptorDatabase(database)) {
  743. return 1;
  744. }
  745. descriptor_pool.reset(new DescriptorPool(database, error_collector.get()));
  746. }
  747. descriptor_pool->EnforceWeakDependencies(true);
  748. if (!ParseInputFiles(descriptor_pool.get(), &parsed_files)) {
  749. return 1;
  750. }
  751. // We construct a separate GeneratorContext for each output location. Note
  752. // that two code generators may output to the same location, in which case
  753. // they should share a single GeneratorContext so that OpenForInsert() works.
  754. GeneratorContextMap output_directories;
  755. // Generate output.
  756. if (mode_ == MODE_COMPILE) {
  757. for (int i = 0; i < output_directives_.size(); i++) {
  758. string output_location = output_directives_[i].output_location;
  759. if (!HasSuffixString(output_location, ".zip") &&
  760. !HasSuffixString(output_location, ".jar")) {
  761. AddTrailingSlash(&output_location);
  762. }
  763. GeneratorContextImpl** map_slot = &output_directories[output_location];
  764. if (*map_slot == NULL) {
  765. // First time we've seen this output location.
  766. *map_slot = new GeneratorContextImpl(parsed_files);
  767. }
  768. if (!GenerateOutput(parsed_files, output_directives_[i], *map_slot)) {
  769. STLDeleteValues(&output_directories);
  770. return 1;
  771. }
  772. }
  773. }
  774. // Write all output to disk.
  775. for (GeneratorContextMap::iterator iter = output_directories.begin();
  776. iter != output_directories.end(); ++iter) {
  777. const string& location = iter->first;
  778. GeneratorContextImpl* directory = iter->second;
  779. if (HasSuffixString(location, "/")) {
  780. if (!directory->WriteAllToDisk(location)) {
  781. STLDeleteValues(&output_directories);
  782. return 1;
  783. }
  784. } else {
  785. if (HasSuffixString(location, ".jar")) {
  786. directory->AddJarManifest();
  787. }
  788. if (!directory->WriteAllToZip(location)) {
  789. STLDeleteValues(&output_directories);
  790. return 1;
  791. }
  792. }
  793. }
  794. if (!dependency_out_name_.empty()) {
  795. GOOGLE_DCHECK(disk_source_tree.get());
  796. if (!GenerateDependencyManifestFile(parsed_files, output_directories,
  797. disk_source_tree.get())) {
  798. return 1;
  799. }
  800. }
  801. STLDeleteValues(&output_directories);
  802. if (!descriptor_set_out_name_.empty()) {
  803. if (!WriteDescriptorSet(parsed_files)) {
  804. return 1;
  805. }
  806. }
  807. if (mode_ == MODE_ENCODE || mode_ == MODE_DECODE) {
  808. if (codec_type_.empty()) {
  809. // HACK: Define an EmptyMessage type to use for decoding.
  810. DescriptorPool pool;
  811. FileDescriptorProto file;
  812. file.set_name("empty_message.proto");
  813. file.add_message_type()->set_name("EmptyMessage");
  814. GOOGLE_CHECK(pool.BuildFile(file) != NULL);
  815. codec_type_ = "EmptyMessage";
  816. if (!EncodeOrDecode(&pool)) {
  817. return 1;
  818. }
  819. } else {
  820. if (!EncodeOrDecode(descriptor_pool.get())) {
  821. return 1;
  822. }
  823. }
  824. }
  825. if (error_collector->FoundErrors()) {
  826. return 1;
  827. }
  828. if (mode_ == MODE_PRINT) {
  829. switch (print_mode_) {
  830. case PRINT_FREE_FIELDS:
  831. for (int i = 0; i < parsed_files.size(); ++i) {
  832. const FileDescriptor* fd = parsed_files[i];
  833. for (int j = 0; j < fd->message_type_count(); ++j) {
  834. PrintFreeFieldNumbers(fd->message_type(j));
  835. }
  836. }
  837. break;
  838. case PRINT_NONE:
  839. GOOGLE_LOG(ERROR) << "If the code reaches here, it usually means a bug of "
  840. "flag parsing in the CommandLineInterface.";
  841. return 1;
  842. // Do not add a default case.
  843. }
  844. }
  845. return 0;
  846. }
  847. bool CommandLineInterface::InitializeDiskSourceTree(
  848. DiskSourceTree* source_tree) {
  849. AddDefaultProtoPaths(&proto_path_);
  850. // Set up the source tree.
  851. for (int i = 0; i < proto_path_.size(); i++) {
  852. source_tree->MapPath(proto_path_[i].first, proto_path_[i].second);
  853. }
  854. // Map input files to virtual paths if possible.
  855. if (!MakeInputsBeProtoPathRelative(source_tree)) {
  856. return false;
  857. }
  858. return true;
  859. }
  860. bool CommandLineInterface::PopulateSimpleDescriptorDatabase(
  861. SimpleDescriptorDatabase* database) {
  862. for (int i = 0; i < descriptor_set_in_names_.size(); i++) {
  863. int fd;
  864. do {
  865. fd = open(descriptor_set_in_names_[i].c_str(), O_RDONLY | O_BINARY);
  866. } while (fd < 0 && errno == EINTR);
  867. if (fd < 0) {
  868. std::cerr << descriptor_set_in_names_[i] << ": "
  869. << strerror(ENOENT) << std::endl;
  870. return false;
  871. }
  872. FileDescriptorSet file_descriptor_set;
  873. bool parsed = file_descriptor_set.ParseFromFileDescriptor(fd);
  874. if (close(fd) != 0) {
  875. std::cerr << descriptor_set_in_names_[i] << ": close: "
  876. << strerror(errno)
  877. << std::endl;
  878. return false;
  879. }
  880. if (!parsed) {
  881. std::cerr << descriptor_set_in_names_[i] << ": Unable to parse."
  882. << std::endl;
  883. return false;
  884. }
  885. for (int j = 0; j < file_descriptor_set.file_size(); j++) {
  886. FileDescriptorProto previously_added_file_descriptor_proto;
  887. if (database->FindFileByName(file_descriptor_set.file(j).name(),
  888. &previously_added_file_descriptor_proto)) {
  889. // already present - skip
  890. continue;
  891. }
  892. if (!database->Add(file_descriptor_set.file(j))) {
  893. return false;
  894. }
  895. }
  896. }
  897. return true;
  898. }
  899. bool CommandLineInterface::ParseInputFiles(
  900. DescriptorPool* descriptor_pool,
  901. std::vector<const FileDescriptor*>* parsed_files) {
  902. // Parse each file.
  903. for (int i = 0; i < input_files_.size(); i++) {
  904. // Import the file.
  905. descriptor_pool->AddUnusedImportTrackFile(input_files_[i]);
  906. const FileDescriptor* parsed_file =
  907. descriptor_pool->FindFileByName(input_files_[i]);
  908. descriptor_pool->ClearUnusedImportTrackFiles();
  909. if (parsed_file == NULL) {
  910. if (!descriptor_set_in_names_.empty()) {
  911. std::cerr << input_files_[i] << ": " << strerror(ENOENT) << std::endl;
  912. }
  913. return false;
  914. }
  915. parsed_files->push_back(parsed_file);
  916. // Enforce --disallow_services.
  917. if (disallow_services_ && parsed_file->service_count() > 0) {
  918. std::cerr << parsed_file->name() << ": This file contains services, but "
  919. "--disallow_services was used." << std::endl;
  920. return false;
  921. }
  922. // Enforce --direct_dependencies
  923. if (direct_dependencies_explicitly_set_) {
  924. bool indirect_imports = false;
  925. for (int i = 0; i < parsed_file->dependency_count(); i++) {
  926. if (direct_dependencies_.find(parsed_file->dependency(i)->name()) ==
  927. direct_dependencies_.end()) {
  928. indirect_imports = true;
  929. std::cerr << parsed_file->name() << ": "
  930. << StringReplace(direct_dependencies_violation_msg_, "%s",
  931. parsed_file->dependency(i)->name(),
  932. true /* replace_all */)
  933. << std::endl;
  934. }
  935. }
  936. if (indirect_imports) {
  937. return false;
  938. }
  939. }
  940. }
  941. return true;
  942. }
  943. void CommandLineInterface::Clear() {
  944. // Clear all members that are set by Run(). Note that we must not clear
  945. // members which are set by other methods before Run() is called.
  946. executable_name_.clear();
  947. proto_path_.clear();
  948. input_files_.clear();
  949. direct_dependencies_.clear();
  950. direct_dependencies_violation_msg_ = kDefaultDirectDependenciesViolationMsg;
  951. output_directives_.clear();
  952. codec_type_.clear();
  953. descriptor_set_in_names_.clear();
  954. descriptor_set_out_name_.clear();
  955. dependency_out_name_.clear();
  956. mode_ = MODE_COMPILE;
  957. print_mode_ = PRINT_NONE;
  958. imports_in_descriptor_set_ = false;
  959. source_info_in_descriptor_set_ = false;
  960. disallow_services_ = false;
  961. direct_dependencies_explicitly_set_ = false;
  962. }
  963. bool CommandLineInterface::MakeInputsBeProtoPathRelative(
  964. DiskSourceTree* source_tree) {
  965. for (int i = 0; i < input_files_.size(); i++) {
  966. // If the input file path is not a physical file path, it must be a virtual
  967. // path.
  968. if (access(input_files_[i].c_str(), F_OK) < 0) {
  969. string disk_file;
  970. if (source_tree->VirtualFileToDiskFile(input_files_[i], &disk_file)) {
  971. return true;
  972. } else {
  973. std::cerr << input_files_[i] << ": " << strerror(ENOENT) << std::endl;
  974. return false;
  975. }
  976. }
  977. string virtual_file, shadowing_disk_file;
  978. switch (source_tree->DiskFileToVirtualFile(
  979. input_files_[i], &virtual_file, &shadowing_disk_file)) {
  980. case DiskSourceTree::SUCCESS:
  981. input_files_[i] = virtual_file;
  982. break;
  983. case DiskSourceTree::SHADOWED:
  984. std::cerr << input_files_[i]
  985. << ": Input is shadowed in the --proto_path by \""
  986. << shadowing_disk_file
  987. << "\". Either use the latter file as your input or reorder "
  988. "the --proto_path so that the former file's location "
  989. "comes first." << std::endl;
  990. return false;
  991. case DiskSourceTree::CANNOT_OPEN:
  992. std::cerr << input_files_[i] << ": " << strerror(errno) << std::endl;
  993. return false;
  994. case DiskSourceTree::NO_MAPPING: {
  995. // Try to interpret the path as a virtual path.
  996. string disk_file;
  997. if (source_tree->VirtualFileToDiskFile(input_files_[i], &disk_file)) {
  998. return true;
  999. } else {
  1000. // The input file path can't be mapped to any --proto_path and it also
  1001. // can't be interpreted as a virtual path.
  1002. std::cerr
  1003. << input_files_[i]
  1004. << ": File does not reside within any path "
  1005. "specified using --proto_path (or -I). You must specify a "
  1006. "--proto_path which encompasses this file. Note that the "
  1007. "proto_path must be an exact prefix of the .proto file "
  1008. "names -- protoc is too dumb to figure out when two paths "
  1009. "(e.g. absolute and relative) are equivalent (it's harder "
  1010. "than you think)."
  1011. << std::endl;
  1012. return false;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. return true;
  1018. }
  1019. bool CommandLineInterface::ExpandArgumentFile(const string& file,
  1020. std::vector<string>* arguments) {
  1021. // The argument file is searched in the working directory only. We don't
  1022. // use the proto import path here.
  1023. std::ifstream file_stream(file.c_str());
  1024. if (!file_stream.is_open()) {
  1025. return false;
  1026. }
  1027. string argument;
  1028. // We don't support any kind of shell expansion right now.
  1029. while (std::getline(file_stream, argument)) {
  1030. arguments->push_back(argument);
  1031. }
  1032. return true;
  1033. }
  1034. CommandLineInterface::ParseArgumentStatus
  1035. CommandLineInterface::ParseArguments(int argc, const char* const argv[]) {
  1036. executable_name_ = argv[0];
  1037. std::vector<string> arguments;
  1038. for (int i = 1; i < argc; ++i) {
  1039. if (argv[i][0] == '@') {
  1040. if (!ExpandArgumentFile(argv[i] + 1, &arguments)) {
  1041. std::cerr << "Failed to open argument file: " << (argv[i] + 1)
  1042. << std::endl;
  1043. return PARSE_ARGUMENT_FAIL;
  1044. }
  1045. continue;
  1046. }
  1047. arguments.push_back(argv[i]);
  1048. }
  1049. // if no arguments are given, show help
  1050. if (arguments.empty()) {
  1051. PrintHelpText();
  1052. return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler.
  1053. }
  1054. // Iterate through all arguments and parse them.
  1055. for (int i = 0; i < arguments.size(); ++i) {
  1056. string name, value;
  1057. if (ParseArgument(arguments[i].c_str(), &name, &value)) {
  1058. // Returned true => Use the next argument as the flag value.
  1059. if (i + 1 == arguments.size() || arguments[i + 1][0] == '-') {
  1060. std::cerr << "Missing value for flag: " << name << std::endl;
  1061. if (name == "--decode") {
  1062. std::cerr << "To decode an unknown message, use --decode_raw."
  1063. << std::endl;
  1064. }
  1065. return PARSE_ARGUMENT_FAIL;
  1066. } else {
  1067. ++i;
  1068. value = arguments[i];
  1069. }
  1070. }
  1071. ParseArgumentStatus status = InterpretArgument(name, value);
  1072. if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE)
  1073. return status;
  1074. }
  1075. // Make sure each plugin option has a matching plugin output.
  1076. bool foundUnknownPluginOption = false;
  1077. for (std::map<string, string>::const_iterator i = plugin_parameters_.begin();
  1078. i != plugin_parameters_.end(); ++i) {
  1079. if (plugins_.find(i->first) != plugins_.end()) {
  1080. continue;
  1081. }
  1082. bool foundImplicitPlugin = false;
  1083. for (std::vector<OutputDirective>::const_iterator j = output_directives_.begin();
  1084. j != output_directives_.end(); ++j) {
  1085. if (j->generator == NULL) {
  1086. string plugin_name = PluginName(plugin_prefix_ , j->name);
  1087. if (plugin_name == i->first) {
  1088. foundImplicitPlugin = true;
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. if (!foundImplicitPlugin) {
  1094. std::cerr << "Unknown flag: "
  1095. // strip prefix + "gen-" and add back "_opt"
  1096. << "--" + i->first.substr(plugin_prefix_.size() + 4) + "_opt"
  1097. << std::endl;
  1098. foundUnknownPluginOption = true;
  1099. }
  1100. }
  1101. if (foundUnknownPluginOption) {
  1102. return PARSE_ARGUMENT_FAIL;
  1103. }
  1104. // If no --proto_path was given, use the current working directory.
  1105. if (proto_path_.empty()) {
  1106. // Don't use make_pair as the old/default standard library on Solaris
  1107. // doesn't support it without explicit template parameters, which are
  1108. // incompatible with C++0x's make_pair.
  1109. proto_path_.push_back(std::pair<string, string>("", "."));
  1110. }
  1111. // Check some errror cases.
  1112. bool decoding_raw = (mode_ == MODE_DECODE) && codec_type_.empty();
  1113. if (decoding_raw && !input_files_.empty()) {
  1114. std::cerr << "When using --decode_raw, no input files should be given."
  1115. << std::endl;
  1116. return PARSE_ARGUMENT_FAIL;
  1117. } else if (!decoding_raw && input_files_.empty()) {
  1118. std::cerr << "Missing input file." << std::endl;
  1119. return PARSE_ARGUMENT_FAIL;
  1120. }
  1121. if (mode_ == MODE_COMPILE && output_directives_.empty() &&
  1122. descriptor_set_out_name_.empty()) {
  1123. std::cerr << "Missing output directives." << std::endl;
  1124. return PARSE_ARGUMENT_FAIL;
  1125. }
  1126. if (mode_ != MODE_COMPILE && !dependency_out_name_.empty()) {
  1127. std::cerr << "Can only use --dependency_out=FILE when generating code."
  1128. << std::endl;
  1129. return PARSE_ARGUMENT_FAIL;
  1130. }
  1131. if (!dependency_out_name_.empty() && input_files_.size() > 1) {
  1132. std::cerr
  1133. << "Can only process one input file when using --dependency_out=FILE."
  1134. << std::endl;
  1135. return PARSE_ARGUMENT_FAIL;
  1136. }
  1137. if (imports_in_descriptor_set_ && descriptor_set_out_name_.empty()) {
  1138. std::cerr << "--include_imports only makes sense when combined with "
  1139. "--descriptor_set_out." << std::endl;
  1140. }
  1141. if (source_info_in_descriptor_set_ && descriptor_set_out_name_.empty()) {
  1142. std::cerr << "--include_source_info only makes sense when combined with "
  1143. "--descriptor_set_out." << std::endl;
  1144. }
  1145. return PARSE_ARGUMENT_DONE_AND_CONTINUE;
  1146. }
  1147. bool CommandLineInterface::ParseArgument(const char* arg,
  1148. string* name, string* value) {
  1149. bool parsed_value = false;
  1150. if (arg[0] != '-') {
  1151. // Not a flag.
  1152. name->clear();
  1153. parsed_value = true;
  1154. *value = arg;
  1155. } else if (arg[1] == '-') {
  1156. // Two dashes: Multi-character name, with '=' separating name and
  1157. // value.
  1158. const char* equals_pos = strchr(arg, '=');
  1159. if (equals_pos != NULL) {
  1160. *name = string(arg, equals_pos - arg);
  1161. *value = equals_pos + 1;
  1162. parsed_value = true;
  1163. } else {
  1164. *name = arg;
  1165. }
  1166. } else {
  1167. // One dash: One-character name, all subsequent characters are the
  1168. // value.
  1169. if (arg[1] == '\0') {
  1170. // arg is just "-". We treat this as an input file, except that at
  1171. // present this will just lead to a "file not found" error.
  1172. name->clear();
  1173. *value = arg;
  1174. parsed_value = true;
  1175. } else {
  1176. *name = string(arg, 2);
  1177. *value = arg + 2;
  1178. parsed_value = !value->empty();
  1179. }
  1180. }
  1181. // Need to return true iff the next arg should be used as the value for this
  1182. // one, false otherwise.
  1183. if (parsed_value) {
  1184. // We already parsed a value for this flag.
  1185. return false;
  1186. }
  1187. if (*name == "-h" || *name == "--help" ||
  1188. *name == "--disallow_services" ||
  1189. *name == "--include_imports" ||
  1190. *name == "--include_source_info" ||
  1191. *name == "--version" ||
  1192. *name == "--decode_raw" ||
  1193. *name == "--print_free_field_numbers") {
  1194. // HACK: These are the only flags that don't take a value.
  1195. // They probably should not be hard-coded like this but for now it's
  1196. // not worth doing better.
  1197. return false;
  1198. }
  1199. // Next argument is the flag value.
  1200. return true;
  1201. }
  1202. CommandLineInterface::ParseArgumentStatus
  1203. CommandLineInterface::InterpretArgument(const string& name,
  1204. const string& value) {
  1205. if (name.empty()) {
  1206. // Not a flag. Just a filename.
  1207. if (value.empty()) {
  1208. std::cerr
  1209. << "You seem to have passed an empty string as one of the "
  1210. "arguments to " << executable_name_
  1211. << ". This is actually "
  1212. "sort of hard to do. Congrats. Unfortunately it is not valid "
  1213. "input so the program is going to die now." << std::endl;
  1214. return PARSE_ARGUMENT_FAIL;
  1215. }
  1216. input_files_.push_back(value);
  1217. } else if (name == "-I" || name == "--proto_path") {
  1218. if (!descriptor_set_in_names_.empty()) {
  1219. std::cerr << "Only one of " << name
  1220. << " and --descriptor_set_in can be specified."
  1221. << std::endl;
  1222. return PARSE_ARGUMENT_FAIL;
  1223. }
  1224. // Java's -classpath (and some other languages) delimits path components
  1225. // with colons. Let's accept that syntax too just to make things more
  1226. // intuitive.
  1227. std::vector<string> parts = Split(
  1228. value, CommandLineInterface::kPathSeparator,
  1229. true);
  1230. for (int i = 0; i < parts.size(); i++) {
  1231. string virtual_path;
  1232. string disk_path;
  1233. string::size_type equals_pos = parts[i].find_first_of('=');
  1234. if (equals_pos == string::npos) {
  1235. virtual_path = "";
  1236. disk_path = parts[i];
  1237. } else {
  1238. virtual_path = parts[i].substr(0, equals_pos);
  1239. disk_path = parts[i].substr(equals_pos + 1);
  1240. }
  1241. if (disk_path.empty()) {
  1242. std::cerr
  1243. << "--proto_path passed empty directory name. (Use \".\" for "
  1244. "current directory.)" << std::endl;
  1245. return PARSE_ARGUMENT_FAIL;
  1246. }
  1247. // Make sure disk path exists, warn otherwise.
  1248. if (access(disk_path.c_str(), F_OK) < 0) {
  1249. // Try the original path; it may have just happened to have a '=' in it.
  1250. if (access(parts[i].c_str(), F_OK) < 0) {
  1251. std::cerr << disk_path << ": warning: directory does not exist."
  1252. << std::endl;
  1253. } else {
  1254. virtual_path = "";
  1255. disk_path = parts[i];
  1256. }
  1257. }
  1258. // Don't use make_pair as the old/default standard library on Solaris
  1259. // doesn't support it without explicit template parameters, which are
  1260. // incompatible with C++0x's make_pair.
  1261. proto_path_.push_back(std::pair<string, string>(virtual_path, disk_path));
  1262. }
  1263. } else if (name == "--direct_dependencies") {
  1264. if (direct_dependencies_explicitly_set_) {
  1265. std::cerr << name << " may only be passed once. To specify multiple "
  1266. "direct dependencies, pass them all as a single "
  1267. "parameter separated by ':'."
  1268. << std::endl;
  1269. return PARSE_ARGUMENT_FAIL;
  1270. }
  1271. direct_dependencies_explicitly_set_ = true;
  1272. std::vector<string> direct = Split(value, ":", true);
  1273. GOOGLE_DCHECK(direct_dependencies_.empty());
  1274. direct_dependencies_.insert(direct.begin(), direct.end());
  1275. } else if (name == "--direct_dependencies_violation_msg") {
  1276. direct_dependencies_violation_msg_ = value;
  1277. } else if (name == "--descriptor_set_in") {
  1278. if (!descriptor_set_in_names_.empty()) {
  1279. std::cerr << name << " may only be passed once. To specify multiple "
  1280. "descriptor sets, pass them all as a single "
  1281. "parameter separated by '"
  1282. << CommandLineInterface::kPathSeparator << "'."
  1283. << std::endl;
  1284. return PARSE_ARGUMENT_FAIL;
  1285. }
  1286. if (value.empty()) {
  1287. std::cerr << name << " requires a non-empty value." << std::endl;
  1288. return PARSE_ARGUMENT_FAIL;
  1289. }
  1290. if (!proto_path_.empty()) {
  1291. std::cerr << "Only one of " << name
  1292. << " and --proto_path can be specified."
  1293. << std::endl;
  1294. return PARSE_ARGUMENT_FAIL;
  1295. }
  1296. if (!dependency_out_name_.empty()) {
  1297. std::cerr << name << " cannot be used with --dependency_out."
  1298. << std::endl;
  1299. return PARSE_ARGUMENT_FAIL;
  1300. }
  1301. descriptor_set_in_names_ = Split(
  1302. value, CommandLineInterface::kPathSeparator,
  1303. true);
  1304. } else if (name == "-o" || name == "--descriptor_set_out") {
  1305. if (!descriptor_set_out_name_.empty()) {
  1306. std::cerr << name << " may only be passed once." << std::endl;
  1307. return PARSE_ARGUMENT_FAIL;
  1308. }
  1309. if (value.empty()) {
  1310. std::cerr << name << " requires a non-empty value." << std::endl;
  1311. return PARSE_ARGUMENT_FAIL;
  1312. }
  1313. if (mode_ != MODE_COMPILE) {
  1314. std::cerr
  1315. << "Cannot use --encode or --decode and generate descriptors at the "
  1316. "same time." << std::endl;
  1317. return PARSE_ARGUMENT_FAIL;
  1318. }
  1319. descriptor_set_out_name_ = value;
  1320. } else if (name == "--dependency_out") {
  1321. if (!dependency_out_name_.empty()) {
  1322. std::cerr << name << " may only be passed once." << std::endl;
  1323. return PARSE_ARGUMENT_FAIL;
  1324. }
  1325. if (value.empty()) {
  1326. std::cerr << name << " requires a non-empty value." << std::endl;
  1327. return PARSE_ARGUMENT_FAIL;
  1328. }
  1329. if (!descriptor_set_in_names_.empty()) {
  1330. std::cerr << name << " cannot be used with --descriptor_set_in."
  1331. << std::endl;
  1332. return PARSE_ARGUMENT_FAIL;
  1333. }
  1334. dependency_out_name_ = value;
  1335. } else if (name == "--include_imports") {
  1336. if (imports_in_descriptor_set_) {
  1337. std::cerr << name << " may only be passed once." << std::endl;
  1338. return PARSE_ARGUMENT_FAIL;
  1339. }
  1340. imports_in_descriptor_set_ = true;
  1341. } else if (name == "--include_source_info") {
  1342. if (source_info_in_descriptor_set_) {
  1343. std::cerr << name << " may only be passed once." << std::endl;
  1344. return PARSE_ARGUMENT_FAIL;
  1345. }
  1346. source_info_in_descriptor_set_ = true;
  1347. } else if (name == "-h" || name == "--help") {
  1348. PrintHelpText();
  1349. return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler.
  1350. } else if (name == "--version") {
  1351. if (!version_info_.empty()) {
  1352. std::cout << version_info_ << std::endl;
  1353. }
  1354. std::cout << "libprotoc "
  1355. << protobuf::internal::VersionString(GOOGLE_PROTOBUF_VERSION)
  1356. << std::endl;
  1357. return PARSE_ARGUMENT_DONE_AND_EXIT; // Exit without running compiler.
  1358. } else if (name == "--disallow_services") {
  1359. disallow_services_ = true;
  1360. } else if (name == "--encode" || name == "--decode" ||
  1361. name == "--decode_raw") {
  1362. if (mode_ != MODE_COMPILE) {
  1363. std::cerr << "Only one of --encode and --decode can be specified."
  1364. << std::endl;
  1365. return PARSE_ARGUMENT_FAIL;
  1366. }
  1367. if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) {
  1368. std::cerr << "Cannot use " << name
  1369. << " and generate code or descriptors at the same time."
  1370. << std::endl;
  1371. return PARSE_ARGUMENT_FAIL;
  1372. }
  1373. mode_ = (name == "--encode") ? MODE_ENCODE : MODE_DECODE;
  1374. if (value.empty() && name != "--decode_raw") {
  1375. std::cerr << "Type name for " << name << " cannot be blank." << std::endl;
  1376. if (name == "--decode") {
  1377. std::cerr << "To decode an unknown message, use --decode_raw."
  1378. << std::endl;
  1379. }
  1380. return PARSE_ARGUMENT_FAIL;
  1381. } else if (!value.empty() && name == "--decode_raw") {
  1382. std::cerr << "--decode_raw does not take a parameter." << std::endl;
  1383. return PARSE_ARGUMENT_FAIL;
  1384. }
  1385. codec_type_ = value;
  1386. } else if (name == "--error_format") {
  1387. if (value == "gcc") {
  1388. error_format_ = ERROR_FORMAT_GCC;
  1389. } else if (value == "msvs") {
  1390. error_format_ = ERROR_FORMAT_MSVS;
  1391. } else {
  1392. std::cerr << "Unknown error format: " << value << std::endl;
  1393. return PARSE_ARGUMENT_FAIL;
  1394. }
  1395. } else if (name == "--plugin") {
  1396. if (plugin_prefix_.empty()) {
  1397. std::cerr << "This compiler does not support plugins." << std::endl;
  1398. return PARSE_ARGUMENT_FAIL;
  1399. }
  1400. string plugin_name;
  1401. string path;
  1402. string::size_type equals_pos = value.find_first_of('=');
  1403. if (equals_pos == string::npos) {
  1404. // Use the basename of the file.
  1405. string::size_type slash_pos = value.find_last_of('/');
  1406. if (slash_pos == string::npos) {
  1407. plugin_name = value;
  1408. } else {
  1409. plugin_name = value.substr(slash_pos + 1);
  1410. }
  1411. path = value;
  1412. } else {
  1413. plugin_name = value.substr(0, equals_pos);
  1414. path = value.substr(equals_pos + 1);
  1415. }
  1416. plugins_[plugin_name] = path;
  1417. } else if (name == "--print_free_field_numbers") {
  1418. if (mode_ != MODE_COMPILE) {
  1419. std::cerr << "Cannot use " << name
  1420. << " and use --encode, --decode or print "
  1421. << "other info at the same time." << std::endl;
  1422. return PARSE_ARGUMENT_FAIL;
  1423. }
  1424. if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) {
  1425. std::cerr << "Cannot use " << name
  1426. << " and generate code or descriptors at the same time."
  1427. << std::endl;
  1428. return PARSE_ARGUMENT_FAIL;
  1429. }
  1430. mode_ = MODE_PRINT;
  1431. print_mode_ = PRINT_FREE_FIELDS;
  1432. } else {
  1433. // Some other flag. Look it up in the generators list.
  1434. const GeneratorInfo* generator_info =
  1435. FindOrNull(generators_by_flag_name_, name);
  1436. if (generator_info == NULL &&
  1437. (plugin_prefix_.empty() || !HasSuffixString(name, "_out"))) {
  1438. // Check if it's a generator option flag.
  1439. generator_info = FindOrNull(generators_by_option_name_, name);
  1440. if (generator_info != NULL) {
  1441. string* parameters = &generator_parameters_[generator_info->flag_name];
  1442. if (!parameters->empty()) {
  1443. parameters->append(",");
  1444. }
  1445. parameters->append(value);
  1446. } else if (HasPrefixString(name, "--") && HasSuffixString(name, "_opt")) {
  1447. string* parameters =
  1448. &plugin_parameters_[PluginName(plugin_prefix_, name)];
  1449. if (!parameters->empty()) {
  1450. parameters->append(",");
  1451. }
  1452. parameters->append(value);
  1453. } else {
  1454. std::cerr << "Unknown flag: " << name << std::endl;
  1455. return PARSE_ARGUMENT_FAIL;
  1456. }
  1457. } else {
  1458. // It's an output flag. Add it to the output directives.
  1459. if (mode_ != MODE_COMPILE) {
  1460. std::cerr << "Cannot use --encode, --decode or print .proto info and "
  1461. "generate code at the same time." << std::endl;
  1462. return PARSE_ARGUMENT_FAIL;
  1463. }
  1464. OutputDirective directive;
  1465. directive.name = name;
  1466. if (generator_info == NULL) {
  1467. directive.generator = NULL;
  1468. } else {
  1469. directive.generator = generator_info->generator;
  1470. }
  1471. // Split value at ':' to separate the generator parameter from the
  1472. // filename. However, avoid doing this if the colon is part of a valid
  1473. // Windows-style absolute path.
  1474. string::size_type colon_pos = value.find_first_of(':');
  1475. if (colon_pos == string::npos || IsWindowsAbsolutePath(value)) {
  1476. directive.output_location = value;
  1477. } else {
  1478. directive.parameter = value.substr(0, colon_pos);
  1479. directive.output_location = value.substr(colon_pos + 1);
  1480. }
  1481. output_directives_.push_back(directive);
  1482. }
  1483. }
  1484. return PARSE_ARGUMENT_DONE_AND_CONTINUE;
  1485. }
  1486. void CommandLineInterface::PrintHelpText() {
  1487. // Sorry for indentation here; line wrapping would be uglier.
  1488. std::cout <<
  1489. "Usage: " << executable_name_ << " [OPTION] PROTO_FILES\n"
  1490. "Parse PROTO_FILES and generate output based on the options given:\n"
  1491. " -IPATH, --proto_path=PATH Specify the directory in which to search for\n"
  1492. " imports. May be specified multiple times;\n"
  1493. " directories will be searched in order. If not\n"
  1494. " given, the current working directory is used.\n"
  1495. " --version Show version info and exit.\n"
  1496. " -h, --help Show this text and exit.\n"
  1497. " --encode=MESSAGE_TYPE Read a text-format message of the given type\n"
  1498. " from standard input and write it in binary\n"
  1499. " to standard output. The message type must\n"
  1500. " be defined in PROTO_FILES or their imports.\n"
  1501. " --decode=MESSAGE_TYPE Read a binary message of the given type from\n"
  1502. " standard input and write it in text format\n"
  1503. " to standard output. The message type must\n"
  1504. " be defined in PROTO_FILES or their imports.\n"
  1505. " --decode_raw Read an arbitrary protocol message from\n"
  1506. " standard input and write the raw tag/value\n"
  1507. " pairs in text format to standard output. No\n"
  1508. " PROTO_FILES should be given when using this\n"
  1509. " flag.\n"
  1510. " --descriptor_set_in=FILES Specifies a delimited list of FILES\n"
  1511. " each containing a FileDescriptorSet (a\n"
  1512. " protocol buffer defined in descriptor.proto).\n"
  1513. " The FileDescriptor for each of the PROTO_FILES\n"
  1514. " provided will be loaded from these\n"
  1515. " FileDescriptorSets. If a FileDescriptor\n"
  1516. " appears multiple times, the first occurrence\n"
  1517. " will be used.\n"
  1518. " -oFILE, Writes a FileDescriptorSet (a protocol buffer,\n"
  1519. " --descriptor_set_out=FILE defined in descriptor.proto) containing all of\n"
  1520. " the input files to FILE.\n"
  1521. " --include_imports When using --descriptor_set_out, also include\n"
  1522. " all dependencies of the input files in the\n"
  1523. " set, so that the set is self-contained.\n"
  1524. " --include_source_info When using --descriptor_set_out, do not strip\n"
  1525. " SourceCodeInfo from the FileDescriptorProto.\n"
  1526. " This results in vastly larger descriptors that\n"
  1527. " include information about the original\n"
  1528. " location of each decl in the source file as\n"
  1529. " well as surrounding comments.\n"
  1530. " --dependency_out=FILE Write a dependency output file in the format\n"
  1531. " expected by make. This writes the transitive\n"
  1532. " set of input file paths to FILE\n"
  1533. " --error_format=FORMAT Set the format in which to print errors.\n"
  1534. " FORMAT may be 'gcc' (the default) or 'msvs'\n"
  1535. " (Microsoft Visual Studio format).\n"
  1536. " --print_free_field_numbers Print the free field numbers of the messages\n"
  1537. " defined in the given proto files. Groups share\n"
  1538. " the same field number space with the parent \n"
  1539. " message. Extension ranges are counted as \n"
  1540. " occupied fields numbers.\n"
  1541. << std::endl;
  1542. if (!plugin_prefix_.empty()) {
  1543. std::cout <<
  1544. " --plugin=EXECUTABLE Specifies a plugin executable to use.\n"
  1545. " Normally, protoc searches the PATH for\n"
  1546. " plugins, but you may specify additional\n"
  1547. " executables not in the path using this flag.\n"
  1548. " Additionally, EXECUTABLE may be of the form\n"
  1549. " NAME=PATH, in which case the given plugin name\n"
  1550. " is mapped to the given executable even if\n"
  1551. " the executable's own name differs." << std::endl;
  1552. }
  1553. for (GeneratorMap::iterator iter = generators_by_flag_name_.begin();
  1554. iter != generators_by_flag_name_.end(); ++iter) {
  1555. // FIXME(kenton): If the text is long enough it will wrap, which is ugly,
  1556. // but fixing this nicely (e.g. splitting on spaces) is probably more
  1557. // trouble than it's worth.
  1558. std::cout << " " << iter->first << "=OUT_DIR "
  1559. << string(19 - iter->first.size(), ' ') // Spaces for alignment.
  1560. << iter->second.help_text << std::endl;
  1561. }
  1562. std::cerr <<
  1563. " @<filename> Read options and filenames from file. If a\n"
  1564. " relative file path is specified, the file\n"
  1565. " will be searched in the working directory.\n"
  1566. " The --proto_path option will not affect how\n"
  1567. " this argument file is searched. Content of\n"
  1568. " the file will be expanded in the position of\n"
  1569. " @<filename> as in the argument list. Note\n"
  1570. " that shell expansion is not applied to the\n"
  1571. " content of the file (i.e., you cannot use\n"
  1572. " quotes, wildcards, escapes, commands, etc.).\n"
  1573. " Each line corresponds to a single argument,\n"
  1574. " even if it contains spaces."
  1575. << std::endl;
  1576. }
  1577. bool CommandLineInterface::GenerateOutput(
  1578. const std::vector<const FileDescriptor*>& parsed_files,
  1579. const OutputDirective& output_directive,
  1580. GeneratorContext* generator_context) {
  1581. // Call the generator.
  1582. string error;
  1583. if (output_directive.generator == NULL) {
  1584. // This is a plugin.
  1585. GOOGLE_CHECK(HasPrefixString(output_directive.name, "--") &&
  1586. HasSuffixString(output_directive.name, "_out"))
  1587. << "Bad name for plugin generator: " << output_directive.name;
  1588. string plugin_name = PluginName(plugin_prefix_ , output_directive.name);
  1589. string parameters = output_directive.parameter;
  1590. if (!plugin_parameters_[plugin_name].empty()) {
  1591. if (!parameters.empty()) {
  1592. parameters.append(",");
  1593. }
  1594. parameters.append(plugin_parameters_[plugin_name]);
  1595. }
  1596. if (!GeneratePluginOutput(parsed_files, plugin_name,
  1597. parameters,
  1598. generator_context, &error)) {
  1599. std::cerr << output_directive.name << ": " << error << std::endl;
  1600. return false;
  1601. }
  1602. } else {
  1603. // Regular generator.
  1604. string parameters = output_directive.parameter;
  1605. if (!generator_parameters_[output_directive.name].empty()) {
  1606. if (!parameters.empty()) {
  1607. parameters.append(",");
  1608. }
  1609. parameters.append(generator_parameters_[output_directive.name]);
  1610. }
  1611. if (!output_directive.generator->GenerateAll(
  1612. parsed_files, parameters, generator_context, &error)) {
  1613. // Generator returned an error.
  1614. std::cerr << output_directive.name << ": " << error << std::endl;
  1615. return false;
  1616. }
  1617. }
  1618. return true;
  1619. }
  1620. bool CommandLineInterface::GenerateDependencyManifestFile(
  1621. const std::vector<const FileDescriptor*>& parsed_files,
  1622. const GeneratorContextMap& output_directories,
  1623. DiskSourceTree* source_tree) {
  1624. FileDescriptorSet file_set;
  1625. std::set<const FileDescriptor*> already_seen;
  1626. for (int i = 0; i < parsed_files.size(); i++) {
  1627. GetTransitiveDependencies(parsed_files[i],
  1628. false,
  1629. false,
  1630. &already_seen,
  1631. file_set.mutable_file());
  1632. }
  1633. std::vector<string> output_filenames;
  1634. for (GeneratorContextMap::const_iterator iter = output_directories.begin();
  1635. iter != output_directories.end(); ++iter) {
  1636. const string& location = iter->first;
  1637. GeneratorContextImpl* directory = iter->second;
  1638. std::vector<string> relative_output_filenames;
  1639. directory->GetOutputFilenames(&relative_output_filenames);
  1640. for (int i = 0; i < relative_output_filenames.size(); i++) {
  1641. string output_filename = location + relative_output_filenames[i];
  1642. if (output_filename.compare(0, 2, "./") == 0) {
  1643. output_filename = output_filename.substr(2);
  1644. }
  1645. output_filenames.push_back(output_filename);
  1646. }
  1647. }
  1648. int fd;
  1649. do {
  1650. fd = open(dependency_out_name_.c_str(),
  1651. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  1652. } while (fd < 0 && errno == EINTR);
  1653. if (fd < 0) {
  1654. perror(dependency_out_name_.c_str());
  1655. return false;
  1656. }
  1657. io::FileOutputStream out(fd);
  1658. io::Printer printer(&out, '$');
  1659. for (int i = 0; i < output_filenames.size(); i++) {
  1660. printer.Print(output_filenames[i].c_str());
  1661. if (i == output_filenames.size() - 1) {
  1662. printer.Print(":");
  1663. } else {
  1664. printer.Print(" \\\n");
  1665. }
  1666. }
  1667. for (int i = 0; i < file_set.file_size(); i++) {
  1668. const FileDescriptorProto& file = file_set.file(i);
  1669. const string& virtual_file = file.name();
  1670. string disk_file;
  1671. if (source_tree &&
  1672. source_tree->VirtualFileToDiskFile(virtual_file, &disk_file)) {
  1673. printer.Print(" $disk_file$", "disk_file", disk_file);
  1674. if (i < file_set.file_size() - 1) printer.Print("\\\n");
  1675. } else {
  1676. std::cerr << "Unable to identify path for file " << virtual_file
  1677. << std::endl;
  1678. return false;
  1679. }
  1680. }
  1681. return true;
  1682. }
  1683. bool CommandLineInterface::GeneratePluginOutput(
  1684. const std::vector<const FileDescriptor*>& parsed_files,
  1685. const string& plugin_name,
  1686. const string& parameter,
  1687. GeneratorContext* generator_context,
  1688. string* error) {
  1689. CodeGeneratorRequest request;
  1690. CodeGeneratorResponse response;
  1691. string processed_parameter = parameter;
  1692. // Build the request.
  1693. if (!processed_parameter.empty()) {
  1694. request.set_parameter(processed_parameter);
  1695. }
  1696. std::set<const FileDescriptor*> already_seen;
  1697. for (int i = 0; i < parsed_files.size(); i++) {
  1698. request.add_file_to_generate(parsed_files[i]->name());
  1699. GetTransitiveDependencies(parsed_files[i],
  1700. true, // Include json_name for plugins.
  1701. true, // Include source code info.
  1702. &already_seen, request.mutable_proto_file());
  1703. }
  1704. google::protobuf::compiler::Version* version =
  1705. request.mutable_compiler_version();
  1706. version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
  1707. version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
  1708. version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
  1709. version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
  1710. // Invoke the plugin.
  1711. Subprocess subprocess;
  1712. if (plugins_.count(plugin_name) > 0) {
  1713. subprocess.Start(plugins_[plugin_name], Subprocess::EXACT_NAME);
  1714. } else {
  1715. subprocess.Start(plugin_name, Subprocess::SEARCH_PATH);
  1716. }
  1717. string communicate_error;
  1718. if (!subprocess.Communicate(request, &response, &communicate_error)) {
  1719. *error = strings::Substitute("$0: $1", plugin_name, communicate_error);
  1720. return false;
  1721. }
  1722. // Write the files. We do this even if there was a generator error in order
  1723. // to match the behavior of a compiled-in generator.
  1724. std::unique_ptr<io::ZeroCopyOutputStream> current_output;
  1725. for (int i = 0; i < response.file_size(); i++) {
  1726. const CodeGeneratorResponse::File& output_file = response.file(i);
  1727. if (!output_file.insertion_point().empty()) {
  1728. string filename = output_file.name();
  1729. // Open a file for insert.
  1730. // We reset current_output to NULL first so that the old file is closed
  1731. // before the new one is opened.
  1732. current_output.reset();
  1733. current_output.reset(generator_context->OpenForInsert(
  1734. filename, output_file.insertion_point()));
  1735. } else if (!output_file.name().empty()) {
  1736. // Starting a new file. Open it.
  1737. // We reset current_output to NULL first so that the old file is closed
  1738. // before the new one is opened.
  1739. current_output.reset();
  1740. current_output.reset(generator_context->Open(output_file.name()));
  1741. } else if (current_output == NULL) {
  1742. *error = strings::Substitute(
  1743. "$0: First file chunk returned by plugin did not specify a file name.",
  1744. plugin_name);
  1745. return false;
  1746. }
  1747. // Use CodedOutputStream for convenience; otherwise we'd need to provide
  1748. // our own buffer-copying loop.
  1749. io::CodedOutputStream writer(current_output.get());
  1750. writer.WriteString(output_file.content());
  1751. }
  1752. // Check for errors.
  1753. if (!response.error().empty()) {
  1754. // Generator returned an error.
  1755. *error = response.error();
  1756. return false;
  1757. }
  1758. return true;
  1759. }
  1760. bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) {
  1761. // Look up the type.
  1762. const Descriptor* type = pool->FindMessageTypeByName(codec_type_);
  1763. if (type == NULL) {
  1764. std::cerr << "Type not defined: " << codec_type_ << std::endl;
  1765. return false;
  1766. }
  1767. DynamicMessageFactory dynamic_factory(pool);
  1768. std::unique_ptr<Message> message(dynamic_factory.GetPrototype(type)->New());
  1769. if (mode_ == MODE_ENCODE) {
  1770. SetFdToTextMode(STDIN_FILENO);
  1771. SetFdToBinaryMode(STDOUT_FILENO);
  1772. } else {
  1773. SetFdToBinaryMode(STDIN_FILENO);
  1774. SetFdToTextMode(STDOUT_FILENO);
  1775. }
  1776. io::FileInputStream in(STDIN_FILENO);
  1777. io::FileOutputStream out(STDOUT_FILENO);
  1778. if (mode_ == MODE_ENCODE) {
  1779. // Input is text.
  1780. ErrorPrinter error_collector(error_format_);
  1781. TextFormat::Parser parser;
  1782. parser.RecordErrorsTo(&error_collector);
  1783. parser.AllowPartialMessage(true);
  1784. if (!parser.Parse(&in, message.get())) {
  1785. std::cerr << "Failed to parse input." << std::endl;
  1786. return false;
  1787. }
  1788. } else {
  1789. // Input is binary.
  1790. if (!message->ParsePartialFromZeroCopyStream(&in)) {
  1791. std::cerr << "Failed to parse input." << std::endl;
  1792. return false;
  1793. }
  1794. }
  1795. if (!message->IsInitialized()) {
  1796. std::cerr << "warning: Input message is missing required fields: "
  1797. << message->InitializationErrorString() << std::endl;
  1798. }
  1799. if (mode_ == MODE_ENCODE) {
  1800. // Output is binary.
  1801. if (!message->SerializePartialToZeroCopyStream(&out)) {
  1802. std::cerr << "output: I/O error." << std::endl;
  1803. return false;
  1804. }
  1805. } else {
  1806. // Output is text.
  1807. if (!TextFormat::Print(*message, &out)) {
  1808. std::cerr << "output: I/O error." << std::endl;
  1809. return false;
  1810. }
  1811. }
  1812. return true;
  1813. }
  1814. bool CommandLineInterface::WriteDescriptorSet(
  1815. const std::vector<const FileDescriptor*>& parsed_files) {
  1816. FileDescriptorSet file_set;
  1817. std::set<const FileDescriptor*> already_seen;
  1818. if (!imports_in_descriptor_set_) {
  1819. // Since we don't want to output transitive dependencies, but we do want
  1820. // things to be in dependency order, add all dependencies that aren't in
  1821. // parsed_files to already_seen. This will short circuit the recursion
  1822. // in GetTransitiveDependencies.
  1823. std::set<const FileDescriptor*> to_output;
  1824. to_output.insert(parsed_files.begin(), parsed_files.end());
  1825. for (int i = 0; i < parsed_files.size(); i++) {
  1826. const FileDescriptor* file = parsed_files[i];
  1827. for (int i = 0; i < file->dependency_count(); i++) {
  1828. const FileDescriptor* dependency = file->dependency(i);
  1829. // if the dependency isn't in parsed files, mark it as already seen
  1830. if (to_output.find(dependency) == to_output.end()) {
  1831. already_seen.insert(dependency);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. for (int i = 0; i < parsed_files.size(); i++) {
  1837. GetTransitiveDependencies(parsed_files[i],
  1838. true, // Include json_name
  1839. source_info_in_descriptor_set_,
  1840. &already_seen, file_set.mutable_file());
  1841. }
  1842. int fd;
  1843. do {
  1844. fd = open(descriptor_set_out_name_.c_str(),
  1845. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  1846. } while (fd < 0 && errno == EINTR);
  1847. if (fd < 0) {
  1848. perror(descriptor_set_out_name_.c_str());
  1849. return false;
  1850. }
  1851. io::FileOutputStream out(fd);
  1852. if (!file_set.SerializeToZeroCopyStream(&out)) {
  1853. std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno())
  1854. << std::endl;
  1855. out.Close();
  1856. return false;
  1857. }
  1858. if (!out.Close()) {
  1859. std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno())
  1860. << std::endl;
  1861. return false;
  1862. }
  1863. return true;
  1864. }
  1865. void CommandLineInterface::GetTransitiveDependencies(
  1866. const FileDescriptor* file,
  1867. bool include_json_name,
  1868. bool include_source_code_info,
  1869. std::set<const FileDescriptor*>* already_seen,
  1870. RepeatedPtrField<FileDescriptorProto>* output) {
  1871. if (!already_seen->insert(file).second) {
  1872. // Already saw this file. Skip.
  1873. return;
  1874. }
  1875. // Add all dependencies.
  1876. for (int i = 0; i < file->dependency_count(); i++) {
  1877. GetTransitiveDependencies(file->dependency(i),
  1878. include_json_name,
  1879. include_source_code_info,
  1880. already_seen, output);
  1881. }
  1882. // Add this file.
  1883. FileDescriptorProto* new_descriptor = output->Add();
  1884. file->CopyTo(new_descriptor);
  1885. if (include_json_name) {
  1886. file->CopyJsonNameTo(new_descriptor);
  1887. }
  1888. if (include_source_code_info) {
  1889. file->CopySourceCodeInfoTo(new_descriptor);
  1890. }
  1891. }
  1892. namespace {
  1893. // Utility function for PrintFreeFieldNumbers.
  1894. // Stores occupied ranges into the ranges parameter, and next level of sub
  1895. // message types into the nested_messages parameter. The FieldRange is left
  1896. // inclusive, right exclusive. i.e. [a, b).
  1897. //
  1898. // Nested Messages:
  1899. // Note that it only stores the nested message type, iff the nested type is
  1900. // either a direct child of the given descriptor, or the nested type is a
  1901. // decendent of the given descriptor and all the nodes between the
  1902. // nested type and the given descriptor are group types. e.g.
  1903. //
  1904. // message Foo {
  1905. // message Bar {
  1906. // message NestedBar {}
  1907. // }
  1908. // group Baz = 1 {
  1909. // group NestedBazGroup = 2 {
  1910. // message Quz {
  1911. // message NestedQuz {}
  1912. // }
  1913. // }
  1914. // message NestedBaz {}
  1915. // }
  1916. // }
  1917. //
  1918. // In this case, Bar, Quz and NestedBaz will be added into the nested types.
  1919. // Since free field numbers of group types will not be printed, this makes sure
  1920. // the nested message types in groups will not be dropped. The nested_messages
  1921. // parameter will contain the direct children (when groups are ignored in the
  1922. // tree) of the given descriptor for the caller to traverse. The declaration
  1923. // order of the nested messages is also preserved.
  1924. typedef std::pair<int, int> FieldRange;
  1925. void GatherOccupiedFieldRanges(
  1926. const Descriptor* descriptor, std::set<FieldRange>* ranges,
  1927. std::vector<const Descriptor*>* nested_messages) {
  1928. std::set<const Descriptor*> groups;
  1929. for (int i = 0; i < descriptor->field_count(); ++i) {
  1930. const FieldDescriptor* fd = descriptor->field(i);
  1931. ranges->insert(FieldRange(fd->number(), fd->number() + 1));
  1932. if (fd->type() == FieldDescriptor::TYPE_GROUP) {
  1933. groups.insert(fd->message_type());
  1934. }
  1935. }
  1936. for (int i = 0; i < descriptor->extension_range_count(); ++i) {
  1937. ranges->insert(FieldRange(descriptor->extension_range(i)->start,
  1938. descriptor->extension_range(i)->end));
  1939. }
  1940. for (int i = 0; i < descriptor->reserved_range_count(); ++i) {
  1941. ranges->insert(FieldRange(descriptor->reserved_range(i)->start,
  1942. descriptor->reserved_range(i)->end));
  1943. }
  1944. // Handle the nested messages/groups in declaration order to make it
  1945. // post-order strict.
  1946. for (int i = 0; i < descriptor->nested_type_count(); ++i) {
  1947. const Descriptor* nested_desc = descriptor->nested_type(i);
  1948. if (groups.find(nested_desc) != groups.end()) {
  1949. GatherOccupiedFieldRanges(nested_desc, ranges, nested_messages);
  1950. } else {
  1951. nested_messages->push_back(nested_desc);
  1952. }
  1953. }
  1954. }
  1955. // Utility function for PrintFreeFieldNumbers.
  1956. // Actually prints the formatted free field numbers for given message name and
  1957. // occupied ranges.
  1958. void FormatFreeFieldNumbers(const string& name,
  1959. const std::set<FieldRange>& ranges) {
  1960. string output;
  1961. StringAppendF(&output, "%-35s free:", name.c_str());
  1962. int next_free_number = 1;
  1963. for (std::set<FieldRange>::const_iterator i = ranges.begin();
  1964. i != ranges.end(); ++i) {
  1965. // This happens when groups re-use parent field numbers, in which
  1966. // case we skip the FieldRange entirely.
  1967. if (next_free_number >= i->second) continue;
  1968. if (next_free_number < i->first) {
  1969. if (next_free_number + 1 == i->first) {
  1970. // Singleton
  1971. StringAppendF(&output, " %d", next_free_number);
  1972. } else {
  1973. // Range
  1974. StringAppendF(&output, " %d-%d", next_free_number, i->first - 1);
  1975. }
  1976. }
  1977. next_free_number = i->second;
  1978. }
  1979. if (next_free_number <= FieldDescriptor::kMaxNumber) {
  1980. StringAppendF(&output, " %d-INF", next_free_number);
  1981. }
  1982. std::cout << output << std::endl;
  1983. }
  1984. } // namespace
  1985. void CommandLineInterface::PrintFreeFieldNumbers(
  1986. const Descriptor* descriptor) {
  1987. std::set<FieldRange> ranges;
  1988. std::vector<const Descriptor*> nested_messages;
  1989. GatherOccupiedFieldRanges(descriptor, &ranges, &nested_messages);
  1990. for (int i = 0; i < nested_messages.size(); ++i) {
  1991. PrintFreeFieldNumbers(nested_messages[i]);
  1992. }
  1993. FormatFreeFieldNumbers(descriptor->full_name(), ranges);
  1994. }
  1995. } // namespace compiler
  1996. } // namespace protobuf
  1997. } // namespace google