cmListFileCache.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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
  124. // non advanced functions or a lot of functions
  125. if(!hasVersion)
  126. {
  127. bool isProblem = true;
  128. if (this->Functions.size() < 30)
  129. {
  130. // the list of simple commands DO NOT ADD TO THIS LIST!!!!!
  131. // these commands must have backwards compatibility forever and
  132. // and that is a lot longer than your tiny mind can comprehend mortal
  133. std::set<std::string> allowedCommands;
  134. allowedCommands.insert("project");
  135. allowedCommands.insert("set");
  136. allowedCommands.insert("if");
  137. allowedCommands.insert("endif");
  138. allowedCommands.insert("else");
  139. allowedCommands.insert("elseif");
  140. allowedCommands.insert("add_executable");
  141. allowedCommands.insert("add_library");
  142. allowedCommands.insert("target_link_libraries");
  143. allowedCommands.insert("option");
  144. allowedCommands.insert("message");
  145. isProblem = false;
  146. for(std::vector<cmListFileFunction>::iterator i
  147. = this->Functions.begin();
  148. i != this->Functions.end(); ++i)
  149. {
  150. std::string name = cmSystemTools::LowerCase(i->Name);
  151. if (allowedCommands.find(name) == allowedCommands.end())
  152. {
  153. isProblem = true;
  154. break;
  155. }
  156. }
  157. }
  158. if (isProblem)
  159. {
  160. // Tell the top level cmMakefile to diagnose
  161. // this violation of CMP0000.
  162. mf->SetCheckCMP0000(true);
  163. // Implicitly set the version for the user.
  164. mf->SetPolicyVersion("2.4");
  165. }
  166. }
  167. }
  168. if(topLevel)
  169. {
  170. bool hasProject = false;
  171. // search for a project command
  172. for(std::vector<cmListFileFunction>::iterator i
  173. = this->Functions.begin();
  174. i != this->Functions.end(); ++i)
  175. {
  176. if(cmSystemTools::LowerCase(i->Name) == "project")
  177. {
  178. hasProject = true;
  179. break;
  180. }
  181. }
  182. // if no project command is found, add one
  183. if(!hasProject)
  184. {
  185. cmListFileFunction project;
  186. project.Name = "PROJECT";
  187. cmListFileArgument prj("Project", false, filename, 0);
  188. project.Arguments.push_back(prj);
  189. this->Functions.insert(this->Functions.begin(),project);
  190. }
  191. }
  192. if(parseError)
  193. {
  194. return false;
  195. }
  196. return true;
  197. }
  198. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  199. cmListFileFunction& function,
  200. const char* filename)
  201. {
  202. // Command name has already been parsed. Read the left paren.
  203. cmListFileLexer_Token* token;
  204. if(!(token = cmListFileLexer_Scan(lexer)))
  205. {
  206. cmOStringStream error;
  207. error << "Error in cmake code at\n"
  208. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  209. << "Parse error. Function missing opening \"(\".";
  210. cmSystemTools::Error(error.str().c_str());
  211. return false;
  212. }
  213. if(token->type != cmListFileLexer_Token_ParenLeft)
  214. {
  215. cmOStringStream error;
  216. error << "Error in cmake code at\n"
  217. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  218. << "Parse error. Expected \"(\", got "
  219. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  220. << " with text \"" << token->text << "\".";
  221. cmSystemTools::Error(error.str().c_str());
  222. return false;
  223. }
  224. // Arguments.
  225. unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer);
  226. unsigned long parenDepth = 0;
  227. while((token = cmListFileLexer_Scan(lexer)))
  228. {
  229. if(token->type == cmListFileLexer_Token_ParenLeft)
  230. {
  231. parenDepth++;
  232. cmListFileArgument a("(",
  233. false, filename, token->line);
  234. function.Arguments.push_back(a);
  235. }
  236. else if(token->type == cmListFileLexer_Token_ParenRight)
  237. {
  238. if (parenDepth == 0)
  239. {
  240. return true;
  241. }
  242. parenDepth--;
  243. cmListFileArgument a(")",
  244. false, filename, token->line);
  245. function.Arguments.push_back(a);
  246. }
  247. else if(token->type == cmListFileLexer_Token_Identifier ||
  248. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  249. {
  250. cmListFileArgument a(token->text,
  251. false, filename, token->line);
  252. function.Arguments.push_back(a);
  253. }
  254. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  255. {
  256. cmListFileArgument a(token->text,
  257. true, filename, token->line);
  258. function.Arguments.push_back(a);
  259. }
  260. else if(token->type != cmListFileLexer_Token_Newline)
  261. {
  262. // Error.
  263. cmOStringStream error;
  264. error << "Error in cmake code at\n"
  265. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer)
  266. << ":\n"
  267. << "Parse error. Function missing ending \")\". "
  268. << "Instead found "
  269. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  270. << " with text \"" << token->text << "\".";
  271. cmSystemTools::Error(error.str().c_str());
  272. return false;
  273. }
  274. lastLine = cmListFileLexer_GetCurrentLine(lexer);
  275. }
  276. cmOStringStream error;
  277. error << "Error in cmake code at\n"
  278. << filename << ":" << lastLine << ":\n"
  279. << "Parse error. Function missing ending \")\". "
  280. << "End of file reached.";
  281. cmSystemTools::Error(error.str().c_str());
  282. return false;
  283. }
  284. //----------------------------------------------------------------------------
  285. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  286. {
  287. os << lfc.FilePath;
  288. if(lfc.Line)
  289. {
  290. os << ":" << lfc.Line;
  291. if(!lfc.Name.empty())
  292. {
  293. os << " (" << lfc.Name << ")";
  294. }
  295. }
  296. return os;
  297. }