cmListFileCache.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 "cmMakefile.h"
  13. #include "cmOutputConverter.h"
  14. #include "cmSystemTools.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, 0);
  220. project.Arguments.push_back(prj);
  221. this->Functions.insert(this->Functions.begin(),project);
  222. }
  223. }
  224. if(parseError)
  225. {
  226. return false;
  227. }
  228. return true;
  229. }
  230. //----------------------------------------------------------------------------
  231. bool cmListFileParser::ParseFunction(const char* name, long line)
  232. {
  233. // Ininitialize a new function call.
  234. this->Function = cmListFileFunction();
  235. this->Function.Name = name;
  236. this->Function.Line = line;
  237. // Command name has already been parsed. Read the left paren.
  238. cmListFileLexer_Token* token;
  239. while((token = cmListFileLexer_Scan(this->Lexer)) &&
  240. token->type == cmListFileLexer_Token_Space) {}
  241. if(!token)
  242. {
  243. std::ostringstream error;
  244. /* clang-format off */
  245. error << "Error in cmake code at\n" << this->FileName << ":"
  246. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  247. << "Parse error. Function missing opening \"(\".";
  248. /* clang-format on */
  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, 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. /* clang-format off */
  364. m << "Syntax " << (isError? "Error":"Warning") << " in cmake code at\n"
  365. << " " << this->FileName << ":" << token->line << ":"
  366. << token->column << "\n"
  367. << "Argument not separated from preceding token by whitespace.";
  368. /* clang-format on */
  369. if(isError)
  370. {
  371. this->Makefile->IssueMessage(cmake::FATAL_ERROR, m.str());
  372. return false;
  373. }
  374. else
  375. {
  376. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  377. return true;
  378. }
  379. }
  380. struct cmListFileBacktrace::Entry: public cmListFileContext
  381. {
  382. Entry(cmListFileContext const& lfc, Entry* up):
  383. cmListFileContext(lfc), Up(up), RefCount(0)
  384. {
  385. if (this->Up)
  386. {
  387. this->Up->Ref();
  388. }
  389. }
  390. ~Entry()
  391. {
  392. if (this->Up)
  393. {
  394. this->Up->Unref();
  395. }
  396. }
  397. void Ref()
  398. {
  399. ++this->RefCount;
  400. }
  401. void Unref()
  402. {
  403. if (--this->RefCount == 0)
  404. {
  405. delete this;
  406. }
  407. }
  408. Entry* Up;
  409. unsigned int RefCount;
  410. };
  411. cmListFileBacktrace::cmListFileBacktrace(cmState::Snapshot bottom,
  412. Entry* up,
  413. cmListFileContext const& lfc):
  414. Bottom(bottom), Cur(new Entry(lfc, up))
  415. {
  416. assert(this->Bottom.IsValid());
  417. this->Cur->Ref();
  418. }
  419. cmListFileBacktrace::cmListFileBacktrace(cmState::Snapshot bottom, Entry* cur):
  420. Bottom(bottom), Cur(cur)
  421. {
  422. if (this->Cur)
  423. {
  424. assert(this->Bottom.IsValid());
  425. this->Cur->Ref();
  426. }
  427. }
  428. cmListFileBacktrace::cmListFileBacktrace(): Bottom(), Cur(0)
  429. {
  430. }
  431. cmListFileBacktrace::cmListFileBacktrace(cmState::Snapshot snapshot):
  432. Bottom(snapshot.GetCallStackBottom()), Cur(0)
  433. {
  434. }
  435. cmListFileBacktrace::cmListFileBacktrace(cmListFileBacktrace const& r):
  436. Bottom(r.Bottom), Cur(r.Cur)
  437. {
  438. if (this->Cur)
  439. {
  440. assert(this->Bottom.IsValid());
  441. this->Cur->Ref();
  442. }
  443. }
  444. cmListFileBacktrace&
  445. cmListFileBacktrace::operator=(cmListFileBacktrace const& r)
  446. {
  447. cmListFileBacktrace tmp(r);
  448. std::swap(this->Cur, tmp.Cur);
  449. std::swap(this->Bottom, tmp.Bottom);
  450. return *this;
  451. }
  452. cmListFileBacktrace::~cmListFileBacktrace()
  453. {
  454. if (this->Cur)
  455. {
  456. this->Cur->Unref();
  457. }
  458. }
  459. cmListFileBacktrace
  460. cmListFileBacktrace::Push(std::string const& file) const
  461. {
  462. // We are entering a file-level scope but have not yet reached
  463. // any specific line or command invocation within it. This context
  464. // is useful to print when it is at the top but otherwise can be
  465. // skipped during call stack printing.
  466. cmListFileContext lfc;
  467. lfc.FilePath = file;
  468. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  469. }
  470. cmListFileBacktrace
  471. cmListFileBacktrace::Push(cmListFileContext const& lfc) const
  472. {
  473. return cmListFileBacktrace(this->Bottom, this->Cur, lfc);
  474. }
  475. cmListFileBacktrace cmListFileBacktrace::Pop() const
  476. {
  477. assert(this->Cur);
  478. return cmListFileBacktrace(this->Bottom, this->Cur->Up);
  479. }
  480. cmListFileContext const& cmListFileBacktrace::Top() const
  481. {
  482. if (this->Cur)
  483. {
  484. return *this->Cur;
  485. }
  486. else
  487. {
  488. static cmListFileContext const empty;
  489. return empty;
  490. }
  491. }
  492. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  493. {
  494. if (!this->Cur)
  495. {
  496. return;
  497. }
  498. cmOutputConverter converter(this->Bottom);
  499. cmListFileContext lfc = *this->Cur;
  500. if (!this->Bottom.GetState()->GetIsInTryCompile())
  501. {
  502. lfc.FilePath = converter.Convert(lfc.FilePath, cmOutputConverter::HOME);
  503. }
  504. out << (lfc.Line ? " at " : " in ") << lfc;
  505. }
  506. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  507. {
  508. if (!this->Cur || !this->Cur->Up)
  509. {
  510. return;
  511. }
  512. bool first = true;
  513. cmOutputConverter converter(this->Bottom);
  514. for (Entry* i = this->Cur->Up; i; i = i->Up)
  515. {
  516. if (i->Name.empty())
  517. {
  518. // Skip this whole-file scope. When we get here we already will
  519. // have printed a more-specific context within the file.
  520. continue;
  521. }
  522. if (first)
  523. {
  524. first = false;
  525. out << "Call Stack (most recent call first):\n";
  526. }
  527. cmListFileContext lfc = *i;
  528. if (!this->Bottom.GetState()->GetIsInTryCompile())
  529. {
  530. lfc.FilePath = converter.Convert(lfc.FilePath, cmOutputConverter::HOME);
  531. }
  532. out << " " << lfc << "\n";
  533. }
  534. }
  535. //----------------------------------------------------------------------------
  536. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  537. {
  538. os << lfc.FilePath;
  539. if(lfc.Line)
  540. {
  541. os << ":" << lfc.Line;
  542. if(!lfc.Name.empty())
  543. {
  544. os << " (" << lfc.Name << ")";
  545. }
  546. }
  547. return os;
  548. }
  549. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  550. {
  551. if(lhs.Line != rhs.Line)
  552. {
  553. return lhs.Line < rhs.Line;
  554. }
  555. return lhs.FilePath < rhs.FilePath;
  556. }
  557. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  558. {
  559. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  560. }
  561. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  562. {
  563. return !(lhs == rhs);
  564. }