1
0

cmVTKWrapPythonCommand.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmVTKWrapPythonCommand.h"
  33. // cmVTKWrapPythonCommand
  34. bool cmVTKWrapPythonCommand::InitialPass(std::vector<std::string> const& args)
  35. {
  36. if(args.size() < 3 )
  37. {
  38. this->SetError("called with incorrect number of arguments");
  39. return false;
  40. }
  41. // Now check and see if the value has been stored in the cache
  42. // already, if so use that value and don't look for the program
  43. if(!m_Makefile->IsOn("VTK_WRAP_PYTHON"))
  44. {
  45. return true;
  46. }
  47. // what is the current source dir
  48. std::string cdir = m_Makefile->GetCurrentDirectory();
  49. // keep the library name
  50. m_LibraryName = args[0];
  51. m_SourceList = args[1];
  52. // get the list of classes for this library
  53. cmMakefile::SourceMap &Classes = m_Makefile->GetSources();
  54. for(std::vector<std::string>::const_iterator j = (args.begin() + 2);
  55. j != args.end(); ++j)
  56. {
  57. cmMakefile::SourceMap::iterator l = Classes.find(*j);
  58. if (l == Classes.end())
  59. {
  60. this->SetError("bad source list passed to VTKWrapPythonCommand");
  61. return false;
  62. }
  63. for(std::vector<cmSourceFile>::iterator i = l->second.begin();
  64. i != l->second.end(); i++)
  65. {
  66. cmSourceFile &curr = *i;
  67. // if we should wrap the class
  68. if (!curr.GetWrapExclude())
  69. {
  70. cmSourceFile file;
  71. file.SetIsAnAbstractClass(curr.IsAnAbstractClass());
  72. std::string newName = curr.GetSourceName() + "Python";
  73. file.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  74. "cxx",false);
  75. std::string hname = cdir + "/" + curr.GetSourceName() + ".h";
  76. m_WrapHeaders.push_back(hname);
  77. // add starting depends
  78. file.GetDepends().push_back(hname);
  79. m_WrapClasses.push_back(file);
  80. }
  81. }
  82. }
  83. return true;
  84. }
  85. void cmVTKWrapPythonCommand::FinalPass()
  86. {
  87. // first we add the rules for all the .h to Python.cxx files
  88. int 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. // Create the init file
  94. std::string res = m_LibraryName;
  95. res += "Init.cxx";
  96. this->CreateInitFile(res);
  97. // add the init file
  98. cmSourceFile cfile;
  99. cfile.SetIsAnAbstractClass(false);
  100. std::string newName = m_LibraryName;
  101. newName += "Init";
  102. cfile.SetName(newName.c_str(), m_Makefile->GetCurrentOutputDirectory(),
  103. "cxx",false);
  104. m_Makefile->AddSource(cfile,m_SourceList.c_str());
  105. // wrap all the .h files
  106. depends.push_back(wpython);
  107. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  108. {
  109. depends.push_back(hints);
  110. }
  111. for(int classNum = 0; classNum < lastClass; classNum++)
  112. {
  113. m_Makefile->AddSource(m_WrapClasses[classNum],m_SourceList.c_str());
  114. std::string res = m_Makefile->GetCurrentOutputDirectory();
  115. res += "/";
  116. res += m_WrapClasses[classNum].GetSourceName() + ".cxx";
  117. std::vector<std::string> args;
  118. args.push_back(m_WrapHeaders[classNum]);
  119. if (strcmp("${VTK_WRAP_HINTS}",hints.c_str()))
  120. {
  121. args.push_back(hints);
  122. }
  123. args.push_back((m_WrapClasses[classNum].IsAnAbstractClass() ? "0" : "1"));
  124. args.push_back(res);
  125. m_Makefile->AddCustomCommand(m_WrapHeaders[classNum].c_str(),
  126. wpython.c_str(), args, depends,
  127. res.c_str(), m_LibraryName.c_str());
  128. }
  129. }
  130. bool cmVTKWrapPythonCommand::CreateInitFile(std::string& res)
  131. {
  132. std::vector<std::string> classes;
  133. int lastClass = m_WrapHeaders.size();
  134. int classNum;
  135. for(classNum = 0; classNum < lastClass; classNum++)
  136. {
  137. std::string cls = m_WrapHeaders[classNum];
  138. cls = cls.substr(0,cls.size()-2);
  139. std::string::size_type pos = cls.rfind('/');
  140. if(pos != std::string::npos)
  141. {
  142. cls = cls.substr(pos+1);
  143. }
  144. classes.push_back(cls);
  145. }
  146. // open the init file
  147. std::string outFileName =
  148. m_Makefile->GetCurrentOutputDirectory();
  149. outFileName += "/" + res;
  150. return this->WriteInit(m_LibraryName.c_str(), outFileName, classes);
  151. }
  152. /* warning this code is also in getclasses.cxx under pcmaker */
  153. bool cmVTKWrapPythonCommand::WriteInit(const char *kitName,
  154. std::string& outFileName,
  155. std::vector<std::string>& classes)
  156. {
  157. unsigned int i;
  158. std::string tempOutputFile = outFileName + ".tmp";
  159. FILE *fout = fopen(tempOutputFile.c_str(),"w");
  160. if (!fout)
  161. {
  162. return false;
  163. }
  164. fprintf(fout,"#include <string.h>\n");
  165. fprintf(fout,"#include \"Python.h\"\n\n");
  166. for (i = 0; i < classes.size(); i++)
  167. {
  168. #ifdef _WIN32
  169. fprintf(fout,"extern \"C\" {__declspec( dllexport) PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  170. #else
  171. fprintf(fout,"extern \"C\" {PyObject *PyVTKClass_%sNew(char *); }\n",classes[i].c_str());
  172. #endif
  173. }
  174. fprintf(fout,"\nstatic PyMethodDef Py%s_ClassMethods[] = {\n",
  175. kitName);
  176. fprintf(fout,"{NULL, NULL}};\n\n");
  177. #ifdef _WIN32
  178. fprintf(fout,"extern \"C\" {__declspec( dllexport) void init%s();}\n\n",kitName);
  179. fprintf(fout,"void init%s()\n{\n",kitName);
  180. #else
  181. fprintf(fout,"extern \"C\" {void initlib%s();}\n\n",kitName);
  182. fprintf(fout,"void initlib%s()\n{\n",kitName);
  183. #endif
  184. /* module init function */
  185. fprintf(fout," PyObject *m, *d, *c;\n\n");
  186. #ifdef _WIN32
  187. fprintf(fout," static char modulename[] = \"%s\";\n",kitName);
  188. #else
  189. fprintf(fout," static char modulename[] = \"lib%s\";\n",kitName);
  190. #endif
  191. fprintf(fout," m = Py_InitModule(modulename, Py%s_ClassMethods);\n",
  192. kitName);
  193. fprintf(fout," d = PyModule_GetDict(m);\n");
  194. fprintf(fout," if (!d) Py_FatalError(\"can't get dictionary for module %s!\");\n\n",
  195. kitName);
  196. for (i = 0; i < classes.size(); i++)
  197. {
  198. fprintf(fout," if ((c = PyVTKClass_%sNew(modulename)))\n",
  199. classes[i].c_str());
  200. fprintf(fout," if (-1 == PyDict_SetItemString(d, \"%s\", c))\n",
  201. classes[i].c_str());
  202. fprintf(fout," Py_FatalError(\"can't add class %s to dictionary!\");\n\n",
  203. classes[i].c_str());
  204. }
  205. fprintf(fout,"}\n\n");
  206. fclose(fout);
  207. // copy the file if different
  208. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  209. outFileName.c_str());
  210. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  211. return true;
  212. }