cmIfCommand.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 "cmIfCommand.h"
  11. #include "cmStringCommand.h"
  12. #include <stdlib.h> // required for atof
  13. #include <list>
  14. #include <cmsys/RegularExpression.hxx>
  15. //=========================================================================
  16. bool cmIfFunctionBlocker::
  17. IsFunctionBlocked(const cmListFileFunction& lff,
  18. cmMakefile &mf,
  19. cmExecutionStatus &inStatus)
  20. {
  21. // we start by recording all the functions
  22. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
  23. {
  24. this->ScopeDepth++;
  25. }
  26. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  27. {
  28. this->ScopeDepth--;
  29. // if this is the endif for this if statement, then start executing
  30. if (!this->ScopeDepth)
  31. {
  32. // Remove the function blocker for this scope or bail.
  33. cmsys::auto_ptr<cmFunctionBlocker>
  34. fb(mf.RemoveFunctionBlocker(this, lff));
  35. if(!fb.get()) { return false; }
  36. // execute the functions for the true parts of the if statement
  37. cmExecutionStatus status;
  38. int scopeDepth = 0;
  39. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  40. {
  41. // keep track of scope depth
  42. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"if"))
  43. {
  44. scopeDepth++;
  45. }
  46. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"endif"))
  47. {
  48. scopeDepth--;
  49. }
  50. // watch for our state change
  51. if (scopeDepth == 0 &&
  52. !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"else"))
  53. {
  54. this->IsBlocking = this->HasRun;
  55. this->HasRun = true;
  56. }
  57. else if (scopeDepth == 0 && !cmSystemTools::Strucmp
  58. (this->Functions[c].Name.c_str(),"elseif"))
  59. {
  60. if (this->HasRun)
  61. {
  62. this->IsBlocking = true;
  63. }
  64. else
  65. {
  66. // Place this call on the call stack.
  67. cmMakefileCall stack_manager(&mf, this->Functions[c], status);
  68. static_cast<void>(stack_manager);
  69. std::string errorString;
  70. std::vector<std::string> expandedArguments;
  71. mf.ExpandArguments(this->Functions[c].Arguments,
  72. expandedArguments);
  73. cmake::MessageType messType;
  74. bool isTrue =
  75. cmIfCommand::IsTrue(expandedArguments, errorString,
  76. &mf, messType);
  77. if (errorString.size())
  78. {
  79. std::string err = "given arguments\n ";
  80. unsigned int i;
  81. for(i =0; i < this->Functions[c].Arguments.size(); ++i)
  82. {
  83. err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
  84. err += this->Functions[c].Arguments[i].Value;
  85. err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
  86. err += " ";
  87. }
  88. err += "\n";
  89. err += errorString;
  90. mf.IssueMessage(messType, err);
  91. if (messType == cmake::FATAL_ERROR)
  92. {
  93. cmSystemTools::SetFatalErrorOccured();
  94. return true;
  95. }
  96. }
  97. if (isTrue)
  98. {
  99. this->IsBlocking = false;
  100. this->HasRun = true;
  101. }
  102. }
  103. }
  104. // should we execute?
  105. else if (!this->IsBlocking)
  106. {
  107. status.Clear();
  108. mf.ExecuteCommand(this->Functions[c],status);
  109. if (status.GetReturnInvoked())
  110. {
  111. inStatus.SetReturnInvoked(true);
  112. return true;
  113. }
  114. if (status.GetBreakInvoked())
  115. {
  116. inStatus.SetBreakInvoked(true);
  117. return true;
  118. }
  119. }
  120. }
  121. return true;
  122. }
  123. }
  124. // record the command
  125. this->Functions.push_back(lff);
  126. // always return true
  127. return true;
  128. }
  129. //=========================================================================
  130. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  131. cmMakefile&)
  132. {
  133. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  134. {
  135. // if the endif has arguments, then make sure
  136. // they match the arguments of the matching if
  137. if (lff.Arguments.size() == 0 ||
  138. lff.Arguments == this->Args)
  139. {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. //=========================================================================
  146. bool cmIfCommand
  147. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  148. cmExecutionStatus &)
  149. {
  150. std::string errorString;
  151. std::vector<std::string> expandedArguments;
  152. this->Makefile->ExpandArguments(args, expandedArguments);
  153. cmake::MessageType status;
  154. bool isTrue =
  155. cmIfCommand::IsTrue(expandedArguments,errorString,
  156. this->Makefile, status);
  157. if (errorString.size())
  158. {
  159. std::string err = "given arguments\n ";
  160. unsigned int i;
  161. for(i =0; i < args.size(); ++i)
  162. {
  163. err += (args[i].Quoted?"\"":"");
  164. err += args[i].Value;
  165. err += (args[i].Quoted?"\"":"");
  166. err += " ";
  167. }
  168. err += "\n";
  169. err += errorString;
  170. if (status == cmake::FATAL_ERROR)
  171. {
  172. this->SetError(err.c_str());
  173. cmSystemTools::SetFatalErrorOccured();
  174. return false;
  175. }
  176. else
  177. {
  178. this->Makefile->IssueMessage(status, err);
  179. }
  180. }
  181. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  182. // if is isn't true block the commands
  183. f->ScopeDepth = 1;
  184. f->IsBlocking = !isTrue;
  185. if (isTrue)
  186. {
  187. f->HasRun = true;
  188. }
  189. f->Args = args;
  190. this->Makefile->AddFunctionBlocker(f);
  191. return true;
  192. }
  193. namespace
  194. {
  195. //=========================================================================
  196. // returns true if succesfull, the resulting bool parsed is stored in result
  197. bool GetBooleanValue(std::string &newArg,
  198. cmMakefile *makefile,
  199. bool &result,
  200. std::string &errorString,
  201. cmPolicies::PolicyStatus Policy12Status,
  202. cmake::MessageType &status)
  203. {
  204. if (Policy12Status != cmPolicies::OLD &&
  205. Policy12Status != cmPolicies::WARN)
  206. {
  207. // please note IsOn(var) does not always equal !IsOff(var)
  208. // that is why each is called
  209. if (cmSystemTools::IsOn(newArg.c_str()))
  210. {
  211. result = true;
  212. return true;
  213. }
  214. if (cmSystemTools::IsOff(newArg.c_str()))
  215. {
  216. result = false;
  217. return true;
  218. }
  219. return false;
  220. }
  221. // Old policy is more complex...
  222. // 0 and 1 are very common, test for them first quickly
  223. if (newArg == "0")
  224. {
  225. result = false;
  226. return true;
  227. }
  228. if (newArg == "1")
  229. {
  230. result = true;
  231. return true;
  232. }
  233. // old behavior is to dereference the var
  234. if (Policy12Status == cmPolicies::OLD)
  235. {
  236. return false;
  237. }
  238. // now test for values that may be the name of a variable
  239. // warn if used
  240. if (cmSystemTools::IsOn(newArg.c_str()))
  241. {
  242. // only warn if the value would change
  243. const char *def = makefile->GetDefinition(newArg.c_str());
  244. if (cmSystemTools::IsOff(def))
  245. {
  246. cmPolicies* policies = makefile->GetPolicies();
  247. errorString = "You have used a variable or argument named \""
  248. + newArg
  249. + "\" in a conditional statement. Please be aware of issues "
  250. + "related to policy CMP0012. "
  251. + policies->GetPolicyWarning(cmPolicies::CMP0012);
  252. status = cmake::AUTHOR_WARNING;
  253. }
  254. return false;
  255. }
  256. if (cmSystemTools::IsOff(newArg.c_str()))
  257. {
  258. // only warn if the value would change
  259. const char *def = makefile->GetDefinition(newArg.c_str());
  260. if (!cmSystemTools::IsOff(def))
  261. {
  262. cmPolicies* policies = makefile->GetPolicies();
  263. errorString = "You have used a variable or argument named \""
  264. + newArg
  265. + "\" in a conditional statement. Please be aware of issues "
  266. + "related to policy CMP0012. "
  267. + policies->GetPolicyWarning(cmPolicies::CMP0012);
  268. status = cmake::AUTHOR_WARNING;
  269. }
  270. return false;
  271. }
  272. return false;
  273. }
  274. //=========================================================================
  275. // returns the resulting boolean value
  276. bool GetBooleanValueWithAutoDereference(
  277. std::string &newArg,
  278. cmMakefile *makefile,
  279. std::string &errorString,
  280. cmPolicies::PolicyStatus Policy12Status,
  281. cmake::MessageType &status)
  282. {
  283. bool result = false;
  284. if (GetBooleanValue(newArg, makefile, result,
  285. errorString, Policy12Status, status))
  286. {
  287. return result;
  288. }
  289. const char *def = makefile->GetDefinition(newArg.c_str());
  290. return !cmSystemTools::IsOff(def);
  291. }
  292. //=========================================================================
  293. void IncrementArguments(std::list<std::string> &newArgs,
  294. std::list<std::string>::iterator &argP1,
  295. std::list<std::string>::iterator &argP2)
  296. {
  297. if (argP1 != newArgs.end())
  298. {
  299. argP1++;
  300. argP2 = argP1;
  301. if (argP1 != newArgs.end())
  302. {
  303. argP2++;
  304. }
  305. }
  306. }
  307. //=========================================================================
  308. // helper function to reduce code duplication
  309. void HandlePredicate(bool value, int &reducible,
  310. std::list<std::string>::iterator &arg,
  311. std::list<std::string> &newArgs,
  312. std::list<std::string>::iterator &argP1,
  313. std::list<std::string>::iterator &argP2)
  314. {
  315. if(value)
  316. {
  317. *arg = "1";
  318. }
  319. else
  320. {
  321. *arg = "0";
  322. }
  323. newArgs.erase(argP1);
  324. argP1 = arg;
  325. IncrementArguments(newArgs,argP1,argP2);
  326. reducible = 1;
  327. }
  328. //=========================================================================
  329. // helper function to reduce code duplication
  330. void HandleBinaryOp(bool value, int &reducible,
  331. std::list<std::string>::iterator &arg,
  332. std::list<std::string> &newArgs,
  333. std::list<std::string>::iterator &argP1,
  334. std::list<std::string>::iterator &argP2)
  335. {
  336. if(value)
  337. {
  338. *arg = "1";
  339. }
  340. else
  341. {
  342. *arg = "0";
  343. }
  344. newArgs.erase(argP2);
  345. newArgs.erase(argP1);
  346. argP1 = arg;
  347. IncrementArguments(newArgs,argP1,argP2);
  348. reducible = 1;
  349. }
  350. //=========================================================================
  351. enum Op { OpLess, OpEqual, OpGreater };
  352. bool HandleVersionCompare(Op op, const char* lhs_str, const char* rhs_str)
  353. {
  354. // Parse out up to 4 components.
  355. unsigned int lhs[4] = {0,0,0,0};
  356. unsigned int rhs[4] = {0,0,0,0};
  357. sscanf(lhs_str, "%u.%u.%u.%u", &lhs[0], &lhs[1], &lhs[2], &lhs[3]);
  358. sscanf(rhs_str, "%u.%u.%u.%u", &rhs[0], &rhs[1], &rhs[2], &rhs[3]);
  359. // Do component-wise comparison.
  360. for(unsigned int i=0; i < 4; ++i)
  361. {
  362. if(lhs[i] < rhs[i])
  363. {
  364. // lhs < rhs, so true if operation is LESS
  365. return op == OpLess;
  366. }
  367. else if(lhs[i] > rhs[i])
  368. {
  369. // lhs > rhs, so true if operation is GREATER
  370. return op == OpGreater;
  371. }
  372. }
  373. // lhs == rhs, so true if operation is EQUAL
  374. return op == OpEqual;
  375. }
  376. //=========================================================================
  377. // level 0 processes parenthetical expressions
  378. bool HandleLevel0(std::list<std::string> &newArgs,
  379. cmMakefile *makefile,
  380. std::string &errorString,
  381. cmake::MessageType &status)
  382. {
  383. int reducible;
  384. do
  385. {
  386. reducible = 0;
  387. std::list<std::string>::iterator arg = newArgs.begin();
  388. while (arg != newArgs.end())
  389. {
  390. if (*arg == "(")
  391. {
  392. // search for the closing paren for this opening one
  393. std::list<std::string>::iterator argClose;
  394. argClose = arg;
  395. argClose++;
  396. unsigned int depth = 1;
  397. while (argClose != newArgs.end() && depth)
  398. {
  399. if (*argClose == "(")
  400. {
  401. depth++;
  402. }
  403. if (*argClose == ")")
  404. {
  405. depth--;
  406. }
  407. argClose++;
  408. }
  409. if (depth)
  410. {
  411. errorString = "mismatched parenthesis in condition";
  412. status = cmake::FATAL_ERROR;
  413. return false;
  414. }
  415. // store the reduced args in this vector
  416. std::vector<std::string> newArgs2;
  417. // copy to the list structure
  418. std::list<std::string>::iterator argP1 = arg;
  419. argP1++;
  420. for(; argP1 != argClose; argP1++)
  421. {
  422. newArgs2.push_back(*argP1);
  423. }
  424. newArgs2.pop_back();
  425. // now recursively invoke IsTrue to handle the values inside the
  426. // parenthetical expression
  427. bool value =
  428. cmIfCommand::IsTrue(newArgs2, errorString, makefile, status);
  429. if(value)
  430. {
  431. *arg = "1";
  432. }
  433. else
  434. {
  435. *arg = "0";
  436. }
  437. argP1 = arg;
  438. argP1++;
  439. // remove the now evaluated parenthetical expression
  440. newArgs.erase(argP1,argClose);
  441. }
  442. ++arg;
  443. }
  444. }
  445. while (reducible);
  446. return true;
  447. }
  448. //=========================================================================
  449. // level one handles most predicates except for NOT
  450. bool HandleLevel1(std::list<std::string> &newArgs,
  451. cmMakefile *makefile,
  452. std::string &, cmake::MessageType &)
  453. {
  454. int reducible;
  455. do
  456. {
  457. reducible = 0;
  458. std::list<std::string>::iterator arg = newArgs.begin();
  459. std::list<std::string>::iterator argP1;
  460. std::list<std::string>::iterator argP2;
  461. while (arg != newArgs.end())
  462. {
  463. argP1 = arg;
  464. IncrementArguments(newArgs,argP1,argP2);
  465. // does a file exist
  466. if (*arg == "EXISTS" && argP1 != newArgs.end())
  467. {
  468. HandlePredicate(
  469. cmSystemTools::FileExists((argP1)->c_str()),
  470. reducible, arg, newArgs, argP1, argP2);
  471. }
  472. // does a directory with this name exist
  473. if (*arg == "IS_DIRECTORY" && argP1 != newArgs.end())
  474. {
  475. HandlePredicate(
  476. cmSystemTools::FileIsDirectory((argP1)->c_str()),
  477. reducible, arg, newArgs, argP1, argP2);
  478. }
  479. // is the given path an absolute path ?
  480. if (*arg == "IS_ABSOLUTE" && argP1 != newArgs.end())
  481. {
  482. HandlePredicate(
  483. cmSystemTools::FileIsFullPath((argP1)->c_str()),
  484. reducible, arg, newArgs, argP1, argP2);
  485. }
  486. // does a command exist
  487. if (*arg == "COMMAND" && argP1 != newArgs.end())
  488. {
  489. HandlePredicate(
  490. makefile->CommandExists((argP1)->c_str()),
  491. reducible, arg, newArgs, argP1, argP2);
  492. }
  493. // does a policy exist
  494. if (*arg == "POLICY" && argP1 != newArgs.end())
  495. {
  496. cmPolicies::PolicyID pid;
  497. HandlePredicate(
  498. makefile->GetPolicies()->GetPolicyID((argP1)->c_str(), pid),
  499. reducible, arg, newArgs, argP1, argP2);
  500. }
  501. // does a target exist
  502. if (*arg == "TARGET" && argP1 != newArgs.end())
  503. {
  504. HandlePredicate(
  505. makefile->FindTargetToUse((argP1)->c_str())? true:false,
  506. reducible, arg, newArgs, argP1, argP2);
  507. }
  508. // is a variable defined
  509. if (*arg == "DEFINED" && argP1 != newArgs.end())
  510. {
  511. size_t argP1len = argP1->size();
  512. bool bdef = false;
  513. if(argP1len > 4 && argP1->substr(0, 4) == "ENV{" &&
  514. argP1->operator[](argP1len-1) == '}')
  515. {
  516. std::string env = argP1->substr(4, argP1len-5);
  517. bdef = cmSystemTools::GetEnv(env.c_str())?true:false;
  518. }
  519. else
  520. {
  521. bdef = makefile->IsDefinitionSet((argP1)->c_str());
  522. }
  523. HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  524. }
  525. ++arg;
  526. }
  527. }
  528. while (reducible);
  529. return true;
  530. }
  531. //=========================================================================
  532. // level two handles most binary operations except for AND OR
  533. bool HandleLevel2(std::list<std::string> &newArgs,
  534. cmMakefile *makefile,
  535. std::string &errorString,
  536. cmake::MessageType &status)
  537. {
  538. int reducible;
  539. const char *def;
  540. const char *def2;
  541. do
  542. {
  543. reducible = 0;
  544. std::list<std::string>::iterator arg = newArgs.begin();
  545. std::list<std::string>::iterator argP1;
  546. std::list<std::string>::iterator argP2;
  547. while (arg != newArgs.end())
  548. {
  549. argP1 = arg;
  550. IncrementArguments(newArgs,argP1,argP2);
  551. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  552. *(argP1) == "MATCHES")
  553. {
  554. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  555. const char* rex = (argP2)->c_str();
  556. cmStringCommand::ClearMatches(makefile);
  557. cmsys::RegularExpression regEntry;
  558. if ( !regEntry.compile(rex) )
  559. {
  560. cmOStringStream error;
  561. error << "Regular expression \"" << rex << "\" cannot compile";
  562. errorString = error.str();
  563. status = cmake::FATAL_ERROR;
  564. return false;
  565. }
  566. if (regEntry.find(def))
  567. {
  568. cmStringCommand::StoreMatches(makefile, regEntry);
  569. *arg = "1";
  570. }
  571. else
  572. {
  573. *arg = "0";
  574. }
  575. newArgs.erase(argP2);
  576. newArgs.erase(argP1);
  577. argP1 = arg;
  578. IncrementArguments(newArgs,argP1,argP2);
  579. reducible = 1;
  580. }
  581. if (argP1 != newArgs.end() && *arg == "MATCHES")
  582. {
  583. *arg = "0";
  584. newArgs.erase(argP1);
  585. argP1 = arg;
  586. IncrementArguments(newArgs,argP1,argP2);
  587. reducible = 1;
  588. }
  589. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  590. (*(argP1) == "LESS" || *(argP1) == "GREATER" ||
  591. *(argP1) == "EQUAL"))
  592. {
  593. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  594. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  595. double lhs;
  596. double rhs;
  597. bool result;
  598. if(sscanf(def, "%lg", &lhs) != 1 ||
  599. sscanf(def2, "%lg", &rhs) != 1)
  600. {
  601. result = false;
  602. }
  603. else if (*(argP1) == "LESS")
  604. {
  605. result = (lhs < rhs);
  606. }
  607. else if (*(argP1) == "GREATER")
  608. {
  609. result = (lhs > rhs);
  610. }
  611. else
  612. {
  613. result = (lhs == rhs);
  614. }
  615. HandleBinaryOp(result,
  616. reducible, arg, newArgs, argP1, argP2);
  617. }
  618. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  619. (*(argP1) == "STRLESS" ||
  620. *(argP1) == "STREQUAL" ||
  621. *(argP1) == "STRGREATER"))
  622. {
  623. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  624. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  625. int val = strcmp(def,def2);
  626. bool result;
  627. if (*(argP1) == "STRLESS")
  628. {
  629. result = (val < 0);
  630. }
  631. else if (*(argP1) == "STRGREATER")
  632. {
  633. result = (val > 0);
  634. }
  635. else // strequal
  636. {
  637. result = (val == 0);
  638. }
  639. HandleBinaryOp(result,
  640. reducible, arg, newArgs, argP1, argP2);
  641. }
  642. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  643. (*(argP1) == "VERSION_LESS" || *(argP1) == "VERSION_GREATER" ||
  644. *(argP1) == "VERSION_EQUAL"))
  645. {
  646. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  647. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  648. Op op = OpEqual;
  649. if(*argP1 == "VERSION_LESS")
  650. {
  651. op = OpLess;
  652. }
  653. else if(*argP1 == "VERSION_GREATER")
  654. {
  655. op = OpGreater;
  656. }
  657. bool result = HandleVersionCompare(op, def, def2);
  658. HandleBinaryOp(result,
  659. reducible, arg, newArgs, argP1, argP2);
  660. }
  661. // is file A newer than file B
  662. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  663. *(argP1) == "IS_NEWER_THAN")
  664. {
  665. int fileIsNewer=0;
  666. bool success=cmSystemTools::FileTimeCompare(arg->c_str(),
  667. (argP2)->c_str(),
  668. &fileIsNewer);
  669. HandleBinaryOp(
  670. (success==false || fileIsNewer==1 || fileIsNewer==0),
  671. reducible, arg, newArgs, argP1, argP2);
  672. }
  673. ++arg;
  674. }
  675. }
  676. while (reducible);
  677. return true;
  678. }
  679. //=========================================================================
  680. // level 3 handles NOT
  681. bool HandleLevel3(std::list<std::string> &newArgs,
  682. cmMakefile *makefile,
  683. std::string &errorString,
  684. cmPolicies::PolicyStatus Policy12Status,
  685. cmake::MessageType &status)
  686. {
  687. int reducible;
  688. do
  689. {
  690. reducible = 0;
  691. std::list<std::string>::iterator arg = newArgs.begin();
  692. std::list<std::string>::iterator argP1;
  693. std::list<std::string>::iterator argP2;
  694. while (arg != newArgs.end())
  695. {
  696. argP1 = arg;
  697. IncrementArguments(newArgs,argP1,argP2);
  698. if (argP1 != newArgs.end() && *arg == "NOT")
  699. {
  700. bool rhs = GetBooleanValueWithAutoDereference(*argP1, makefile,
  701. errorString,
  702. Policy12Status,
  703. status);
  704. HandlePredicate(!rhs, reducible, arg, newArgs, argP1, argP2);
  705. }
  706. ++arg;
  707. }
  708. }
  709. while (reducible);
  710. return true;
  711. }
  712. //=========================================================================
  713. // level 4 handles AND OR
  714. bool HandleLevel4(std::list<std::string> &newArgs,
  715. cmMakefile *makefile,
  716. std::string &errorString,
  717. cmPolicies::PolicyStatus Policy12Status,
  718. cmake::MessageType &status)
  719. {
  720. int reducible;
  721. bool lhs;
  722. bool rhs;
  723. do
  724. {
  725. reducible = 0;
  726. std::list<std::string>::iterator arg = newArgs.begin();
  727. std::list<std::string>::iterator argP1;
  728. std::list<std::string>::iterator argP2;
  729. while (arg != newArgs.end())
  730. {
  731. argP1 = arg;
  732. IncrementArguments(newArgs,argP1,argP2);
  733. if (argP1 != newArgs.end() && *(argP1) == "AND" &&
  734. argP2 != newArgs.end())
  735. {
  736. lhs = GetBooleanValueWithAutoDereference(*arg, makefile,
  737. errorString,
  738. Policy12Status,
  739. status);
  740. rhs = GetBooleanValueWithAutoDereference(*argP2, makefile,
  741. errorString,
  742. Policy12Status,
  743. status);
  744. HandleBinaryOp((lhs && rhs),
  745. reducible, arg, newArgs, argP1, argP2);
  746. }
  747. if (argP1 != newArgs.end() && *(argP1) == "OR" &&
  748. argP2 != newArgs.end())
  749. {
  750. lhs = GetBooleanValueWithAutoDereference(*arg, makefile,
  751. errorString,
  752. Policy12Status,
  753. status);
  754. rhs = GetBooleanValueWithAutoDereference(*argP2, makefile,
  755. errorString,
  756. Policy12Status,
  757. status);
  758. HandleBinaryOp((lhs || rhs),
  759. reducible, arg, newArgs, argP1, argP2);
  760. }
  761. ++arg;
  762. }
  763. }
  764. while (reducible);
  765. return true;
  766. }
  767. }
  768. //=========================================================================
  769. // order of operations,
  770. // 1. ( ) -- parenthetical groups
  771. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  772. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  773. // 4. NOT
  774. // 5. AND OR
  775. //
  776. // There is an issue on whether the arguments should be values of references,
  777. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  778. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  779. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  780. // take numeric values or variable names. STRLESS and STRGREATER take
  781. // variable names but if the variable name is not found it will use the name
  782. // directly. AND OR take variables or the values 0 or 1.
  783. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  784. std::string &errorString, cmMakefile *makefile,
  785. cmake::MessageType &status)
  786. {
  787. errorString = "";
  788. // handle empty invocation
  789. if (args.size() < 1)
  790. {
  791. return false;
  792. }
  793. // store the reduced args in this vector
  794. std::list<std::string> newArgs;
  795. // copy to the list structure
  796. for(unsigned int i = 0; i < args.size(); ++i)
  797. {
  798. newArgs.push_back(args[i]);
  799. }
  800. // now loop through the arguments and see if we can reduce any of them
  801. // we do this multiple times. Once for each level of precedence
  802. // parens
  803. if (!HandleLevel0(newArgs, makefile, errorString, status))
  804. {
  805. return false;
  806. }
  807. //predicates
  808. if (!HandleLevel1(newArgs, makefile, errorString, status))
  809. {
  810. return false;
  811. }
  812. // binary ops
  813. if (!HandleLevel2(newArgs, makefile, errorString, status))
  814. {
  815. return false;
  816. }
  817. // used to store the value of policy CMP0012 for performance
  818. cmPolicies::PolicyStatus Policy12Status =
  819. makefile->GetPolicyStatus(cmPolicies::CMP0012);
  820. // NOT
  821. if (!HandleLevel3(newArgs, makefile, errorString,
  822. Policy12Status, status))
  823. {
  824. return false;
  825. }
  826. // AND OR
  827. if (!HandleLevel4(newArgs, makefile, errorString,
  828. Policy12Status, status))
  829. {
  830. return false;
  831. }
  832. // now at the end there should only be one argument left
  833. if (newArgs.size() != 1)
  834. {
  835. errorString = "Unknown arguments specified";
  836. return false;
  837. }
  838. return GetBooleanValueWithAutoDereference(*(newArgs.begin()),
  839. makefile,
  840. errorString,
  841. Policy12Status,
  842. status);
  843. }
  844. //=========================================================================
  845. const char* cmIfCommand::GetVariableOrString(const char* str,
  846. const cmMakefile* mf)
  847. {
  848. const char* def = mf->GetDefinition(str);
  849. if(!def)
  850. {
  851. def = str;
  852. }
  853. return def;
  854. }