cmListFileCache.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. {
  135. return false;
  136. }
  137. bool parseError = false;
  138. this->ModifiedTime = cmSystemTools::ModifiedTime(filename);
  139. {
  140. cmListFileParser parser(this, mf, filename);
  141. parseError = !parser.ParseFile();
  142. }
  143. if(parseError)
  144. {
  145. this->ModifiedTime = 0;
  146. }
  147. // do we need a cmake_policy(VERSION call?
  148. if(topLevel)
  149. {
  150. bool hasVersion = false;
  151. // search for the right policy command
  152. for(std::vector<cmListFileFunction>::iterator i
  153. = this->Functions.begin();
  154. i != this->Functions.end(); ++i)
  155. {
  156. if (cmSystemTools::LowerCase(i->Name) == "cmake_minimum_required")
  157. {
  158. hasVersion = true;
  159. break;
  160. }
  161. }
  162. // if no policy command is found this is an error if they use any
  163. // non advanced functions or a lot of functions
  164. if(!hasVersion)
  165. {
  166. bool isProblem = true;
  167. if (this->Functions.size() < 30)
  168. {
  169. // the list of simple commands DO NOT ADD TO THIS LIST!!!!!
  170. // these commands must have backwards compatibility forever and
  171. // and that is a lot longer than your tiny mind can comprehend mortal
  172. std::set<std::string> allowedCommands;
  173. allowedCommands.insert("project");
  174. allowedCommands.insert("set");
  175. allowedCommands.insert("if");
  176. allowedCommands.insert("endif");
  177. allowedCommands.insert("else");
  178. allowedCommands.insert("elseif");
  179. allowedCommands.insert("add_executable");
  180. allowedCommands.insert("add_library");
  181. allowedCommands.insert("target_link_libraries");
  182. allowedCommands.insert("option");
  183. allowedCommands.insert("message");
  184. isProblem = false;
  185. for(std::vector<cmListFileFunction>::iterator i
  186. = this->Functions.begin();
  187. i != this->Functions.end(); ++i)
  188. {
  189. std::string name = cmSystemTools::LowerCase(i->Name);
  190. if (allowedCommands.find(name) == allowedCommands.end())
  191. {
  192. isProblem = true;
  193. break;
  194. }
  195. }
  196. }
  197. if (isProblem)
  198. {
  199. // Tell the top level cmMakefile to diagnose
  200. // this violation of CMP0000.
  201. mf->SetCheckCMP0000(true);
  202. // Implicitly set the version for the user.
  203. mf->SetPolicyVersion("2.4");
  204. }
  205. }
  206. }
  207. if(topLevel)
  208. {
  209. bool hasProject = false;
  210. // search for a project command
  211. for(std::vector<cmListFileFunction>::iterator i
  212. = this->Functions.begin();
  213. i != this->Functions.end(); ++i)
  214. {
  215. if(cmSystemTools::LowerCase(i->Name) == "project")
  216. {
  217. hasProject = true;
  218. break;
  219. }
  220. }
  221. // if no project command is found, add one
  222. if(!hasProject)
  223. {
  224. cmListFileFunction project;
  225. project.Name = "PROJECT";
  226. cmListFileArgument prj("Project", cmListFileArgument::Unquoted,
  227. filename, 0);
  228. project.Arguments.push_back(prj);
  229. this->Functions.insert(this->Functions.begin(),project);
  230. }
  231. }
  232. if(parseError)
  233. {
  234. return false;
  235. }
  236. return true;
  237. }
  238. //----------------------------------------------------------------------------
  239. bool cmListFileParser::ParseFunction(const char* name, long line)
  240. {
  241. // Inintialize a new function call.
  242. this->Function = cmListFileFunction();
  243. this->Function.FilePath = this->FileName;
  244. this->Function.Name = name;
  245. this->Function.Line = line;
  246. // Command name has already been parsed. Read the left paren.
  247. cmListFileLexer_Token* token;
  248. while((token = cmListFileLexer_Scan(this->Lexer)) &&
  249. token->type == cmListFileLexer_Token_Space) {}
  250. if(!token)
  251. {
  252. cmOStringStream error;
  253. error << "Error in cmake code at\n" << this->FileName << ":"
  254. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  255. << "Parse error. Function missing opening \"(\".";
  256. cmSystemTools::Error(error.str().c_str());
  257. return false;
  258. }
  259. if(token->type != cmListFileLexer_Token_ParenLeft)
  260. {
  261. cmOStringStream error;
  262. error << "Error in cmake code at\n" << this->FileName << ":"
  263. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  264. << "Parse error. Expected \"(\", got "
  265. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  266. << " with text \"" << token->text << "\".";
  267. cmSystemTools::Error(error.str().c_str());
  268. return false;
  269. }
  270. // Arguments.
  271. unsigned long lastLine;
  272. unsigned long parenDepth = 0;
  273. this->Separation = SeparationOkay;
  274. while((lastLine = cmListFileLexer_GetCurrentLine(this->Lexer),
  275. token = cmListFileLexer_Scan(this->Lexer)))
  276. {
  277. if(token->type == cmListFileLexer_Token_Space ||
  278. token->type == cmListFileLexer_Token_Newline)
  279. {
  280. this->Separation = SeparationOkay;
  281. continue;
  282. }
  283. if(token->type == cmListFileLexer_Token_ParenLeft)
  284. {
  285. parenDepth++;
  286. this->Separation = SeparationOkay;
  287. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  288. {
  289. return false;
  290. }
  291. }
  292. else if(token->type == cmListFileLexer_Token_ParenRight)
  293. {
  294. if (parenDepth == 0)
  295. {
  296. return true;
  297. }
  298. parenDepth--;
  299. this->Separation = SeparationOkay;
  300. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  301. {
  302. return false;
  303. }
  304. this->Separation = SeparationWarning;
  305. }
  306. else if(token->type == cmListFileLexer_Token_Identifier ||
  307. token->type == cmListFileLexer_Token_ArgumentUnquoted)
  308. {
  309. if(!this->AddArgument(token, cmListFileArgument::Unquoted))
  310. {
  311. return false;
  312. }
  313. this->Separation = SeparationWarning;
  314. }
  315. else if(token->type == cmListFileLexer_Token_ArgumentQuoted)
  316. {
  317. if(!this->AddArgument(token, cmListFileArgument::Quoted))
  318. {
  319. return false;
  320. }
  321. this->Separation = SeparationWarning;
  322. }
  323. else if(token->type == cmListFileLexer_Token_ArgumentBracket)
  324. {
  325. if(!this->AddArgument(token, cmListFileArgument::Bracket))
  326. {
  327. return false;
  328. }
  329. this->Separation = SeparationError;
  330. }
  331. else if(token->type == cmListFileLexer_Token_CommentBracket)
  332. {
  333. this->Separation = SeparationError;
  334. }
  335. else
  336. {
  337. // Error.
  338. cmOStringStream error;
  339. error << "Error in cmake code at\n" << this->FileName << ":"
  340. << cmListFileLexer_GetCurrentLine(this->Lexer) << ":\n"
  341. << "Parse error. Function missing ending \")\". "
  342. << "Instead found "
  343. << cmListFileLexer_GetTypeAsString(this->Lexer, token->type)
  344. << " with text \"" << token->text << "\".";
  345. cmSystemTools::Error(error.str().c_str());
  346. return false;
  347. }
  348. }
  349. cmOStringStream error;
  350. error << "Error in cmake code at\n"
  351. << this->FileName << ":" << lastLine << ":\n"
  352. << "Parse error. Function missing ending \")\". "
  353. << "End of file reached.";
  354. cmSystemTools::Error(error.str().c_str());
  355. return false;
  356. }
  357. //----------------------------------------------------------------------------
  358. bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
  359. cmListFileArgument::Delimiter delim)
  360. {
  361. cmListFileArgument a(token->text, delim, this->FileName, token->line);
  362. this->Function.Arguments.push_back(a);
  363. if(this->Separation == SeparationOkay)
  364. {
  365. return true;
  366. }
  367. bool isError = (this->Separation == SeparationError ||
  368. delim == cmListFileArgument::Bracket);
  369. cmOStringStream m;
  370. m << "Syntax " << (isError? "Error":"Warning") << " in cmake code at\n"
  371. << " " << this->FileName << ":" << token->line << ":"
  372. << token->column << "\n"
  373. << "Argument not separated from preceding token by whitespace.";
  374. if(isError)
  375. {
  376. this->Makefile->IssueMessage(cmake::FATAL_ERROR, m.str());
  377. return false;
  378. }
  379. else
  380. {
  381. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  382. return true;
  383. }
  384. }
  385. //----------------------------------------------------------------------------
  386. void cmListFileBacktrace::MakeRelative()
  387. {
  388. if (this->Relative)
  389. {
  390. return;
  391. }
  392. for (cmListFileBacktrace::iterator i = this->begin();
  393. i != this->end(); ++i)
  394. {
  395. i->FilePath = this->LocalGenerator->Convert(i->FilePath,
  396. cmLocalGenerator::HOME);
  397. }
  398. this->Relative = true;
  399. }
  400. //----------------------------------------------------------------------------
  401. std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
  402. {
  403. os << lfc.FilePath;
  404. if(lfc.Line)
  405. {
  406. os << ":" << lfc.Line;
  407. if(!lfc.Name.empty())
  408. {
  409. os << " (" << lfc.Name << ")";
  410. }
  411. }
  412. return os;
  413. }