cmVTKWrapTclCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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. std::string srcDir = cdir;
  93. if (curr)
  94. {
  95. file.SetProperty("ABSTRACT",curr->GetProperty("ABSTRACT"));
  96. srcDir = cmSystemTools::GetFilenamePath(curr->GetFullPath());
  97. }
  98. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*j);
  99. std::string newName = srcName + "Tcl";
  100. std::string hname = srcDir + "/" + srcName + ".h";
  101. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  102. "cxx",false);
  103. m_WrapHeaders.push_back(hname);
  104. // add starting depends
  105. file.GetDepends().push_back(hname);
  106. m_WrapClasses.push_back(file);
  107. sourceListValue += ";";
  108. sourceListValue += newName + ".cxx";
  109. }
  110. }
  111. // add the init file
  112. cmSourceFile cfile;
  113. cfile.SetProperty("ABSTRACT","0");
  114. std::string newName = m_LibraryName;
  115. newName += "Init";
  116. this->CreateInitFile(res);
  117. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  118. "cxx",false);
  119. m_Makefile->AddSource(cfile);
  120. m_Makefile->AddDefinition(m_SourceList.c_str(), sourceListValue.c_str());
  121. }
  122. return true;
  123. }
  124. void cmVTKWrapTclCommand::FinalPass()
  125. {
  126. // first we add the rules for all the .h to Tcl.cxx files
  127. size_t lastClass = m_WrapClasses.size();
  128. std::vector<std::string> depends;
  129. std::string wtcl = "${VTK_WRAP_TCL_EXE}";
  130. std::string hints = "${VTK_WRAP_HINTS}";
  131. m_Makefile->ExpandVariablesInString(hints);
  132. // wrap all the .h files
  133. depends.push_back(wtcl);
  134. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  135. {
  136. depends.push_back(hints);
  137. }
  138. for(size_t classNum = 0; classNum < lastClass; classNum++)
  139. {
  140. m_Makefile->AddSource(m_WrapClasses[classNum]);
  141. std::vector<std::string> args;
  142. args.push_back(m_WrapHeaders[classNum]);
  143. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  144. {
  145. args.push_back(hints);
  146. }
  147. args.push_back((m_WrapClasses[classNum].GetPropertyAsBool("ABSTRACT") ? "0" : "1"));
  148. std::string res = m_Makefile->GetCurrentOutputDirectory();
  149. res += "/";
  150. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  151. args.push_back(res);
  152. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  153. wtcl.c_str(), args, depends,
  154. res.c_str(), m_LibraryName.c_str());
  155. }
  156. }
  157. bool cmVTKWrapTclCommand::CreateInitFile(std::string& res)
  158. {
  159. /* we have to make sure that the name is the correct case */
  160. std::string kitName = cmSystemTools::Capitalized(m_LibraryName);
  161. std::vector<std::string> classes;
  162. size_t lastClass = m_WrapHeaders.size();
  163. size_t classNum;
  164. for(classNum = 0; classNum < lastClass; classNum++)
  165. {
  166. if (!m_WrapClasses[classNum].GetPropertyAsBool("ABSTRACT"))
  167. {
  168. std::string cls = m_WrapHeaders[classNum];
  169. cls = cls.substr(0,cls.size()-2);
  170. std::string::size_type pos = cls.rfind('/');
  171. if(pos != std::string::npos)
  172. {
  173. cls = cls.substr(pos+1);
  174. }
  175. classes.push_back(cls);
  176. }
  177. }
  178. // open the init file
  179. std::string outFileName =
  180. m_Makefile->GetCurrentOutputDirectory();
  181. outFileName += "/" + res;
  182. return this->WriteInit(kitName.c_str(), outFileName, classes);
  183. }
  184. /* warning this code is also in getclasses.cxx under pcmaker */
  185. bool cmVTKWrapTclCommand::WriteInit(const char *kitName,
  186. std::string& outFileName,
  187. std::vector<std::string>& classes)
  188. {
  189. unsigned int i;
  190. std::string tempOutputFile = outFileName + ".tmp";
  191. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  192. if (!fout)
  193. {
  194. cmSystemTools::Error("Failed to open TclInit file for ", tempOutputFile.c_str());
  195. return false;
  196. }
  197. // capitalized commands just once
  198. std::vector<std::string> capcommands;
  199. for (i = 0; i < m_Commands.size(); i++)
  200. {
  201. capcommands.push_back(cmSystemTools::Capitalized(m_Commands[i]));
  202. }
  203. fprintf(fout,"#include \"vtkTclUtil.h\"\n");
  204. fprintf(fout,"#include \"vtkVersion.h\"\n");
  205. fprintf(fout,"#define VTK_TCL_TO_STRING(x) VTK_TCL_TO_STRING0(x)\n");
  206. fprintf(fout,"#define VTK_TCL_TO_STRING0(x) #x\n");
  207. fprintf(fout,
  208. "extern \"C\"\n"
  209. "{\n"
  210. "#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4) && (TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)\n"
  211. " typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, CONST84 char *[]);\n"
  212. "#else\n"
  213. " typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, char *[]);\n"
  214. "#endif\n"
  215. "}\n"
  216. "\n");
  217. for (i = 0; i < classes.size(); i++)
  218. {
  219. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  220. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  221. }
  222. if (!strcmp(kitName,"Vtkcommontcl"))
  223. {
  224. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n"
  225. " int argc, char *argv[]);\n");
  226. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  227. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  228. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  229. fprintf(fout,"int vtkCommandForward(ClientData cd, Tcl_Interp *interp,\n"
  230. " int argc, char *argv[]){\n"
  231. " return vtkCommand(cd, interp, argc, argv);\n"
  232. "}\n");
  233. }
  234. else
  235. {
  236. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  237. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  238. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  239. }
  240. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  241. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  242. for (i = 0; i < m_Commands.size(); i++)
  243. {
  244. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  245. capcommands[i].c_str());
  246. }
  247. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
  248. kitName);
  249. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  250. kitName);
  251. /* create an extern ref to the generic delete function */
  252. fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
  253. if (!strcmp(kitName,"Vtkcommontcl"))
  254. {
  255. fprintf(fout,"extern \"C\"\n{\nvoid vtkCommonDeleteAssocData(ClientData cd)\n");
  256. fprintf(fout," {\n");
  257. fprintf(fout," vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
  258. fprintf(fout," delete tis;\n }\n}\n");
  259. }
  260. /* the main declaration */
  261. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  262. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  263. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  264. kitName);
  265. if (!strcmp(kitName,"Vtkcommontcl"))
  266. {
  267. fprintf(fout,
  268. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  269. fprintf(fout,
  270. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  271. fprintf(fout,"\n");
  272. fprintf(fout,"\n");
  273. fprintf(fout,
  274. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  275. fprintf(fout,
  276. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  277. fprintf(fout,
  278. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  279. fprintf(fout,
  280. " Tcl_SetAssocData(interp,(char *) \"vtk\",NULL,(ClientData *)info);\n");
  281. fprintf(fout,
  282. " Tcl_CreateExitHandler(vtkCommonDeleteAssocData,(ClientData *)info);\n");
  283. /* create special vtkCommand command */
  284. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",\n"
  285. " reinterpret_cast<vtkTclCommandType>(vtkCommandForward),\n"
  286. " (ClientData *)NULL, NULL);\n\n");
  287. }
  288. for (i = 0; i < m_Commands.size(); i++)
  289. {
  290. fprintf(fout," %s_Init(interp);\n", capcommands[i].c_str());
  291. }
  292. fprintf(fout,"\n");
  293. for (i = 0; i < classes.size(); i++)
  294. {
  295. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  296. classes[i].c_str(), classes[i].c_str());
  297. fprintf(fout," %sCommand);\n",classes[i].c_str());
  298. }
  299. fprintf(fout," char pkgName[]=\"%s\";\n", m_LibraryName.c_str());
  300. fprintf(fout," char pkgVers[]=VTK_TCL_TO_STRING(VTK_MAJOR_VERSION)"
  301. " \".\" "
  302. "VTK_TCL_TO_STRING(VTK_MINOR_VERSION);\n");
  303. fprintf(fout," Tcl_PkgProvide(interp, pkgName, pkgVers);\n");
  304. fprintf(fout," return TCL_OK;\n}\n");
  305. fclose(fout);
  306. // copy the file if different
  307. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  308. outFileName.c_str());
  309. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  310. return true;
  311. }