cmVTKWrapPythonCommand.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. std::string sourceListValue;
  36. // was the list already populated
  37. const char *def = m_Makefile->GetDefinition(m_SourceList.c_str());
  38. if (def)
  39. {
  40. sourceListValue = def;
  41. sourceListValue += ";";
  42. }
  43. // Create the init file
  44. std::string res = m_LibraryName;
  45. res += "Init.cxx";
  46. this->CreateInitFile(res);
  47. // add the init file
  48. cmSourceFile cfile;
  49. cfile.SetIsAnAbstractClass(false);
  50. std::string newName = m_LibraryName;
  51. newName += "Init";
  52. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  53. "cxx",false);
  54. m_Makefile->AddSource(cfile);
  55. sourceListValue += newName + ".cxx";
  56. // get the list of classes for this library
  57. for(std::vector<std::string>::iterator j = (args.begin() + 2);
  58. j != args.end(); ++j)
  59. {
  60. cmSourceFile *curr = m_Makefile->GetSource(j->c_str());
  61. // if we should wrap the class
  62. if (!curr || !curr->GetWrapExclude())
  63. {
  64. cmSourceFile file;
  65. if (curr)
  66. {
  67. file.SetIsAnAbstractClass(curr->IsAnAbstractClass());
  68. }
  69. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*j);
  70. std::string newName = srcName + "Python";
  71. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  72. "cxx",false);
  73. std::string hname = cdir + "/" + srcName + ".h";
  74. m_WrapHeaders.push_back(hname);
  75. // add starting depends
  76. file.GetDepends().push_back(hname);
  77. m_WrapClasses.push_back(file);
  78. sourceListValue += ";";
  79. sourceListValue += newName + ".cxx";
  80. }
  81. }
  82. m_Makefile->AddDefinition(m_SourceList.c_str(), sourceListValue.c_str());
  83. return true;
  84. }
  85. void cmVTKWrapPythonCommand::FinalPass()
  86. {
  87. // first we add the rules for all the .h to Python.cxx files
  88. size_t lastClass = m_WrapClasses.size();
  89. std::vector<std::string> depends;
  90. std::string wpython = "${VTK_WRAP_PYTHON_EXE}";
  91. std::string hints = "${VTK_WRAP_HINTS}";
  92. m_Makefile->ExpandVariablesInString(hints);
  93. // wrap all the .h files
  94. depends.push_back(wpython);
  95. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  96. {
  97. depends.push_back(hints);
  98. }
  99. for(size_t classNum = 0; classNum < lastClass; classNum++)
  100. {
  101. m_Makefile->AddSource(m_WrapClasses[classNum]);
  102. std::string res = m_Makefile->GetCurrentOutputDirectory();
  103. res += "/";
  104. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  105. std::vector<std::string> args;
  106. args.push_back(m_WrapHeaders[classNum]);
  107. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  108. {
  109. args.push_back(hints);
  110. }
  111. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  112. args.push_back(res);
  113. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  114. wpython.c_str(), args, depends,
  115. res.c_str(), m_LibraryName.c_str());
  116. }
  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,"// Generated by cmVTKWrapPythonCommand in CMake\n\n");
  153. fprintf(fout,"#include <string.h>\n");
  154. fprintf(fout,"#include \"Python.h\"\n\n");
  155. fprintf(fout,"// Handle compiler warning messages, etc.\n"
  156. "#if defined( _MSC_VER ) && !defined(VTK_DISPLAY_WIN32_WARNINGS)\n"
  157. "#pragma warning ( disable : 4706 )\n"
  158. "#endif // Windows Warnings \n\n");
  159. for (i = 0; i < classes.size(); i++)
  160. {
  161. #ifdef _WIN32
  162. fprintf(fout,"extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  163. #else
  164. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  165. #endif
  166. }
  167. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  168. kitName);
  169. fprintf(fout,"{NULL, NULL, 0, NULL}};\n\n");
  170. #ifdef _WIN32
  171. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",kitName);
  172. fprintf(fout,"void init%s()\n{\n",kitName);
  173. #else
  174. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  175. fprintf(fout,"void initlib%s()\n{\n",kitName);
  176. #endif
  177. /* module init function */
  178. fprintf(fout," PyObject *m, *d, *c;\n\n");
  179. #ifdef _WIN32
  180. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  181. #else
  182. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  183. #endif
  184. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  185. kitName);
  186. fprintf(fout," d = PyModule_GetDict(m);\n");
  187. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  188. kitName);
  189. for (i = 0; i < classes.size(); i++)
  190. {
  191. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  192. classes[i].c_str());
  193. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  194. classes[i].c_str());
  195. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  196. classes[i].c_str());
  197. }
  198. fprintf(fout,"}\n\n");
  199. fclose(fout);
  200. // copy the file if different
  201. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  202. outFileName.c_str());
  203. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  204. return true;
  205. }