cmIfCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmIfCommand.h"
  14. #include <stdlib.h> // required for atof
  15. #include <deque>
  16. #include <cmsys/RegularExpression.hxx>
  17. bool cmIfFunctionBlocker::
  18. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  19. {
  20. const char* name = lff.m_Name.c_str();
  21. const std::vector<cmListFileArgument>& args = lff.m_Arguments;
  22. // always let if statements through
  23. if (!strcmp(name,"IF"))
  24. {
  25. return false;
  26. }
  27. // watch for our ELSE or ENDIF
  28. if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
  29. {
  30. if (args == m_Args)
  31. {
  32. // if it was an else statement then we should change state
  33. // and block this Else Command
  34. if (!strcmp(name,"ELSE"))
  35. {
  36. m_IsBlocking = !m_IsBlocking;
  37. return true;
  38. }
  39. // otherwise it must be an ENDIF statement, in that case remove the
  40. // function blocker
  41. mf.RemoveFunctionBlocker(lff);
  42. return true;
  43. }
  44. else if(args.empty())
  45. {
  46. std::string err = "Empty arguments for ";
  47. err += name;
  48. err += ". Did you mean ";
  49. err += name;
  50. err += "( ";
  51. for(std::vector<cmListFileArgument>::const_iterator a = m_Args.begin();
  52. a != m_Args.end();++a)
  53. {
  54. err += (a->Quoted?"\"":"");
  55. err += a->Value;
  56. err += (a->Quoted?"\"":"");
  57. err += " ";
  58. }
  59. err += ")?";
  60. cmSystemTools::Error(err.c_str());
  61. }
  62. }
  63. return m_IsBlocking;
  64. }
  65. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  66. cmMakefile&)
  67. {
  68. if (lff.m_Name == "ENDIF")
  69. {
  70. if (lff.m_Arguments == m_Args)
  71. {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. void cmIfFunctionBlocker::
  78. ScopeEnded(cmMakefile &mf)
  79. {
  80. std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  81. errmsg += mf.GetCurrentDirectory();
  82. errmsg += "\nThe arguments are: ";
  83. for(std::vector<cmListFileArgument>::const_iterator j = m_Args.begin();
  84. j != m_Args.end(); ++j)
  85. {
  86. errmsg += (j->Quoted?"\"":"");
  87. errmsg += j->Value;
  88. errmsg += (j->Quoted?"\"":"");
  89. errmsg += " ";
  90. }
  91. cmSystemTools::Message(errmsg.c_str(), "Warning");
  92. }
  93. bool cmIfCommand::InvokeInitialPass(const std::vector<cmListFileArgument>& args)
  94. {
  95. bool isValid;
  96. std::vector<std::string> expandedArguments;
  97. m_Makefile->ExpandArguments(args, expandedArguments);
  98. bool isTrue = cmIfCommand::IsTrue(expandedArguments,isValid,m_Makefile);
  99. if (!isValid)
  100. {
  101. std::string err = "An IF command had incorrect arguments: ";
  102. unsigned int i;
  103. for(i =0; i < args.size(); ++i)
  104. {
  105. err += (args[i].Quoted?"\"":"");
  106. err += args[i].Value;
  107. err += (args[i].Quoted?"\"":"");
  108. err += " ";
  109. }
  110. this->SetError(err.c_str());
  111. return false;
  112. }
  113. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  114. // if is isn't true block the commands
  115. f->m_IsBlocking = !isTrue;
  116. f->m_Args = args;
  117. m_Makefile->AddFunctionBlocker(f);
  118. return true;
  119. }
  120. // order of operations,
  121. // EXISTS COMMAND DEFINED
  122. // MATCHES LESS GREATER EQUAL STRLESS STRGREATER
  123. // AND OR
  124. //
  125. // There is an issue on whether the arguments should be values of references,
  126. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  127. // or should it really do IF (${FOO} AND ${BAR}) Currently EXISTS COMMAND and
  128. // DEFINED all take values. EQUAL, LESS and GREATER can take numeric values or
  129. // variable names. STRLESS and STRGREATER take variable names but if the
  130. // variable name is not found it will use the name directly. AND OR take
  131. // variables or the values 0 or 1.
  132. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  133. bool &isValid, const cmMakefile *makefile)
  134. {
  135. // check for the different signatures
  136. isValid = false;
  137. const char *def;
  138. const char *def2;
  139. // handle empty invocation
  140. if (args.size() < 1)
  141. {
  142. isValid = true;
  143. return false;
  144. }
  145. // store the reduced args in this vector
  146. std::deque<std::string> newArgs;
  147. int reducible;
  148. unsigned int i;
  149. // copy to the list structure
  150. for(i = 0; i < args.size(); ++i)
  151. {
  152. newArgs.push_back(args[i]);
  153. }
  154. // now loop through the arguments and see if we can reduce any of them
  155. // we do this multiple times. Once for each level of precedence
  156. do
  157. {
  158. reducible = 0;
  159. std::deque<std::string>::iterator arg = newArgs.begin();
  160. while (arg != newArgs.end())
  161. {
  162. // does a file exist
  163. if (*arg == "EXISTS" && arg + 1 != newArgs.end())
  164. {
  165. if(cmSystemTools::FileExists((arg + 1)->c_str()))
  166. {
  167. *arg = "1";
  168. }
  169. else
  170. {
  171. *arg = "0";
  172. }
  173. newArgs.erase(arg+1);
  174. reducible = 1;
  175. }
  176. // does a command exist
  177. if (*arg == "COMMAND" && arg + 1 != newArgs.end())
  178. {
  179. if(makefile->CommandExists((arg + 1)->c_str()))
  180. {
  181. *arg = "1";
  182. }
  183. else
  184. {
  185. *arg = "0";
  186. }
  187. newArgs.erase(arg+1);
  188. reducible = 1;
  189. }
  190. // is a variable defined
  191. if (*arg == "DEFINED" && arg + 1 != newArgs.end())
  192. {
  193. def = makefile->GetDefinition((arg + 1)->c_str());
  194. if(def)
  195. {
  196. *arg = "1";
  197. }
  198. else
  199. {
  200. *arg = "0";
  201. }
  202. newArgs.erase(arg+1);
  203. reducible = 1;
  204. }
  205. ++arg;
  206. }
  207. }
  208. while (reducible);
  209. // now loop through the arguments and see if we can reduce any of them
  210. // we do this multiple times. Once for each level of precedence
  211. do
  212. {
  213. reducible = 0;
  214. std::deque<std::string>::iterator arg = newArgs.begin();
  215. while (arg != newArgs.end())
  216. {
  217. if (arg + 1 != newArgs.end() && arg + 2 != newArgs.end() &&
  218. *(arg + 1) == "MATCHES")
  219. {
  220. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  221. cmsys::RegularExpression regEntry((arg+2)->c_str());
  222. if (regEntry.find(def))
  223. {
  224. *arg = "1";
  225. }
  226. else
  227. {
  228. *arg = "0";
  229. }
  230. newArgs.erase(arg+2);
  231. newArgs.erase(arg+1);
  232. reducible = 1;
  233. }
  234. if (arg + 1 != newArgs.end() && *arg == "MATCHES")
  235. {
  236. *arg = "0";
  237. newArgs.erase(arg+1);
  238. reducible = 1;
  239. }
  240. if (arg + 1 != newArgs.end() && arg + 2 != newArgs.end() &&
  241. (*(arg + 1) == "LESS" || *(arg + 1) == "GREATER" ||
  242. *(arg + 1) == "EQUAL"))
  243. {
  244. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  245. def2 = cmIfCommand::GetVariableOrString((arg + 2)->c_str(), makefile);
  246. if (*(arg + 1) == "LESS")
  247. {
  248. if(atof(def) < atof(def2))
  249. {
  250. *arg = "1";
  251. }
  252. else
  253. {
  254. *arg = "0";
  255. }
  256. }
  257. else if (*(arg + 1) == "GREATER")
  258. {
  259. if(atof(def) > atof(def2))
  260. {
  261. *arg = "1";
  262. }
  263. else
  264. {
  265. *arg = "0";
  266. }
  267. }
  268. else
  269. {
  270. if(atof(def) == atof(def2))
  271. {
  272. *arg = "1";
  273. }
  274. else
  275. {
  276. *arg = "0";
  277. }
  278. }
  279. newArgs.erase(arg+2);
  280. newArgs.erase(arg+1);
  281. reducible = 1;
  282. }
  283. if (arg + 1 != newArgs.end() && arg + 2 != newArgs.end() &&
  284. (*(arg + 1) == "STRLESS" || *(arg + 1) == "STRGREATER"))
  285. {
  286. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  287. def2 = cmIfCommand::GetVariableOrString((arg + 2)->c_str(), makefile);
  288. if (*(arg + 1) == "STRLESS")
  289. {
  290. if(strcmp(def,def2) < 0)
  291. {
  292. *arg = "1";
  293. }
  294. else
  295. {
  296. *arg = "0";
  297. }
  298. }
  299. else
  300. {
  301. if(strcmp(def,def2) > 0)
  302. {
  303. *arg = "1";
  304. }
  305. else
  306. {
  307. *arg = "0";
  308. }
  309. }
  310. newArgs.erase(arg+2);
  311. newArgs.erase(arg+1);
  312. reducible = 1;
  313. }
  314. ++arg;
  315. }
  316. }
  317. while (reducible);
  318. // now loop through the arguments and see if we can reduce any of them
  319. // we do this multiple times. Once for each level of precedence
  320. do
  321. {
  322. reducible = 0;
  323. std::deque<std::string>::iterator arg = newArgs.begin();
  324. while (arg != newArgs.end())
  325. {
  326. if (arg + 1 != newArgs.end() && *arg == "NOT")
  327. {
  328. def = cmIfCommand::GetVariableOrNumber((arg + 1)->c_str(), makefile);
  329. if(!cmSystemTools::IsOff(def))
  330. {
  331. *arg = "0";
  332. }
  333. else
  334. {
  335. *arg = "1";
  336. }
  337. newArgs.erase(arg+1);
  338. reducible = 1;
  339. }
  340. ++arg;
  341. }
  342. }
  343. while (reducible);
  344. // now loop through the arguments and see if we can reduce any of them
  345. // we do this multiple times. Once for each level of precedence
  346. do
  347. {
  348. reducible = 0;
  349. std::deque<std::string>::iterator arg = newArgs.begin();
  350. while (arg != newArgs.end())
  351. {
  352. if (arg + 1 != newArgs.end() && *(arg + 1) == "AND" &&
  353. arg + 2 != newArgs.end())
  354. {
  355. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  356. def2 = cmIfCommand::GetVariableOrNumber((arg + 2)->c_str(), makefile);
  357. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  358. {
  359. *arg = "0";
  360. }
  361. else
  362. {
  363. *arg = "1";
  364. }
  365. newArgs.erase(arg+2);
  366. newArgs.erase(arg+1);
  367. reducible = 1;
  368. }
  369. if (arg + 1 != newArgs.end() && *(arg + 1) == "OR" &&
  370. arg + 2 != newArgs.end())
  371. {
  372. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  373. def2 = cmIfCommand::GetVariableOrNumber((arg + 2)->c_str(), makefile);
  374. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  375. {
  376. *arg = "0";
  377. }
  378. else
  379. {
  380. *arg = "1";
  381. }
  382. newArgs.erase(arg+2);
  383. newArgs.erase(arg+1);
  384. reducible = 1;
  385. }
  386. ++arg;
  387. }
  388. }
  389. while (reducible);
  390. // now at the end there should only be one argument left
  391. if (newArgs.size() == 1)
  392. {
  393. isValid = true;
  394. if (newArgs[0] == "0")
  395. {
  396. return false;
  397. }
  398. if (newArgs[0] == "1")
  399. {
  400. return true;
  401. }
  402. def = makefile->GetDefinition(args[0].c_str());
  403. if(cmSystemTools::IsOff(def))
  404. {
  405. return false;
  406. }
  407. }
  408. return true;
  409. }
  410. const char* cmIfCommand::GetVariableOrString(const char* str,
  411. const cmMakefile* mf)
  412. {
  413. const char* def = mf->GetDefinition(str);
  414. if(!def)
  415. {
  416. def = str;
  417. }
  418. return def;
  419. }
  420. const char* cmIfCommand::GetVariableOrNumber(const char* str,
  421. const cmMakefile* mf)
  422. {
  423. const char* def = mf->GetDefinition(str);
  424. if(!def)
  425. {
  426. if (atoi(str))
  427. {
  428. def = str;
  429. }
  430. }
  431. return def;
  432. }