1
0

cmVTKWrapPythonCommand.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmVTKWrapPythonCommand.h"
  14. // cmVTKWrapPythonCommand
  15. bool cmVTKWrapPythonCommand::InitialPass(std::vector<std::string> const& args)
  16. {
  17. if(args.size() < 3 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // Now check and see if the value has been stored in the cache
  23. // already, if so use that value and don't look for the program
  24. if(!m_Makefile->IsOn("VTK_WRAP_PYTHON"))
  25. {
  26. return true;
  27. }
  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>::const_iterator j = (args.begin() + 2);
  36. j != args.end(); ++j)
  37. {
  38. cmMakefile::SourceMap::iterator l = Classes.find(*j);
  39. if (l == Classes.end())
  40. {
  41. this->SetError("bad source list passed to VTKWrapPythonCommand");
  42. return false;
  43. }
  44. for(std::vector<cmSourceFile>::iterator i = l->second.begin();
  45. i != l->second.end(); i++)
  46. {
  47. cmSourceFile &curr = *i;
  48. // if we should wrap the class
  49. if (!curr.GetWrapExclude())
  50. {
  51. cmSourceFile file;
  52. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  53. std::string newName = curr.GetSourceName() + "Python";
  54. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  55. "cxx",false);
  56. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  57. m_WrapHeaders.push_back(hname);
  58. // add starting depends
  59. file.GetDepends().push_back(hname);
  60. m_WrapClasses.push_back(file);
  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. size_t 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. m_Makefile->ExpandVariablesInString(hints);
  74. // Create the init file
  75. std::string res = m_LibraryName;
  76. res += "Init.cxx";
  77. this->CreateInitFile(res);
  78. // add the init file
  79. cmSourceFile cfile;
  80. cfile.SetIsAnAbstractClass(false);
  81. std::string newName = m_LibraryName;
  82. newName += "Init";
  83. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  84. "cxx",false);
  85. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  86. // wrap all the .h files
  87. depends.push_back(wpython);
  88. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  89. {
  90. depends.push_back(hints);
  91. }
  92. for(size_t classNum = 0; classNum < lastClass; classNum++)
  93. {
  94. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  95. std::string res = m_Makefile->GetCurrentOutputDirectory();
  96. res += "/";
  97. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  98. std::vector<std::string> args;
  99. args.push_back(m_WrapHeaders[classNum]);
  100. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  101. {
  102. args.push_back(hints);
  103. }
  104. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  105. args.push_back(res);
  106. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  107. wpython.c_str(), args, depends,
  108. res.c_str(), m_LibraryName.c_str());
  109. }
  110. }
  111. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  112. {
  113. std::vector<std::string> classes;
  114. size_t lastClass = m_WrapHeaders.size();
  115. size_t classNum;
  116. for(classNum = 0; classNum < lastClass; classNum++)
  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. // open the init file
  128. std::string outFileName =
  129. m_Makefile->GetCurrentOutputDirectory();
  130. outFileName += "/" + res;
  131. return this->WriteInit(m_LibraryName.c_str(), outFileName, classes);
  132. }
  133. /* warning this code is also in getclasses.cxx under pcmaker */
  134. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  135. std::string& outFileName,
  136. std::vector<std::string>& classes)
  137. {
  138. unsigned int i;
  139. std::string tempOutputFile = outFileName + ".tmp";
  140. FILE *fout = fopen(tempOutputFile.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. #ifdef _WIN32
  150. fprintf(fout,"extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  151. #else
  152. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  153. #endif
  154. }
  155. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  156. kitName);
  157. fprintf(fout,"{NULL, NULL}};\n\n");
  158. #ifdef _WIN32
  159. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",kitName);
  160. fprintf(fout,"void init%s()\n{\n",kitName);
  161. #else
  162. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  163. fprintf(fout,"void initlib%s()\n{\n",kitName);
  164. #endif
  165. /* module init function */
  166. fprintf(fout," PyObject *m, *d, *c;\n\n");
  167. #ifdef _WIN32
  168. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  169. #else
  170. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  171. #endif
  172. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  173. kitName);
  174. fprintf(fout," d = PyModule_GetDict(m);\n");
  175. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  176. kitName);
  177. for (i = 0; i < classes.size(); i++)
  178. {
  179. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  180. classes[i].c_str());
  181. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  182. classes[i].c_str());
  183. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  184. classes[i].c_str());
  185. }
  186. fprintf(fout,"}\n\n");
  187. fclose(fout);
  188. // copy the file if different
  189. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  190. outFileName.c_str());
  191. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  192. return true;
  193. }