cmCableWrapTclCommand.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "cmMakeDepend.h"
  37. cmCableWrapTclCommand::cmCableWrapTclCommand():
  38. m_CableClassSet(NULL)
  39. {
  40. }
  41. cmCableWrapTclCommand::~cmCableWrapTclCommand()
  42. {
  43. if(m_CableClassSet)
  44. {
  45. delete m_CableClassSet;
  46. }
  47. }
  48. // cmCableWrapTclCommand
  49. bool cmCableWrapTclCommand::InitialPass(std::vector<std::string>& args)
  50. {
  51. if(args.size() < 2)
  52. {
  53. this->SetError("called with incorrect number of arguments");
  54. return false;
  55. }
  56. // First, we want to expand all CMAKE variables in all arguments.
  57. for(std::vector<std::string>::iterator a = args.begin();
  58. a != args.end(); ++a)
  59. {
  60. m_Makefile->ExpandVariablesInString(*a);
  61. }
  62. // Prepare to iterate through the arguments.
  63. std::vector<std::string>::const_iterator arg = args.begin();
  64. // The first argument is the name of the target.
  65. m_TargetName = *arg++;
  66. // Create the new class set.
  67. m_CableClassSet = new cmCableClassSet(m_TargetName.c_str());
  68. // Add all the regular entries.
  69. for(; (arg != args.end()) && (*arg != "SOURCES_BEGIN"); ++arg)
  70. {
  71. m_CableClassSet->ParseAndAddElement(arg->c_str(), m_Makefile);
  72. }
  73. // Add any sources that are associated with all the members.
  74. if(arg != args.end())
  75. {
  76. for(++arg; arg != args.end(); ++arg)
  77. {
  78. m_CableClassSet->AddSource(arg->c_str());
  79. }
  80. }
  81. this->GenerateCableFiles();
  82. // Add the source list to the target.
  83. m_Makefile->GetTargets()[m_TargetName.c_str()].GetSourceLists().push_back(m_TargetName);
  84. return true;
  85. }
  86. /**
  87. * Generate the files that CABLE will use to generate the wrappers.
  88. */
  89. void cmCableWrapTclCommand::GenerateCableFiles() const
  90. {
  91. // Each wrapped class may have an associated "tag" that represents
  92. // an alternative name without funky C++ syntax in it. This makes
  93. // it easier to refer to the class in a Tcl script. We will also
  94. // use the tags to make easy-to-read, unique file names for each
  95. // class's wrapper. Count the number of times each tag is used.
  96. // Warn if a tag is used more than once.
  97. std::map<cmStdString, unsigned int> tagCounts;
  98. for(cmCableClassSet::CableClassMap::const_iterator
  99. c = m_CableClassSet->Begin(); c != m_CableClassSet->End(); ++c)
  100. {
  101. std::string tag = c->second->GetTag();
  102. if((++tagCounts[tag] > 1) && (tag != ""))
  103. {
  104. std::string message =
  105. "CABLE_WRAP_TCL has found two classes with the tag "+tag
  106. +" for target "+m_TargetName;
  107. cmSystemTools::Message(message.c_str(), "Warning");
  108. }
  109. }
  110. // Each class wrapper will be written into its own CABLE "group"
  111. // file. This should hold the names of the groups generated so that
  112. // the package configuration file can tell cable how to generate the
  113. // package initialization code.
  114. std::vector<std::string> groupTags;
  115. // Setup the output directory name and make sure it exists.
  116. std::string outDir = m_Makefile->GetCurrentOutputDirectory();
  117. cmSystemTools::MakeDirectory((outDir+"/Tcl").c_str());
  118. // Write out the cable configuration files with one class per group.
  119. // Try to name the groups based on their class's tag, but use an
  120. // index to disambiguate tag repeats (mostly used for empty tags).
  121. std::map<cmStdString, unsigned int> tagIndexes;
  122. for(cmCableClassSet::CableClassMap::const_iterator
  123. c = m_CableClassSet->Begin(); c != m_CableClassSet->End(); ++c)
  124. {
  125. // Construct the group's tag-based name, with index if necessary.
  126. std::string tag = c->second->GetTag();
  127. std::string groupTag;
  128. if(tagCounts[tag] > 1)
  129. {
  130. unsigned int tagIndex = tagIndexes[tag]++;
  131. std::strstream indexStrStream;
  132. indexStrStream << tagIndex << std::ends;
  133. std::string indexStr = indexStrStream.str();
  134. groupTag = "_"+indexStr;
  135. }
  136. if(tag != "")
  137. {
  138. groupTag += "_"+tag;
  139. }
  140. // Save this group tag in the list of tags for the main package
  141. // configuration file below.
  142. groupTags.push_back(groupTag);
  143. // Actually generate the class's configuration file.
  144. this->GenerateCableClassFiles(c->first.c_str(), *(c->second),
  145. groupTag.c_str());
  146. }
  147. // Construct the output file names.
  148. std::string packageConfigName = outDir+"/Tcl/"+m_TargetName+"_config.xml";
  149. std::string packageTclFileName = "Tcl/"+m_TargetName+"_tcl";
  150. std::string packageTclFullName = outDir+"/"+packageTclFileName;
  151. // Generate the main package configuration file for CABLE. This
  152. // just lists the "group" files generated above.
  153. cmGeneratedFileStream packageConfig(packageConfigName.c_str());
  154. if(packageConfig)
  155. {
  156. packageConfig <<
  157. "<CableConfiguration package=\"" << m_TargetName.c_str() << "\">\n";
  158. for(std::vector<std::string>::const_iterator g = groupTags.begin();
  159. g != groupTags.end(); ++g)
  160. {
  161. packageConfig <<
  162. " <Group name=\"" << m_TargetName.c_str() << g->c_str() << "\"/>\n";
  163. }
  164. packageConfig <<
  165. "</CableConfiguration>\n";
  166. packageConfig.close();
  167. }
  168. else
  169. {
  170. cmSystemTools::Error("Error opening CABLE configuration file for writing: ",
  171. packageConfigName.c_str());
  172. }
  173. // Generate the rule to run CABLE for the package configuration file.
  174. std::string command = "${CABLE}";
  175. m_Makefile->ExpandVariablesInString(command);
  176. std::vector<std::string> depends;
  177. depends.push_back(command);
  178. std::string commandArgs = " "+packageConfigName+
  179. " -tcl "+packageTclFullName+".cxx";
  180. depends.push_back(packageConfigName);
  181. std::vector<std::string> outputs;
  182. outputs.push_back(packageTclFileName+".cxx");
  183. m_Makefile->AddCustomCommand(packageConfigName.c_str(),
  184. command.c_str(),
  185. commandArgs.c_str(),
  186. depends,
  187. outputs, m_TargetName.c_str());
  188. // Add the generated source to the package target's source list.
  189. cmSourceFile file;
  190. file.SetName(packageTclFileName.c_str(), outDir.c_str(), "cxx", false);
  191. // Set dependency hints.
  192. file.GetDepends().push_back("wrapCalls.h");
  193. m_Makefile->AddSource(file, m_TargetName.c_str());
  194. }
  195. void cmCableWrapTclCommand::GenerateCableClassFiles(const char* name,
  196. const cmCableClass& c,
  197. const char* groupTag) const
  198. {
  199. std::string outDir = m_Makefile->GetCurrentOutputDirectory();
  200. std::string className = name;
  201. std::string groupName = m_TargetName+groupTag;
  202. std::string classConfigName = outDir+"/Tcl/"+groupName+"_config_tcl.xml";
  203. std::string classCxxName = outDir+"/Tcl/"+groupName+"_cxx.cc";
  204. std::string classXmlName = outDir+"/Tcl/"+groupName+"_cxx.xml";
  205. std::string classTclFileName = "Tcl/"+groupName+"_tcl";
  206. std::string classTclFullName = outDir+"/"+classTclFileName;
  207. cmGeneratedFileStream classConfig(classConfigName.c_str());
  208. if(classConfig)
  209. {
  210. classConfig <<
  211. "<CableConfiguration source=\"" << classXmlName.c_str() << "\" "
  212. "group=\"" << groupName.c_str() << "\">\n";
  213. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  214. source != c.SourcesEnd(); ++source)
  215. {
  216. classConfig <<
  217. " <Header name=\"" << source->c_str() << "\"/>\n";
  218. }
  219. classConfig <<
  220. " <Class name=\"_wrap_::wrapper::Wrapper\">\n";
  221. if(c.GetTag() != "")
  222. {
  223. classConfig <<
  224. " <AlternateName name=\"" << c.GetTag().c_str() << "\"/>\n";
  225. }
  226. classConfig <<
  227. " </Class>\n"
  228. "</CableConfiguration>\n";
  229. classConfig.close();
  230. }
  231. else
  232. {
  233. cmSystemTools::Error("Error opening CABLE configuration file for writing: ",
  234. classConfigName.c_str());
  235. }
  236. cmGeneratedFileStream classCxx(classCxxName.c_str());
  237. if(classCxx)
  238. {
  239. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  240. source != c.SourcesEnd(); ++source)
  241. {
  242. classCxx <<
  243. "#include \"" << source->c_str() << "\"\n";
  244. }
  245. classCxx <<
  246. "\n"
  247. "namespace _wrap_\n"
  248. "{\n"
  249. "\n"
  250. "struct wrapper\n"
  251. "{\n"
  252. " typedef ::" << className.c_str() << " Wrapper;\n"
  253. "};\n"
  254. "\n"
  255. "template <typename T> void Eat(T) {}\n"
  256. "\n"
  257. "void InstantiateMemberDeclarations()\n"
  258. "{\n"
  259. " Eat(sizeof(wrapper::Wrapper));\n"
  260. "}\n"
  261. "\n"
  262. "}\n";
  263. classCxx.close();
  264. }
  265. else
  266. {
  267. cmSystemTools::Error("Error opening file for writing: ",
  268. classCxxName.c_str());
  269. }
  270. // Generate the rule to have GCC-XML parse the classes to be wrapped.
  271. {
  272. std::string command = this->GetGccXmlFromCache();
  273. std::vector<std::string> depends;
  274. depends.push_back(command);
  275. // Get the dependencies of the file.
  276. cmMakeDepend md;
  277. md.SetMakefile(m_Makefile);
  278. const cmDependInformation* dependInfo =
  279. md.FindDependencies(classCxxName.c_str());
  280. if(dependInfo)
  281. {
  282. for(cmDependInformation::DependencySet::const_iterator d =
  283. dependInfo->m_DependencySet.begin();
  284. d != dependInfo->m_DependencySet.end(); ++d)
  285. {
  286. // Make sure the full path is given. If not, the dependency was
  287. // not found.
  288. if((*d)->m_FullPath != "")
  289. {
  290. depends.push_back((*d)->m_FullPath);
  291. }
  292. }
  293. }
  294. std::string commandArgs = this->GetGccXmlFlagsFromCache();
  295. commandArgs += " ";
  296. commandArgs += m_Makefile->GetDefineFlags();
  297. commandArgs += " -I\"";
  298. commandArgs += m_Makefile->GetStartDirectory();
  299. commandArgs += "\"";
  300. const std::vector<std::string>& includes =
  301. m_Makefile->GetIncludeDirectories();
  302. for(std::vector<std::string>::const_iterator i = includes.begin();
  303. i != includes.end(); ++i)
  304. {
  305. commandArgs += " -I";
  306. commandArgs += cmSystemTools::EscapeSpaces(i->c_str());
  307. }
  308. commandArgs += " -fxml="+classXmlName+" "+classCxxName;
  309. std::vector<std::string> outputs;
  310. outputs.push_back(classXmlName);
  311. m_Makefile->AddCustomCommand(classCxxName.c_str(),
  312. command.c_str(),
  313. commandArgs.c_str(),
  314. depends,
  315. outputs, m_TargetName.c_str());
  316. }
  317. // Generate the rule to run cable on the GCC-XML output to generate wrappers.
  318. {
  319. std::string command = this->GetCableFromCache();
  320. std::vector<std::string> depends;
  321. depends.push_back(command);
  322. std::string commandArgs = " "+classConfigName+" -tcl "+classTclFullName+".cxx";
  323. depends.push_back(classConfigName);
  324. depends.push_back(classXmlName);
  325. std::vector<std::string> outputs;
  326. outputs.push_back(classTclFileName+".cxx");
  327. m_Makefile->AddCustomCommand(classConfigName.c_str(),
  328. command.c_str(),
  329. commandArgs.c_str(),
  330. depends,
  331. outputs, m_TargetName.c_str());
  332. }
  333. // Add the generated source to the package's source list.
  334. cmSourceFile file;
  335. file.SetName(classTclFileName.c_str(), outDir.c_str(), "cxx", false);
  336. // Set dependency hints.
  337. for(cmCableClass::Sources::const_iterator source = c.SourcesBegin();
  338. source != c.SourcesEnd(); ++source)
  339. {
  340. file.GetDepends().push_back(*source);
  341. }
  342. file.GetDepends().push_back("wrapCalls.h");
  343. m_Makefile->AddSource(file, m_TargetName.c_str());
  344. }
  345. /**
  346. * Get the "GCCXML" cache entry value. If there is no cache entry for GCCXML,
  347. * one will be created and initialized to NOTFOUND.
  348. */
  349. std::string cmCableWrapTclCommand::GetGccXmlFromCache() const
  350. {
  351. const char* gccxml =
  352. m_Makefile->GetDefinition("GCCXML");
  353. if(gccxml)
  354. { return gccxml; }
  355. m_Makefile->AddCacheDefinition("GCCXML",
  356. "NOTFOUND",
  357. "Path to GCC-XML executable.",
  358. cmCacheManager::FILEPATH);
  359. return "NOTFOUND";
  360. }
  361. /**
  362. * Get the "GCCXML_FLAGS" cache entry value. If there is no cache
  363. * entry for GCCXML_FLAGS, one will be created and initialized "".
  364. */
  365. std::string cmCableWrapTclCommand::GetGccXmlFlagsFromCache() const
  366. {
  367. const char* gccxmlFlags =
  368. m_Makefile->GetDefinition("GCCXML_FLAGS");
  369. if(gccxmlFlags)
  370. { return gccxmlFlags; }
  371. m_Makefile->AddCacheDefinition(
  372. "GCCXML_FLAGS",
  373. "",
  374. "Flags to GCC-XML to get it to parse the native compiler's headers.",
  375. cmCacheManager::STRING);
  376. return "";
  377. }
  378. /**
  379. * Get the "CABLE" cache entry value. If there is no cache entry for CABLE,
  380. * one will be created and initialized to NOTFOUND.
  381. */
  382. std::string cmCableWrapTclCommand::GetCableFromCache() const
  383. {
  384. const char* cable =
  385. m_Makefile->GetDefinition("CABLE");
  386. if(cable)
  387. { return cable; }
  388. m_Makefile->AddCacheDefinition("CABLE",
  389. "NOTFOUND",
  390. "Path to CABLE executable.",
  391. cmCacheManager::FILEPATH);
  392. return "NOTFOUND";
  393. }