cmVTKWrapPythonCommand.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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&
  16. argsIn)
  17. {
  18. if(argsIn.size() < 3 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. std::vector<std::string> args;
  24. m_Makefile->ExpandSourceListArguments(argsIn, args, 2);
  25. // Now check and see if the value has been stored in the cache
  26. // already, if so use that value and don't look for the program
  27. if(!m_Makefile->IsOn("VTK_WRAP_PYTHON"))
  28. {
  29. return true;
  30. }
  31. // what is the current source dir
  32. std::string cdir = m_Makefile->GetCurrentDirectory();
  33. // keep the library name
  34. m_LibraryName = args[0];
  35. m_SourceList = args[1];
  36. std::string sourceListValue;
  37. // was the list already populated
  38. const char *def = m_Makefile->GetDefinition(m_SourceList.c_str());
  39. if (def)
  40. {
  41. sourceListValue = def;
  42. sourceListValue += ";";
  43. }
  44. // Create the init file
  45. std::string res = m_LibraryName;
  46. res += "Init.cxx";
  47. // add the init file
  48. std::string initName = m_LibraryName;
  49. initName += "Init";
  50. sourceListValue += initName + ".cxx";
  51. // get the list of classes for this library
  52. for(std::vector<std::string>::iterator j = (args.begin() + 2);
  53. j != args.end(); ++j)
  54. {
  55. cmSourceFile *curr = m_Makefile->GetSource(j->c_str());
  56. // if we should wrap the class
  57. if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE"))
  58. {
  59. cmSourceFile file;
  60. if (curr)
  61. {
  62. file.SetProperty("ABSTRACT",curr->GetProperty("ABSTRACT"));
  63. }
  64. std::string srcName = cmSystemTools::GetFilenameWithoutExtension(*j);
  65. std::string newName = srcName + "Python";
  66. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  67. "cxx",false);
  68. std::string hname = cdir + "/" + srcName + ".h";
  69. m_WrapHeaders.push_back(hname);
  70. // add starting depends
  71. file.GetDepends().push_back(hname);
  72. m_WrapClasses.push_back(file);
  73. sourceListValue += ";";
  74. sourceListValue += newName + ".cxx";
  75. }
  76. }
  77. cmSourceFile cfile;
  78. cfile.SetProperty("ABSTRACT","0");
  79. this->CreateInitFile(res);
  80. cfile.SetName(initName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  81. "cxx",false);
  82. m_Makefile->AddSource(cfile);
  83. m_Makefile->AddDefinition(m_SourceList.c_str(), sourceListValue.c_str());
  84. return true;
  85. }
  86. void cmVTKWrapPythonCommand::FinalPass()
  87. {
  88. // first we add the rules for all the .h to Python.cxx files
  89. size_t lastClass = m_WrapClasses.size();
  90. std::vector<std::string> depends;
  91. const char* wpython = m_Makefile->
  92. GetRequiredDefinition("VTK_WRAP_PYTHON_EXE");
  93. const char* hints = m_Makefile->GetDefinition("VTK_WRAP_HINTS");
  94. // wrap all the .h files
  95. depends.push_back(wpython);
  96. if(hints)
  97. {
  98. depends.push_back(hints);
  99. }
  100. for(size_t classNum = 0; classNum < lastClass; classNum++)
  101. {
  102. m_Makefile->AddSource(m_WrapClasses[classNum]);
  103. std::string res = m_Makefile->GetCurrentOutputDirectory();
  104. res += "/";
  105. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  106. cmCustomCommandLine commandLine;
  107. commandLine.push_back(wpython);
  108. commandLine.push_back(m_WrapHeaders[classNum]);
  109. if(hints)
  110. {
  111. commandLine.push_back(hints);
  112. }
  113. commandLine.push_back(
  114. (m_WrapClasses[classNum].GetPropertyAsBool("ABSTRACT") ? "0" : "1"));
  115. commandLine.push_back(res);
  116. cmCustomCommandLines commandLines;
  117. commandLines.push_back(commandLine);
  118. std::vector<std::string> outputs;
  119. outputs.push_back(res);
  120. const char* no_comment = 0;
  121. m_Makefile->AddCustomCommandOldStyle(m_LibraryName.c_str(),
  122. outputs,
  123. depends,
  124. m_WrapHeaders[classNum].c_str(),
  125. commandLines,
  126. no_comment);
  127. }
  128. }
  129. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  130. {
  131. std::vector<std::string> classes;
  132. size_t lastClass = m_WrapHeaders.size();
  133. size_t classNum;
  134. for(classNum = 0; classNum < lastClass; classNum++)
  135. {
  136. std::string cls = m_WrapHeaders[classNum];
  137. cls = cls.substr(0,cls.size()-2);
  138. std::string::size_type pos = cls.rfind('/');
  139. if(pos != std::string::npos)
  140. {
  141. cls = cls.substr(pos+1);
  142. }
  143. classes.push_back(cls);
  144. }
  145. // open the init file
  146. std::string outFileName =
  147. m_Makefile->GetCurrentOutputDirectory();
  148. outFileName += "/" + res;
  149. return this->WriteInit(m_LibraryName.c_str(), outFileName, classes);
  150. }
  151. /* warning this code is also in getclasses.cxx under pcmaker */
  152. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  153. std::string& outFileName,
  154. std::vector<std::string>& classes)
  155. {
  156. unsigned int i;
  157. std::string tempOutputFile = outFileName + ".tmp";
  158. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  159. if (!fout)
  160. {
  161. cmSystemTools::ReportLastSystemError("cmVTKWrapPythonCommand error:");
  162. return false;
  163. }
  164. fprintf(fout,"// Generated by cmVTKWrapPythonCommand in CMake\n\n");
  165. fprintf(fout,"#include <string.h>\n");
  166. fprintf(fout,"#include \"Python.h\"\n\n");
  167. fprintf(fout,"// Handle compiler warning messages, etc.\n"
  168. "#if defined( _MSC_VER ) && !defined(VTK_DISPLAY_WIN32_WARNINGS)\n"
  169. "#pragma warning ( disable : 4706 )\n"
  170. "#endif // Windows Warnings \n\n");
  171. for (i = 0; i < classes.size(); i++)
  172. {
  173. #ifdef _WIN32
  174. fprintf(fout,
  175. "extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  176. #else
  177. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",
  178. classes[i].c_str());
  179. #endif
  180. }
  181. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  182. kitName);
  183. fprintf(fout,"{NULL, NULL, 0, NULL}};\n\n");
  184. #ifdef _WIN32
  185. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",
  186. kitName);
  187. fprintf(fout,"void init%s()\n{\n",kitName);
  188. #else
  189. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  190. fprintf(fout,"void initlib%s()\n{\n",kitName);
  191. #endif
  192. /* module init function */
  193. fprintf(fout," PyObject *m, *d, *c;\n\n");
  194. #ifdef _WIN32
  195. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  196. #else
  197. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  198. #endif
  199. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  200. kitName);
  201. fprintf(fout," d = PyModule_GetDict(m);\n");
  202. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  203. kitName);
  204. for (i = 0; i < classes.size(); i++)
  205. {
  206. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  207. classes[i].c_str());
  208. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  209. classes[i].c_str());
  210. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  211. classes[i].c_str());
  212. }
  213. fprintf(fout,"}\n\n");
  214. fclose(fout);
  215. // copy the file if different
  216. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  217. outFileName.c_str());
  218. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  219. return true;
  220. }