cmVTKWrapTclCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. std::string sourceListValue;
  73. // was the list already populated
  74. const char *def = m_Makefile->GetDefinition(m_SourceList.c_str());
  75. if (def)
  76. {
  77. sourceListValue = def;
  78. sourceListValue += ";";
  79. }
  80. // Create the init file
  81. std::string res = m_LibraryName;
  82. res += "Init.cxx";
  83. sourceListValue += res;
  84. for(std::vector<std::string>::iterator j = (sources.begin() + 1);
  85. j != sources.end(); ++j)
  86. {
  87. cmSourceFile *curr = m_Makefile->GetSource(j->c_str());
  88. // if we should wrap the class
  89. if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE"))
  90. {
  91. cmSourceFile file;
  92. if (curr)
  93. {
  94. file.SetProperty("ABSTRACT",curr->GetProperty("ABSTRACT"));
  95. }
  96. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*j);
  97. std::string newName = srcName + "Tcl";
  98. std::string hname = cdir + "/" + srcName + ".h";
  99. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  100. "cxx",false);
  101. m_WrapHeaders.push_back(hname);
  102. // add starting depends
  103. file.GetDepends().push_back(hname);
  104. m_WrapClasses.push_back(file);
  105. sourceListValue += ";";
  106. sourceListValue += newName + ".cxx";
  107. }
  108. }
  109. // add the init file
  110. cmSourceFile cfile;
  111. cfile.SetProperty("ABSTRACT","0");
  112. std::string newName = m_LibraryName;
  113. newName += "Init";
  114. this->CreateInitFile(res);
  115. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  116. "cxx",false);
  117. m_Makefile->AddSource(cfile);
  118. m_Makefile->AddDefinition(m_SourceList.c_str(), sourceListValue.c_str());
  119. }
  120. return true;
  121. }
  122. void cmVTKWrapTclCommand::FinalPass()
  123. {
  124. // first we add the rules for all the .h to Tcl.cxx files
  125. size_t lastClass = m_WrapClasses.size();
  126. std::vector<std::string> depends;
  127. std::string wtcl = "${VTK_WRAP_TCL_EXE}";
  128. std::string hints = "${VTK_WRAP_HINTS}";
  129. m_Makefile->ExpandVariablesInString(hints);
  130. // wrap all the .h files
  131. depends.push_back(wtcl);
  132. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  133. {
  134. depends.push_back(hints);
  135. }
  136. for(size_t classNum = 0; classNum < lastClass; classNum++)
  137. {
  138. m_Makefile->AddSource(m_WrapClasses[classNum]);
  139. std::vector<std::string> args;
  140. args.push_back(m_WrapHeaders[classNum]);
  141. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  142. {
  143. args.push_back(hints);
  144. }
  145. args.push_back((m_WrapClasses[classNum].GetPropertyAsBool("ABSTRACT") ? "0" : "1"));
  146. std::string res = m_Makefile->GetCurrentOutputDirectory();
  147. res += "/";
  148. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  149. args.push_back(res);
  150. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  151. wtcl.c_str(), args, depends,
  152. res.c_str(), m_LibraryName.c_str());
  153. }
  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].GetPropertyAsBool("ABSTRACT"))
  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. fprintf(fout,
  203. "extern \"C\"\n"
  204. "{\n"
  205. " typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, char *[]);\n"
  206. "}\n"
  207. "\n");
  208. for (i = 0; i < classes.size(); i++)
  209. {
  210. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  211. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  212. }
  213. if (!strcmp(kitName,"Vtkcommontcl"))
  214. {
  215. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n"
  216. " int argc, char *argv[]);\n");
  217. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  218. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  219. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  220. }
  221. else
  222. {
  223. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  224. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  225. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  226. }
  227. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  228. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  229. for (i = 0; i < m_Commands.size(); i++)
  230. {
  231. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  232. capcommands[i].c_str());
  233. }
  234. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
  235. kitName);
  236. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  237. kitName);
  238. /* create an extern ref to the generic delete function */
  239. fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
  240. if (!strcmp(kitName,"Vtkcommontcl"))
  241. {
  242. fprintf(fout,"extern \"C\"\n{\nvoid vtkCommonDeleteAssocData(ClientData cd)\n");
  243. fprintf(fout," {\n");
  244. fprintf(fout," vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
  245. fprintf(fout," delete tis;\n }\n}\n");
  246. }
  247. /* the main declaration */
  248. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  249. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  250. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  251. kitName);
  252. if (!strcmp(kitName,"Vtkcommontcl"))
  253. {
  254. fprintf(fout,
  255. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  256. fprintf(fout,
  257. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  258. fprintf(fout,"\n");
  259. fprintf(fout,"\n");
  260. fprintf(fout,
  261. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  262. fprintf(fout,
  263. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  264. fprintf(fout,
  265. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  266. fprintf(fout,
  267. " Tcl_SetAssocData(interp,(char *) \"vtk\",NULL,(ClientData *)info);\n");
  268. fprintf(fout,
  269. " Tcl_CreateExitHandler(vtkCommonDeleteAssocData,(ClientData *)info);\n");
  270. /* create special vtkCommand command */
  271. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",\n"
  272. " reinterpret_cast<vtkTclCommandType>(vtkCommand),\n"
  273. " (ClientData *)NULL, NULL);\n\n");
  274. }
  275. for (i = 0; i < m_Commands.size(); i++)
  276. {
  277. fprintf(fout," %s_Init(interp);\n", capcommands[i].c_str());
  278. }
  279. fprintf(fout,"\n");
  280. for (i = 0; i < classes.size(); i++)
  281. {
  282. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  283. classes[i].c_str(), classes[i].c_str());
  284. fprintf(fout," %sCommand);\n",classes[i].c_str());
  285. }
  286. fprintf(fout," return TCL_OK;\n}\n");
  287. fclose(fout);
  288. // copy the file if different
  289. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  290. outFileName.c_str());
  291. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  292. return true;
  293. }