cmVTKWrapPythonCommand.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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& 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. cmSystemTools::ExpandListArguments(argsIn, args);
  24. // Now check and see if the value has been stored in the cache
  25. // already, if so use that value and don't look for the program
  26. if(!m_Makefile->IsOn("VTK_WRAP_PYTHON"))
  27. {
  28. return true;
  29. }
  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>::const_iterator j = (args.begin() + 2);
  38. j != args.end(); ++j)
  39. {
  40. cmMakefile::SourceMap::iterator l = Classes.find(*j);
  41. if (l == Classes.end())
  42. {
  43. this->SetError("bad source list passed to VTKWrapPythonCommand");
  44. return false;
  45. }
  46. for(std::vector<cmSourceFile*>::iterator i = l->second.begin();
  47. i != l->second.end(); i++)
  48. {
  49. cmSourceFile &curr = *(*i);
  50. // if we should wrap the class
  51. if (!curr.GetWrapExclude())
  52. {
  53. cmSourceFile file;
  54. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  55. std::string newName = curr.GetSourceName() + "Python";
  56. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  57. "cxx",false);
  58. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  59. m_WrapHeaders.push_back(hname);
  60. // add starting depends
  61. file.GetDepends().push_back(hname);
  62. m_WrapClasses.push_back(file);
  63. }
  64. }
  65. }
  66. return true;
  67. }
  68. void cmVTKWrapPythonCommand::FinalPass()
  69. {
  70. // first we add the rules for all the .h to Python.cxx files
  71. size_t lastClass = m_WrapClasses.size();
  72. std::vector<std::string> depends;
  73. std::string wpython = "${VTK_WRAP_PYTHON_EXE}";
  74. std::string hints = "${VTK_WRAP_HINTS}";
  75. m_Makefile->ExpandVariablesInString(hints);
  76. // Create the init file
  77. std::string res = m_LibraryName;
  78. res += "Init.cxx";
  79. this->CreateInitFile(res);
  80. // add the init file
  81. cmSourceFile cfile;
  82. cfile.SetIsAnAbstractClass(false);
  83. std::string newName = m_LibraryName;
  84. newName += "Init";
  85. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  86. "cxx",false);
  87. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  88. // wrap all the .h files
  89. depends.push_back(wpython);
  90. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  91. {
  92. depends.push_back(hints);
  93. }
  94. for(size_t classNum = 0; classNum < lastClass; classNum++)
  95. {
  96. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  97. std::string res = m_Makefile->GetCurrentOutputDirectory();
  98. res += "/";
  99. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  100. std::vector<std::string> args;
  101. args.push_back(m_WrapHeaders[classNum]);
  102. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  103. {
  104. args.push_back(hints);
  105. }
  106. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  107. args.push_back(res);
  108. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  109. wpython.c_str(), args, depends,
  110. res.c_str(), m_LibraryName.c_str());
  111. }
  112. }
  113. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  114. {
  115. std::vector<std::string> classes;
  116. size_t lastClass = m_WrapHeaders.size();
  117. size_t classNum;
  118. for(classNum = 0; classNum < lastClass; classNum++)
  119. {
  120. std::string cls = m_WrapHeaders[classNum];
  121. cls = cls.substr(0,cls.size()-2);
  122. std::string::size_type pos = cls.rfind('/');
  123. if(pos != std::string::npos)
  124. {
  125. cls = cls.substr(pos+1);
  126. }
  127. classes.push_back(cls);
  128. }
  129. // open the init file
  130. std::string outFileName =
  131. m_Makefile->GetCurrentOutputDirectory();
  132. outFileName += "/" + res;
  133. return this->WriteInit(m_LibraryName.c_str(), outFileName, classes);
  134. }
  135. /* warning this code is also in getclasses.cxx under pcmaker */
  136. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  137. std::string& outFileName,
  138. std::vector<std::string>& classes)
  139. {
  140. unsigned int i;
  141. std::string tempOutputFile = outFileName + ".tmp";
  142. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  143. if (!fout)
  144. {
  145. return false;
  146. }
  147. fprintf(fout,"#include <string.h>\n");
  148. fprintf(fout,"#include \"Python.h\"\n\n");
  149. for (i = 0; i < classes.size(); i++)
  150. {
  151. #ifdef _WIN32
  152. fprintf(fout,"extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  153. #else
  154. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  155. #endif
  156. }
  157. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  158. kitName);
  159. fprintf(fout,"{NULL, NULL}};\n\n");
  160. #ifdef _WIN32
  161. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",kitName);
  162. fprintf(fout,"void init%s()\n{\n",kitName);
  163. #else
  164. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  165. fprintf(fout,"void initlib%s()\n{\n",kitName);
  166. #endif
  167. /* module init function */
  168. fprintf(fout," PyObject *m, *d, *c;\n\n");
  169. #ifdef _WIN32
  170. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  171. #else
  172. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  173. #endif
  174. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  175. kitName);
  176. fprintf(fout," d = PyModule_GetDict(m);\n");
  177. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  178. kitName);
  179. for (i = 0; i < classes.size(); i++)
  180. {
  181. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  182. classes[i].c_str());
  183. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  184. classes[i].c_str());
  185. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  186. classes[i].c_str());
  187. }
  188. fprintf(fout,"}\n\n");
  189. fclose(fout);
  190. // copy the file if different
  191. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  192. outFileName.c_str());
  193. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  194. return true;
  195. }