cmVTKWrapPythonCommand.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "cmVTKWrapPythonCommand.h"
  12. // cmVTKWrapPythonCommand
  13. bool cmVTKWrapPythonCommand::Invoke(std::vector<std::string>& args)
  14. {
  15. if(args.size() < 3 )
  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. if(!cmCacheManager::GetInstance()->IsOn("VTK_WRAP_PYTHON"))
  23. {
  24. return true;
  25. }
  26. // add in a depend in the vtkVTKWrapPython executable
  27. m_Makefile->AddUtility("vtkWrapPython");
  28. // what is the current source dir
  29. std::string cdir = m_Makefile->GetCurrentDirectory();
  30. // keep the library name
  31. m_LibraryName = args[0];
  32. m_SourceList = args[1];
  33. // get the list of classes for this library
  34. cmMakefile::SourceMap &Classes = m_Makefile->GetSources();
  35. for(std::vector<std::string>::iterator j = (args.begin() + 2);
  36. j != args.end(); ++j)
  37. {
  38. for(cmMakefile::SourceMap::iterator l = Classes.begin();
  39. l != Classes.end(); l++)
  40. {
  41. for(std::vector<cmSourceFile>::iterator i = l->second.begin();
  42. i != l->second.end(); i++)
  43. {
  44. cmSourceFile &curr = *i;
  45. // if we should wrap the class
  46. if (!curr.GetWrapExclude())
  47. {
  48. cmSourceFile file;
  49. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  50. std::string newName = curr.GetSourceName() + "Python";
  51. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  52. "cxx",false);
  53. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  54. m_WrapHeaders.push_back(hname);
  55. // add starting depends
  56. file.GetDepends().push_back(hname);
  57. m_WrapClasses.push_back(file);
  58. }
  59. }
  60. }
  61. }
  62. return true;
  63. }
  64. void cmVTKWrapPythonCommand::FinalPass()
  65. {
  66. // first we add the rules for all the .h to Python.cxx files
  67. int lastClass = m_WrapClasses.size();
  68. std::vector<std::string> depends;
  69. std::string wpython = "${VTK_WRAP_PYTHON_EXE}";
  70. std::string hints = "${VTK_WRAP_HINTS}";
  71. // Create the init file
  72. std::string res = m_LibraryName;
  73. res += "Init.cxx";
  74. this->CreateInitFile(res);
  75. // add the init file
  76. cmSourceFile cfile;
  77. cfile.SetIsAnAbstractClass(false);
  78. std::string newName = m_LibraryName;
  79. newName += "Init";
  80. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  81. "cxx",false);
  82. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  83. // wrap all the .h files
  84. depends.push_back(wpython);
  85. for(int classNum = 0; classNum < lastClass; classNum++)
  86. {
  87. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  88. std::string res = m_WrapClasses[classNum].GetSourceName() + ".cxx";
  89. std::string cmd = wpython + " " + m_WrapHeaders[classNum] + " "
  90. + hints + (m_WrapClasses[classNum].IsAnAbstractClass() ? " 0 " : " 1 ") + " > " + m_WrapClasses[classNum].GetSourceName() + ".cxx";
  91. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  92. cmd.c_str(), depends,
  93. res.c_str(), m_LibraryName.c_str());
  94. }
  95. }
  96. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  97. {
  98. unsigned int i;
  99. /* we have to make sure that the name is the correct case */
  100. std::string kitName = m_LibraryName;
  101. if (kitName[0] > 90) kitName[0] -= 32;
  102. for (i = 1; i < kitName.size(); i++)
  103. {
  104. if ((kitName[i] > 64)&&(kitName[i] < 91))
  105. {
  106. kitName[i] += 32;
  107. }
  108. }
  109. std::vector<std::string> classes;
  110. int lastClass = m_WrapHeaders.size();
  111. int classNum;
  112. for(classNum = 0; classNum < lastClass; classNum++)
  113. {
  114. if (!m_WrapClasses[classNum].IsAnAbstractClass())
  115. {
  116. std::string cls = m_WrapHeaders[classNum];
  117. cls = cls.substr(0,cls.size()-2);
  118. std::string::size_type pos = cls.rfind('/');
  119. if(pos != std::string::npos)
  120. {
  121. cls = cls.substr(pos+1);
  122. }
  123. classes.push_back(cls);
  124. }
  125. }
  126. // open the init file
  127. std::string outFileName =
  128. m_Makefile->GetCurrentOutputDirectory();
  129. outFileName += "/" + res;
  130. return this->WriteInit(kitName.c_str(), outFileName, classes);
  131. }
  132. /* warning this code is also in getclasses.cxx under pcmaker */
  133. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  134. 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 <string.h>\n");
  144. fprintf(fout,"#include \"Python.h\"\n\n");
  145. for (i = 0; i < classes.size(); i++)
  146. {
  147. fprintf(fout,"extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  148. }
  149. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  150. kitName);
  151. fprintf(fout,"{NULL, NULL}};\n\n");
  152. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",kitName);
  153. /* module init function */
  154. fprintf(fout,"void init%s()\n{\n",kitName);
  155. fprintf(fout," PyObject *m, *d, *c;\n\n");
  156. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  157. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  158. kitName);
  159. fprintf(fout," d = PyModule_GetDict(m);\n");
  160. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  161. kitName);
  162. for (i = 0; i < classes.size(); i++)
  163. {
  164. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  165. classes[i].c_str());
  166. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  167. classes[i].c_str());
  168. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  169. classes[i].c_str());
  170. }
  171. fprintf(fout,"}\n\n");
  172. fclose(fout);
  173. return true;
  174. }