cmVTKWrapPythonCommand.cxx 7.9 KB

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