cmVTKMakeInstantiatorCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmVTKMakeInstantiatorCommand.h"
  14. #include "cmCacheManager.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmSourceFile.h"
  17. bool
  18. cmVTKMakeInstantiatorCommand
  19. ::InitialPass(std::vector<std::string> const& argsIn)
  20. {
  21. if(argsIn.size() < 3)
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. std::vector<std::string> args;
  27. m_Makefile->ExpandSourceListArguments(argsIn, args, 2);
  28. std::string sourceListValue;
  29. m_ClassName = args[0];
  30. std::vector<cmStdString> inSourceLists;
  31. m_ExportMacro = "-";
  32. bool includesMode = false;
  33. bool oldVersion = true;
  34. // Find the path of the files to be generated.
  35. std::string filePath = m_Makefile->GetCurrentOutputDirectory();
  36. std::string headerPath = filePath;
  37. // Check whether to use the old or new form.
  38. if(m_Makefile->GetDefinition("VTK_USE_INSTANTIATOR_NEW"))
  39. {
  40. oldVersion = false;
  41. }
  42. for(unsigned int i=2;i < args.size();++i)
  43. {
  44. if(args[i] == "HEADER_LOCATION")
  45. {
  46. includesMode = false;
  47. if(++i < args.size())
  48. {
  49. headerPath = args[i];
  50. }
  51. else
  52. {
  53. this->SetError("HEADER_LOCATION option used without value.");
  54. return false;
  55. }
  56. }
  57. else if(args[i] == "EXPORT_MACRO")
  58. {
  59. includesMode = false;
  60. if(++i < args.size())
  61. {
  62. m_ExportMacro = args[i];
  63. }
  64. else
  65. {
  66. this->SetError("EXPORT_MACRO option used without value.");
  67. return false;
  68. }
  69. }
  70. else if(args[i] == "INCLUDES")
  71. {
  72. includesMode = true;
  73. }
  74. // If not an option, it must be another input source list name or
  75. // an include file.
  76. else
  77. {
  78. if(!includesMode)
  79. {
  80. inSourceLists.push_back(args[i]);
  81. }
  82. else
  83. {
  84. m_Includes.push_back(args[i]);
  85. }
  86. }
  87. }
  88. if(m_ExportMacro == "-")
  89. {
  90. this->SetError("No EXPORT_MACRO option given.");
  91. return false;
  92. }
  93. for(std::vector<cmStdString>::const_iterator s = inSourceLists.begin();
  94. s != inSourceLists.end(); ++s)
  95. {
  96. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*s);
  97. cmSourceFile *sf = m_Makefile->GetSource(s->c_str());
  98. // Wrap-excluded and abstract classes do not have a New() method.
  99. // vtkIndent and vtkTimeStamp are special cases and are not
  100. // vtkObject subclasses.
  101. if(
  102. (!sf || (!sf->GetPropertyAsBool("WRAP_EXCLUDE") &&
  103. !sf->GetPropertyAsBool("ABSTRACT"))) &&
  104. ((srcName != "vtkIndent") && (srcName != "vtkTimeStamp")))
  105. {
  106. m_Classes.push_back(srcName);
  107. }
  108. }
  109. // Generate the header with the class declaration.
  110. {
  111. std::string fileName = m_ClassName + ".h";
  112. std::string fullName = headerPath+"/"+fileName;
  113. // Generate the output file with copy-if-different.
  114. cmGeneratedFileStream fout(fullName.c_str());
  115. fout.SetCopyIfDifferent(true);
  116. // Actually generate the code in the file.
  117. if(!oldVersion)
  118. {
  119. this->GenerateHeaderFile(fout);
  120. }
  121. else
  122. {
  123. this->OldGenerateHeaderFile(fout);
  124. }
  125. }
  126. // Generate the implementation file.
  127. {
  128. std::string fileName = m_ClassName + ".cxx";
  129. std::string fullName = filePath+"/"+fileName;
  130. // Generate the output file with copy-if-different.
  131. {
  132. cmGeneratedFileStream fout(fullName.c_str());
  133. fout.SetCopyIfDifferent(true);
  134. // Actually generate the code in the file.
  135. if(!oldVersion)
  136. {
  137. this->GenerateImplementationFile(fout);
  138. }
  139. else
  140. {
  141. this->OldGenerateImplementationFile(fout);
  142. }
  143. }
  144. // Add the generated source file into the source list.
  145. cmSourceFile file;
  146. file.SetProperty("WRAP_EXCLUDE","1");
  147. file.SetProperty("ABSTRACT","0");
  148. file.SetName(fileName.c_str(), filePath.c_str(),
  149. m_Makefile->GetSourceExtensions(),
  150. m_Makefile->GetHeaderExtensions());
  151. m_Makefile->AddSource(file);
  152. sourceListValue += file.GetSourceName() + ".cxx";
  153. }
  154. if(oldVersion)
  155. {
  156. int groupSize = 10;
  157. size_t numClasses = m_Classes.size();
  158. size_t numFullBlocks = numClasses / groupSize;
  159. size_t lastBlockSize = numClasses % groupSize;
  160. size_t numBlocks = numFullBlocks + ((lastBlockSize>0)? 1:0);
  161. // Generate the files with the ::New() calls to each class. These
  162. // are done in groups to keep the translation unit size smaller.
  163. for(unsigned int block=0; block < numBlocks;++block)
  164. {
  165. std::string fileName = this->OldGenerateCreationFileName(block);
  166. std::string fullName = filePath+"/"+fileName;
  167. // Generate the output file with copy-if-different.
  168. {
  169. cmGeneratedFileStream fout(fullName.c_str());
  170. fout.SetCopyIfDifferent(true);
  171. size_t thisBlockSize =
  172. (block < numFullBlocks)? groupSize:lastBlockSize;
  173. // Actually generate the code in the file.
  174. this->OldGenerateCreationFile(fout,
  175. block*groupSize,
  176. static_cast<int>(thisBlockSize));
  177. }
  178. // Add the generated source file into the source list.
  179. cmSourceFile file;
  180. file.SetProperty("WRAP_EXCLUDE","1");
  181. file.SetProperty("ABSTRACT","0");
  182. file.SetName(fileName.c_str(), filePath.c_str(),
  183. m_Makefile->GetSourceExtensions(),
  184. m_Makefile->GetHeaderExtensions());
  185. m_Makefile->AddSource(file);
  186. sourceListValue += ";";
  187. sourceListValue += file.GetSourceName() + ".cxx";
  188. }
  189. }
  190. m_Makefile->AddDefinition(args[1].c_str(), sourceListValue.c_str());
  191. return true;
  192. }
  193. // Generates the class header file with the definition of the class
  194. // and its initializer class.
  195. void
  196. cmVTKMakeInstantiatorCommand
  197. ::GenerateHeaderFile(std::ostream& os)
  198. {
  199. os <<
  200. "#ifndef __" << m_ClassName.c_str() << "_h\n"
  201. "#define __" << m_ClassName.c_str() << "_h\n"
  202. "\n"
  203. "#include \"vtkInstantiator.h\"\n";
  204. for(unsigned int i=0;i < m_Includes.size();++i)
  205. {
  206. os << "#include \"" << m_Includes[i].c_str() << "\"\n";
  207. }
  208. // Write the instantiator class definition.
  209. os <<
  210. "\n"
  211. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "\n"
  212. "{\n"
  213. "public:\n"
  214. " " << m_ClassName.c_str() << "();\n"
  215. " ~" << m_ClassName.c_str() << "();\n"
  216. "private:\n"
  217. " static void ClassInitialize();\n"
  218. " static void ClassFinalize();\n"
  219. " static unsigned int Count;\n"
  220. "};\n"
  221. "\n";
  222. // Write the initialization instance to make sure the creation
  223. // functions get registered when this generated header is included.
  224. os <<
  225. "static "
  226. << m_ClassName.c_str() << " "
  227. << m_ClassName.c_str() << "Initializer;\n"
  228. "\n"
  229. "#endif\n";
  230. }
  231. // Generates the file with the implementation of the class. All
  232. // methods except the actual object creation functions are generated
  233. // here.
  234. void
  235. cmVTKMakeInstantiatorCommand
  236. ::GenerateImplementationFile(std::ostream& os)
  237. {
  238. // Include the instantiator class header.
  239. os <<
  240. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  241. "\n";
  242. // Write the extern declarations for all the creation functions.
  243. for(unsigned int i=0;i < m_Classes.size();++i)
  244. {
  245. os << "extern vtkObject* vtkInstantiator" << m_Classes[i].c_str() << "New();\n";
  246. }
  247. // Write the ClassInitialize method to register all the creation functions.
  248. os <<
  249. "\n"
  250. "void " << m_ClassName.c_str() << "::ClassInitialize()\n"
  251. "{\n";
  252. for(unsigned int i=0;i < m_Classes.size();++i)
  253. {
  254. os << " vtkInstantiator::RegisterInstantiator(\""
  255. << m_Classes[i].c_str() << "\", vtkInstantiator"
  256. << m_Classes[i].c_str() << "New);\n";
  257. }
  258. // Write the ClassFinalize method to unregister all the creation functions.
  259. os <<
  260. "}\n"
  261. "\n"
  262. "void " << m_ClassName.c_str() << "::ClassFinalize()\n"
  263. "{\n";
  264. for(unsigned int i=0;i < m_Classes.size();++i)
  265. {
  266. os << " vtkInstantiator::UnRegisterInstantiator(\""
  267. << m_Classes[i].c_str() << "\", vtkInstantiator"
  268. << m_Classes[i].c_str() << "New);\n";
  269. }
  270. // Write the constructor and destructor of the initializer class to
  271. // call the ClassInitialize and ClassFinalize methods at the right
  272. // time.
  273. os <<
  274. "}\n"
  275. "\n" <<
  276. m_ClassName.c_str() << "::" << m_ClassName.c_str() << "()\n"
  277. "{\n"
  278. " if(++" << m_ClassName.c_str() << "::Count == 1)\n"
  279. " { " << m_ClassName.c_str() << "::ClassInitialize(); }\n"
  280. "}\n"
  281. "\n" <<
  282. m_ClassName.c_str() << "::~" << m_ClassName.c_str() << "()\n"
  283. "{\n"
  284. " if(--" << m_ClassName.c_str() << "::Count == 0)\n"
  285. " { " << m_ClassName.c_str() << "::ClassFinalize(); }\n"
  286. "}\n"
  287. "\n"
  288. "// Number of translation units that include this class's header.\n"
  289. "// Purposely not initialized. Default is static initialization to 0.\n"
  290. "unsigned int " << m_ClassName.c_str() << "::Count;\n";
  291. }
  292. std::string
  293. cmVTKMakeInstantiatorCommand::OldGenerateCreationFileName(unsigned int block)
  294. {
  295. cmOStringStream nameStr;
  296. nameStr << m_ClassName.c_str() << block << ".cxx";
  297. std::string result = nameStr.str();
  298. return result;
  299. }
  300. // Generates a file that includes the headers of the classes it knows
  301. // how to create and provides functions which create the classes with
  302. // the New() method.
  303. void
  304. cmVTKMakeInstantiatorCommand
  305. ::OldGenerateCreationFile(std::ostream& os, unsigned int groupStart,
  306. unsigned int groupSize)
  307. {
  308. // Need to include header of generated class.
  309. os <<
  310. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  311. "\n";
  312. // Include class files.
  313. for(unsigned int i=0;i < groupSize;++i)
  314. {
  315. os << "#include \"" << m_Classes[groupStart+i].c_str() << ".h\"\n";
  316. }
  317. os <<
  318. "\n";
  319. // Write the create function implementations.
  320. for(unsigned int i=0;i < groupSize;++i)
  321. {
  322. os << "vtkObject* " << m_ClassName.c_str() << "::Create_"
  323. << m_Classes[groupStart+i].c_str() << "() { return "
  324. << m_Classes[groupStart+i].c_str() << "::New(); }\n";
  325. }
  326. }
  327. // Generates the class header file with the definition of the class
  328. // and its initializer class.
  329. void
  330. cmVTKMakeInstantiatorCommand
  331. ::OldGenerateHeaderFile(std::ostream& os)
  332. {
  333. os <<
  334. "#ifndef __" << m_ClassName.c_str() << "_h\n"
  335. "#define __" << m_ClassName.c_str() << "_h\n"
  336. "\n"
  337. "#include \"vtkInstantiator.h\"\n";
  338. for(unsigned int i=0;i < m_Includes.size();++i)
  339. {
  340. os << "#include \"" << m_Includes[i].c_str() << "\"\n";
  341. }
  342. os <<
  343. "\n"
  344. "class " << m_ClassName.c_str() << "Initialize;\n"
  345. "\n"
  346. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "\n"
  347. "{\n"
  348. " friend class " << m_ClassName.c_str() << "Initialize;\n"
  349. "\n"
  350. " static void ClassInitialize();\n"
  351. " static void ClassFinalize();\n"
  352. "\n";
  353. for(unsigned int i=0;i < m_Classes.size();++i)
  354. {
  355. os << " static vtkObject* Create_" << m_Classes[i].c_str() << "();\n";
  356. }
  357. // Write the initializer class to make sure the creation functions
  358. // get registered when this generated header is included.
  359. os <<
  360. "};\n"
  361. "\n"
  362. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "Initialize\n"
  363. "{\n"
  364. "public:\n"
  365. " " << m_ClassName.c_str() << "Initialize();\n"
  366. " ~" << m_ClassName.c_str() << "Initialize();\n"
  367. "private:\n"
  368. " static unsigned int Count;\n"
  369. "};\n"
  370. "\n"
  371. "static " << m_ClassName.c_str() << "Initialize " << m_ClassName.c_str() << "Initializer;\n"
  372. "\n"
  373. "#endif\n";
  374. }
  375. // Generates the file with the implementation of the class. All
  376. // methods except the actual object creation functions are generated
  377. // here.
  378. void
  379. cmVTKMakeInstantiatorCommand
  380. ::OldGenerateImplementationFile(std::ostream& os)
  381. {
  382. // Write the ClassInitialize method to register all the creation functions.
  383. os <<
  384. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  385. "\n"
  386. "void " << m_ClassName.c_str() << "::ClassInitialize()\n"
  387. "{\n";
  388. for(unsigned int i=0;i < m_Classes.size();++i)
  389. {
  390. os << " vtkInstantiator::RegisterInstantiator(\""
  391. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  392. << m_Classes[i].c_str() << ");\n";
  393. }
  394. // Write the ClassFinalize method to unregister all the creation functions.
  395. os <<
  396. "}\n"
  397. "\n"
  398. "void " << m_ClassName.c_str() << "::ClassFinalize()\n"
  399. "{\n";
  400. for(unsigned int i=0;i < m_Classes.size();++i)
  401. {
  402. os << " vtkInstantiator::UnRegisterInstantiator(\""
  403. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  404. << m_Classes[i].c_str() << ");\n";
  405. }
  406. // Write the constructor and destructor of the initializer class to
  407. // call the ClassInitialize and ClassFinalize methods at the right
  408. // time.
  409. os <<
  410. "}\n"
  411. "\n" <<
  412. m_ClassName.c_str() << "Initialize::" << m_ClassName.c_str() << "Initialize()\n"
  413. "{\n"
  414. " if(++" << m_ClassName.c_str() << "Initialize::Count == 1)\n"
  415. " { " << m_ClassName.c_str() << "::ClassInitialize(); }\n"
  416. "}\n"
  417. "\n" <<
  418. m_ClassName.c_str() << "Initialize::~" << m_ClassName.c_str() << "Initialize()\n"
  419. "{\n"
  420. " if(--" << m_ClassName.c_str() << "Initialize::Count == 0)\n"
  421. " { " << m_ClassName.c_str() << "::ClassFinalize(); }\n"
  422. "}\n"
  423. "\n"
  424. "// Number of translation units that include this class's header.\n"
  425. "// Purposely not initialized. Default is static initialization to 0.\n"
  426. "unsigned int " << m_ClassName.c_str() << "Initialize::Count;\n";
  427. }