cmListFileCache.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.4",
  133. "For backwards compatibility, what version of CMake commands and "
  134. "syntax should this version of CMake try to support.",
  135. cmCacheManager::STRING);
  136. }
  137. switch (mf->GetPolicyStatus(cmPolicies::CMP_0000))
  138. {
  139. case cmPolicies::WARN:
  140. cmSystemTools::Message(
  141. mf->GetPolicies()->GetPolicyWarning
  142. (cmPolicies::CMP_0000).c_str(),"Warning");
  143. case cmPolicies::OLD:
  144. break;
  145. default:
  146. cmSystemTools::Error(
  147. mf->GetPolicies()->GetRequiredPolicyError
  148. (cmPolicies::CMP_0000).c_str());
  149. return false;
  150. }
  151. }
  152. }
  153. if(topLevel)
  154. {
  155. bool hasProject = false;
  156. // search for a project command
  157. for(std::vector<cmListFileFunction>::iterator i
  158. = this->Functions.begin();
  159. i != this->Functions.end(); ++i)
  160. {
  161. if(cmSystemTools::LowerCase(i->Name) == "project")
  162. {
  163. hasProject = true;
  164. break;
  165. }
  166. }
  167. // if no project command is found, add one
  168. if(!hasProject)
  169. {
  170. cmListFileFunction project;
  171. project.Name = "PROJECT";
  172. cmListFileArgument prj("Project", false, filename, 0);
  173. project.Arguments.push_back(prj);
  174. this->Functions.insert(this->Functions.begin(),project);
  175. }
  176. }
  177. if(parseError)
  178. {
  179. return false;
  180. }
  181. return true;
  182. }
  183. bool cmListFileCacheParseFunction(cmListFileLexer* lexer,
  184. cmListFileFunction& function,
  185. const char* filename)
  186. {
  187. // Command name has already been parsed. Read the left paren.
  188. cmListFileLexer_Token* token;
  189. if(!(token = cmListFileLexer_Scan(lexer)))
  190. {
  191. cmOStringStream error;
  192. error << "Error in cmake code at\n"
  193. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  194. << "Parse error. Function missing opening \"(\".";
  195. cmSystemTools::Error(error.str().c_str());
  196. return false;
  197. }
  198. if(token->type != cmListFileLexer_Token_ParenLeft)
  199. {
  200. cmOStringStream error;
  201. error << "Error in cmake code at\n"
  202. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer) << ":\n"
  203. << "Parse error. Expected \"(\", got "
  204. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  205. << " with text \"" << token->text << "\".";
  206. cmSystemTools::Error(error.str().c_str());
  207. return false;
  208. }
  209. // Arguments.
  210. unsigned long lastLine = cmListFileLexer_GetCurrentLine(lexer);
  211. while((token = cmListFileLexer_Scan(lexer)))
  212. {
  213. if(token->type == cmListFileLexer_Token_ParenRight)
  214. {
  215. return true;
  216. }
  217. else if(token->type == cmListFileLexer_Token_Identifier ||
  218. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  219. {
  220. cmListFileArgument a(token->text,
  221. false, filename, token->line);
  222. function.Arguments.push_back(a);
  223. }
  224. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  225. {
  226. cmListFileArgument a(token->text,
  227. true, filename, token->line);
  228. function.Arguments.push_back(a);
  229. }
  230. else if(token->type != cmListFileLexer_Token_Newline)
  231. {
  232. // Error.
  233. cmOStringStream error;
  234. error << "Error in cmake code at\n"
  235. << filename << ":" << cmListFileLexer_GetCurrentLine(lexer)
  236. << ":\n"
  237. << "Parse error. Function missing ending \")\". "
  238. << "Instead found "
  239. << cmListFileLexer_GetTypeAsString(lexer, token->type)
  240. << " with text \"" << token->text << "\".";
  241. cmSystemTools::Error(error.str().c_str());
  242. return false;
  243. }
  244. lastLine = cmListFileLexer_GetCurrentLine(lexer);
  245. }
  246. cmOStringStream error;
  247. error << "Error in cmake code at\n"
  248. << filename << ":" << lastLine << ":\n"
  249. << "Parse error. Function missing ending \")\". "
  250. << "End of file reached.";
  251. cmSystemTools::Error(error.str().c_str());
  252. return false;
  253. }