cmVTKWrapTclCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. this->LibraryName = argsIn[0];
  25. if (argsIn[1] == std::string("SOURCES"))
  26. {
  27. this->Makefile->ExpandSourceListArguments(argsIn, args, 3);
  28. }
  29. else
  30. {
  31. this->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(!this->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. this->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 = this->Makefile->GetCurrentDirectory();
  70. // get the resulting source list name
  71. this->SourceList = sources[0];
  72. std::string sourceListValue;
  73. // was the list already populated
  74. const char *def = this->Makefile->GetDefinition(this->SourceList.c_str());
  75. if (def)
  76. {
  77. sourceListValue = def;
  78. sourceListValue += ";";
  79. }
  80. // Create the init file
  81. std::string res = this->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 = this->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(), this->Makefile->GetCurrentOutputDirectory(),
  102. "cxx",false);
  103. this->WrapHeaders.push_back(hname);
  104. // add starting depends
  105. file.GetDepends().push_back(hname);
  106. this->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 = this->LibraryName;
  115. newName += "Init";
  116. this->CreateInitFile(res);
  117. cfile.SetName(newName.c_str(), this->Makefile->GetCurrentOutputDirectory(),
  118. "cxx",false);
  119. this->Makefile->AddSource(cfile);
  120. this->Makefile->AddDefinition(this->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 = this->WrapClasses.size();
  128. std::vector<std::string> depends;
  129. const char* wtcl = this->Makefile->GetRequiredDefinition("VTK_WRAP_TCL_EXE");
  130. const char* hints = this->Makefile->GetDefinition("VTK_WRAP_HINTS");
  131. // wrap all the .h files
  132. depends.push_back(wtcl);
  133. if(hints)
  134. {
  135. depends.push_back(hints);
  136. }
  137. for(size_t classNum = 0; classNum < lastClass; classNum++)
  138. {
  139. this->Makefile->AddSource(this->WrapClasses[classNum]);
  140. cmCustomCommandLine commandLine;
  141. commandLine.push_back(wtcl);
  142. commandLine.push_back(this->WrapHeaders[classNum]);
  143. if(hints)
  144. {
  145. commandLine.push_back(hints);
  146. }
  147. commandLine.push_back((this->WrapClasses[classNum].
  148. GetPropertyAsBool("ABSTRACT") ? "0" : "1"));
  149. std::string res = this->Makefile->GetCurrentOutputDirectory();
  150. res += "/";
  151. res += this->WrapClasses[classNum].GetSourceName() + ".cxx";
  152. commandLine.push_back(res);
  153. cmCustomCommandLines commandLines;
  154. commandLines.push_back(commandLine);
  155. std::vector<std::string> outputs;
  156. outputs.push_back(res);
  157. const char* no_comment = 0;
  158. this->Makefile->AddCustomCommandOldStyle(this->LibraryName.c_str(),
  159. outputs,
  160. depends,
  161. this->WrapHeaders[classNum].c_str(),
  162. commandLines,
  163. no_comment);
  164. }
  165. }
  166. bool cmVTKWrapTclCommand::CreateInitFile(std::string& res)
  167. {
  168. /* we have to make sure that the name is the correct case */
  169. std::string kitName = cmSystemTools::Capitalized(this->LibraryName);
  170. std::vector<std::string> classes;
  171. size_t lastClass = this->WrapHeaders.size();
  172. size_t classNum;
  173. for(classNum = 0; classNum < lastClass; classNum++)
  174. {
  175. if (!this->WrapClasses[classNum].GetPropertyAsBool("ABSTRACT"))
  176. {
  177. std::string cls = this->WrapHeaders[classNum];
  178. cls = cls.substr(0,cls.size()-2);
  179. std::string::size_type pos = cls.rfind('/');
  180. if(pos != std::string::npos)
  181. {
  182. cls = cls.substr(pos+1);
  183. }
  184. classes.push_back(cls);
  185. }
  186. }
  187. // open the init file
  188. std::string outFileName =
  189. this->Makefile->GetCurrentOutputDirectory();
  190. outFileName += "/" + res;
  191. return this->WriteInit(kitName.c_str(), outFileName, classes);
  192. }
  193. /* warning this code is also in getclasses.cxx under pcmaker */
  194. bool cmVTKWrapTclCommand::WriteInit(const char *kitName,
  195. std::string& outFileName,
  196. std::vector<std::string>& classes)
  197. {
  198. unsigned int i;
  199. std::string tempOutputFile = outFileName + ".tmp";
  200. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  201. if (!fout)
  202. {
  203. cmSystemTools::Error("Failed to open TclInit file for ",
  204. tempOutputFile.c_str());
  205. cmSystemTools::ReportLastSystemError("");
  206. return false;
  207. }
  208. // capitalized commands just once
  209. std::vector<std::string> capcommands;
  210. for (i = 0; i < this->Commands.size(); i++)
  211. {
  212. capcommands.push_back(cmSystemTools::Capitalized(this->Commands[i]));
  213. }
  214. fprintf(fout,"#include \"vtkTclUtil.h\"\n");
  215. fprintf(fout,"#include \"vtkVersion.h\"\n");
  216. fprintf(fout,"#define VTK_TCL_TO_STRING(x) VTK_TCL_TO_STRING0(x)\n");
  217. fprintf(fout,"#define VTK_TCL_TO_STRING0(x) #x\n");
  218. fprintf(fout,
  219. "extern \"C\"\n"
  220. "{\n"
  221. "#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4) && (TCL_RELEASE_LEVEL >= TCL_FINAL_RELEASE)\n"
  222. " typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, CONST84 char *[]);\n"
  223. "#else\n"
  224. " typedef int (*vtkTclCommandType)(ClientData, Tcl_Interp *,int, char *[]);\n"
  225. "#endif\n"
  226. "}\n"
  227. "\n");
  228. for (i = 0; i < classes.size(); i++)
  229. {
  230. fprintf(fout,"int %sCommand(ClientData cd, Tcl_Interp *interp,\n int argc, char *argv[]);\n",classes[i].c_str());
  231. fprintf(fout,"ClientData %sNewCommand();\n",classes[i].c_str());
  232. }
  233. if (!strcmp(kitName,"Vtkcommontcl"))
  234. {
  235. fprintf(fout,"int vtkCommand(ClientData cd, Tcl_Interp *interp,\n"
  236. " int argc, char *argv[]);\n");
  237. fprintf(fout,"\nTcl_HashTable vtkInstanceLookup;\n");
  238. fprintf(fout,"Tcl_HashTable vtkPointerLookup;\n");
  239. fprintf(fout,"Tcl_HashTable vtkCommandLookup;\n");
  240. fprintf(fout,"int vtkCommandForward(ClientData cd, Tcl_Interp *interp,\n"
  241. " int argc, char *argv[]){\n"
  242. " return vtkCommand(cd, interp, argc, argv);\n"
  243. "}\n");
  244. }
  245. else
  246. {
  247. fprintf(fout,"\nextern Tcl_HashTable vtkInstanceLookup;\n");
  248. fprintf(fout,"extern Tcl_HashTable vtkPointerLookup;\n");
  249. fprintf(fout,"extern Tcl_HashTable vtkCommandLookup;\n");
  250. }
  251. fprintf(fout,"extern void vtkTclDeleteObjectFromHash(void *);\n");
  252. fprintf(fout,"extern void vtkTclListInstances(Tcl_Interp *interp, ClientData arg);\n");
  253. for (i = 0; i < this->Commands.size(); i++)
  254. {
  255. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  256. capcommands[i].c_str());
  257. }
  258. fprintf(fout,"\n\nextern \"C\" {int VTK_EXPORT %s_SafeInit(Tcl_Interp *interp);}\n",
  259. kitName);
  260. fprintf(fout,"\nextern \"C\" {int VTK_EXPORT %s_Init(Tcl_Interp *interp);}\n",
  261. kitName);
  262. /* create an extern ref to the generic delete function */
  263. fprintf(fout,"\nextern void vtkTclGenericDeleteObject(ClientData cd);\n");
  264. if (!strcmp(kitName,"Vtkcommontcl"))
  265. {
  266. fprintf(fout,"extern \"C\"\n{\nvoid vtkCommonDeleteAssocData(ClientData cd)\n");
  267. fprintf(fout," {\n");
  268. fprintf(fout," vtkTclInterpStruct *tis = static_cast<vtkTclInterpStruct*>(cd);\n");
  269. fprintf(fout," delete tis;\n }\n}\n");
  270. }
  271. /* the main declaration */
  272. fprintf(fout,"\n\nint VTK_EXPORT %s_SafeInit(Tcl_Interp *interp)\n{\n",kitName);
  273. fprintf(fout," return %s_Init(interp);\n}\n",kitName);
  274. fprintf(fout,"\n\nint VTK_EXPORT %s_Init(Tcl_Interp *interp)\n{\n",
  275. kitName);
  276. if (!strcmp(kitName,"Vtkcommontcl"))
  277. {
  278. fprintf(fout,
  279. " vtkTclInterpStruct *info = new vtkTclInterpStruct;\n");
  280. fprintf(fout,
  281. " info->Number = 0; info->InDelete = 0; info->DebugOn = 0;\n");
  282. fprintf(fout,"\n");
  283. fprintf(fout,"\n");
  284. fprintf(fout,
  285. " Tcl_InitHashTable(&info->InstanceLookup, TCL_STRING_KEYS);\n");
  286. fprintf(fout,
  287. " Tcl_InitHashTable(&info->PointerLookup, TCL_STRING_KEYS);\n");
  288. fprintf(fout,
  289. " Tcl_InitHashTable(&info->CommandLookup, TCL_STRING_KEYS);\n");
  290. fprintf(fout,
  291. " Tcl_SetAssocData(interp,(char *) \"vtk\",NULL,(ClientData *)info);\n");
  292. fprintf(fout,
  293. " Tcl_CreateExitHandler(vtkCommonDeleteAssocData,(ClientData *)info);\n");
  294. /* create special vtkCommand command */
  295. fprintf(fout," Tcl_CreateCommand(interp,(char *) \"vtkCommand\",\n"
  296. " reinterpret_cast<vtkTclCommandType>(vtkCommandForward),\n"
  297. " (ClientData *)NULL, NULL);\n\n");
  298. }
  299. for (i = 0; i < this->Commands.size(); i++)
  300. {
  301. fprintf(fout," %s_Init(interp);\n", capcommands[i].c_str());
  302. }
  303. fprintf(fout,"\n");
  304. for (i = 0; i < classes.size(); i++)
  305. {
  306. fprintf(fout," vtkTclCreateNew(interp,(char *) \"%s\", %sNewCommand,\n",
  307. classes[i].c_str(), classes[i].c_str());
  308. fprintf(fout," %sCommand);\n",classes[i].c_str());
  309. }
  310. fprintf(fout," char pkgName[]=\"%s\";\n", this->LibraryName.c_str());
  311. fprintf(fout," char pkgVers[]=VTK_TCL_TO_STRING(VTK_MAJOR_VERSION)"
  312. " \".\" "
  313. "VTK_TCL_TO_STRING(VTK_MINOR_VERSION);\n");
  314. fprintf(fout," Tcl_PkgProvide(interp, pkgName, pkgVers);\n");
  315. fprintf(fout," return TCL_OK;\n}\n");
  316. fclose(fout);
  317. // copy the file if different
  318. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  319. outFileName.c_str());
  320. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  321. return true;
  322. }