cmListFileCache.cxx 13 KB

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