cmListFileCache.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "cmOutputConverter.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, 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. // Inintialize 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. error << "Error in cmake code at\n" << this->FileName << ":"
  245. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  246. << "Parse error. Function missing opening \"(\".";
  247. cmSystemTools::Error(error.str().c_str());
  248. return false;
  249. }
  250. if(token->type != cmListFileLexer_Token_ParenLeft)
  251. {
  252. std::ostringstream error;
  253. error << "Error in cmake code at\n" << this->FileName << ":"
  254. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  255. << "Parse error. Expected \"(\", got "
  256. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  257. << " with text \"" << token->text << "\".";
  258. cmSystemTools::Error(error.str().c_str());
  259. return false;
  260. }
  261. // Arguments.
  262. unsigned long lastLine;
  263. unsigned long parenDepth = 0;
  264. this->Separation = SeparationOkay;
  265. while((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
  266. token = cmListFileLexer_Scan(this->Lexer)))
  267. {
  268. if(token->type == cmListFileLexer_Token_Space ||
  269. token->type == cmListFileLexer_Token_Newline)
  270. {
  271. this->Separation = SeparationOkay;
  272. continue;
  273. }
  274. if(token->type == cmListFileLexer_Token_ParenLeft)
  275. {
  276. parenDepth++;
  277. this->Separation = SeparationOkay;
  278. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  279. {
  280. return false;
  281. }
  282. }
  283. else if(token->type == cmListFileLexer_Token_ParenRight)
  284. {
  285. if (parenDepth == 0)
  286. {
  287. return true;
  288. }
  289. parenDepth--;
  290. this->Separation = SeparationOkay;
  291. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  292. {
  293. return false;
  294. }
  295. this->Separation = SeparationWarning;
  296. }
  297. else if(token->type == cmListFileLexer_Token_Identifier ||
  298. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  299. {
  300. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  301. {
  302. return false;
  303. }
  304. this->Separation = SeparationWarning;
  305. }
  306. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  307. {
  308. if(!this->AddArgument(token, cmListFileArgument::Quoted))
  309. {
  310. return false;
  311. }
  312. this->Separation = SeparationWarning;
  313. }
  314. else if(token->type == cmListFileLexer_Token_ArgumentBracket)
  315. {
  316. if(!this->AddArgument(token, cmListFileArgument::Bracket))
  317. {
  318. return false;
  319. }
  320. this->Separation = SeparationError;
  321. }
  322. else if(token->type == cmListFileLexer_Token_CommentBracket)
  323. {
  324. this->Separation = SeparationError;
  325. }
  326. else
  327. {
  328. // Error.
  329. std::ostringstream error;
  330. error << "Error in cmake code at\n" << this->FileName << ":"
  331. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  332. << "Parse error. Function missing ending \")\". "
  333. << "Instead found "
  334. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  335. << " with text \"" << token->text << "\".";
  336. cmSystemTools::Error(error.str().c_str());
  337. return false;
  338. }
  339. }
  340. std::ostringstream error;
  341. error << "Error in cmake code at\n"
  342. << this->FileName << ":" << lastLine << ":\n"
  343. << "Parse error. Function missing ending \")\". "
  344. << "End of file reached.";
  345. cmSystemTools::Error(error.str().c_str());
  346. return false;
  347. }
  348. //----------------------------------------------------------------------------
  349. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  350. cmListFileArgument::Delimiter delim)
  351. {
  352. cmListFileArgument a(token->text, delim, token->line);
  353. this->Function.Arguments.push_back(a);
  354. if(this->Separation == SeparationOkay)
  355. {
  356. return true;
  357. }
  358. bool isError = (this->Separation == SeparationError ||
  359. delim == cmListFileArgument::Bracket);
  360. std::ostringstream m;
  361. m << "Syntax " << (isError? "Error":"Warning") << " in cmake code at\n"
  362. << " " << this->FileName << ":" << token->line << ":"
  363. << token->column << "\n"
  364. << "Argument not separated from preceding token by whitespace.";
  365. if(isError)
  366. {
  367. this->Makefile->IssueMessage(cmake::FATAL_ERROR, m.str());
  368. return false;
  369. }
  370. else
  371. {
  372. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  373. return true;
  374. }
  375. }
  376. cmListFileBacktrace::cmListFileBacktrace(cmState::Snapshot snapshot,
  377. cmCommandContext const& cc)
  378. : Context(cc)
  379. , Snapshot(snapshot)
  380. {
  381. if (this->Snapshot.IsValid())
  382. {
  383. this->Snapshot.Keep();
  384. }
  385. }
  386. cmListFileBacktrace::~cmListFileBacktrace()
  387. {
  388. }
  389. void cmListFileBacktrace::PrintTitle(std::ostream& out) const
  390. {
  391. if (!this->Snapshot.IsValid())
  392. {
  393. return;
  394. }
  395. cmOutputConverter converter(this->Snapshot);
  396. cmListFileContext lfc =
  397. cmListFileContext::FromCommandContext(
  398. this->Context, this->Snapshot.GetExecutionListFile());
  399. lfc.FilePath = converter.Convert(lfc.FilePath, cmOutputConverter::HOME);
  400. out << (lfc.Line ? " at " : " in ") << lfc;
  401. }
  402. void cmListFileBacktrace::PrintCallStack(std::ostream& out) const
  403. {
  404. if (!this->Snapshot.IsValid())
  405. {
  406. return;
  407. }
  408. cmState::Snapshot parent = this->Snapshot.GetCallStackParent();
  409. if (!parent.IsValid() || parent.GetExecutionListFile().empty())
  410. {
  411. return;
  412. }
  413. cmOutputConverter converter(this->Snapshot);
  414. std::string commandName = this->Snapshot.GetEntryPointCommand();
  415. long commandLine = this->Snapshot.GetEntryPointLine();
  416. out << "Call Stack (most recent call first):\n";
  417. while(parent.IsValid())
  418. {
  419. cmListFileContext lfc;
  420. lfc.Name = commandName;
  421. lfc.Line = commandLine;
  422. lfc.FilePath = converter.Convert(parent.GetExecutionListFile(),
  423. cmOutputConverter::HOME);
  424. out << " " << lfc << "\n";
  425. commandName = parent.GetEntryPointCommand();
  426. commandLine = parent.GetEntryPointLine();
  427. parent = parent.GetCallStackParent();
  428. }
  429. }
  430. //----------------------------------------------------------------------------
  431. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  432. {
  433. os << lfc.FilePath;
  434. if(lfc.Line)
  435. {
  436. os << ":" << lfc.Line;
  437. if(!lfc.Name.empty())
  438. {
  439. os << " (" << lfc.Name << ")";
  440. }
  441. }
  442. return os;
  443. }
  444. bool operator<(const cmListFileContext& lhs, const cmListFileContext& rhs)
  445. {
  446. if(lhs.Line != rhs.Line)
  447. {
  448. return lhs.Line < rhs.Line;
  449. }
  450. return lhs.FilePath < rhs.FilePath;
  451. }
  452. bool operator==(const cmListFileContext& lhs, const cmListFileContext& rhs)
  453. {
  454. return lhs.Line == rhs.Line && lhs.FilePath == rhs.FilePath;
  455. }
  456. bool operator!=(const cmListFileContext& lhs, const cmListFileContext& rhs)
  457. {
  458. return !(lhs == rhs);
  459. }