command_line_interface.cc 76 KB

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