ASResult.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "ASResult.h"
  23. #include "Tuning.h"
  24. #include "Utils.h"
  25. #include <fstream>
  26. namespace ASBindingGenerator
  27. {
  28. ASGeneratedFile_WithRegistrationFunction::ASGeneratedFile_WithRegistrationFunction(const string& outputFilePath, const string& functionName)
  29. {
  30. outputFilePath_ = outputFilePath;
  31. functionName_ = functionName;
  32. }
  33. // ============================================================================
  34. void ASGeneratedFile_Classes::Save()
  35. {
  36. ofstream out(outputFilePath_);
  37. out <<
  38. "// DO NOT EDIT. This file is generated\n"
  39. "\n"
  40. "// We need register all types before registration of any members because members can use any types\n"
  41. "\n"
  42. "#include \"../Precompiled.h\"\n"
  43. "#include \"../AngelScript/APITemplates.h\"\n"
  44. "\n"
  45. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  46. "\n"
  47. "namespace Urho3D\n"
  48. "{\n"
  49. "\n"
  50. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  51. "{\n"
  52. << reg_.str() <<
  53. "}\n"
  54. "\n"
  55. "}\n";
  56. }
  57. // ============================================================================
  58. void ASGeneratedFile_Members_HighPriority::Save()
  59. {
  60. ofstream out(outputFilePath_);
  61. out <<
  62. "// DO NOT EDIT. This file is generated\n"
  63. "\n"
  64. "// We need register default constructors before any members to allow using in Array<type>\n"
  65. "\n"
  66. "#include \"../Precompiled.h\"\n"
  67. "#include \"../AngelScript/APITemplates.h\"\n"
  68. "\n"
  69. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  70. "\n"
  71. "namespace Urho3D\n"
  72. "{\n"
  73. "\n"
  74. "void FakeAddRef(void* ptr);\n"
  75. "void FakeReleaseRef(void* ptr);\n"
  76. "\n"
  77. << glue_.str() <<
  78. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  79. "{\n"
  80. << reg_.str() <<
  81. "}\n"
  82. "\n"
  83. "}\n";
  84. }
  85. // ============================================================================
  86. void ASGeneratedFile_Members::Save()
  87. {
  88. ofstream out(outputFilePath_);
  89. out <<
  90. "// DO NOT EDIT. This file is generated\n"
  91. "\n"
  92. "#include \"../Precompiled.h\"\n"
  93. "#include \"../AngelScript/APITemplates.h\"\n"
  94. "\n"
  95. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  96. "#include \"../AngelScript/Manual.h\"\n"
  97. "\n"
  98. "namespace Urho3D\n"
  99. "{\n"
  100. "\n"
  101. "void FakeAddRef(void* ptr);\n"
  102. "void FakeReleaseRef(void* ptr);\n"
  103. "\n"
  104. << glue_.str() <<
  105. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  106. "{\n"
  107. << reg_.str() <<
  108. "}\n"
  109. "\n"
  110. "}\n";
  111. }
  112. // ============================================================================
  113. void ASGeneratedFile_GlobalVariables::Save()
  114. {
  115. ofstream out(outputFilePath_);
  116. out <<
  117. "// DO NOT EDIT. This file is generated\n"
  118. "\n"
  119. "#include \"../Precompiled.h\"\n"
  120. "#include \"../AngelScript/APITemplates.h\"\n"
  121. "\n"
  122. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  123. "\n"
  124. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  125. "#undef M_PI\n"
  126. "\n"
  127. "namespace Urho3D\n"
  128. "{\n"
  129. "\n"
  130. << glue_.str() <<
  131. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  132. "{\n"
  133. << reg_.str() <<
  134. "}\n"
  135. "\n"
  136. "}\n";
  137. }
  138. // ============================================================================
  139. void ASGeneratedFile_GlobalFunctions::Save()
  140. {
  141. ofstream out(outputFilePath_);
  142. out <<
  143. "// DO NOT EDIT. This file is generated\n"
  144. "\n"
  145. "#include \"../Precompiled.h\"\n"
  146. "#include \"../AngelScript/APITemplates.h\"\n"
  147. "\n"
  148. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  149. "\n"
  150. "namespace Urho3D\n"
  151. "{\n"
  152. "\n"
  153. << glue_.str() <<
  154. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  155. "{\n"
  156. << reg_.str() <<
  157. "}\n"
  158. "\n"
  159. "}\n";
  160. }
  161. // ============================================================================
  162. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  163. {
  164. outputFilePath_ = outputFilePath;
  165. }
  166. void ASGeneratedFile_Templates::Save()
  167. {
  168. ofstream out(outputFilePath_);
  169. out <<
  170. "// DO NOT EDIT. This file is generated\n"
  171. "\n"
  172. "#pragma once\n"
  173. "\n"
  174. "#include \"../Precompiled.h\"\n"
  175. "#include \"../AngelScript/APITemplates.h\"\n"
  176. "\n"
  177. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  178. "\n"
  179. "#include \"../AngelScript/Manual.h\"\n"
  180. "\n"
  181. "namespace Urho3D\n"
  182. "{\n"
  183. "\n"
  184. "void FakeAddRef(void* ptr);\n"
  185. "void FakeReleaseRef(void* ptr);\n"
  186. "\n"
  187. << glue_.str()
  188. << reg_.str() <<
  189. "}\n";
  190. }
  191. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  192. {
  193. if (insideDefine_ == rhs.insideDefine_)
  194. return name_ < rhs.name_;
  195. return insideDefine_ < rhs.insideDefine_;
  196. }
  197. namespace Result
  198. {
  199. vector<ProcessedEnum> enums_;
  200. // Write result to GeneratedEnums.cpp
  201. static void SaveEnums(const string& outputBasePath)
  202. {
  203. sort(enums_.begin(), enums_.end());
  204. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedEnums.cpp");
  205. ofs <<
  206. "// DO NOT EDIT. This file is generated\n"
  207. "\n"
  208. "// We need register all enums before registration of any members because members can use any enums\n"
  209. "\n"
  210. "#include \"../Precompiled.h\"\n"
  211. "#include \"../AngelScript/APITemplates.h\"\n"
  212. "\n"
  213. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  214. "\n"
  215. "namespace Urho3D\n"
  216. "{\n"
  217. "\n";
  218. for (const ProcessedEnum& processedEnum : enums_)
  219. {
  220. if (!processedEnum.glue_.size())
  221. continue;
  222. if (!processedEnum.insideDefine_.empty())
  223. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  224. ofs << "// " << processedEnum.comment_ << "\n";
  225. for (const string& glue : processedEnum.glue_)
  226. ofs << glue << "\n";
  227. if (!processedEnum.insideDefine_.empty())
  228. ofs << "#endif\n";
  229. ofs << "\n";
  230. }
  231. ofs <<
  232. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  233. "{\n";
  234. bool isFirst = true;
  235. string openedDefine;
  236. for (const ProcessedEnum& processedEnum : enums_)
  237. {
  238. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  239. {
  240. ofs << "#endif\n";
  241. openedDefine.clear();
  242. }
  243. if (!isFirst)
  244. ofs << "\n";
  245. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  246. {
  247. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  248. openedDefine = processedEnum.insideDefine_;
  249. }
  250. ofs << " // " << processedEnum.comment_ << "\n";
  251. for (const string& registration : processedEnum.registration_)
  252. ofs << " " << registration << "\n";
  253. isFirst = false;
  254. }
  255. if (!openedDefine.empty())
  256. ofs << "#endif\n";
  257. ofs <<
  258. "}\n"
  259. "\n"
  260. "}\n";
  261. }
  262. // ============================================================================
  263. // List of all required header files
  264. static vector<string> headers_;
  265. // Discarded header files for statistic
  266. static vector<string> ignoredHeaders_;
  267. // Add header to lists if not added yet
  268. void AddHeader(const string& headerFile)
  269. {
  270. if (IsIgnoredHeader(headerFile))
  271. {
  272. if (!CONTAINS(ignoredHeaders_, headerFile))
  273. ignoredHeaders_.push_back(headerFile);
  274. }
  275. else
  276. {
  277. if (!CONTAINS(headers_, headerFile))
  278. headers_.push_back(headerFile);
  279. }
  280. }
  281. // Write result to GeneratedIncludes.h
  282. static void SaveIncludes(const string& outputBasePath)
  283. {
  284. sort(headers_.begin(), headers_.end());
  285. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  286. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedIncludes.h");
  287. ofs <<
  288. "// DO NOT EDIT. This file is generated\n"
  289. "\n"
  290. "#pragma once\n"
  291. "\n";
  292. string openedDefine;
  293. bool isFirst = true;
  294. for (const string& header : headers_)
  295. {
  296. string insideDefine = InsideDefine(header);
  297. if (insideDefine != openedDefine)
  298. {
  299. if (!openedDefine.empty())
  300. {
  301. ofs << "#endif\n";
  302. openedDefine.clear();
  303. }
  304. if (!isFirst) // First include can be guarded. Avoid print \n before it
  305. ofs << "\n";
  306. if (!insideDefine.empty())
  307. {
  308. ofs << "#ifdef " << insideDefine << "\n";
  309. openedDefine = insideDefine;
  310. }
  311. }
  312. ofs << "#include \"" << header << "\"\n";
  313. isFirst = false;
  314. }
  315. if (!openedDefine.empty())
  316. {
  317. ofs << "#endif\n";
  318. openedDefine.clear();
  319. }
  320. if (headers_.size() > 0)
  321. ofs << "\n";
  322. if (ignoredHeaders_.size() > 0)
  323. ofs << "// Ignored headers\n\n";
  324. isFirst = true;
  325. for (const string& header : ignoredHeaders_)
  326. {
  327. string insideDefine = InsideDefine(header);
  328. if (insideDefine != openedDefine)
  329. {
  330. if (!openedDefine.empty())
  331. {
  332. ofs << "//#endif\n";
  333. openedDefine.clear();
  334. }
  335. if (!isFirst) // First include can be guarded. Avoid print \n before it
  336. ofs << "\n";
  337. if (!insideDefine.empty())
  338. {
  339. ofs << "//#ifdef " << insideDefine << "\n";
  340. openedDefine = insideDefine;
  341. }
  342. }
  343. ofs << "//#include \"" << header << "\"\n";
  344. isFirst = false;
  345. }
  346. if (!openedDefine.empty())
  347. ofs << "//#endif\n";
  348. }
  349. }
  350. void SaveResult(const string& outputBasePath)
  351. {
  352. Result::SaveEnums(outputBasePath);
  353. Result::SaveIncludes(outputBasePath);
  354. }
  355. }