cmVTKWrapTclCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmVTKWrapTclCommand.h"
  33. // cmVTKWrapTclCommand
  34. bool cmVTKWrapTclCommand::InitialPass(std::vector<std::string> const& args)
  35. {
  36. if(args.size() < 3 )
  37. {
  38. this->SetError("called with incorrect number of arguments");
  39. return false;
  40. }
  41. // keep the library name
  42. m_LibraryName = args[0];
  43. // Now check and see if the value has been stored in the cache
  44. // already, if so use that value and don't look for the program
  45. if(!m_Makefile->IsOn("VTK_WRAP_TCL"))
  46. {
  47. return true;
  48. }
  49. // extract the sources and commands parameters
  50. std::vector<std::string> sources;
  51. bool doing_sources = true;
  52. for(std::vector<std::string>::const_iterator j = (args.begin() + 1);
  53. j != args.end(); ++j)
  54. {
  55. if(*j == "SOURCES")
  56. {
  57. doing_sources = true;
  58. }
  59. else if (*j == "COMMANDS")
  60. {
  61. doing_sources = false;
  62. }
  63. else
  64. {
  65. if(doing_sources)
  66. {
  67. sources.push_back(*j);
  68. }
  69. else
  70. {
  71. m_Commands.push_back(*j);
  72. }
  73. }
  74. }
  75. // get the list of classes for this library
  76. if (sources.size())
  77. {
  78. // what is the current source dir
  79. std::string cdir = m_Makefile->GetCurrentDirectory();
  80. // get the resulting source list name
  81. m_SourceList = sources[0];
  82. cmMakefile::SourceMap &Classes = m_Makefile->GetSources();
  83. for(std::vector<std::string>::iterator j = (sources.begin() + 1);
  84. j != sources.end(); ++j)
  85. {
  86. cmMakefile::SourceMap::iterator l = Classes.find(*j);
  87. if (l == Classes.end())
  88. {
  89. this->SetError("bad source list passed to VTKWrapTclCommand");
  90. return false;
  91. }
  92. for(std::vector<cmSourceFile>::iterator i = l->second.begin();
  93. i != l->second.end(); i++)
  94. {
  95. cmSourceFile &curr = *i;
  96. // if we should wrap the class
  97. if (!curr.GetWrapExclude())
  98. {
  99. cmSourceFile file;
  100. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  101. std::string newName = curr.GetSourceName() + "Tcl";
  102. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  103. "cxx",false);
  104. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  105. m_WrapHeaders.push_back(hname);
  106. // add starting depends
  107. file.GetDepends().push_back(hname);
  108. m_WrapClasses.push_back(file);
  109. }
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. void cmVTKWrapTclCommand::FinalPass()
  116. {
  117. // first we add the rules for all the .h to Tcl.cxx files
  118. int lastClass = m_WrapClasses.size();
  119. std::vector<std::string> depends;
  120. std::string wtcl = "${VTK_WRAP_TCL_EXE}";
  121. std::string hints = "${VTK_WRAP_HINTS}";
  122. m_Makefile->ExpandVariablesInString(hints);
  123. // Create the init file
  124. std::string res = m_LibraryName;
  125. res += "Init.cxx";
  126. this->CreateInitFile(res);
  127. // add the init file
  128. cmSourceFile cfile;
  129. cfile.SetIsAnAbstractClass(false);
  130. std::string newName = m_LibraryName;
  131. newName += "Init";
  132. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  133. "cxx",false);
  134. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  135. // wrap all the .h files
  136. depends.push_back(wtcl);
  137. depends.push_back(hints);
  138. for(int classNum = 0; classNum < lastClass; classNum++)
  139. {
  140. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  141. std::string res = m_WrapClasses[classNum].GetSourceName() + ".cxx";
  142. std::vector<std::string> args;
  143. args.push_back(m_WrapHeaders[classNum]);
  144. args.push_back(hints);
  145. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  146. std::string tmp = m_Makefile->GetCurrentOutputDirectory();
  147. tmp += "/";
  148. tmp += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  149. args.push_back(tmp);
  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. int lastClass = m_WrapHeaders.size();
  161. int classNum;
  162. for(classNum = 0; classNum < lastClass; classNum++)
  163. {
  164. if (!m_WrapClasses[classNum].IsAnAbstractClass())
  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. for (i = 0; i < classes.size(); i++)
  203. {
  204. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  205. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  206. }
  207. if (!strcmp(kitName,"Vtkcommontcl"))
  208. {
  209. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n");
  210. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  211. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  212. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  213. }
  214. else
  215. {
  216. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  217. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  218. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  219. }
  220. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  221. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  222. for (i = 0; i < m_Commands.size(); i++)
  223. {
  224. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  225. capcommands[i].c_str());
  226. }
  227. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
  228. kitName);
  229. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  230. kitName);
  231. /* create an extern ref to the generic delete function */
  232. fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
  233. if (!strcmp(kitName,"Vtkcommontcl"))
  234. {
  235. fprintf(fout,"void vtkCommonDeleteAssocData(ClientData cd, Tcl_Interp *)\n");
  236. fprintf(fout," {\n");
  237. fprintf(fout," vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
  238. fprintf(fout," delete tis;\n }\n");
  239. }
  240. /* the main declaration */
  241. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  242. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  243. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  244. kitName);
  245. if (!strcmp(kitName,"Vtkcommontcl"))
  246. {
  247. fprintf(fout,
  248. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  249. fprintf(fout,
  250. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  251. fprintf(fout,"\n");
  252. fprintf(fout,"\n");
  253. fprintf(fout,
  254. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  255. fprintf(fout,
  256. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  257. fprintf(fout,
  258. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  259. fprintf(fout,
  260. " Tcl_SetAssocData(interp,(char *) \"vtk\",vtkCommonDeleteAssocData,(ClientData *)info);\n");
  261. /* create special vtkCommand command */
  262. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",vtkCommand,\n (ClientData *)NULL, NULL);\n\n");
  263. }
  264. for (i = 0; i < m_Commands.size(); i++)
  265. {
  266. fprintf(fout," %s_Init(interp);\n", capcommands[i].c_str());
  267. }
  268. fprintf(fout,"\n");
  269. for (i = 0; i < classes.size(); i++)
  270. {
  271. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  272. classes[i].c_str(), classes[i].c_str());
  273. fprintf(fout," %sCommand);\n",classes[i].c_str());
  274. }
  275. fprintf(fout," return TCL_OK;\n}\n");
  276. fclose(fout);
  277. // copy the file if different
  278. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  279. outFileName.c_str());
  280. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  281. return true;
  282. }