cmCableDefineSetCommand.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "cmCableDefineSetCommand.h"
  33. #include "cmCacheManager.h"
  34. #include "cmRegularExpression.h"
  35. // cmCableDefineSetCommand
  36. bool cmCableDefineSetCommand::Invoke(std::vector<std::string>& args)
  37. {
  38. if(args.size() < 2)
  39. {
  40. this->SetError("called with incorrect number of arguments");
  41. return false;
  42. }
  43. // This command needs access to the Cable data.
  44. this->SetupCableData();
  45. std::vector<std::string>::const_iterator arg = args.begin();
  46. // The first argument is the name of the set.
  47. m_SetName = *arg++;
  48. // All arguments until a "SOURCE_FILES" are the elements to be placed in
  49. // the set.
  50. for(; (arg != args.end()) && (*arg != "SOURCE_FILES"); ++arg)
  51. {
  52. // If the element cannot be added, return an error.
  53. // This can occur when a tag is not specified and can't be generated.
  54. if(!this->AddElement(*arg))
  55. { return false; }
  56. }
  57. // If we are not at the end, the "SOURCE_FILES" keyword has been
  58. // encountered.
  59. if(arg != args.end())
  60. {
  61. // The rest of the arguments are source files to be included in
  62. // any package which references the set.
  63. for(++arg; arg != args.end(); ++arg)
  64. {
  65. if(!this->AddSourceFile(*arg))
  66. { return false; }
  67. }
  68. }
  69. // Write this command's configuration output.
  70. this->WriteConfiguration();
  71. return true;
  72. }
  73. /**
  74. * Write the CABLE configuration code to define this Set.
  75. */
  76. void cmCableDefineSetCommand::WriteConfiguration() const
  77. {
  78. cmRegularExpression needCdataBlock("[&<>]");
  79. // Get the ouptut information from the cmCableData.
  80. std::ostream& os = m_CableData->GetOutputStream();
  81. cmCableData::Indentation indent = m_CableData->GetIndentation();
  82. // Output the code.
  83. os << indent << "<" << this->GetXmlTag() << " name=\"" << m_SetName.c_str() << "\">" << std::endl;
  84. for(std::vector<std::string>::const_iterator e = m_SourceHeaders.begin();
  85. e != m_SourceHeaders.end(); ++e)
  86. {
  87. os << indent << " <File name=\"" << e->c_str() << "\"/>" << std::endl;
  88. }
  89. for(std::vector<std::string>::const_iterator e = m_InstantiationSources.begin();
  90. e != m_InstantiationSources.end(); ++e)
  91. {
  92. os << indent << " <File name=\"" << e->c_str()
  93. << "\" purpose=\"instantiate\"/>" << std::endl;
  94. }
  95. for(Elements::const_iterator e = m_Elements.begin();
  96. e != m_Elements.end(); ++e)
  97. {
  98. os << indent << " <Element";
  99. // Only output the tag if it is not the empty string.
  100. if(e->first.length() > 0)
  101. {
  102. os << " tag=\"" << e->first.c_str() << "\"";
  103. }
  104. os << ">";
  105. if(needCdataBlock.find(e->second.c_str()))
  106. {
  107. os << "<![CDATA[" << e->second.c_str() << "]]>";
  108. }
  109. else
  110. {
  111. os << e->second.c_str();
  112. }
  113. os << "</Element>" << std::endl;
  114. }
  115. os << indent << "</" << this->GetXmlTag() << ">" << std::endl;
  116. }
  117. /**
  118. * Add an element to the set. The given string is the argument to the
  119. * command describing the element. There are two formats allowed:
  120. * "code" = The code describing the element to CABLE is simply given.
  121. * The GenerateTag() method will guess at a good tag for the
  122. * code.
  123. * "tag:code" = The left side of a single colon is text describing the tag.
  124. * GenerateTag() will not be called.
  125. */
  126. bool cmCableDefineSetCommand::AddElement(const std::string& arg)
  127. {
  128. // A regular expression to match the tagged element specification.
  129. cmRegularExpression tagGiven("^([A-Za-z_0-9]*)[ \t]*:[ \t]*([^:].*|::.*)$");
  130. std::string tag;
  131. std::string code;
  132. if(tagGiven.find(arg.c_str()))
  133. {
  134. // A tag was given. Use it.
  135. tag = tagGiven.match(1);
  136. code = tagGiven.match(2);
  137. }
  138. else
  139. {
  140. // No tag was given. Try to generate one.
  141. if(!this->GenerateTag(arg, tag))
  142. { return false; }
  143. code = arg;
  144. }
  145. // Add an element with the given tag and code.
  146. m_Elements.push_back(Element(tag, code));
  147. return true;
  148. }
  149. /**
  150. * Given the string representing a set element, automatically generate
  151. * the CABLE element tag for it.
  152. *
  153. * **This function determines how the output language of all
  154. * CABLE-generated wrappers will look!**
  155. */
  156. bool
  157. cmCableDefineSetCommand::GenerateTag(const std::string& element,
  158. std::string& tag)
  159. {
  160. // Hold the regular expressions for matching against the element.
  161. cmRegularExpression regex;
  162. // If the element's code begins in a $, it is referring to a set name.
  163. // The set's elements have their own tags, so we don't need one.
  164. regex.compile("^[ \t]*\\$");
  165. if(regex.find(element))
  166. { tag = ""; return true; }
  167. // Test for simple integer
  168. regex.compile("^[ \t]*([0-9]*)[ \t]*$");
  169. if(regex.find(element))
  170. {
  171. tag = "_";
  172. tag.append(regex.match(1));
  173. return true;
  174. }
  175. // Test for basic integer type
  176. regex.compile("^[ \t]*(unsigned[ ]|signed[ ])?[ \t]*(char|short|int|long|long[ ]long)[ \t]*$");
  177. if(regex.find(element))
  178. {
  179. tag = "_";
  180. if(regex.match(1) == "unsigned ")
  181. { tag.append("u"); }
  182. if(regex.match(2) == "long long")
  183. { tag.append("llong"); }
  184. else
  185. { tag.append(regex.match(2)); }
  186. return true;
  187. }
  188. // Test for basic floating-point type
  189. regex.compile("^[ \t]*(long[ ]|)[ \t]*(float|double)[ \t]*$");
  190. if(regex.find(element))
  191. {
  192. tag = "_";
  193. if(regex.match(1) == "long ")
  194. tag.append("l");
  195. tag.append(regex.match(2));
  196. return true;
  197. }
  198. // Test for basic wide-character type
  199. regex.compile("^[ \t]*(wchar_t)[ \t]*$");
  200. if(regex.find(element))
  201. {
  202. tag = "_wchar";
  203. return true;
  204. }
  205. // Test for plain type name (without template arguments).
  206. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)[ \t]*$");
  207. if(regex.find(element))
  208. {
  209. // The tag is the same as the type.
  210. tag = regex.match(1);
  211. return true;
  212. }
  213. // Test for template class instance.
  214. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)<.*[ \t]*$");
  215. if(regex.find(element))
  216. {
  217. // The tag is the type without arguments (the arguments may have
  218. // their own tags).
  219. tag = regex.match(1);
  220. return true;
  221. }
  222. // We can't generate a tag.
  223. std::string err =
  224. ("doesn't know how to generate tag for element \""+element+"\" in set \""
  225. +m_SetName+"\"\nPlease specify one with the \"tag:element\" syntax.");
  226. this->SetError(err.c_str());
  227. tag = "";
  228. return false;
  229. }
  230. /**
  231. * Add a source file associated with this set. Any package referencing
  232. * this set will automatically include this source file.
  233. */
  234. bool cmCableDefineSetCommand::AddSourceFile(const std::string& file)
  235. {
  236. // We must locate the file in the include path so that we can detect
  237. // its extension, and whether there is more than one to find.
  238. std::string header = file+".h";
  239. std::string txx = file+".txx";
  240. m_Makefile->ExpandVariablesInString(header);
  241. m_Makefile->ExpandVariablesInString(txx);
  242. // See if the file just exists here. The compiler's search path will
  243. // locate it.
  244. if(cmSystemTools::FileExists(header.c_str()))
  245. {
  246. m_SourceHeaders.push_back(header);
  247. // See if there is a matching .txx as well.
  248. if(cmSystemTools::FileExists(txx.c_str()))
  249. {
  250. m_InstantiationSources.push_back(txx);
  251. }
  252. return true;
  253. }
  254. // We must look for the file in the include search path.
  255. const std::vector<std::string>& includeDirectories =
  256. m_Makefile->GetIncludeDirectories();
  257. for(std::vector<std::string>::const_iterator dir = includeDirectories.begin();
  258. dir != includeDirectories.end(); ++dir)
  259. {
  260. std::string path = *dir + "/";
  261. m_Makefile->ExpandVariablesInString(path);
  262. if(cmSystemTools::FileExists((path+header).c_str()))
  263. {
  264. m_SourceHeaders.push_back(header);
  265. // See if there is a matching .txx as well.
  266. if(cmSystemTools::FileExists((path+txx).c_str()))
  267. {
  268. m_InstantiationSources.push_back(txx);
  269. }
  270. return true;
  271. }
  272. }
  273. // We couldn't locate the source file. Report the error.
  274. std::string err = "couldn't find source file " + header;
  275. this->SetError(err.c_str());
  276. return false;
  277. }