cmVTKWrapTclCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "cmVTKWrapTclCommand.h"
  14. // cmVTKWrapTclCommand
  15. bool cmVTKWrapTclCommand::InitialPass(std::vector<std::string> const& argsIn)
  16. {
  17. if(argsIn.size() < 3 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::vector<std::string> args;
  23. // keep the library name
  24. m_LibraryName = argsIn[0];
  25. if (argsIn[1] == std::string("SOURCES"))
  26. {
  27. m_Makefile->ExpandSourceListArguments(argsIn, args, 3);
  28. }
  29. else
  30. {
  31. m_Makefile->ExpandSourceListArguments(argsIn, args, 2);
  32. }
  33. // Now check and see if the value has been stored in the cache
  34. // already, if so use that value and don't look for the program
  35. if(!m_Makefile->IsOn("VTK_WRAP_TCL"))
  36. {
  37. return true;
  38. }
  39. // extract the sources and commands parameters
  40. std::vector<std::string> sources;
  41. bool doing_sources = true;
  42. for(std::vector<std::string>::const_iterator j = (args.begin() + 1);
  43. j != args.end(); ++j)
  44. {
  45. if(*j == "SOURCES")
  46. {
  47. doing_sources = true;
  48. }
  49. else if (*j == "COMMANDS")
  50. {
  51. doing_sources = false;
  52. }
  53. else
  54. {
  55. if(doing_sources)
  56. {
  57. sources.push_back(*j);
  58. }
  59. else
  60. {
  61. m_Commands.push_back(*j);
  62. }
  63. }
  64. }
  65. // get the list of classes for this library
  66. if (sources.size())
  67. {
  68. // what is the current source dir
  69. std::string cdir = m_Makefile->GetCurrentDirectory();
  70. // get the resulting source list name
  71. m_SourceList = sources[0];
  72. for(std::vector<std::string>::iterator j = (sources.begin() + 1);
  73. j != sources.end(); ++j)
  74. {
  75. cmSourceFile *curr = m_Makefile->GetSource(j->c_str());
  76. // if we should wrap the class
  77. if (!curr || !curr->GetWrapExclude())
  78. {
  79. cmSourceFile file;
  80. if (curr)
  81. {
  82. file.SetIsAnAbstractClass(curr->IsAnAbstractClass());
  83. }
  84. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*j);
  85. std::string newName = srcName + "Tcl";
  86. std::string hname = cdir + "/" + srcName + ".h";
  87. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  88. "cxx",false);
  89. m_WrapHeaders.push_back(hname);
  90. // add starting depends
  91. file.GetDepends().push_back(hname);
  92. m_WrapClasses.push_back(file);
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. void cmVTKWrapTclCommand::FinalPass()
  99. {
  100. // first we add the rules for all the .h to Tcl.cxx files
  101. size_t lastClass = m_WrapClasses.size();
  102. std::vector<std::string> depends;
  103. std::string wtcl = "${VTK_WRAP_TCL_EXE}";
  104. std::string hints = "${VTK_WRAP_HINTS}";
  105. m_Makefile->ExpandVariablesInString(hints);
  106. std::string sourceListValue;
  107. // was the list already populated
  108. const char *def = m_Makefile->GetDefinition(m_SourceList.c_str());
  109. if (def)
  110. {
  111. sourceListValue = def;
  112. sourceListValue += ";";
  113. }
  114. // Create the init file
  115. std::string res = m_LibraryName;
  116. res += "Init.cxx";
  117. this->CreateInitFile(res);
  118. // add the init file
  119. cmSourceFile cfile;
  120. cfile.SetIsAnAbstractClass(false);
  121. std::string newName = m_LibraryName;
  122. newName += "Init";
  123. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  124. "cxx",false);
  125. m_Makefile->AddSource(cfile);
  126. sourceListValue += newName + ".cxx";
  127. // wrap all the .h files
  128. depends.push_back(wtcl);
  129. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  130. {
  131. depends.push_back(hints);
  132. }
  133. for(size_t classNum = 0; classNum < lastClass; classNum++)
  134. {
  135. m_Makefile->AddSource(m_WrapClasses[classNum]);
  136. std::vector<std::string> args;
  137. args.push_back(m_WrapHeaders[classNum]);
  138. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  139. {
  140. args.push_back(hints);
  141. }
  142. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  143. std::string res = m_Makefile->GetCurrentOutputDirectory();
  144. res += "/";
  145. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  146. args.push_back(res);
  147. sourceListValue += ";";
  148. sourceListValue += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  149. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  150. wtcl.c_str(), args, depends,
  151. res.c_str(), m_LibraryName.c_str());
  152. }
  153. m_Makefile->AddDefinition(m_SourceList.c_str(), sourceListValue.c_str());
  154. }
  155. bool cmVTKWrapTclCommand::CreateInitFile(std::string& res)
  156. {
  157. /* we have to make sure that the name is the correct case */
  158. std::string kitName = cmSystemTools::Capitalized(m_LibraryName);
  159. std::vector<std::string> classes;
  160. size_t lastClass = m_WrapHeaders.size();
  161. size_t classNum;
  162. for(classNum = 0; classNum < lastClass; classNum++)
  163. {
  164. if (!m_WrapClasses[classNum].IsAnAbstractClass())
  165. {
  166. std::string cls = m_WrapHeaders[classNum];
  167. cls = cls.substr(0,cls.size()-2);
  168. std::string::size_type pos = cls.rfind('/');
  169. if(pos != std::string::npos)
  170. {
  171. cls = cls.substr(pos+1);
  172. }
  173. classes.push_back(cls);
  174. }
  175. }
  176. // open the init file
  177. std::string outFileName =
  178. m_Makefile->GetCurrentOutputDirectory();
  179. outFileName += "/" + res;
  180. return this->WriteInit(kitName.c_str(), outFileName, classes);
  181. }
  182. /* warning this code is also in getclasses.cxx under pcmaker */
  183. bool cmVTKWrapTclCommand::WriteInit(const char *kitName,
  184. std::string& outFileName,
  185. std::vector<std::string>& classes)
  186. {
  187. unsigned int i;
  188. std::string tempOutputFile = outFileName + ".tmp";
  189. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  190. if (!fout)
  191. {
  192. cmSystemTools::Error("Failed to open TclInit file for ", tempOutputFile.c_str());
  193. return false;
  194. }
  195. // capitalized commands just once
  196. std::vector<std::string> capcommands;
  197. for (i = 0; i < m_Commands.size(); i++)
  198. {
  199. capcommands.push_back(cmSystemTools::Capitalized(m_Commands[i]));
  200. }
  201. fprintf(fout,"#include \"vtkTclUtil.h\"\n");
  202. for (i = 0; i < classes.size(); i++)
  203. {
  204. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  205. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  206. }
  207. if (!strcmp(kitName,"Vtkcommontcl"))
  208. {
  209. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n");
  210. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  211. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  212. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  213. }
  214. else
  215. {
  216. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  217. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  218. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  219. }
  220. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  221. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  222. for (i = 0; i < m_Commands.size(); i++)
  223. {
  224. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  225. capcommands[i].c_str());
  226. }
  227. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
  228. kitName);
  229. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  230. kitName);
  231. /* create an extern ref to the generic delete function */
  232. fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
  233. if (!strcmp(kitName,"Vtkcommontcl"))
  234. {
  235. fprintf(fout,"void vtkCommonDeleteAssocData(ClientData cd)\n");
  236. fprintf(fout," {\n");
  237. fprintf(fout," vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
  238. fprintf(fout," delete tis;\n }\n");
  239. }
  240. /* the main declaration */
  241. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  242. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  243. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  244. kitName);
  245. if (!strcmp(kitName,"Vtkcommontcl"))
  246. {
  247. fprintf(fout,
  248. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  249. fprintf(fout,
  250. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  251. fprintf(fout,"\n");
  252. fprintf(fout,"\n");
  253. fprintf(fout,
  254. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  255. fprintf(fout,
  256. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  257. fprintf(fout,
  258. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  259. fprintf(fout,
  260. " Tcl_SetAssocData(interp,(char *) \"vtk\",NULL,(ClientData *)info);\n");
  261. fprintf(fout,
  262. " Tcl_CreateExitHandler(vtkCommonDeleteAssocData,(ClientData *)info);\n");
  263. /* create special vtkCommand command */
  264. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",vtkCommand,\n (ClientData *)NULL, NULL);\n\n");
  265. }
  266. for (i = 0; i < m_Commands.size(); i++)
  267. {
  268. fprintf(fout," %s_Init(interp);\n", capcommands[i].c_str());
  269. }
  270. fprintf(fout,"\n");
  271. for (i = 0; i < classes.size(); i++)
  272. {
  273. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  274. classes[i].c_str(), classes[i].c_str());
  275. fprintf(fout," %sCommand);\n",classes[i].c_str());
  276. }
  277. fprintf(fout," return TCL_OK;\n}\n");
  278. fclose(fout);
  279. // copy the file if different
  280. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  281. outFileName.c_str());
  282. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  283. return true;
  284. }