cmListFileCache.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  154. }
  155. }
  156. if (isProblem)
  157. {
  158. cmOStringStream msg;
  159. msg << "No cmake_minimum_required command is present. "
  160. << "A line of code such as\n"
  161. << " cmake_minimum_required(VERSION "
  162. << cmVersion::GetMajorVersion() << "."
  163. << cmVersion::GetMinorVersion()
  164. << ")\n"
  165. << "should be added at the top of the file. "
  166. << "The version specified may be lower if you wish to "
  167. << "support older CMake versions for this project. "
  168. << "For more information run "
  169. << "\"cmake --help-policy CMP0000\".";
  170. switch (mf->GetPolicyStatus(cmPolicies::CMP0000))
  171. {
  172. case cmPolicies::WARN:
  173. mf->IssueMessage(cmake::AUTHOR_WARNING, msg.str().c_str());
  174. case cmPolicies::OLD:
  175. // Implicitly set the version for the user.
  176. mf->SetPolicyVersion("2.4");
  177. break;
  178. default:
  179. mf->IssueMessage(cmake::FATAL_ERROR, msg.str().c_str());
  180. return false;
  181. }
  182. }
  183. }
  184. }
  185. if(topLevel)
  186. {
  187. bool hasProject = false;
  188. // search for a project command
  189. for(std::vector<cmListFileFunction>::iterator i
  190. = this->Functions.begin();
  191. i != this->Functions.end(); ++i)
  192. {
  193. if(cmSystemTools::LowerCase(i->Name) == "project")
  194. {
  195. hasProject = true;
  196. break;
  197. }
  198. }
  199. // if no project command is found, add one
  200. if(!hasProject)
  201. {
  202. cmListFileFunction project;
  203. project.Name = "PROJECT";
  204. cmListFileArgument prj("Project", false, filename, 0);
  205. project.Arguments.push_back(prj);
  206. this->Functions.insert(this->Functions.begin(),project);
  207. }
  208. }
  209. if(parseError)
  210. {
  211. return false;
  212. }
  213. return true;
  214. }
  215. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  216. cmListFileFunction& function,
  217. const char* filename)
  218. {
  219. // Command name has already been parsed. Read the left paren.
  220. cmListFileLexer_Token* token;
  221. if(!(token = cmListFileLexer_Scan(lexer)))
  222. {
  223. cmOStringStream error;
  224. error << "Error in cmake code at\n"
  225. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  226. << "Parse error. Function missing opening \"(\".";
  227. cmSystemTools::Error(error.str().c_str());
  228. return false;
  229. }
  230. if(token->type != cmListFileLexer_Token_ParenLeft)
  231. {
  232. cmOStringStream error;
  233. error << "Error in cmake code at\n"
  234. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  235. << "Parse error. Expected \"(\", got "
  236. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  237. << " with text \"" << token->text << "\".";
  238. cmSystemTools::Error(error.str().c_str());
  239. return false;
  240. }
  241. // Arguments.
  242. unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer);
  243. while((token = cmListFileLexer_Scan(lexer)))
  244. {
  245. if(token->type == cmListFileLexer_Token_ParenRight)
  246. {
  247. return true;
  248. }
  249. else if(token->type == cmListFileLexer_Token_Identifier ||
  250. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  251. {
  252. cmListFileArgument a(token->text,
  253. false, filename, token->line);
  254. function.Arguments.push_back(a);
  255. }
  256. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  257. {
  258. cmListFileArgument a(token->text,
  259. true, filename, token->line);
  260. function.Arguments.push_back(a);
  261. }
  262. else if(token->type != cmListFileLexer_Token_Newline)
  263. {
  264. // Error.
  265. cmOStringStream error;
  266. error << "Error in cmake code at\n"
  267. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer)
  268. << ":\n"
  269. << "Parse error. Function missing ending \")\". "
  270. << "Instead found "
  271. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  272. << " with text \"" << token->text << "\".";
  273. cmSystemTools::Error(error.str().c_str());
  274. return false;
  275. }
  276. lastLine = cmListFileLexer_GetCurrentLine(lexer);
  277. }
  278. cmOStringStream error;
  279. error << "Error in cmake code at\n"
  280. << filename << ":" << lastLine << ":\n"
  281. << "Parse error. Function missing ending \")\". "
  282. << "End of file reached.";
  283. cmSystemTools::Error(error.str().c_str());
  284. return false;
  285. }
  286. //----------------------------------------------------------------------------
  287. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  288. {
  289. os << lfc.FilePath;
  290. if(lfc.Line)
  291. {
  292. os << ":" << lfc.Line;
  293. if(!lfc.Name.empty())
  294. {
  295. os << " (" << lfc.Name << ")";
  296. }
  297. }
  298. return os;
  299. }