main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_option.h"
  17. #include "code_generator.h"
  18. void version()
  19. {
  20. cout << "v" << GENERATOR_VERSION << endl;
  21. }
  22. void usage()
  23. {
  24. cout << "Version : " << GENERATOR_VERSION << endl;
  25. cout << "Usage : " << EXECUTE_FILENAME << " [OPTION] " << "*." << TC_Common::lower(IDL_TYPE) << " file" << endl;
  26. cout << " --version print " << EXECUTE_FILENAME << " version" << endl;
  27. cout << " --rpc-path=[DIRECTORY] specify the path of rpc module." << endl;
  28. cout << " --stream-path=[DIRECTORY] specify the path of stream module." << endl;
  29. cout << " --allow-reserved-namespace allow you to use reserved word as a namespace." << endl;
  30. cout << " --dir=DIRECTORY generate source file to DIRECTORY." << endl;
  31. cout << " --relative use current path." << endl;
  32. cout << " --base=DIRECTORY where to search " << IDL_TYPE << " files." << endl;
  33. cout << " --r generate source all " << IDL_TYPE << " files." << endl;
  34. cout << " --r-minimal minimize the dependent members." << endl;
  35. cout << " --r-reserved list of names(split by \",\") that should be keeped." << endl;
  36. cout << " --client just for client side source file." << endl;
  37. cout << " --server just for server side source file." << endl;
  38. cout << " --ts generate typescript file." << endl;
  39. cout << " --dts generate d.ts file." << endl;
  40. cout << " --long-type=[number|string|bigint] use <Number|String|BigInt> represent <long> type, default is <Number>." << endl;
  41. cout << " --string-binary-encoding get string raw bytes <BinBuffer>." << endl;
  42. cout << " --enum-reverse-mappings reverse mapping from enum values to enum names." << endl;
  43. cout << " --optimize=[0|s] support \"s\" to reduce code size, default is 0." << endl;
  44. cout << endl;
  45. cout << EXECUTE_FILENAME << " support type: boolean char short int long float double list map" << endl;
  46. exit(0);
  47. }
  48. void check(vector<string> &vFiles)
  49. {
  50. for(size_t i = 0; i < vFiles.size(); i++)
  51. {
  52. string ext = TC_File::extractFileExt(vFiles[i]);
  53. if(ext == TC_Common::lower(IDL_TYPE))
  54. {
  55. if(!TC_File::isFileExist(vFiles[i]))
  56. {
  57. cerr << "file '" << vFiles[i] << "' not exists" << endl;
  58. usage();
  59. exit(0);
  60. }
  61. }
  62. else
  63. {
  64. cerr << "only support "<< TC_Common::lower(IDL_TYPE) << " file." << endl;
  65. usage();
  66. exit(0);
  67. }
  68. }
  69. }
  70. int main(int argc, char* argv[])
  71. {
  72. if(argc < 2)
  73. {
  74. usage();
  75. }
  76. try
  77. {
  78. TC_Option option;
  79. option.decode(argc, argv);
  80. vector<string> vFiles = option.getSingle();
  81. check(vFiles);
  82. if (option.hasParam("version"))
  83. {
  84. version();
  85. return 0;
  86. }
  87. if(option.hasParam("help"))
  88. {
  89. usage();
  90. return 0;
  91. }
  92. if(option.hasParam("base"))
  93. {
  94. if (::chdir(option.getValue("base").c_str()) != 0) {
  95. return -1;
  96. }
  97. }
  98. #define ALLOW_USE_RESERVED_NAMESPACE_V(name, keeped) \
  99. g_parse->set##name(keeped);
  100. #define ALLOW_USE_RESERVED_NAMESPACE_BASE(name, keeped) \
  101. ALLOW_USE_RESERVED_NAMESPACE_V(name, keeped)
  102. #define ALLOW_USE_RESERVED_NAMESPACE(keeped) \
  103. ALLOW_USE_RESERVED_NAMESPACE_BASE(IDL_NAMESPACE, keeped)
  104. ALLOW_USE_RESERVED_NAMESPACE(option.hasParam("allow-reserved-namespace"));
  105. #undef ALLOW_USE_RESERVED_NAMESPACE
  106. #undef ALLOW_USE_RESERVED_NAMESPACE_BASE
  107. #undef ALLOW_USE_RESERVED_NAMESPACE_V
  108. g_parse->setTars(option.hasParam("with-tars"));
  109. g_parse->setUseCurrentPath(option.hasParam("relative"));
  110. CodeGenerator generator;
  111. generator.setRpcPath(option.hasParam("rpc-path")?option.getValue("rpc-path"):RPC_MODULE_PATH);
  112. generator.setStreamPath(option.hasParam("stream-path")?option.getValue("stream-path"):STREAM_MODULE_PATH);
  113. generator.setEnableClient(option.hasParam("client"));
  114. generator.setEnableServer(option.hasParam("server"));
  115. generator.setTargetPath(option.hasParam("dir")?option.getValue("dir"):"./");
  116. generator.setUseSpecialPath(option.hasParam("relative"));
  117. generator.setStringBinaryEncoding(option.hasParam("string-binary-encoding"));
  118. generator.setEnumReverseMappings(option.hasParam("enum-reverse-mappings"));
  119. generator.setEnableTS(option.hasParam("ts"));
  120. generator.setEnableDTS(option.hasParam("dts"));
  121. if (option.hasParam("long-type"))
  122. {
  123. string longType = TC_Common::lower(option.getValue("long-type"));
  124. if (longType == "string")
  125. {
  126. generator.setLongType(CodeGenerator::String);
  127. }
  128. else if (longType == "bigint")
  129. {
  130. generator.setLongType(CodeGenerator::BigInt);
  131. }
  132. }
  133. if (option.hasParam("optimize"))
  134. {
  135. string level = TC_Common::lower(option.getValue("optimize"));
  136. if (level == "s")
  137. {
  138. generator.setOptimize(CodeGenerator::Os);
  139. }
  140. else
  141. {
  142. generator.setOptimize(CodeGenerator::O0);
  143. }
  144. }
  145. bool _bRecursive = option.hasParam("r");
  146. bool _bMinimalMembers = option.hasParam("r-minimal");
  147. if (option.hasParam("r-reserved"))
  148. {
  149. set<string> vMembers;
  150. vector<string> vReserved = TC_Common::sepstr<string>(option.getValue("r-reserved"), ",");
  151. for (size_t i = 0; i < vReserved.size(); i++)
  152. {
  153. if (vReserved[i].empty())
  154. {
  155. continue;
  156. }
  157. if (TC_Common::sepstr<string>(vReserved[i], "::").size() == 2)
  158. {
  159. vMembers.insert(TC_Common::trim(vReserved[i]));
  160. } else {
  161. cerr << "reserved member must be match <Module>::<Member> pattern" << endl;
  162. return 0;
  163. }
  164. }
  165. if (vMembers.size() > 0)
  166. {
  167. _bMinimalMembers = true;
  168. generator.setDependent(vMembers);
  169. }
  170. }
  171. if (!_bRecursive && _bMinimalMembers)
  172. {
  173. cerr << "Missing --r flag" << endl;
  174. return 0;
  175. }
  176. generator.setRecursive(_bRecursive);
  177. generator.setMinimalMembers(_bMinimalMembers);
  178. for (size_t i = 0; i < vFiles.size(); i++)
  179. {
  180. generator.createFile(vFiles[i], true);
  181. }
  182. }
  183. catch(exception& e)
  184. {
  185. cerr<<e.what()<<endl;
  186. }
  187. return 0;
  188. }