cmVTKMakeInstantiatorCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. // Actually generate the code in the file.
  116. if(!oldVersion)
  117. {
  118. this->GenerateHeaderFile(fout.GetStream());
  119. }
  120. else
  121. {
  122. this->OldGenerateHeaderFile(fout.GetStream());
  123. }
  124. }
  125. // Generate the implementation file.
  126. {
  127. std::string fileName = m_ClassName + ".cxx";
  128. std::string fullName = filePath+"/"+fileName;
  129. // Generate the output file with copy-if-different.
  130. {
  131. cmGeneratedFileStream fout(fullName.c_str());
  132. // Actually generate the code in the file.
  133. if(!oldVersion)
  134. {
  135. this->GenerateImplementationFile(fout.GetStream());
  136. }
  137. else
  138. {
  139. this->OldGenerateImplementationFile(fout.GetStream());
  140. }
  141. }
  142. // Add the generated source file into the source list.
  143. cmSourceFile file;
  144. file.SetProperty("WRAP_EXCLUDE","1");
  145. file.SetProperty("ABSTRACT","0");
  146. file.SetName(fileName.c_str(), filePath.c_str(),
  147. m_Makefile->GetSourceExtensions(),
  148. m_Makefile->GetHeaderExtensions());
  149. m_Makefile->AddSource(file);
  150. sourceListValue += file.GetSourceName() + ".cxx";
  151. }
  152. if(oldVersion)
  153. {
  154. int groupSize = 10;
  155. size_t numClasses = m_Classes.size();
  156. size_t numFullBlocks = numClasses / groupSize;
  157. size_t lastBlockSize = numClasses % groupSize;
  158. size_t numBlocks = numFullBlocks + ((lastBlockSize>0)? 1:0);
  159. // Generate the files with the ::New() calls to each class. These
  160. // are done in groups to keep the translation unit size smaller.
  161. for(unsigned int block=0; block < numBlocks;++block)
  162. {
  163. std::string fileName = this->OldGenerateCreationFileName(block);
  164. std::string fullName = filePath+"/"+fileName;
  165. // Generate the output file with copy-if-different.
  166. {
  167. cmGeneratedFileStream fout(fullName.c_str());
  168. size_t thisBlockSize =
  169. (block < numFullBlocks)? groupSize:lastBlockSize;
  170. // Actually generate the code in the file.
  171. this->OldGenerateCreationFile(fout.GetStream(),
  172. block*groupSize,
  173. static_cast<int>(thisBlockSize));
  174. }
  175. // Add the generated source file into the source list.
  176. cmSourceFile file;
  177. file.SetProperty("WRAP_EXCLUDE","1");
  178. file.SetProperty("ABSTRACT","0");
  179. file.SetName(fileName.c_str(), filePath.c_str(),
  180. m_Makefile->GetSourceExtensions(),
  181. m_Makefile->GetHeaderExtensions());
  182. m_Makefile->AddSource(file);
  183. sourceListValue += ";";
  184. sourceListValue += file.GetSourceName() + ".cxx";
  185. }
  186. }
  187. m_Makefile->AddDefinition(args[1].c_str(), sourceListValue.c_str());
  188. return true;
  189. }
  190. // Generates the class header file with the definition of the class
  191. // and its initializer class.
  192. void
  193. cmVTKMakeInstantiatorCommand
  194. ::GenerateHeaderFile(std::ostream& os)
  195. {
  196. os <<
  197. "#ifndef __" << m_ClassName.c_str() << "_h\n"
  198. "#define __" << m_ClassName.c_str() << "_h\n"
  199. "\n"
  200. "#include \"vtkInstantiator.h\"\n";
  201. for(unsigned int i=0;i < m_Includes.size();++i)
  202. {
  203. os << "#include \"" << m_Includes[i].c_str() << "\"\n";
  204. }
  205. // Write the instantiator class definition.
  206. os <<
  207. "\n"
  208. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "\n"
  209. "{\n"
  210. "public:\n"
  211. " " << m_ClassName.c_str() << "();\n"
  212. " ~" << m_ClassName.c_str() << "();\n"
  213. "private:\n"
  214. " static void ClassInitialize();\n"
  215. " static void ClassFinalize();\n"
  216. " static unsigned int Count;\n"
  217. "};\n"
  218. "\n";
  219. // Write the initialization instance to make sure the creation
  220. // functions get registered when this generated header is included.
  221. os <<
  222. "static "
  223. << m_ClassName.c_str() << " "
  224. << m_ClassName.c_str() << "Initializer;\n"
  225. "\n"
  226. "#endif\n";
  227. }
  228. // Generates the file with the implementation of the class. All
  229. // methods except the actual object creation functions are generated
  230. // here.
  231. void
  232. cmVTKMakeInstantiatorCommand
  233. ::GenerateImplementationFile(std::ostream& os)
  234. {
  235. // Include the instantiator class header.
  236. os <<
  237. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  238. "\n";
  239. // Write the extern declarations for all the creation functions.
  240. for(unsigned int i=0;i < m_Classes.size();++i)
  241. {
  242. os << "extern vtkObject* vtkInstantiator" << m_Classes[i].c_str() << "New();\n";
  243. }
  244. // Write the ClassInitialize method to register all the creation functions.
  245. os <<
  246. "\n"
  247. "void " << m_ClassName.c_str() << "::ClassInitialize()\n"
  248. "{\n";
  249. for(unsigned int i=0;i < m_Classes.size();++i)
  250. {
  251. os << " vtkInstantiator::RegisterInstantiator(\""
  252. << m_Classes[i].c_str() << "\", vtkInstantiator"
  253. << m_Classes[i].c_str() << "New);\n";
  254. }
  255. // Write the ClassFinalize method to unregister all the creation functions.
  256. os <<
  257. "}\n"
  258. "\n"
  259. "void " << m_ClassName.c_str() << "::ClassFinalize()\n"
  260. "{\n";
  261. for(unsigned int i=0;i < m_Classes.size();++i)
  262. {
  263. os << " vtkInstantiator::UnRegisterInstantiator(\""
  264. << m_Classes[i].c_str() << "\", vtkInstantiator"
  265. << m_Classes[i].c_str() << "New);\n";
  266. }
  267. // Write the constructor and destructor of the initializer class to
  268. // call the ClassInitialize and ClassFinalize methods at the right
  269. // time.
  270. os <<
  271. "}\n"
  272. "\n" <<
  273. m_ClassName.c_str() << "::" << m_ClassName.c_str() << "()\n"
  274. "{\n"
  275. " if(++" << m_ClassName.c_str() << "::Count == 1)\n"
  276. " { " << m_ClassName.c_str() << "::ClassInitialize(); }\n"
  277. "}\n"
  278. "\n" <<
  279. m_ClassName.c_str() << "::~" << m_ClassName.c_str() << "()\n"
  280. "{\n"
  281. " if(--" << m_ClassName.c_str() << "::Count == 0)\n"
  282. " { " << m_ClassName.c_str() << "::ClassFinalize(); }\n"
  283. "}\n"
  284. "\n"
  285. "// Number of translation units that include this class's header.\n"
  286. "// Purposely not initialized. Default is static initialization to 0.\n"
  287. "unsigned int " << m_ClassName.c_str() << "::Count;\n";
  288. }
  289. std::string
  290. cmVTKMakeInstantiatorCommand::OldGenerateCreationFileName(unsigned int block)
  291. {
  292. cmOStringStream nameStr;
  293. nameStr << m_ClassName.c_str() << block << ".cxx";
  294. std::string result = nameStr.str();
  295. return result;
  296. }
  297. // Generates a file that includes the headers of the classes it knows
  298. // how to create and provides functions which create the classes with
  299. // the New() method.
  300. void
  301. cmVTKMakeInstantiatorCommand
  302. ::OldGenerateCreationFile(std::ostream& os, unsigned int groupStart,
  303. unsigned int groupSize)
  304. {
  305. // Need to include header of generated class.
  306. os <<
  307. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  308. "\n";
  309. // Include class files.
  310. for(unsigned int i=0;i < groupSize;++i)
  311. {
  312. os << "#include \"" << m_Classes[groupStart+i].c_str() << ".h\"\n";
  313. }
  314. os <<
  315. "\n";
  316. // Write the create function implementations.
  317. for(unsigned int i=0;i < groupSize;++i)
  318. {
  319. os << "vtkObject* " << m_ClassName.c_str() << "::Create_"
  320. << m_Classes[groupStart+i].c_str() << "() { return "
  321. << m_Classes[groupStart+i].c_str() << "::New(); }\n";
  322. }
  323. }
  324. // Generates the class header file with the definition of the class
  325. // and its initializer class.
  326. void
  327. cmVTKMakeInstantiatorCommand
  328. ::OldGenerateHeaderFile(std::ostream& os)
  329. {
  330. os <<
  331. "#ifndef __" << m_ClassName.c_str() << "_h\n"
  332. "#define __" << m_ClassName.c_str() << "_h\n"
  333. "\n"
  334. "#include \"vtkInstantiator.h\"\n";
  335. for(unsigned int i=0;i < m_Includes.size();++i)
  336. {
  337. os << "#include \"" << m_Includes[i].c_str() << "\"\n";
  338. }
  339. os <<
  340. "\n"
  341. "class " << m_ClassName.c_str() << "Initialize;\n"
  342. "\n"
  343. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "\n"
  344. "{\n"
  345. " friend class " << m_ClassName.c_str() << "Initialize;\n"
  346. "\n"
  347. " static void ClassInitialize();\n"
  348. " static void ClassFinalize();\n"
  349. "\n";
  350. for(unsigned int i=0;i < m_Classes.size();++i)
  351. {
  352. os << " static vtkObject* Create_" << m_Classes[i].c_str() << "();\n";
  353. }
  354. // Write the initializer class to make sure the creation functions
  355. // get registered when this generated header is included.
  356. os <<
  357. "};\n"
  358. "\n"
  359. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "Initialize\n"
  360. "{\n"
  361. "public:\n"
  362. " " << m_ClassName.c_str() << "Initialize();\n"
  363. " ~" << m_ClassName.c_str() << "Initialize();\n"
  364. "private:\n"
  365. " static unsigned int Count;\n"
  366. "};\n"
  367. "\n"
  368. "static " << m_ClassName.c_str() << "Initialize " << m_ClassName.c_str() << "Initializer;\n"
  369. "\n"
  370. "#endif\n";
  371. }
  372. // Generates the file with the implementation of the class. All
  373. // methods except the actual object creation functions are generated
  374. // here.
  375. void
  376. cmVTKMakeInstantiatorCommand
  377. ::OldGenerateImplementationFile(std::ostream& os)
  378. {
  379. // Write the ClassInitialize method to register all the creation functions.
  380. os <<
  381. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  382. "\n"
  383. "void " << m_ClassName.c_str() << "::ClassInitialize()\n"
  384. "{\n";
  385. for(unsigned int i=0;i < m_Classes.size();++i)
  386. {
  387. os << " vtkInstantiator::RegisterInstantiator(\""
  388. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  389. << m_Classes[i].c_str() << ");\n";
  390. }
  391. // Write the ClassFinalize method to unregister all the creation functions.
  392. os <<
  393. "}\n"
  394. "\n"
  395. "void " << m_ClassName.c_str() << "::ClassFinalize()\n"
  396. "{\n";
  397. for(unsigned int i=0;i < m_Classes.size();++i)
  398. {
  399. os << " vtkInstantiator::UnRegisterInstantiator(\""
  400. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  401. << m_Classes[i].c_str() << ");\n";
  402. }
  403. // Write the constructor and destructor of the initializer class to
  404. // call the ClassInitialize and ClassFinalize methods at the right
  405. // time.
  406. os <<
  407. "}\n"
  408. "\n" <<
  409. m_ClassName.c_str() << "Initialize::" << m_ClassName.c_str() << "Initialize()\n"
  410. "{\n"
  411. " if(++" << m_ClassName.c_str() << "Initialize::Count == 1)\n"
  412. " { " << m_ClassName.c_str() << "::ClassInitialize(); }\n"
  413. "}\n"
  414. "\n" <<
  415. m_ClassName.c_str() << "Initialize::~" << m_ClassName.c_str() << "Initialize()\n"
  416. "{\n"
  417. " if(--" << m_ClassName.c_str() << "Initialize::Count == 0)\n"
  418. " { " << m_ClassName.c_str() << "::ClassFinalize(); }\n"
  419. "}\n"
  420. "\n"
  421. "// Number of translation units that include this class's header.\n"
  422. "// Purposely not initialized. Default is static initialization to 0.\n"
  423. "unsigned int " << m_ClassName.c_str() << "Initialize::Count;\n";
  424. }