ASResult.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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_Members::Save()
  35. {
  36. ofstream out(outputFilePath_);
  37. out <<
  38. "// DO NOT EDIT. This file is generated\n"
  39. "\n"
  40. "#include \"../Precompiled.h\"\n"
  41. "#include \"../AngelScript/APITemplates.h\"\n"
  42. "\n"
  43. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  44. "#include \"../AngelScript/Manual.h\"\n"
  45. "\n"
  46. "namespace Urho3D\n"
  47. "{\n"
  48. "\n"
  49. "void FakeAddRef(void* ptr);\n"
  50. "void FakeReleaseRef(void* ptr);\n"
  51. "\n"
  52. << glue_.str() <<
  53. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  54. "{\n"
  55. << reg_.str() <<
  56. "}\n"
  57. "\n"
  58. "}\n";
  59. }
  60. // ============================================================================
  61. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  62. {
  63. outputFilePath_ = outputFilePath;
  64. }
  65. void ASGeneratedFile_Templates::Save()
  66. {
  67. ofstream out(outputFilePath_);
  68. out <<
  69. "// DO NOT EDIT. This file is generated\n"
  70. "\n"
  71. "#pragma once\n"
  72. "\n"
  73. "#include \"../Precompiled.h\"\n"
  74. "#include \"../AngelScript/APITemplates.h\"\n"
  75. "\n"
  76. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  77. "\n"
  78. "#include \"../AngelScript/Manual.h\"\n"
  79. "\n"
  80. "namespace Urho3D\n"
  81. "{\n"
  82. "\n"
  83. "void FakeAddRef(void* ptr);\n"
  84. "void FakeReleaseRef(void* ptr);\n"
  85. "\n"
  86. << glue_.str()
  87. << reg_.str() <<
  88. "}\n";
  89. }
  90. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  91. {
  92. if (insideDefine_ != rhs.insideDefine_)
  93. return insideDefine_ < rhs.insideDefine_;
  94. return name_ < rhs.name_;
  95. }
  96. bool ProcessedGlobalFunction::operator <(const ProcessedGlobalFunction& rhs) const
  97. {
  98. if (insideDefine_ != rhs.insideDefine_)
  99. return insideDefine_ < rhs.insideDefine_;
  100. if (name_ != rhs.name_)
  101. return name_ < rhs.name_;
  102. // Overloads with the same name may exist
  103. if (comment_ != rhs.comment_)
  104. return comment_ < rhs.comment_;
  105. // Different specializations of the same template and aliases have the same comment
  106. return registration_ < rhs.registration_;
  107. }
  108. bool ProcessedGlobalVariable::operator <(const ProcessedGlobalVariable& rhs) const
  109. {
  110. if (insideDefine_ != rhs.insideDefine_)
  111. return insideDefine_ < rhs.insideDefine_;
  112. return name_ < rhs.name_;
  113. }
  114. bool ProcessedClass::operator <(const ProcessedClass& rhs) const
  115. {
  116. if (insideDefine_ != rhs.insideDefine_)
  117. return insideDefine_ < rhs.insideDefine_;
  118. return name_ < rhs.name_;
  119. }
  120. namespace Result
  121. {
  122. vector<ProcessedEnum> enums_;
  123. // Write result to GeneratedEnums.cpp
  124. static void SaveEnums(const string& outputBasePath)
  125. {
  126. sort(enums_.begin(), enums_.end());
  127. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedEnums.cpp");
  128. ofs <<
  129. "// DO NOT EDIT. This file is generated\n"
  130. "\n"
  131. "// We need register all enums before registration of any functions because functions can use any enums\n"
  132. "\n"
  133. "#include \"../Precompiled.h\"\n"
  134. "#include \"../AngelScript/APITemplates.h\"\n"
  135. "\n"
  136. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  137. "\n"
  138. "namespace Urho3D\n"
  139. "{\n"
  140. "\n";
  141. for (const ProcessedEnum& processedEnum : enums_)
  142. {
  143. if (!processedEnum.glue_.size())
  144. continue;
  145. if (!processedEnum.insideDefine_.empty())
  146. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  147. ofs << "// " << processedEnum.comment_ << "\n";
  148. for (const string& glue : processedEnum.glue_)
  149. ofs << glue << "\n";
  150. if (!processedEnum.insideDefine_.empty())
  151. ofs << "#endif\n";
  152. ofs << "\n";
  153. }
  154. ofs <<
  155. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  156. "{\n";
  157. bool isFirst = true;
  158. string openedDefine;
  159. for (const ProcessedEnum& processedEnum : enums_)
  160. {
  161. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  162. {
  163. ofs << "#endif\n";
  164. openedDefine.clear();
  165. }
  166. if (!isFirst)
  167. ofs << "\n";
  168. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  169. {
  170. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  171. openedDefine = processedEnum.insideDefine_;
  172. }
  173. ofs << " // " << processedEnum.comment_ << "\n";
  174. for (const string& registration : processedEnum.registration_)
  175. ofs << " " << registration << "\n";
  176. isFirst = false;
  177. }
  178. if (!openedDefine.empty())
  179. ofs << "#endif\n";
  180. ofs <<
  181. "}\n"
  182. "\n"
  183. "}\n";
  184. }
  185. // ============================================================================
  186. vector<ProcessedGlobalFunction> globalFunctions_;
  187. // Write result to GlobalFunctions.cpp
  188. static void SaveGlobalFunctions(const string& outputBasePath)
  189. {
  190. sort(globalFunctions_.begin(), globalFunctions_.end());
  191. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalFunctions.cpp");
  192. ofs <<
  193. "// DO NOT EDIT. This file is generated\n"
  194. "\n"
  195. "#include \"../Precompiled.h\"\n"
  196. "#include \"../AngelScript/APITemplates.h\"\n"
  197. "\n"
  198. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  199. "\n"
  200. "namespace Urho3D\n"
  201. "{\n"
  202. "\n";
  203. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  204. {
  205. if (globalFunction.glue_.empty())
  206. continue;
  207. if (!globalFunction.insideDefine_.empty())
  208. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  209. ofs << "// " << globalFunction.comment_ << "\n";
  210. ofs << globalFunction.glue_ << "\n";
  211. if (!globalFunction.insideDefine_.empty())
  212. ofs << "#endif\n";
  213. ofs << "\n";
  214. }
  215. ofs <<
  216. "void ASRegisterGeneratedGlobalFunctions(asIScriptEngine* engine)\n"
  217. "{\n";
  218. bool isFirst = true;
  219. string openedDefine;
  220. string lastComment;
  221. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  222. {
  223. if (globalFunction.insideDefine_ != openedDefine && !openedDefine.empty())
  224. {
  225. ofs << "#endif\n";
  226. openedDefine.clear();
  227. }
  228. if (!isFirst && lastComment != globalFunction.comment_)
  229. ofs << "\n";
  230. if (globalFunction.insideDefine_ != openedDefine && !globalFunction.insideDefine_.empty())
  231. {
  232. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  233. openedDefine = globalFunction.insideDefine_;
  234. }
  235. if (lastComment != globalFunction.comment_)
  236. ofs << " // " << globalFunction.comment_ << "\n";
  237. ofs << " " << globalFunction.registration_ << "\n";
  238. isFirst = false;
  239. lastComment = globalFunction.comment_;
  240. }
  241. if (!openedDefine.empty())
  242. ofs << "#endif\n";
  243. ofs <<
  244. "}\n"
  245. "\n"
  246. "}\n";
  247. }
  248. // ============================================================================
  249. vector<ProcessedGlobalVariable> globalVariables_;
  250. // Write result to GlobalVariables.cpp
  251. static void SaveGlobalVariables(const string& outputBasePath)
  252. {
  253. sort(globalVariables_.begin(), globalVariables_.end());
  254. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalVariables.cpp");
  255. ofs <<
  256. "// DO NOT EDIT. This file is generated\n"
  257. "\n"
  258. "#include \"../Precompiled.h\"\n"
  259. "#include \"../AngelScript/APITemplates.h\"\n"
  260. "\n"
  261. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  262. "\n"
  263. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  264. "#undef M_PI\n"
  265. "\n"
  266. "namespace Urho3D\n"
  267. "{\n"
  268. "\n"
  269. "void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)\n"
  270. "{\n";
  271. bool isFirst = true;
  272. string openedDefine;
  273. string lastComment;
  274. for (const ProcessedGlobalVariable& globalVariable : globalVariables_)
  275. {
  276. if (globalVariable.insideDefine_ != openedDefine && !openedDefine.empty())
  277. {
  278. ofs << "#endif\n";
  279. openedDefine.clear();
  280. }
  281. if (!isFirst && lastComment != globalVariable.comment_)
  282. ofs << "\n";
  283. if (globalVariable.insideDefine_ != openedDefine && !globalVariable.insideDefine_.empty())
  284. {
  285. ofs << "#ifdef " << globalVariable.insideDefine_ << "\n";
  286. openedDefine = globalVariable.insideDefine_;
  287. }
  288. if (lastComment != globalVariable.comment_)
  289. ofs << " // " << globalVariable.comment_ << "\n";
  290. ofs << " " << globalVariable.registration_ << "\n";
  291. isFirst = false;
  292. lastComment = globalVariable.comment_;
  293. }
  294. if (!openedDefine.empty())
  295. ofs << "#endif\n";
  296. ofs <<
  297. "}\n"
  298. "\n"
  299. "}\n";
  300. }
  301. // ============================================================================
  302. vector<ProcessedClass> classes_;
  303. // Write result to GeneratedObjectTypes.cpp
  304. static void SaveObjectTypes(const string& outputBasePath)
  305. {
  306. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedObjectTypes.cpp");
  307. ofs <<
  308. "// DO NOT EDIT. This file is generated\n"
  309. "\n"
  310. "// We need register all types before registration of any functions because functions can use any types\n"
  311. "\n"
  312. "#include \"../Precompiled.h\"\n"
  313. "#include \"../AngelScript/APITemplates.h\"\n"
  314. "\n"
  315. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  316. "\n"
  317. "namespace Urho3D\n"
  318. "{\n"
  319. "\n"
  320. "void ASRegisterGeneratedObjectTypes(asIScriptEngine* engine)\n"
  321. "{\n";
  322. string openedDefine;
  323. bool isFirst = true;
  324. for (const ProcessedClass& processedClass : classes_)
  325. {
  326. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  327. {
  328. ofs << "#endif\n";
  329. openedDefine.clear();
  330. }
  331. if (!isFirst)
  332. ofs << "\n";
  333. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  334. {
  335. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  336. openedDefine = processedClass.insideDefine_;
  337. }
  338. ofs
  339. << " // " << processedClass.comment_ << "\n"
  340. << " " << processedClass.objectTypeRegistration_ << "\n";
  341. isFirst = false;
  342. }
  343. if (!openedDefine.empty())
  344. ofs << "#endif\n";
  345. ofs <<
  346. "}\n"
  347. "\n"
  348. "}\n";
  349. }
  350. // Write result to GeneratedDefaultConstructors.cpp
  351. static void SaveDefaultConstructors(const string& outputBasePath)
  352. {
  353. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedDefaultConstructors.cpp");
  354. ofs <<
  355. "// DO NOT EDIT. This file is generated\n"
  356. "\n"
  357. "// We need register default constructors before any members to allow using in Array<type>\n"
  358. "\n"
  359. "#include \"../Precompiled.h\"\n"
  360. "#include \"../AngelScript/APITemplates.h\"\n"
  361. "\n"
  362. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  363. "\n"
  364. "namespace Urho3D\n"
  365. "{\n";
  366. string openedDefine;
  367. for (const ProcessedClass& processedClass : classes_)
  368. {
  369. if (!processedClass.defaultConstructor_)
  370. continue;
  371. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  372. {
  373. ofs << "\n#endif\n";
  374. openedDefine.clear();
  375. }
  376. ofs << "\n";
  377. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  378. {
  379. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  380. openedDefine = processedClass.insideDefine_;
  381. }
  382. ofs <<
  383. "// " << processedClass.defaultConstructor_->comment_ << "\n" <<
  384. processedClass.defaultConstructor_->glue_;
  385. }
  386. if (!openedDefine.empty())
  387. {
  388. ofs << "\n#endif\n";
  389. openedDefine.clear();
  390. }
  391. ofs <<
  392. "\n"
  393. "void ASRegisterGeneratedDefaultConstructors(asIScriptEngine* engine)\n"
  394. "{\n";
  395. bool isFirst = true;
  396. for (const ProcessedClass& processedClass : classes_)
  397. {
  398. if (!processedClass.defaultConstructor_)
  399. continue;
  400. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  401. {
  402. ofs << "#endif\n";
  403. openedDefine.clear();
  404. }
  405. if (!isFirst)
  406. ofs << "\n";
  407. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  408. {
  409. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  410. openedDefine = processedClass.insideDefine_;
  411. }
  412. ofs <<
  413. " // " << processedClass.defaultConstructor_->comment_ << "\n" <<
  414. " " << processedClass.defaultConstructor_->registration_ << "\n";
  415. isFirst = false;
  416. }
  417. if (!openedDefine.empty())
  418. ofs << "#endif\n";
  419. ofs <<
  420. "}\n"
  421. "\n"
  422. "}\n";
  423. }
  424. // Write result to GeneratedClasses.cpp
  425. static void SaveGeneratedClasses(const string& outputBasePath)
  426. {
  427. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClasses.cpp");
  428. ofs <<
  429. "// DO NOT EDIT. This file is generated\n"
  430. "\n"
  431. "#include \"../Precompiled.h\"\n"
  432. "#include \"../AngelScript/APITemplates.h\"\n"
  433. "\n"
  434. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  435. "#include \"../AngelScript/Manual.h\"\n"
  436. "\n"
  437. "namespace Urho3D\n"
  438. "{\n"
  439. "\n"
  440. "void FakeAddRef(void* ptr);\n"
  441. "void FakeReleaseRef(void* ptr);\n";
  442. string openedDefine;
  443. for (const ProcessedClass& processedClass : classes_)
  444. {
  445. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  446. {
  447. ofs << "\n#endif // def " << openedDefine << "\n";
  448. openedDefine.clear();
  449. }
  450. ofs << "\n";
  451. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  452. {
  453. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  454. openedDefine = processedClass.insideDefine_;
  455. }
  456. if (processedClass.destructor_)
  457. {
  458. ofs <<
  459. "// " << processedClass.destructor_->comment_ << "\n"
  460. << processedClass.destructor_->glue_ <<
  461. "\n";
  462. }
  463. ofs <<
  464. "// " << processedClass.comment_ << "\n"
  465. "static void Register_" << processedClass.name_ << "(asIScriptEngine* engine)\n"
  466. "{\n";
  467. /*
  468. for (string nonDefaultConstructor : processedClass.nonDefaultConstructors_)
  469. ofs << " // " << nonDefaultConstructor << "\n";
  470. */
  471. if (processedClass.destructor_)
  472. {
  473. ofs <<
  474. " // " << processedClass.destructor_->comment_ << "\n"
  475. " " << processedClass.destructor_->registration_ << "\n";
  476. }
  477. ofs << "}\n";
  478. }
  479. if (!openedDefine.empty())
  480. {
  481. ofs << "\n#endif // def " << openedDefine << "\n";
  482. openedDefine.clear();
  483. }
  484. ofs <<
  485. "\n"
  486. "void ASRegisterGeneratedClasses(asIScriptEngine* engine)\n"
  487. "{\n";
  488. bool isFirst = true;
  489. for (const ProcessedClass& processedClass : classes_)
  490. {
  491. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  492. {
  493. ofs << "#endif\n";
  494. openedDefine.clear();
  495. }
  496. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  497. {
  498. if (!isFirst)
  499. ofs << "\n";
  500. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  501. openedDefine = processedClass.insideDefine_;
  502. }
  503. ofs << " Register_" << processedClass.name_ << "(engine);\n";
  504. isFirst = false;
  505. }
  506. if (!openedDefine.empty())
  507. ofs << "#endif\n";
  508. ofs <<
  509. "}\n"
  510. "\n"
  511. "}\n";
  512. }
  513. static void SaveClasses(const string& outputBasePath)
  514. {
  515. sort(classes_.begin(), classes_.end());
  516. SaveObjectTypes(outputBasePath);
  517. SaveDefaultConstructors(outputBasePath);
  518. SaveGeneratedClasses(outputBasePath);
  519. }
  520. // ============================================================================
  521. // List of all required header files
  522. static vector<string> headers_;
  523. // Discarded header files for statistic
  524. static vector<string> ignoredHeaders_;
  525. // Add header to lists if not added yet
  526. void AddHeader(const string& headerFile)
  527. {
  528. if (IsIgnoredHeader(headerFile))
  529. {
  530. if (!CONTAINS(ignoredHeaders_, headerFile))
  531. ignoredHeaders_.push_back(headerFile);
  532. }
  533. else
  534. {
  535. if (!CONTAINS(headers_, headerFile))
  536. headers_.push_back(headerFile);
  537. }
  538. }
  539. // Write result to GeneratedIncludes.h
  540. static void SaveIncludes(const string& outputBasePath)
  541. {
  542. sort(headers_.begin(), headers_.end());
  543. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  544. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedIncludes.h");
  545. ofs <<
  546. "// DO NOT EDIT. This file is generated\n"
  547. "\n"
  548. "#pragma once\n"
  549. "\n";
  550. string openedDefine;
  551. bool isFirst = true;
  552. for (const string& header : headers_)
  553. {
  554. string insideDefine = InsideDefine(header);
  555. if (insideDefine != openedDefine)
  556. {
  557. if (!openedDefine.empty())
  558. {
  559. ofs << "#endif\n";
  560. openedDefine.clear();
  561. }
  562. if (!isFirst) // First include can be guarded. Avoid print \n before it
  563. ofs << "\n";
  564. if (!insideDefine.empty())
  565. {
  566. ofs << "#ifdef " << insideDefine << "\n";
  567. openedDefine = insideDefine;
  568. }
  569. }
  570. ofs << "#include \"" << header << "\"\n";
  571. isFirst = false;
  572. }
  573. if (!openedDefine.empty())
  574. {
  575. ofs << "#endif\n";
  576. openedDefine.clear();
  577. }
  578. if (headers_.size() > 0)
  579. ofs << "\n";
  580. if (ignoredHeaders_.size() > 0)
  581. ofs << "// Ignored headers\n\n";
  582. isFirst = true;
  583. for (const string& header : ignoredHeaders_)
  584. {
  585. string insideDefine = InsideDefine(header);
  586. if (insideDefine != openedDefine)
  587. {
  588. if (!openedDefine.empty())
  589. {
  590. ofs << "//#endif\n";
  591. openedDefine.clear();
  592. }
  593. if (!isFirst) // First include can be guarded. Avoid print \n before it
  594. ofs << "\n";
  595. if (!insideDefine.empty())
  596. {
  597. ofs << "//#ifdef " << insideDefine << "\n";
  598. openedDefine = insideDefine;
  599. }
  600. }
  601. ofs << "//#include \"" << header << "\"\n";
  602. isFirst = false;
  603. }
  604. if (!openedDefine.empty())
  605. ofs << "//#endif\n";
  606. }
  607. }
  608. void SaveResult(const string& outputBasePath)
  609. {
  610. Result::SaveEnums(outputBasePath);
  611. Result::SaveGlobalFunctions(outputBasePath);
  612. Result::SaveGlobalVariables(outputBasePath);
  613. Result::SaveClasses(outputBasePath);
  614. Result::SaveIncludes(outputBasePath);
  615. }
  616. }