cmVTKWrapPythonCommand.cxx 6.3 KB

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