cmCableWrapTclCommand.cxx 18 KB

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