cmVTKMakeInstantiatorCommand.cxx 14 KB

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