cmListFileCache.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <cmsys/RegularExpression.hxx>
  18. #ifdef __BORLANDC__
  19. # pragma warn -8060 /* possibly incorrect assignment */
  20. #endif
  21. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  22. cmListFileFunction& function,
  23. const char* filename);
  24. bool cmListFile::ParseFile(const char* filename,
  25. bool topLevel,
  26. cmMakefile *mf)
  27. {
  28. if(!cmSystemTools::FileExists(filename))
  29. {
  30. return false;
  31. }
  32. // Create the scanner.
  33. cmListFileLexer* lexer = cmListFileLexer_New();
  34. if(!lexer)
  35. {
  36. cmSystemTools::Error("cmListFileCache: error allocating lexer ");
  37. return false;
  38. }
  39. // Open the file.
  40. if(!cmListFileLexer_SetFileName(lexer, filename))
  41. {
  42. cmListFileLexer_Delete(lexer);
  43. cmSystemTools::Error("cmListFileCache: error can not open file ",
  44. filename);
  45. return false;
  46. }
  47. // Use a simple recursive-descent parser to process the token
  48. // stream.
  49. this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
  50. bool parseError = false;
  51. bool haveNewline = true;
  52. cmListFileLexer_Token* token;
  53. while(!parseError && (token = cmListFileLexer_Scan(lexer)))
  54. {
  55. if(token->type == cmListFileLexer_Token_Newline)
  56. {
  57. haveNewline = true;
  58. }
  59. else if(token->type == cmListFileLexer_Token_Identifier)
  60. {
  61. if(haveNewline)
  62. {
  63. haveNewline = false;
  64. cmListFileFunction inFunction;
  65. inFunction.Name = token->text;
  66. inFunction.FilePath = filename;
  67. inFunction.Line = token->line;
  68. if(cmListFileCacheParseFunction(lexer, inFunction, filename))
  69. {
  70. this->Functions.push_back(inFunction);
  71. }
  72. else
  73. {
  74. parseError = true;
  75. }
  76. }
  77. else
  78. {
  79. cmOStringStream error;
  80. error << "Error in cmake code at\n"
  81. << filename << ":" << token->line << ":\n"
  82. << "Parse error. Expected a newline, got "
  83. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  84. << " with text \"" << token->text << "\".";
  85. cmSystemTools::Error(error.str().c_str());
  86. parseError = true;
  87. }
  88. }
  89. else
  90. {
  91. cmOStringStream error;
  92. error << "Error in cmake code at\n"
  93. << filename << ":" << token->line << ":\n"
  94. << "Parse error. Expected a command name, got "
  95. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  96. << " with text \""
  97. << token->text << "\".";
  98. cmSystemTools::Error(error.str().c_str());
  99. parseError = true;
  100. }
  101. }
  102. if (parseError)
  103. {
  104. this->ModifiedTime = 0;
  105. }
  106. cmListFileLexer_Delete(lexer);
  107. // do we need a cmake_policy(VERSION call?
  108. if(topLevel)
  109. {
  110. bool hasPolicy = false;
  111. // search for the right policy command
  112. for(std::vector<cmListFileFunction>::iterator i
  113. = this->Functions.begin();
  114. i != this->Functions.end(); ++i)
  115. {
  116. if (cmSystemTools::LowerCase(i->Name) == "cmake_policy" &&
  117. i->Arguments.size() &&
  118. cmSystemTools::LowerCase(i->Arguments[0].Value) == "version")
  119. {
  120. hasPolicy = true;
  121. break;
  122. }
  123. }
  124. // if no policy command is found this is an error
  125. if(!hasPolicy)
  126. {
  127. // add in the old CMAKE_BACKWARDS_COMPATIBILITY var for old CMake compatibility
  128. if (!mf->GetCacheManager()->
  129. GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  130. {
  131. mf->AddCacheDefinition
  132. ("CMAKE_BACKWARDS_COMPATIBILITY", "2.5",
  133. "For backwards compatibility, what version of CMake "
  134. "commands and "
  135. "syntax should this version of CMake try to support.",
  136. cmCacheManager::STRING);
  137. }
  138. switch (mf->GetPolicyStatus(cmPolicies::CMP_0000))
  139. {
  140. case cmPolicies::WARN:
  141. mf->IssueWarning(
  142. mf->GetPolicies()->GetPolicyWarning(cmPolicies::CMP_0000)
  143. );
  144. case cmPolicies::OLD:
  145. break;
  146. default:
  147. mf->IssueError(
  148. mf->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP_0000)
  149. );
  150. return false;
  151. }
  152. }
  153. else
  154. {
  155. // add in the old CMAKE_BACKWARDS_COMPATIBILITY var for old CMake compatibility
  156. if (!mf->GetCacheManager()->
  157. GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  158. {
  159. mf->AddCacheDefinition
  160. ("CMAKE_BACKWARDS_COMPATIBILITY", "2.5",
  161. "For backwards compatibility, what version of CMake "
  162. "commands and "
  163. "syntax should this version of CMake try to support.",
  164. cmCacheManager::INTERNAL);
  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. while((token = cmListFileLexer_Scan(lexer)))
  227. {
  228. if(token->type == cmListFileLexer_Token_ParenRight)
  229. {
  230. return true;
  231. }
  232. else if(token->type == cmListFileLexer_Token_Identifier ||
  233. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  234. {
  235. cmListFileArgument a(token->text,
  236. false, filename, token->line);
  237. function.Arguments.push_back(a);
  238. }
  239. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  240. {
  241. cmListFileArgument a(token->text,
  242. true, filename, token->line);
  243. function.Arguments.push_back(a);
  244. }
  245. else if(token->type != cmListFileLexer_Token_Newline)
  246. {
  247. // Error.
  248. cmOStringStream error;
  249. error << "Error in cmake code at\n"
  250. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer)
  251. << ":\n"
  252. << "Parse error. Function missing ending \")\". "
  253. << "Instead found "
  254. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  255. << " with text \"" << token->text << "\".";
  256. cmSystemTools::Error(error.str().c_str());
  257. return false;
  258. }
  259. lastLine = cmListFileLexer_GetCurrentLine(lexer);
  260. }
  261. cmOStringStream error;
  262. error << "Error in cmake code at\n"
  263. << filename << ":" << lastLine << ":\n"
  264. << "Parse error. Function missing ending \")\". "
  265. << "End of file reached.";
  266. cmSystemTools::Error(error.str().c_str());
  267. return false;
  268. }