cmIfCommand.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. // level 0 processes parenthetical expressions
  374. bool HandleLevel0(std::list<std::string> &newArgs,
  375. cmMakefile *makefile,
  376. std::string &errorString,
  377. cmake::MessageType &status)
  378. {
  379. int reducible;
  380. do
  381. {
  382. reducible = 0;
  383. std::list<std::string>::iterator arg = newArgs.begin();
  384. while (arg != newArgs.end())
  385. {
  386. if (*arg == "(")
  387. {
  388. // search for the closing paren for this opening one
  389. std::list<std::string>::iterator argClose;
  390. argClose = arg;
  391. argClose++;
  392. unsigned int depth = 1;
  393. while (argClose != newArgs.end() && depth)
  394. {
  395. if (*argClose == "(")
  396. {
  397. depth++;
  398. }
  399. if (*argClose == ")")
  400. {
  401. depth--;
  402. }
  403. argClose++;
  404. }
  405. if (depth)
  406. {
  407. errorString = "mismatched parenthesis in condition";
  408. status = cmake::FATAL_ERROR;
  409. return false;
  410. }
  411. // store the reduced args in this vector
  412. std::vector<std::string> newArgs2;
  413. // copy to the list structure
  414. std::list<std::string>::iterator argP1 = arg;
  415. argP1++;
  416. for(; argP1 != argClose; argP1++)
  417. {
  418. newArgs2.push_back(*argP1);
  419. }
  420. newArgs2.pop_back();
  421. // now recursively invoke IsTrue to handle the values inside the
  422. // parenthetical expression
  423. bool value =
  424. cmIfCommand::IsTrue(newArgs2, errorString, makefile, status);
  425. if(value)
  426. {
  427. *arg = "1";
  428. }
  429. else
  430. {
  431. *arg = "0";
  432. }
  433. argP1 = arg;
  434. argP1++;
  435. // remove the now evaluated parenthetical expression
  436. newArgs.erase(argP1,argClose);
  437. }
  438. ++arg;
  439. }
  440. }
  441. while (reducible);
  442. return true;
  443. }
  444. //=========================================================================
  445. // level one handles most predicates except for NOT
  446. bool HandleLevel1(std::list<std::string> &newArgs,
  447. cmMakefile *makefile,
  448. std::string &, cmake::MessageType &)
  449. {
  450. int reducible;
  451. do
  452. {
  453. reducible = 0;
  454. std::list<std::string>::iterator arg = newArgs.begin();
  455. std::list<std::string>::iterator argP1;
  456. std::list<std::string>::iterator argP2;
  457. while (arg != newArgs.end())
  458. {
  459. argP1 = arg;
  460. IncrementArguments(newArgs,argP1,argP2);
  461. // does a file exist
  462. if (*arg == "EXISTS" && argP1 != newArgs.end())
  463. {
  464. HandlePredicate(
  465. cmSystemTools::FileExists((argP1)->c_str()),
  466. reducible, arg, newArgs, argP1, argP2);
  467. }
  468. // does a directory with this name exist
  469. if (*arg == "IS_DIRECTORY" && argP1 != newArgs.end())
  470. {
  471. HandlePredicate(
  472. cmSystemTools::FileIsDirectory((argP1)->c_str()),
  473. reducible, arg, newArgs, argP1, argP2);
  474. }
  475. // does a symlink with this name exist
  476. if (*arg == "IS_SYMLINK" && argP1 != newArgs.end())
  477. {
  478. HandlePredicate(
  479. cmSystemTools::FileIsSymlink((argP1)->c_str()),
  480. reducible, arg, newArgs, argP1, argP2);
  481. }
  482. // is the given path an absolute path ?
  483. if (*arg == "IS_ABSOLUTE" && argP1 != newArgs.end())
  484. {
  485. HandlePredicate(
  486. cmSystemTools::FileIsFullPath((argP1)->c_str()),
  487. reducible, arg, newArgs, argP1, argP2);
  488. }
  489. // does a command exist
  490. if (*arg == "COMMAND" && argP1 != newArgs.end())
  491. {
  492. HandlePredicate(
  493. makefile->CommandExists((argP1)->c_str()),
  494. reducible, arg, newArgs, argP1, argP2);
  495. }
  496. // does a policy exist
  497. if (*arg == "POLICY" && argP1 != newArgs.end())
  498. {
  499. cmPolicies::PolicyID pid;
  500. HandlePredicate(
  501. makefile->GetPolicies()->GetPolicyID((argP1)->c_str(), pid),
  502. reducible, arg, newArgs, argP1, argP2);
  503. }
  504. // does a target exist
  505. if (*arg == "TARGET" && argP1 != newArgs.end())
  506. {
  507. HandlePredicate(
  508. makefile->FindTargetToUse((argP1)->c_str())? true:false,
  509. reducible, arg, newArgs, argP1, argP2);
  510. }
  511. // is a variable defined
  512. if (*arg == "DEFINED" && argP1 != newArgs.end())
  513. {
  514. size_t argP1len = argP1->size();
  515. bool bdef = false;
  516. if(argP1len > 4 && argP1->substr(0, 4) == "ENV{" &&
  517. argP1->operator[](argP1len-1) == '}')
  518. {
  519. std::string env = argP1->substr(4, argP1len-5);
  520. bdef = cmSystemTools::GetEnv(env.c_str())?true:false;
  521. }
  522. else
  523. {
  524. bdef = makefile->IsDefinitionSet((argP1)->c_str());
  525. }
  526. HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  527. }
  528. ++arg;
  529. }
  530. }
  531. while (reducible);
  532. return true;
  533. }
  534. //=========================================================================
  535. // level two handles most binary operations except for AND OR
  536. bool HandleLevel2(std::list<std::string> &newArgs,
  537. cmMakefile *makefile,
  538. std::string &errorString,
  539. cmake::MessageType &status)
  540. {
  541. int reducible;
  542. const char *def;
  543. const char *def2;
  544. do
  545. {
  546. reducible = 0;
  547. std::list<std::string>::iterator arg = newArgs.begin();
  548. std::list<std::string>::iterator argP1;
  549. std::list<std::string>::iterator argP2;
  550. while (arg != newArgs.end())
  551. {
  552. argP1 = arg;
  553. IncrementArguments(newArgs,argP1,argP2);
  554. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  555. *(argP1) == "MATCHES")
  556. {
  557. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  558. const char* rex = (argP2)->c_str();
  559. cmStringCommand::ClearMatches(makefile);
  560. cmsys::RegularExpression regEntry;
  561. if ( !regEntry.compile(rex) )
  562. {
  563. cmOStringStream error;
  564. error << "Regular expression \"" << rex << "\" cannot compile";
  565. errorString = error.str();
  566. status = cmake::FATAL_ERROR;
  567. return false;
  568. }
  569. if (regEntry.find(def))
  570. {
  571. cmStringCommand::StoreMatches(makefile, regEntry);
  572. *arg = "1";
  573. }
  574. else
  575. {
  576. *arg = "0";
  577. }
  578. newArgs.erase(argP2);
  579. newArgs.erase(argP1);
  580. argP1 = arg;
  581. IncrementArguments(newArgs,argP1,argP2);
  582. reducible = 1;
  583. }
  584. if (argP1 != newArgs.end() && *arg == "MATCHES")
  585. {
  586. *arg = "0";
  587. newArgs.erase(argP1);
  588. argP1 = arg;
  589. IncrementArguments(newArgs,argP1,argP2);
  590. reducible = 1;
  591. }
  592. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  593. (*(argP1) == "LESS" || *(argP1) == "GREATER" ||
  594. *(argP1) == "EQUAL"))
  595. {
  596. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  597. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  598. double lhs;
  599. double rhs;
  600. bool result;
  601. if(sscanf(def, "%lg", &lhs) != 1 ||
  602. sscanf(def2, "%lg", &rhs) != 1)
  603. {
  604. result = false;
  605. }
  606. else if (*(argP1) == "LESS")
  607. {
  608. result = (lhs < rhs);
  609. }
  610. else if (*(argP1) == "GREATER")
  611. {
  612. result = (lhs > rhs);
  613. }
  614. else
  615. {
  616. result = (lhs == rhs);
  617. }
  618. HandleBinaryOp(result,
  619. reducible, arg, newArgs, argP1, argP2);
  620. }
  621. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  622. (*(argP1) == "STRLESS" ||
  623. *(argP1) == "STREQUAL" ||
  624. *(argP1) == "STRGREATER"))
  625. {
  626. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  627. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  628. int val = strcmp(def,def2);
  629. bool result;
  630. if (*(argP1) == "STRLESS")
  631. {
  632. result = (val < 0);
  633. }
  634. else if (*(argP1) == "STRGREATER")
  635. {
  636. result = (val > 0);
  637. }
  638. else // strequal
  639. {
  640. result = (val == 0);
  641. }
  642. HandleBinaryOp(result,
  643. reducible, arg, newArgs, argP1, argP2);
  644. }
  645. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  646. (*(argP1) == "VERSION_LESS" || *(argP1) == "VERSION_GREATER" ||
  647. *(argP1) == "VERSION_EQUAL"))
  648. {
  649. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  650. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  651. cmSystemTools::CompareOp op = cmSystemTools::OP_EQUAL;
  652. if(*argP1 == "VERSION_LESS")
  653. {
  654. op = cmSystemTools::OP_LESS;
  655. }
  656. else if(*argP1 == "VERSION_GREATER")
  657. {
  658. op = cmSystemTools::OP_GREATER;
  659. }
  660. bool result = cmSystemTools::VersionCompare(op, def, def2);
  661. HandleBinaryOp(result,
  662. reducible, arg, newArgs, argP1, argP2);
  663. }
  664. // is file A newer than file B
  665. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  666. *(argP1) == "IS_NEWER_THAN")
  667. {
  668. int fileIsNewer=0;
  669. bool success=cmSystemTools::FileTimeCompare(arg->c_str(),
  670. (argP2)->c_str(),
  671. &fileIsNewer);
  672. HandleBinaryOp(
  673. (success==false || fileIsNewer==1 || fileIsNewer==0),
  674. reducible, arg, newArgs, argP1, argP2);
  675. }
  676. ++arg;
  677. }
  678. }
  679. while (reducible);
  680. return true;
  681. }
  682. //=========================================================================
  683. // level 3 handles NOT
  684. bool HandleLevel3(std::list<std::string> &newArgs,
  685. cmMakefile *makefile,
  686. std::string &errorString,
  687. cmPolicies::PolicyStatus Policy12Status,
  688. cmake::MessageType &status)
  689. {
  690. int reducible;
  691. do
  692. {
  693. reducible = 0;
  694. std::list<std::string>::iterator arg = newArgs.begin();
  695. std::list<std::string>::iterator argP1;
  696. std::list<std::string>::iterator argP2;
  697. while (arg != newArgs.end())
  698. {
  699. argP1 = arg;
  700. IncrementArguments(newArgs,argP1,argP2);
  701. if (argP1 != newArgs.end() && *arg == "NOT")
  702. {
  703. bool rhs = GetBooleanValueWithAutoDereference(*argP1, makefile,
  704. errorString,
  705. Policy12Status,
  706. status);
  707. HandlePredicate(!rhs, reducible, arg, newArgs, argP1, argP2);
  708. }
  709. ++arg;
  710. }
  711. }
  712. while (reducible);
  713. return true;
  714. }
  715. //=========================================================================
  716. // level 4 handles AND OR
  717. bool HandleLevel4(std::list<std::string> &newArgs,
  718. cmMakefile *makefile,
  719. std::string &errorString,
  720. cmPolicies::PolicyStatus Policy12Status,
  721. cmake::MessageType &status)
  722. {
  723. int reducible;
  724. bool lhs;
  725. bool rhs;
  726. do
  727. {
  728. reducible = 0;
  729. std::list<std::string>::iterator arg = newArgs.begin();
  730. std::list<std::string>::iterator argP1;
  731. std::list<std::string>::iterator argP2;
  732. while (arg != newArgs.end())
  733. {
  734. argP1 = arg;
  735. IncrementArguments(newArgs,argP1,argP2);
  736. if (argP1 != newArgs.end() && *(argP1) == "AND" &&
  737. argP2 != newArgs.end())
  738. {
  739. lhs = GetBooleanValueWithAutoDereference(*arg, makefile,
  740. errorString,
  741. Policy12Status,
  742. status);
  743. rhs = GetBooleanValueWithAutoDereference(*argP2, makefile,
  744. errorString,
  745. Policy12Status,
  746. status);
  747. HandleBinaryOp((lhs && rhs),
  748. reducible, arg, newArgs, argP1, argP2);
  749. }
  750. if (argP1 != newArgs.end() && *(argP1) == "OR" &&
  751. argP2 != newArgs.end())
  752. {
  753. lhs = GetBooleanValueWithAutoDereference(*arg, makefile,
  754. errorString,
  755. Policy12Status,
  756. status);
  757. rhs = GetBooleanValueWithAutoDereference(*argP2, makefile,
  758. errorString,
  759. Policy12Status,
  760. status);
  761. HandleBinaryOp((lhs || rhs),
  762. reducible, arg, newArgs, argP1, argP2);
  763. }
  764. ++arg;
  765. }
  766. }
  767. while (reducible);
  768. return true;
  769. }
  770. }
  771. //=========================================================================
  772. // order of operations,
  773. // 1. ( ) -- parenthetical groups
  774. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  775. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  776. // 4. NOT
  777. // 5. AND OR
  778. //
  779. // There is an issue on whether the arguments should be values of references,
  780. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  781. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  782. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  783. // take numeric values or variable names. STRLESS and STRGREATER take
  784. // variable names but if the variable name is not found it will use the name
  785. // directly. AND OR take variables or the values 0 or 1.
  786. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  787. std::string &errorString, cmMakefile *makefile,
  788. cmake::MessageType &status)
  789. {
  790. errorString = "";
  791. // handle empty invocation
  792. if (args.size() < 1)
  793. {
  794. return false;
  795. }
  796. // store the reduced args in this vector
  797. std::list<std::string> newArgs;
  798. // copy to the list structure
  799. for(unsigned int i = 0; i < args.size(); ++i)
  800. {
  801. newArgs.push_back(args[i]);
  802. }
  803. // now loop through the arguments and see if we can reduce any of them
  804. // we do this multiple times. Once for each level of precedence
  805. // parens
  806. if (!HandleLevel0(newArgs, makefile, errorString, status))
  807. {
  808. return false;
  809. }
  810. //predicates
  811. if (!HandleLevel1(newArgs, makefile, errorString, status))
  812. {
  813. return false;
  814. }
  815. // binary ops
  816. if (!HandleLevel2(newArgs, makefile, errorString, status))
  817. {
  818. return false;
  819. }
  820. // used to store the value of policy CMP0012 for performance
  821. cmPolicies::PolicyStatus Policy12Status =
  822. makefile->GetPolicyStatus(cmPolicies::CMP0012);
  823. // NOT
  824. if (!HandleLevel3(newArgs, makefile, errorString,
  825. Policy12Status, status))
  826. {
  827. return false;
  828. }
  829. // AND OR
  830. if (!HandleLevel4(newArgs, makefile, errorString,
  831. Policy12Status, status))
  832. {
  833. return false;
  834. }
  835. // now at the end there should only be one argument left
  836. if (newArgs.size() != 1)
  837. {
  838. errorString = "Unknown arguments specified";
  839. status = cmake::FATAL_ERROR;
  840. return false;
  841. }
  842. return GetBooleanValueWithAutoDereference(*(newArgs.begin()),
  843. makefile,
  844. errorString,
  845. Policy12Status,
  846. status, true);
  847. }
  848. //=========================================================================
  849. const char* cmIfCommand::GetVariableOrString(const char* str,
  850. const cmMakefile* mf)
  851. {
  852. const char* def = mf->GetDefinition(str);
  853. if(!def)
  854. {
  855. def = str;
  856. }
  857. return def;
  858. }