command_line_interface.cc 87 KB

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