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