1
0

cmVTKWrapPythonCommand.cxx 7.2 KB

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