cmVTKWrapPythonCommand.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. this->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(!this->Makefile->IsOn("VTK_WRAP_PYTHON"))
  28. {
  29. return true;
  30. }
  31. // what is the current source dir
  32. std::string cdir = this->Makefile->GetCurrentDirectory();
  33. // keep the library name
  34. this->LibraryName = args[0];
  35. this->SourceList = args[1];
  36. std::string sourceListValue;
  37. // was the list already populated
  38. const char *def = this->Makefile->GetDefinition(this->SourceList.c_str());
  39. if (def)
  40. {
  41. sourceListValue = def;
  42. sourceListValue += ";";
  43. }
  44. // Create the init file
  45. std::string res = this->LibraryName;
  46. res += "Init.cxx";
  47. // add the init file
  48. std::string initName = this->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 = this->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(),
  67. this->Makefile->GetCurrentOutputDirectory(),
  68. "cxx",false);
  69. std::string hname = cdir + "/" + srcName + ".h";
  70. this->WrapHeaders.push_back(hname);
  71. // add starting depends
  72. file.GetDepends().push_back(hname);
  73. this->WrapClasses.push_back(file);
  74. sourceListValue += ";";
  75. sourceListValue += newName + ".cxx";
  76. }
  77. }
  78. cmSourceFile cfile;
  79. cfile.SetProperty("ABSTRACT","0");
  80. this->CreateInitFile(res);
  81. cfile.SetName(initName.c_str(), this->Makefile->GetCurrentOutputDirectory(),
  82. "cxx",false);
  83. this->Makefile->AddSource(cfile);
  84. this->Makefile->AddDefinition(this->SourceList.c_str(),
  85. sourceListValue.c_str());
  86. return true;
  87. }
  88. void cmVTKWrapPythonCommand::FinalPass()
  89. {
  90. // first we add the rules for all the .h to Python.cxx files
  91. size_t lastClass = this->WrapClasses.size();
  92. std::vector<std::string> depends;
  93. const char* wpython =
  94. this->Makefile->GetRequiredDefinition("VTK_WRAP_PYTHON_EXE");
  95. const char* hints = this->Makefile->GetDefinition("VTK_WRAP_HINTS");
  96. // wrap all the .h files
  97. depends.push_back(wpython);
  98. if(hints)
  99. {
  100. depends.push_back(hints);
  101. }
  102. for(size_t classNum = 0; classNum < lastClass; classNum++)
  103. {
  104. this->Makefile->AddSource(this->WrapClasses[classNum]);
  105. std::string res = this->Makefile->GetCurrentOutputDirectory();
  106. res += "/";
  107. res += this->WrapClasses[classNum].GetSourceName() + ".cxx";
  108. cmCustomCommandLine commandLine;
  109. commandLine.push_back(wpython);
  110. commandLine.push_back(this->WrapHeaders[classNum]);
  111. if(hints)
  112. {
  113. commandLine.push_back(hints);
  114. }
  115. commandLine.push_back((this->WrapClasses[classNum].
  116. GetPropertyAsBool("ABSTRACT") ? "0" : "1"));
  117. commandLine.push_back(res);
  118. cmCustomCommandLines commandLines;
  119. commandLines.push_back(commandLine);
  120. std::vector<std::string> outputs;
  121. outputs.push_back(res);
  122. const char* no_comment = 0;
  123. this->Makefile->AddCustomCommandOldStyle(this->LibraryName.c_str(),
  124. outputs,
  125. depends,
  126. this->WrapHeaders[classNum].c_str(),
  127. commandLines,
  128. no_comment);
  129. }
  130. }
  131. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  132. {
  133. std::vector<std::string> classes;
  134. size_t lastClass = this->WrapHeaders.size();
  135. size_t classNum;
  136. for(classNum = 0; classNum < lastClass; classNum++)
  137. {
  138. std::string cls = this->WrapHeaders[classNum];
  139. cls = cls.substr(0,cls.size()-2);
  140. std::string::size_type pos = cls.rfind('/');
  141. if(pos != std::string::npos)
  142. {
  143. cls = cls.substr(pos+1);
  144. }
  145. classes.push_back(cls);
  146. }
  147. // open the init file
  148. std::string outFileName =
  149. this->Makefile->GetCurrentOutputDirectory();
  150. outFileName += "/" + res;
  151. return this->WriteInit(this->LibraryName.c_str(), outFileName, classes);
  152. }
  153. /* warning this code is also in getclasses.cxx under pcmaker */
  154. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  155. std::string& outFileName,
  156. std::vector<std::string>& classes)
  157. {
  158. unsigned int i;
  159. std::string tempOutputFile = outFileName + ".tmp";
  160. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  161. if (!fout)
  162. {
  163. cmSystemTools::ReportLastSystemError("cmVTKWrapPythonCommand error:");
  164. return false;
  165. }
  166. fprintf(fout,"// Generated by cmVTKWrapPythonCommand in CMake\n\n");
  167. fprintf(fout,"#include <string.h>\n");
  168. fprintf(fout,"#include \"Python.h\"\n\n");
  169. fprintf(fout,"// Handle compiler warning messages, etc.\n"
  170. "#if defined( _MSC_VER ) && !defined(VTK_DISPLAY_WIN32_WARNINGS)\n"
  171. "#pragma warning ( disable : 4706 )\n"
  172. "#endif // Windows Warnings \n\n");
  173. for (i = 0; i < classes.size(); i++)
  174. {
  175. #ifdef _WIN32
  176. fprintf(fout, "extern \"C\" {__declspec( dllexport) "
  177. "PyObject *PyVTKClass_%sNew(char *); }\n", classes[i].c_str());
  178. #else
  179. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",
  180. classes[i].c_str());
  181. #endif
  182. }
  183. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  184. kitName);
  185. fprintf(fout,"{NULL, NULL, 0, NULL}};\n\n");
  186. #ifdef _WIN32
  187. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",
  188. kitName);
  189. fprintf(fout,"void init%s()\n{\n",kitName);
  190. #else
  191. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  192. fprintf(fout,"void initlib%s()\n{\n",kitName);
  193. #endif
  194. /* module init function */
  195. fprintf(fout," PyObject *m, *d, *c;\n\n");
  196. #ifdef _WIN32
  197. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  198. #else
  199. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  200. #endif
  201. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  202. kitName);
  203. fprintf(fout," d = PyModule_GetDict(m);\n");
  204. fprintf(fout," if (!d) Py_FatalError"
  205. "(\"can't get dictionary for module %s!\");\n\n", kitName);
  206. for (i = 0; i < classes.size(); i++)
  207. {
  208. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  209. classes[i].c_str());
  210. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  211. classes[i].c_str());
  212. fprintf(fout,
  213. " Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  214. classes[i].c_str());
  215. }
  216. fprintf(fout,"}\n\n");
  217. fclose(fout);
  218. // copy the file if different
  219. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  220. outFileName.c_str());
  221. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  222. return true;
  223. }