cmVTKWrapTclCommand.cxx 9.8 KB

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