cmListFileCache.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmListFileCache.h"
  14. #include "cmListFileLexer.h"
  15. #include "cmSystemTools.h"
  16. #include "cmMakefile.h"
  17. #include "cmVersion.h"
  18. #include <cmsys/RegularExpression.hxx>
  19. #ifdef __BORLANDC__
  20. # pragma warn -8060 /* possibly incorrect assignment */
  21. #endif
  22. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  23. cmListFileFunction& function,
  24. const char* filename);
  25. bool cmListFile::ParseFile(const char* filename,
  26. bool topLevel,
  27. cmMakefile *mf)
  28. {
  29. if(!cmSystemTools::FileExists(filename))
  30. {
  31. return false;
  32. }
  33. // Create the scanner.
  34. cmListFileLexer* lexer = cmListFileLexer_New();
  35. if(!lexer)
  36. {
  37. cmSystemTools::Error("cmListFileCache: error allocating lexer ");
  38. return false;
  39. }
  40. // Open the file.
  41. if(!cmListFileLexer_SetFileName(lexer, filename))
  42. {
  43. cmListFileLexer_Delete(lexer);
  44. cmSystemTools::Error("cmListFileCache: error can not open file ",
  45. filename);
  46. return false;
  47. }
  48. // Use a simple recursive-descent parser to process the token
  49. // stream.
  50. this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
  51. bool parseError = false;
  52. bool haveNewline = true;
  53. cmListFileLexer_Token* token;
  54. while(!parseError && (token = cmListFileLexer_Scan(lexer)))
  55. {
  56. if(token->type == cmListFileLexer_Token_Newline)
  57. {
  58. haveNewline = true;
  59. }
  60. else if(token->type == cmListFileLexer_Token_Identifier)
  61. {
  62. if(haveNewline)
  63. {
  64. haveNewline = false;
  65. cmListFileFunction inFunction;
  66. inFunction.Name = token->text;
  67. inFunction.FilePath = filename;
  68. inFunction.Line = token->line;
  69. if(cmListFileCacheParseFunction(lexer, inFunction, filename))
  70. {
  71. this->Functions.push_back(inFunction);
  72. }
  73. else
  74. {
  75. parseError = true;
  76. }
  77. }
  78. else
  79. {
  80. cmOStringStream error;
  81. error << "Error in cmake code at\n"
  82. << filename << ":" << token->line << ":\n"
  83. << "Parse error. Expected a newline, got "
  84. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  85. << " with text \"" << token->text << "\".";
  86. cmSystemTools::Error(error.str().c_str());
  87. parseError = true;
  88. }
  89. }
  90. else
  91. {
  92. cmOStringStream error;
  93. error << "Error in cmake code at\n"
  94. << filename << ":" << token->line << ":\n"
  95. << "Parse error. Expected a command name, got "
  96. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  97. << " with text \""
  98. << token->text << "\".";
  99. cmSystemTools::Error(error.str().c_str());
  100. parseError = true;
  101. }
  102. }
  103. if (parseError)
  104. {
  105. this->ModifiedTime = 0;
  106. }
  107. cmListFileLexer_Delete(lexer);
  108. // do we need a cmake_policy(VERSION call?
  109. if(topLevel)
  110. {
  111. bool hasVersion = false;
  112. // search for the right policy command
  113. for(std::vector<cmListFileFunction>::iterator i
  114. = this->Functions.begin();
  115. i != this->Functions.end(); ++i)
  116. {
  117. if (cmSystemTools::LowerCase(i->Name) == "cmake_minimum_required")
  118. {
  119. hasVersion = true;
  120. break;
  121. }
  122. }
  123. // if no policy command is found this is an error if they use any non advanced functions or a lot of functions
  124. if(!hasVersion)
  125. {
  126. bool isProblem = true;
  127. if (this->Functions.size() < 30)
  128. {
  129. // the list of simple commands DO NOT ADD TO THIS LIST!!!!!
  130. // these commands must have backwards compatibility forever and
  131. // and that is a lot longer than your tiny mind can comprehend mortal
  132. std::set<std::string> allowedCommands;
  133. allowedCommands.insert("project");
  134. allowedCommands.insert("set");
  135. allowedCommands.insert("if");
  136. allowedCommands.insert("endif");
  137. allowedCommands.insert("else");
  138. allowedCommands.insert("elseif");
  139. allowedCommands.insert("add_executable");
  140. allowedCommands.insert("add_library");
  141. allowedCommands.insert("target_link_libraries");
  142. allowedCommands.insert("option");
  143. allowedCommands.insert("message");
  144. isProblem = false;
  145. for(std::vector<cmListFileFunction>::iterator i
  146. = this->Functions.begin();
  147. i != this->Functions.end(); ++i)
  148. {
  149. std::string name = cmSystemTools::LowerCase(i->Name);
  150. if (allowedCommands.find(name) == allowedCommands.end())
  151. {
  152. isProblem = true;
  153. break;
  154. }
  155. }
  156. }
  157. if (isProblem)
  158. {
  159. // Tell the top level cmMakefile to diagnose
  160. // this violation of CMP0000.
  161. mf->SetCheckCMP0000(true);
  162. // Implicitly set the version for the user.
  163. mf->SetPolicyVersion("2.4");
  164. }
  165. }
  166. }
  167. if(topLevel)
  168. {
  169. bool hasProject = false;
  170. // search for a project command
  171. for(std::vector<cmListFileFunction>::iterator i
  172. = this->Functions.begin();
  173. i != this->Functions.end(); ++i)
  174. {
  175. if(cmSystemTools::LowerCase(i->Name) == "project")
  176. {
  177. hasProject = true;
  178. break;
  179. }
  180. }
  181. // if no project command is found, add one
  182. if(!hasProject)
  183. {
  184. cmListFileFunction project;
  185. project.Name = "PROJECT";
  186. cmListFileArgument prj("Project", false, filename, 0);
  187. project.Arguments.push_back(prj);
  188. this->Functions.insert(this->Functions.begin(),project);
  189. }
  190. }
  191. if(parseError)
  192. {
  193. return false;
  194. }
  195. return true;
  196. }
  197. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  198. cmListFileFunction& function,
  199. const char* filename)
  200. {
  201. // Command name has already been parsed. Read the left paren.
  202. cmListFileLexer_Token* token;
  203. if(!(token = cmListFileLexer_Scan(lexer)))
  204. {
  205. cmOStringStream error;
  206. error << "Error in cmake code at\n"
  207. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  208. << "Parse error. Function missing opening \"(\".";
  209. cmSystemTools::Error(error.str().c_str());
  210. return false;
  211. }
  212. if(token->type != cmListFileLexer_Token_ParenLeft)
  213. {
  214. cmOStringStream error;
  215. error << "Error in cmake code at\n"
  216. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  217. << "Parse error. Expected \"(\", got "
  218. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  219. << " with text \"" << token->text << "\".";
  220. cmSystemTools::Error(error.str().c_str());
  221. return false;
  222. }
  223. // Arguments.
  224. unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer);
  225. while((token = cmListFileLexer_Scan(lexer)))
  226. {
  227. if(token->type == cmListFileLexer_Token_ParenRight)
  228. {
  229. return true;
  230. }
  231. else if(token->type == cmListFileLexer_Token_Identifier ||
  232. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  233. {
  234. cmListFileArgument a(token->text,
  235. false, filename, token->line);
  236. function.Arguments.push_back(a);
  237. }
  238. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  239. {
  240. cmListFileArgument a(token->text,
  241. true, filename, token->line);
  242. function.Arguments.push_back(a);
  243. }
  244. else if(token->type != cmListFileLexer_Token_Newline)
  245. {
  246. // Error.
  247. cmOStringStream error;
  248. error << "Error in cmake code at\n"
  249. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer)
  250. << ":\n"
  251. << "Parse error. Function missing ending \")\". "
  252. << "Instead found "
  253. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  254. << " with text \"" << token->text << "\".";
  255. cmSystemTools::Error(error.str().c_str());
  256. return false;
  257. }
  258. lastLine = cmListFileLexer_GetCurrentLine(lexer);
  259. }
  260. cmOStringStream error;
  261. error << "Error in cmake code at\n"
  262. << filename << ":" << lastLine << ":\n"
  263. << "Parse error. Function missing ending \")\". "
  264. << "End of file reached.";
  265. cmSystemTools::Error(error.str().c_str());
  266. return false;
  267. }
  268. //----------------------------------------------------------------------------
  269. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  270. {
  271. os << lfc.FilePath;
  272. if(lfc.Line)
  273. {
  274. os << ":" << lfc.Line;
  275. if(!lfc.Name.empty())
  276. {
  277. os << " (" << lfc.Name << ")";
  278. }
  279. }
  280. return os;
  281. }