cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmListFileCache.h"
  11. #include "cmListFileLexer.h"
  12. #include "cmSystemTools.h"
  13. #include "cmMakefile.h"
  14. #include "cmVersion.h"
  15. #include <cmsys/RegularExpression.hxx>
  16. #ifdef __BORLANDC__
  17. # pragma warn -8060 /* possibly incorrect assignment */
  18. #endif
  19. //----------------------------------------------------------------------------
  20. struct cmListFileParser
  21. {
  22. cmListFileParser(cmListFile* lf, cmMakefile* mf, const char* filename);
  23. ~cmListFileParser();
  24. bool ParseFile();
  25. bool ParseFunction(const char* name, long line);
  26. bool AddArgument(cmListFileLexer_Token* token,
  27. cmListFileArgument::Delimiter delim);
  28. cmListFile* ListFile;
  29. cmMakefile* Makefile;
  30. const char* FileName;
  31. cmListFileLexer* Lexer;
  32. cmListFileFunction Function;
  33. enum { SeparationOkay, SeparationWarning, SeparationError} Separation;
  34. };
  35. //----------------------------------------------------------------------------
  36. cmListFileParser::cmListFileParser(cmListFile* lf, cmMakefile* mf,
  37. const char* filename):
  38. ListFile(lf), Makefile(mf), FileName(filename),
  39. Lexer(cmListFileLexer_New())
  40. {
  41. }
  42. //----------------------------------------------------------------------------
  43. cmListFileParser::~cmListFileParser()
  44. {
  45. cmListFileLexer_Delete(this->Lexer);
  46. }
  47. //----------------------------------------------------------------------------
  48. bool cmListFileParser::ParseFile()
  49. {
  50. // Open the file.
  51. cmListFileLexer_BOM bom;
  52. if(!cmListFileLexer_SetFileName(this->Lexer, this->FileName, &bom))
  53. {
  54. cmSystemTools::Error("cmListFileCache: error can not open file ",
  55. this->FileName);
  56. return false;
  57. }
  58. // Verify the Byte-Order-Mark, if any.
  59. if(bom != cmListFileLexer_BOM_None &&
  60. bom != cmListFileLexer_BOM_UTF8)
  61. {
  62. cmListFileLexer_SetFileName(this->Lexer, 0, 0);
  63. cmOStringStream m;
  64. m << "File\n " << this->FileName << "\n"
  65. << "starts with a Byte-Order-Mark that is not UTF-8.";
  66. this->Makefile->IssueMessage(cmake::FATAL_ERROR, m.str());
  67. return false;
  68. }
  69. // Use a simple recursive-descent parser to process the token
  70. // stream.
  71. bool haveNewline = true;
  72. while(cmListFileLexer_Token* token =
  73. cmListFileLexer_Scan(this->Lexer))
  74. {
  75. if(token->type == cmListFileLexer_Token_Space)
  76. {
  77. }
  78. else if(token->type == cmListFileLexer_Token_Newline)
  79. {
  80. haveNewline = true;
  81. }
  82. else if(token->type == cmListFileLexer_Token_CommentBracket)
  83. {
  84. haveNewline = false;
  85. }
  86. else if(token->type == cmListFileLexer_Token_Identifier)
  87. {
  88. if(haveNewline)
  89. {
  90. haveNewline = false;
  91. if(this->ParseFunction(token->text, token->line))
  92. {
  93. this->ListFile->Functions.push_back(this->Function);
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. else
  101. {
  102. cmOStringStream error;
  103. error << "Error in cmake code at\n"
  104. << this->FileName << ":" << token->line << ":\n"
  105. << "Parse error. Expected a newline, got "
  106. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  107. << " with text \"" << token->text << "\".";
  108. cmSystemTools::Error(error.str().c_str());
  109. return false;
  110. }
  111. }
  112. else
  113. {
  114. cmOStringStream error;
  115. error << "Error in cmake code at\n"
  116. << this->FileName << ":" << token->line << ":\n"
  117. << "Parse error. Expected a command name, got "
  118. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  119. << " with text \""
  120. << token->text << "\".";
  121. cmSystemTools::Error(error.str().c_str());
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. //----------------------------------------------------------------------------
  128. bool cmListFile::ParseFile(const char* filename,
  129. bool topLevel,
  130. cmMakefile *mf)
  131. {
  132. if(!cmSystemTools::FileExists(filename))
  133. {
  134. return false;
  135. }
  136. bool parseError = false;
  137. this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
  138. {
  139. cmListFileParser parser(this, mf, filename);
  140. parseError = !parser.ParseFile();
  141. }
  142. if(parseError)
  143. {
  144. this->ModifiedTime = 0;
  145. }
  146. // do we need a cmake_policy(VERSION call?
  147. if(topLevel)
  148. {
  149. bool hasVersion = false;
  150. // search for the right policy command
  151. for(std::vector<cmListFileFunction>::iterator i
  152. = this->Functions.begin();
  153. i != this->Functions.end(); ++i)
  154. {
  155. if (cmSystemTools::LowerCase(i->Name) == "cmake_minimum_required")
  156. {
  157. hasVersion = true;
  158. break;
  159. }
  160. }
  161. // if no policy command is found this is an error if they use any
  162. // non advanced functions or a lot of functions
  163. if(!hasVersion)
  164. {
  165. bool isProblem = true;
  166. if (this->Functions.size() < 30)
  167. {
  168. // the list of simple commands DO NOT ADD TO THIS LIST!!!!!
  169. // these commands must have backwards compatibility forever and
  170. // and that is a lot longer than your tiny mind can comprehend mortal
  171. std::set<std::string> allowedCommands;
  172. allowedCommands.insert("project");
  173. allowedCommands.insert("set");
  174. allowedCommands.insert("if");
  175. allowedCommands.insert("endif");
  176. allowedCommands.insert("else");
  177. allowedCommands.insert("elseif");
  178. allowedCommands.insert("add_executable");
  179. allowedCommands.insert("add_library");
  180. allowedCommands.insert("target_link_libraries");
  181. allowedCommands.insert("option");
  182. allowedCommands.insert("message");
  183. isProblem = false;
  184. for(std::vector<cmListFileFunction>::iterator i
  185. = this->Functions.begin();
  186. i != this->Functions.end(); ++i)
  187. {
  188. std::string name = cmSystemTools::LowerCase(i->Name);
  189. if (allowedCommands.find(name) == allowedCommands.end())
  190. {
  191. isProblem = true;
  192. break;
  193. }
  194. }
  195. }
  196. if (isProblem)
  197. {
  198. // Tell the top level cmMakefile to diagnose
  199. // this violation of CMP0000.
  200. mf->SetCheckCMP0000(true);
  201. // Implicitly set the version for the user.
  202. mf->SetPolicyVersion("2.4");
  203. }
  204. }
  205. }
  206. if(topLevel)
  207. {
  208. bool hasProject = false;
  209. // search for a project command
  210. for(std::vector<cmListFileFunction>::iterator i
  211. = this->Functions.begin();
  212. i != this->Functions.end(); ++i)
  213. {
  214. if(cmSystemTools::LowerCase(i->Name) == "project")
  215. {
  216. hasProject = true;
  217. break;
  218. }
  219. }
  220. // if no project command is found, add one
  221. if(!hasProject)
  222. {
  223. cmListFileFunction project;
  224. project.Name = "PROJECT";
  225. cmListFileArgument prj("Project", cmListFileArgument::Unquoted,
  226. filename, 0);
  227. project.Arguments.push_back(prj);
  228. this->Functions.insert(this->Functions.begin(),project);
  229. }
  230. }
  231. if(parseError)
  232. {
  233. return false;
  234. }
  235. return true;
  236. }
  237. //----------------------------------------------------------------------------
  238. bool cmListFileParser::ParseFunction(const char* name, long line)
  239. {
  240. // Inintialize a new function call.
  241. this->Function = cmListFileFunction();
  242. this->Function.FilePath = this->FileName;
  243. this->Function.Name = name;
  244. this->Function.Line = line;
  245. // Command name has already been parsed. Read the left paren.
  246. cmListFileLexer_Token* token;
  247. while((token = cmListFileLexer_Scan(this->Lexer)) &&
  248. token->type == cmListFileLexer_Token_Space) {}
  249. if(!token)
  250. {
  251. cmOStringStream error;
  252. error << "Error in cmake code at\n" << this->FileName << ":"
  253. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  254. << "Parse error. Function missing opening \"(\".";
  255. cmSystemTools::Error(error.str().c_str());
  256. return false;
  257. }
  258. if(token->type != cmListFileLexer_Token_ParenLeft)
  259. {
  260. cmOStringStream error;
  261. error << "Error in cmake code at\n" << this->FileName << ":"
  262. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  263. << "Parse error. Expected \"(\", got "
  264. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  265. << " with text \"" << token->text << "\".";
  266. cmSystemTools::Error(error.str().c_str());
  267. return false;
  268. }
  269. // Arguments.
  270. unsigned long lastLine;
  271. unsigned long parenDepth = 0;
  272. this->Separation = SeparationOkay;
  273. while((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
  274. token = cmListFileLexer_Scan(this->Lexer)))
  275. {
  276. if(token->type == cmListFileLexer_Token_Space ||
  277. token->type == cmListFileLexer_Token_Newline)
  278. {
  279. this->Separation = SeparationOkay;
  280. continue;
  281. }
  282. if(token->type == cmListFileLexer_Token_ParenLeft)
  283. {
  284. parenDepth++;
  285. this->Separation = SeparationOkay;
  286. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  287. {
  288. return false;
  289. }
  290. }
  291. else if(token->type == cmListFileLexer_Token_ParenRight)
  292. {
  293. if (parenDepth == 0)
  294. {
  295. return true;
  296. }
  297. parenDepth--;
  298. this->Separation = SeparationOkay;
  299. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  300. {
  301. return false;
  302. }
  303. this->Separation = SeparationWarning;
  304. }
  305. else if(token->type == cmListFileLexer_Token_Identifier ||
  306. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  307. {
  308. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  309. {
  310. return false;
  311. }
  312. this->Separation = SeparationWarning;
  313. }
  314. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  315. {
  316. if(!this->AddArgument(token, cmListFileArgument::Quoted))
  317. {
  318. return false;
  319. }
  320. this->Separation = SeparationWarning;
  321. }
  322. else if(token->type == cmListFileLexer_Token_ArgumentBracket)
  323. {
  324. if(!this->AddArgument(token, cmListFileArgument::Bracket))
  325. {
  326. return false;
  327. }
  328. this->Separation = SeparationError;
  329. }
  330. else if(token->type == cmListFileLexer_Token_CommentBracket)
  331. {
  332. this->Separation = SeparationError;
  333. }
  334. else
  335. {
  336. // Error.
  337. cmOStringStream error;
  338. error << "Error in cmake code at\n" << this->FileName << ":"
  339. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  340. << "Parse error. Function missing ending \")\". "
  341. << "Instead found "
  342. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  343. << " with text \"" << token->text << "\".";
  344. cmSystemTools::Error(error.str().c_str());
  345. return false;
  346. }
  347. }
  348. cmOStringStream error;
  349. error << "Error in cmake code at\n"
  350. << this->FileName << ":" << lastLine << ":\n"
  351. << "Parse error. Function missing ending \")\". "
  352. << "End of file reached.";
  353. cmSystemTools::Error(error.str().c_str());
  354. return false;
  355. }
  356. //----------------------------------------------------------------------------
  357. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  358. cmListFileArgument::Delimiter delim)
  359. {
  360. cmListFileArgument a(token->text, delim, this->FileName, token->line);
  361. this->Function.Arguments.push_back(a);
  362. if(this->Separation == SeparationOkay)
  363. {
  364. return true;
  365. }
  366. bool isError = (this->Separation == SeparationError ||
  367. delim == cmListFileArgument::Bracket);
  368. cmOStringStream m;
  369. m << "Syntax " << (isError? "Error":"Warning") << " in cmake code at\n"
  370. << " " << this->FileName << ":" << token->line << ":"
  371. << token->column << "\n"
  372. << "Argument not separated from preceding token by whitespace.";
  373. if(isError)
  374. {
  375. this->Makefile->IssueMessage(cmake::FATAL_ERROR, m.str().c_str());
  376. return false;
  377. }
  378. else
  379. {
  380. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, m.str().c_str());
  381. return true;
  382. }
  383. }
  384. //----------------------------------------------------------------------------
  385. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  386. {
  387. os << lfc.FilePath;
  388. if(lfc.Line)
  389. {
  390. os << ":" << lfc.Line;
  391. if(!lfc.Name.empty())
  392. {
  393. os << " (" << lfc.Name << ")";
  394. }
  395. }
  396. return os;
  397. }