cmVTKWrapPythonCommand.cxx 8.4 KB

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