cmIfCommand.cxx 28 KB

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