cmVTKWrapTclCommand.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmVTKWrapTclCommand.h"
  12. // cmVTKWrapTclCommand
  13. bool cmVTKWrapTclCommand::Invoke(std::vector<std::string>& args)
  14. {
  15. if(args.size() < 3 )
  16. {
  17. this->SetError("called with incorrect number of arguments");
  18. return false;
  19. }
  20. // Now check and see if the value has been stored in the cache
  21. // already, if so use that value and don't look for the program
  22. if(!cmCacheManager::GetInstance()->IsOn("VTK_WRAP_TCL"))
  23. {
  24. return true;
  25. }
  26. // add in a depend in the vtkVTKWrapTcl executable
  27. m_Makefile->AddUtility("vtkWrapTcl");
  28. // what is the current source dir
  29. std::string cdir = m_Makefile->GetCurrentDirectory();
  30. // keep the library name
  31. m_LibraryName = args[0];
  32. m_SourceList = args[1];
  33. // get the list of classes for this library
  34. cmMakefile::SourceMap &Classes = m_Makefile->GetSources();
  35. for(std::vector<std::string>::iterator j = (args.begin() + 2);
  36. j != args.end(); ++j)
  37. {
  38. for(cmMakefile::SourceMap::iterator l = Classes.begin();
  39. l != Classes.end(); l++)
  40. {
  41. for(std::vector<cmSourceFile>::iterator i = l->second.begin();
  42. i != l->second.end(); i++)
  43. {
  44. cmSourceFile &curr = *i;
  45. // if we should wrap the class
  46. if (!curr.GetWrapExclude())
  47. {
  48. cmSourceFile file;
  49. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  50. std::string newName = curr.GetSourceName() + "Tcl";
  51. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  52. "cxx",false);
  53. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  54. m_WrapHeaders.push_back(hname);
  55. // add starting depends
  56. file.GetDepends().push_back(hname);
  57. m_WrapClasses.push_back(file);
  58. }
  59. }
  60. }
  61. }
  62. return true;
  63. }
  64. void cmVTKWrapTclCommand::FinalPass()
  65. {
  66. // first we add the rules for all the .h to Tcl.cxx files
  67. int lastClass = m_WrapClasses.size();
  68. std::vector<std::string> depends;
  69. std::string wtcl = "${VTK_WRAP_TCL_EXE}";
  70. std::string hints = "${VTK_WRAP_HINTS}";
  71. // Create the init file
  72. std::string res = m_LibraryName;
  73. res += "Init.cxx";
  74. this->CreateInitFile(res);
  75. // add the init file
  76. cmSourceFile cfile;
  77. cfile.SetIsAnAbstractClass(false);
  78. std::string newName = m_LibraryName;
  79. newName += "Init";
  80. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  81. "cxx",false);
  82. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  83. // wrap all the .h files
  84. depends.push_back(wtcl);
  85. for(int classNum = 0; classNum < lastClass; classNum++)
  86. {
  87. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  88. std::string res = m_WrapClasses[classNum].GetSourceName() + ".cxx";
  89. std::string cmd = wtcl + " " + m_WrapHeaders[classNum] + " "
  90. + hints + (m_WrapClasses[classNum].IsAnAbstractClass() ? " 0 " : " 1 ") + " > " + m_WrapClasses[classNum].GetSourceName() + ".cxx";
  91. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  92. cmd.c_str(), depends,
  93. res.c_str(), m_LibraryName.c_str());
  94. }
  95. }
  96. bool cmVTKWrapTclCommand::CreateInitFile(std::string& res)
  97. {
  98. unsigned int i;
  99. /* we have to make sure that the name is the correct case */
  100. std::string kitName = m_LibraryName;
  101. if (kitName[0] > 90) kitName[0] -= 32;
  102. for (i = 1; i < kitName.size(); i++)
  103. {
  104. if ((kitName[i] > 64)&&(kitName[i] < 91))
  105. {
  106. kitName[i] += 32;
  107. }
  108. }
  109. std::vector<std::string> classes;
  110. int lastClass = m_WrapHeaders.size();
  111. int classNum;
  112. for(classNum = 0; classNum < lastClass; classNum++)
  113. {
  114. if (!m_WrapClasses[classNum].IsAnAbstractClass())
  115. {
  116. std::string cls = m_WrapHeaders[classNum];
  117. cls = cls.substr(0,cls.size()-2);
  118. std::string::size_type pos = cls.rfind('/');
  119. if(pos != std::string::npos)
  120. {
  121. cls = cls.substr(pos+1);
  122. }
  123. classes.push_back(cls);
  124. }
  125. }
  126. // open the init file
  127. std::string outFileName =
  128. m_Makefile->GetCurrentOutputDirectory();
  129. outFileName += "/" + res;
  130. return this->WriteInit(kitName.c_str(), outFileName, classes);
  131. }
  132. /* warning this code is also in getclasses.cxx under pcmaker */
  133. bool cmVTKWrapTclCommand::WriteInit(const char *kitName,
  134. std::string& outFileName,
  135. std::vector<std::string>& classes)
  136. {
  137. unsigned int i;
  138. FILE *fout = fopen(outFileName.c_str(),"w");
  139. if (!fout)
  140. {
  141. return false;
  142. }
  143. fprintf(fout,"#include \"vtkTclUtil.h\"\n");
  144. for (i = 0; i < classes.size(); i++)
  145. {
  146. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  147. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  148. }
  149. if (!strcmp(kitName,"Vtkcommon"))
  150. {
  151. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n");
  152. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  153. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  154. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  155. }
  156. else
  157. {
  158. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  159. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  160. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  161. }
  162. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  163. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  164. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n\n",
  165. kitName);
  166. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n\n",
  167. kitName);
  168. /* create an extern ref to the generic delete function */
  169. fprintf(fout,"\n\nextern void vtkTclGenericDeleteObject(ClientData cd);\n\n");
  170. /* the main declaration */
  171. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  172. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  173. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  174. kitName);
  175. if (!strcmp(kitName,"Vtkcommon"))
  176. {
  177. fprintf(fout,
  178. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  179. fprintf(fout,
  180. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  181. fprintf(fout,"\n");
  182. fprintf(fout,"\n");
  183. fprintf(fout,
  184. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  185. fprintf(fout,
  186. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  187. fprintf(fout,
  188. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  189. fprintf(fout,
  190. " Tcl_SetAssocData(interp,(char *) \"vtk\",NULL,(ClientData *)info);\n");
  191. /* create special vtkCommand command */
  192. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",vtkCommand,\n (ClientData *)NULL, NULL);\n\n");
  193. }
  194. for (i = 0; i < classes.size(); i++)
  195. {
  196. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  197. classes[i].c_str(), classes[i].c_str());
  198. fprintf(fout," %sCommand);\n",classes[i].c_str());
  199. }
  200. fprintf(fout," return TCL_OK;\n}\n");
  201. fclose(fout);
  202. return true;
  203. }