cmListFileCache.cxx 12 KB

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