cmVTKMakeInstantiatorCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. unsigned int groupSize = 10;
  32. bool includesMode = false;
  33. // Find the path of the files to be generated.
  34. std::string filePath = m_Makefile->GetCurrentOutputDirectory();
  35. std::string headerPath = filePath;
  36. for(unsigned int i=2;i < args.size();++i)
  37. {
  38. if(args[i] == "GROUP_SIZE")
  39. {
  40. includesMode = false;
  41. if(++i < args.size())
  42. {
  43. groupSize = atoi(args[i].c_str());
  44. }
  45. else
  46. {
  47. this->SetError("GROUP_SIZE option used without value.");
  48. return false;
  49. }
  50. }
  51. else if(args[i] == "HEADER_LOCATION")
  52. {
  53. includesMode = false;
  54. if(++i < args.size())
  55. {
  56. headerPath = args[i];
  57. }
  58. else
  59. {
  60. this->SetError("HEADER_LOCATION option used without value.");
  61. return false;
  62. }
  63. }
  64. else if(args[i] == "EXPORT_MACRO")
  65. {
  66. includesMode = false;
  67. if(++i < args.size())
  68. {
  69. m_ExportMacro = args[i];
  70. }
  71. else
  72. {
  73. this->SetError("EXPORT_MACRO option used without value.");
  74. return false;
  75. }
  76. }
  77. else if(args[i] == "INCLUDES")
  78. {
  79. includesMode = true;
  80. }
  81. // If not an option, it must be another input source list name or
  82. // an include file.
  83. else
  84. {
  85. if(!includesMode)
  86. {
  87. inSourceLists.push_back(args[i]);
  88. }
  89. else
  90. {
  91. m_Includes.push_back(args[i]);
  92. }
  93. }
  94. }
  95. if(m_ExportMacro == "-")
  96. {
  97. this->SetError("No EXPORT_MACRO option given.");
  98. return false;
  99. }
  100. for(std::vector<cmStdString>::const_iterator s = inSourceLists.begin();
  101. s != inSourceLists.end(); ++s)
  102. {
  103. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*s);
  104. cmSourceFile *sf = m_Makefile->GetSource(s->c_str());
  105. // Wrap-excluded and abstract classes do not have a New() method.
  106. // vtkIndent and vtkTimeStamp are special cases and are not
  107. // vtkObject subclasses.
  108. if(
  109. (!sf || (!sf->GetWrapExclude() && !sf->GetIsAnAbstractClass())) &&
  110. ((srcName != "vtkIndent") && (srcName != "vtkTimeStamp")))
  111. {
  112. m_Classes.push_back(srcName);
  113. }
  114. }
  115. // Generate the header with the class declaration.
  116. {
  117. std::string fileName = m_ClassName + ".h";
  118. std::string fullName = headerPath+"/"+fileName;
  119. // Generate the output file with copy-if-different.
  120. cmGeneratedFileStream fout(fullName.c_str());
  121. // Actually generate the code in the file.
  122. this->GenerateHeaderFile(fout.GetStream());
  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. this->GenerateImplementationFile(fout.GetStream());
  133. }
  134. // Add the generated source file into the source list.
  135. cmSourceFile file;
  136. file.SetWrapExclude(true);
  137. file.SetIsAnAbstractClass(false);
  138. file.SetName(fileName.c_str(), filePath.c_str(),
  139. m_Makefile->GetSourceExtensions(),
  140. m_Makefile->GetHeaderExtensions());
  141. m_Makefile->AddSource(file);
  142. sourceListValue += file.GetSourceName() + ".cxx";
  143. }
  144. size_t numClasses = m_Classes.size();
  145. size_t numFullBlocks = numClasses / groupSize;
  146. size_t lastBlockSize = numClasses % groupSize;
  147. size_t numBlocks = numFullBlocks + ((lastBlockSize>0)? 1:0);
  148. // Generate the files with the ::New() calls to each class. These
  149. // are done in groups to keep the translation unit size smaller.
  150. for(unsigned int block=0; block < numBlocks;++block)
  151. {
  152. std::string fileName = this->GenerateCreationFileName(block);
  153. std::string fullName = filePath+"/"+fileName;
  154. // Generate the output file with copy-if-different.
  155. {
  156. cmGeneratedFileStream fout(fullName.c_str());
  157. unsigned int thisBlockSize =
  158. (block < numFullBlocks)? groupSize:lastBlockSize;
  159. // Actually generate the code in the file.
  160. this->GenerateCreationFile(fout.GetStream(),
  161. block*groupSize, thisBlockSize);
  162. }
  163. // Add the generated source file into the source list.
  164. cmSourceFile file;
  165. file.SetWrapExclude(true);
  166. file.SetIsAnAbstractClass(false);
  167. file.SetName(fileName.c_str(), filePath.c_str(),
  168. m_Makefile->GetSourceExtensions(),
  169. m_Makefile->GetHeaderExtensions());
  170. m_Makefile->AddSource(file);
  171. sourceListValue += ";";
  172. sourceListValue += file.GetSourceName() + ".cxx";
  173. }
  174. m_Makefile->AddDefinition(args[1].c_str(), sourceListValue.c_str());
  175. return true;
  176. }
  177. std::string
  178. cmVTKMakeInstantiatorCommand::GenerateCreationFileName(unsigned int block)
  179. {
  180. cmStringStream nameStr;
  181. nameStr << m_ClassName.c_str() << block << ".cxx";
  182. std::string result = nameStr.str();
  183. return result;
  184. }
  185. // Generates the class header file with the definition of the class
  186. // and its initializer class.
  187. void
  188. cmVTKMakeInstantiatorCommand
  189. ::GenerateHeaderFile(std::ostream& os)
  190. {
  191. os <<
  192. "#ifndef __" << m_ClassName.c_str() << "_h\n"
  193. "#define __" << m_ClassName.c_str() << "_h\n"
  194. "\n"
  195. "#include \"vtkInstantiator.h\"\n";
  196. for(unsigned int i=0;i < m_Includes.size();++i)
  197. {
  198. os << "#include \"" << m_Includes[i].c_str() << "\"\n";
  199. }
  200. os <<
  201. "\n"
  202. "class " << m_ClassName.c_str() << "Initialize;\n"
  203. "\n"
  204. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "\n"
  205. "{\n"
  206. " friend class " << m_ClassName.c_str() << "Initialize;\n"
  207. "\n"
  208. " static void ClassInitialize();\n"
  209. " static void ClassFinalize();\n"
  210. "\n";
  211. for(unsigned int i=0;i < m_Classes.size();++i)
  212. {
  213. os << " static vtkObject* Create_" << m_Classes[i].c_str() << "();\n";
  214. }
  215. // Write the initializer class to make sure the creation functions
  216. // get registered when this generated header is included.
  217. os <<
  218. "};\n"
  219. "\n"
  220. "class " << m_ExportMacro.c_str() << " " << m_ClassName.c_str() << "Initialize\n"
  221. "{\n"
  222. "public:\n"
  223. " " << m_ClassName.c_str() << "Initialize();\n"
  224. " ~" << m_ClassName.c_str() << "Initialize();\n"
  225. "private:\n"
  226. " static unsigned int Count;\n"
  227. "};\n"
  228. "\n"
  229. "static " << m_ClassName.c_str() << "Initialize " << m_ClassName.c_str() << "Initializer;\n"
  230. "\n"
  231. "#endif\n";
  232. }
  233. // Generates the file with the implementation of the class. All
  234. // methods except the actual object creation functions are generated
  235. // here.
  236. void
  237. cmVTKMakeInstantiatorCommand
  238. ::GenerateImplementationFile(std::ostream& os)
  239. {
  240. // Write the ClassInitialize method to register all the creation functions.
  241. os <<
  242. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  243. "\n"
  244. "void " << m_ClassName.c_str() << "::ClassInitialize()\n"
  245. "{\n";
  246. for(unsigned int i=0;i < m_Classes.size();++i)
  247. {
  248. os << " vtkInstantiator::RegisterInstantiator(\""
  249. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  250. << m_Classes[i].c_str() << ");\n";
  251. }
  252. // Write the ClassFinalize method to unregister all the creation functions.
  253. os <<
  254. "}\n"
  255. "\n"
  256. "void " << m_ClassName.c_str() << "::ClassFinalize()\n"
  257. "{\n";
  258. for(unsigned int i=0;i < m_Classes.size();++i)
  259. {
  260. os << " vtkInstantiator::UnRegisterInstantiator(\""
  261. << m_Classes[i].c_str() << "\", " << m_ClassName.c_str() << "::Create_"
  262. << m_Classes[i].c_str() << ");\n";
  263. }
  264. // Write the constructor and destructor of the initializer class to
  265. // call the ClassInitialize and ClassFinalize methods at the right
  266. // time.
  267. os <<
  268. "}\n"
  269. "\n" <<
  270. m_ClassName.c_str() << "Initialize::" << m_ClassName.c_str() << "Initialize()\n"
  271. "{\n"
  272. " if(++" << m_ClassName.c_str() << "Initialize::Count == 1)\n"
  273. " { " << m_ClassName.c_str() << "::ClassInitialize(); }\n"
  274. "}\n"
  275. "\n" <<
  276. m_ClassName.c_str() << "Initialize::~" << m_ClassName.c_str() << "Initialize()\n"
  277. "{\n"
  278. " if(--" << m_ClassName.c_str() << "Initialize::Count == 0)\n"
  279. " { " << m_ClassName.c_str() << "::ClassFinalize(); }\n"
  280. "}\n"
  281. "\n"
  282. "// Number of translation units that include this class's header.\n"
  283. "// Purposely not initialized. Default is static initialization to 0.\n"
  284. "unsigned int " << m_ClassName.c_str() << "Initialize::Count;\n";
  285. }
  286. // Generates a file that includes the headers of the classes it knows
  287. // how to create and provides functions which create the classes with
  288. // the New() method.
  289. void
  290. cmVTKMakeInstantiatorCommand
  291. ::GenerateCreationFile(std::ostream& os, unsigned int groupStart,
  292. unsigned int groupSize)
  293. {
  294. // Need to include header of generated class.
  295. os <<
  296. "#include \"" << m_ClassName.c_str() << ".h\"\n"
  297. "\n";
  298. // Include class files.
  299. for(unsigned int i=0;i < groupSize;++i)
  300. {
  301. os << "#include \"" << m_Classes[groupStart+i].c_str() << ".h\"\n";
  302. }
  303. os <<
  304. "\n";
  305. // Write the create function implementations.
  306. for(unsigned int i=0;i < groupSize;++i)
  307. {
  308. os << "vtkObject* " << m_ClassName.c_str() << "::Create_"
  309. << m_Classes[groupStart+i].c_str() << "() { return "
  310. << m_Classes[groupStart+i].c_str() << "::New(); }\n";
  311. }
  312. }