cmWrapTclCommand.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "cmWrapTclCommand.h"
  12. // cmWrapTclCommand
  13. bool cmWrapTclCommand::Invoke(std::vector<std::string>& args)
  14. {
  15. if(args.size() < 0 )
  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. const char* cacheValue
  23. = cmCacheManager::GetInstance()->GetCacheValue("WRAP_TCL");
  24. if(!cacheValue)
  25. {
  26. cmCacheManager::GetInstance()->AddCacheEntry("WRAP_TCL","1",
  27. cmCacheManager::BOOL);
  28. m_Makefile->AddDefinition("WRAP_TCL", "1");
  29. }
  30. else
  31. {
  32. m_Makefile->AddDefinition("WRAP_TCL", cacheValue);
  33. // if it is turned off then return
  34. if (!strcmp(cacheValue,"0"))
  35. {
  36. return true;
  37. }
  38. }
  39. // get the list of classes for this library
  40. std::vector<cmClassFile> &classes = m_Makefile->GetClasses();
  41. // add in new classes for the wrappers
  42. int lastClass = classes.size();
  43. // what is the current source dir
  44. std::string cdir = m_Makefile->GetCurrentDirectory();
  45. for(int classNum = 0; classNum < lastClass; classNum++)
  46. {
  47. cmClassFile &curr = classes[classNum];
  48. // if we should wrap the class
  49. if (!curr.m_WrapExclude)
  50. {
  51. cmClassFile file;
  52. file.m_AbstractClass = curr.m_AbstractClass;
  53. std::string newName = curr.m_ClassName + "Tcl";
  54. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  55. "cxx",false);
  56. m_WrapClasses.push_back(file);
  57. std::string hname = cdir + "/" + curr.m_ClassName + ".h";
  58. m_WrapHeaders.push_back(hname);
  59. }
  60. }
  61. return true;
  62. }
  63. void cmWrapTclCommand::FinalPass()
  64. {
  65. // first we add the rules for all the .h to Tcl.cxx files
  66. int lastClass = m_WrapClasses.size();
  67. std::vector<std::string> depends;
  68. std::string wtcl = "${WRAP_TCL_EXE}";
  69. std::string hints = "${WRAP_HINTS}";
  70. m_Makefile->ExpandVariablesInString(wtcl);
  71. m_Makefile->ExpandVariablesInString(hints);
  72. // Create the init file
  73. std::string res = m_Makefile->GetLibraryName();
  74. res += "Init.cxx";
  75. this->CreateInitFile(res);
  76. // add the init file
  77. cmClassFile cfile;
  78. cfile.m_AbstractClass = false;
  79. std::string newName = m_Makefile->GetLibraryName();
  80. newName += "Init";
  81. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  82. "cxx",false);
  83. m_Makefile->AddClass(cfile);
  84. // wrap all the .h files
  85. depends.push_back(wtcl);
  86. for(int classNum = 0; classNum < lastClass; classNum++)
  87. {
  88. m_Makefile->AddClass(m_WrapClasses[classNum]);
  89. std::string res = m_WrapClasses[classNum].m_ClassName + ".cxx";
  90. std::string cmd = wtcl + " " + m_WrapHeaders[classNum] + " "
  91. + hints + (m_WrapClasses[classNum].m_AbstractClass ? " 0 " : " 1 ") + " > " + m_WrapClasses[classNum].m_ClassName + ".cxx";
  92. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  93. res.c_str(),
  94. cmd.c_str(), depends);
  95. }
  96. }
  97. bool cmWrapTclCommand::CreateInitFile(std::string& res)
  98. {
  99. unsigned int i;
  100. /* we have to make sure that the name is the correct case */
  101. std::string kitName = m_Makefile->GetLibraryName();
  102. if (kitName[0] > 90) kitName[0] -= 32;
  103. for (i = 1; i < kitName.size(); i++)
  104. {
  105. if ((kitName[i] > 64)&&(kitName[i] < 91))
  106. {
  107. kitName[i] += 32;
  108. }
  109. }
  110. std::vector<std::string> classes;
  111. int lastClass = m_WrapHeaders.size();
  112. int classNum;
  113. for(classNum = 0; classNum < lastClass; classNum++)
  114. {
  115. if (!m_WrapClasses[classNum].m_AbstractClass)
  116. {
  117. std::string cls = m_WrapHeaders[classNum];
  118. cls = cls.substr(0,cls.size()-2);
  119. std::string::size_type pos = cls.rfind('/');
  120. if(pos != std::string::npos)
  121. {
  122. cls = cls.substr(pos+1);
  123. }
  124. classes.push_back(cls);
  125. }
  126. }
  127. // open the init file
  128. std::string outFileName =
  129. m_Makefile->GetCurrentOutputDirectory();
  130. outFileName += "/" + res;
  131. return this->WriteInit(kitName.c_str(), outFileName, classes);
  132. }
  133. /* warning this code is also in getclasses.cxx under pcmaker */
  134. bool cmWrapTclCommand::WriteInit(const char *kitName, 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. }