cmCableWrapTclCommand.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "cmCableWrapTclCommand.h"
  33. #include "cmCacheManager.h"
  34. #include "cmTarget.h"
  35. #include "cmGeneratedFileStream.h"
  36. cmCableWrapTclCommand::cmCableWrapTclCommand():
  37. m_CableClassSet(NULL)
  38. {
  39. }
  40. cmCableWrapTclCommand::~cmCableWrapTclCommand()
  41. {
  42. if(m_CableClassSet)
  43. {
  44. delete m_CableClassSet;
  45. }
  46. }
  47. // cmCableWrapTclCommand
  48. bool cmCableWrapTclCommand::Invoke(std::vector<std::string>& args)
  49. {
  50. if(args.size() < 2)
  51. {
  52. this->SetError("called with incorrect number of arguments");
  53. return false;
  54. }
  55. // Prepare to iterate through the arguments.
  56. std::vector<std::string>::const_iterator arg = args.begin();
  57. // The first argument is the name of the target.
  58. m_TargetName = *arg++;
  59. // Create the new class set.
  60. m_CableClassSet = new cmCableClassSet(m_TargetName.c_str());
  61. // Add all the regular entries.
  62. for(; (arg != args.end()) && (*arg != "SOURCES_BEGIN"); ++arg)
  63. {
  64. m_CableClassSet->ParseAndAddElement(arg->c_str(), m_Makefile);
  65. }
  66. // Add any sources that are associated with all the members.
  67. if(arg != args.end())
  68. {
  69. for(++arg; arg != args.end(); ++arg)
  70. {
  71. m_CableClassSet->AddSource(arg->c_str());
  72. }
  73. }
  74. this->GenerateCableFiles();
  75. // Add the source list to the target.
  76. m_Makefile->GetTargets()[m_TargetName.c_str()].GetSourceLists().push_back(m_TargetName);
  77. return true;
  78. }
  79. /**
  80. * Generate the files that CABLE will use to generate the wrappers.
  81. */
  82. void cmCableWrapTclCommand::GenerateCableFiles() const
  83. {
  84. // Setup the output directory.
  85. std::string outDir = m_Makefile->GetCurrentOutputDirectory();
  86. cmSystemTools::MakeDirectory((outDir+"/Tcl").c_str());
  87. std::string packageConfigName = outDir+"/Tcl/"+m_TargetName+"_config.xml";
  88. std::string packageTclName = outDir+"/Tcl/"+m_TargetName+"_tcl";
  89. // Generate the main package configuration file for CABLE.
  90. cmGeneratedFileStream packageConfig(packageConfigName.c_str());
  91. if(packageConfig)
  92. {
  93. packageConfig <<
  94. "<CableConfiguration package=\"" << m_TargetName.c_str() << "\">\n";
  95. for(unsigned int i=0; i < m_CableClassSet->Size(); ++i)
  96. {
  97. packageConfig <<
  98. " <Group name=\"" << m_TargetName.c_str() << "_" << i << "\"/>\n";
  99. }
  100. packageConfig <<
  101. "</CableConfiguration>\n";
  102. packageConfig.close();
  103. }
  104. else
  105. {
  106. cmSystemTools::Error("Error opening CABLE configuration file for writing: ",
  107. packageConfigName.c_str());
  108. }
  109. {
  110. std::string command = "${CABLE}";
  111. m_Makefile->ExpandVariablesInString(command);
  112. std::vector<std::string> depends;
  113. depends.push_back(command);
  114. command = cmSystemTools::EscapeSpaces(command.c_str());
  115. command += " "+packageConfigName+" -tcl "+packageTclName+".cxx";
  116. depends.push_back(packageConfigName);
  117. std::vector<std::string> outputs;
  118. outputs.push_back(packageTclName+".cxx");
  119. m_Makefile->AddCustomCommand(packageConfigName.c_str(),
  120. command.c_str(),
  121. depends,
  122. outputs, m_TargetName.c_str());
  123. }
  124. // Add the generated source to the package's source list.
  125. cmSourceFile file;
  126. file.SetName(packageTclName.c_str(), outDir.c_str(), "cxx", false);
  127. // Set dependency hints.
  128. file.GetDepends().push_back("wrapCalls.h");
  129. m_Makefile->AddSource(file, m_TargetName.c_str());
  130. unsigned int index = 0;
  131. for(cmCableClassSet::CableClassMap::const_iterator
  132. c = m_CableClassSet->Begin(); c != m_CableClassSet->End(); ++c, ++index)
  133. {
  134. this->GenerateCableClassFiles(c->first.c_str(), c->second, index);
  135. }
  136. }
  137. void cmCableWrapTclCommand::GenerateCableClassFiles(const char* name,
  138. const cmCableClass& c,
  139. unsigned int index) const
  140. {
  141. std::strstream indexStrStream;
  142. indexStrStream << index << std::ends;
  143. std::string indexStr = indexStrStream.str();
  144. std::string outDir = m_Makefile->GetCurrentOutputDirectory();
  145. std::string className = name;
  146. std::string groupName = m_TargetName+"_"+indexStr;
  147. std::string classConfigName = outDir+"/Tcl/"+groupName+"_config_tcl.xml";
  148. std::string classCxxName = outDir+"/Tcl/"+groupName+"_cxx.cxx";
  149. std::string classXmlName = outDir+"/Tcl/"+groupName+"_cxx.xml";
  150. std::string classTclName = outDir+"/Tcl/"+groupName+"_tcl";
  151. cmGeneratedFileStream classConfig(classConfigName.c_str());
  152. if(classConfig)
  153. {
  154. classConfig <<
  155. "<CableConfiguration source=\"" << classXmlName.c_str() << "\" "
  156. "group=\"" << groupName.c_str() << "\">\n";
  157. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  158. source != c.SourcesEnd(); ++source)
  159. {
  160. classConfig <<
  161. " <Header name=\"" << source->c_str() << "\"/>\n";
  162. }
  163. classConfig <<
  164. " <Class name=\"_wrap_::wrapper::Wrapper\"/>\n"
  165. "</CableConfiguration>\n";
  166. classConfig.close();
  167. }
  168. else
  169. {
  170. cmSystemTools::Error("Error opening CABLE configuration file for writing: ",
  171. classConfigName.c_str());
  172. }
  173. cmGeneratedFileStream classCxx(classCxxName.c_str());
  174. if(classCxx)
  175. {
  176. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  177. source != c.SourcesEnd(); ++source)
  178. {
  179. classCxx <<
  180. "#include \"" << source->c_str() << "\"\n";
  181. }
  182. classCxx <<
  183. "\n"
  184. "namespace _wrap_\n"
  185. "{\n"
  186. "\n"
  187. "struct wrapper\n"
  188. "{\n"
  189. " typedef ::" << className.c_str() << " Wrapper;\n"
  190. "};\n"
  191. "\n"
  192. "template <typename T> void Eat(T) {}\n"
  193. "\n"
  194. "void InstantiateMemberDeclarations()\n"
  195. "{\n"
  196. " Eat(sizeof(wrapper::Wrapper));\n"
  197. "}\n"
  198. "\n"
  199. "}\n";
  200. classCxx.close();
  201. }
  202. else
  203. {
  204. cmSystemTools::Error("Error opening file for writing: ",
  205. classCxxName.c_str());
  206. }
  207. {
  208. std::string command = "${GCCXML}";
  209. m_Makefile->ExpandVariablesInString(command);
  210. // Only add the rule if GCC-XML is available.
  211. if((command != "") && (command != "${GCCXML}"))
  212. {
  213. std::vector<std::string> depends;
  214. depends.push_back(command);
  215. command = cmSystemTools::EscapeSpaces(command.c_str());
  216. command += " ${CMAKE_CXXFLAGS} ${INCLUDE_FLAGS} -fsyntax-only -fxml=" + classXmlName + " " + classCxxName;
  217. std::vector<std::string> outputs;
  218. outputs.push_back(classXmlName);
  219. m_Makefile->AddCustomCommand(classCxxName.c_str(),
  220. command.c_str(),
  221. depends,
  222. outputs, m_TargetName.c_str());
  223. }
  224. }
  225. {
  226. std::string command = "${CABLE}";
  227. m_Makefile->ExpandVariablesInString(command);
  228. std::vector<std::string> depends;
  229. depends.push_back(command);
  230. command = cmSystemTools::EscapeSpaces(command.c_str());
  231. command += " "+classConfigName+" -tcl "+classTclName+".cxx";
  232. depends.push_back(classConfigName);
  233. depends.push_back(classXmlName);
  234. std::vector<std::string> outputs;
  235. outputs.push_back(classTclName+".cxx");
  236. m_Makefile->AddCustomCommand(classConfigName.c_str(),
  237. command.c_str(),
  238. depends,
  239. outputs, m_TargetName.c_str());
  240. }
  241. // Add the generated source to the package's source list.
  242. cmSourceFile file;
  243. file.SetName(classTclName.c_str(), outDir.c_str(), "cxx", false);
  244. // Set dependency hints.
  245. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  246. source != c.SourcesEnd(); ++source)
  247. {
  248. file.GetDepends().push_back(*source);
  249. }
  250. file.GetDepends().push_back("wrapCalls.h");
  251. m_Makefile->AddSource(file, m_TargetName.c_str());
  252. }