cmVTKWrapTclCommand.cxx 9.9 KB

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