cmIfCommand.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 "cmStringCommand.h"
  15. #include <stdlib.h> // required for atof
  16. #include <list>
  17. #include <cmsys/RegularExpression.hxx>
  18. //=========================================================================
  19. bool cmIfFunctionBlocker::
  20. IsFunctionBlocked(const cmListFileFunction& lff,
  21. cmMakefile &mf,
  22. cmExecutionStatus &inStatus)
  23. {
  24. // Prevent recusion and don't let this blocker block its own
  25. // commands.
  26. if (this->Executing)
  27. {
  28. return false;
  29. }
  30. // we start by recording all the functions
  31. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
  32. {
  33. this->ScopeDepth++;
  34. }
  35. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  36. {
  37. this->ScopeDepth--;
  38. // if this is the endif for this if statement, then start executing
  39. if (!this->ScopeDepth)
  40. {
  41. // execute the functions for the true parts of the if statement
  42. this->Executing = true;
  43. cmExecutionStatus status;
  44. int scopeDepth = 0;
  45. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  46. {
  47. // keep track of scope depth
  48. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"if"))
  49. {
  50. scopeDepth++;
  51. }
  52. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"endif"))
  53. {
  54. scopeDepth--;
  55. }
  56. // watch for our state change
  57. if (scopeDepth == 0 &&
  58. !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"else"))
  59. {
  60. this->IsBlocking = this->HasRun;
  61. this->HasRun = true;
  62. }
  63. else if (scopeDepth == 0 && !cmSystemTools::Strucmp
  64. (this->Functions[c].Name.c_str(),"elseif"))
  65. {
  66. if (this->HasRun)
  67. {
  68. this->IsBlocking = true;
  69. }
  70. else
  71. {
  72. char* errorString = 0;
  73. std::vector<std::string> expandedArguments;
  74. mf.ExpandArguments(this->Functions[c].Arguments,
  75. expandedArguments);
  76. bool isTrue =
  77. cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
  78. if (errorString)
  79. {
  80. std::string err = "had incorrect arguments: ";
  81. unsigned int i;
  82. for(i =0; i < this->Functions[c].Arguments.size(); ++i)
  83. {
  84. err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
  85. err += this->Functions[c].Arguments[i].Value;
  86. err += (this->Functions[c].Arguments[i].Quoted?"\"":"");
  87. err += " ";
  88. }
  89. err += "(";
  90. err += errorString;
  91. err += ").";
  92. cmSystemTools::Error(err.c_str());
  93. delete [] errorString;
  94. return false;
  95. }
  96. if (isTrue)
  97. {
  98. this->IsBlocking = false;
  99. this->HasRun = true;
  100. }
  101. }
  102. }
  103. // should we execute?
  104. else if (!this->IsBlocking)
  105. {
  106. status.Clear();
  107. mf.ExecuteCommand(this->Functions[c],status);
  108. if (status.GetReturnInvoked())
  109. {
  110. inStatus.SetReturnInvoked(true);
  111. mf.RemoveFunctionBlocker(lff);
  112. return true;
  113. }
  114. if (status.GetBreakInvoked())
  115. {
  116. inStatus.SetBreakInvoked(true);
  117. mf.RemoveFunctionBlocker(lff);
  118. return true;
  119. }
  120. }
  121. }
  122. mf.RemoveFunctionBlocker(lff);
  123. return true;
  124. }
  125. }
  126. // record the command
  127. this->Functions.push_back(lff);
  128. // always return true
  129. return true;
  130. }
  131. //=========================================================================
  132. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  133. cmMakefile&)
  134. {
  135. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  136. {
  137. // if the endif has arguments, then make sure
  138. // they match the arguments of the matching if
  139. if (lff.Arguments.size() == 0 ||
  140. lff.Arguments == this->Args)
  141. {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. //=========================================================================
  148. void cmIfFunctionBlocker::ScopeEnded(cmMakefile &mf)
  149. {
  150. std::string errmsg = "The end of a CMakeLists file was reached with an "
  151. "IF statement that was not closed properly.\nWithin the directory: ";
  152. errmsg += mf.GetCurrentDirectory();
  153. errmsg += "\nThe arguments are: ";
  154. for(std::vector<cmListFileArgument>::const_iterator j = this->Args.begin();
  155. j != this->Args.end(); ++j)
  156. {
  157. errmsg += (j->Quoted?"\"":"");
  158. errmsg += j->Value;
  159. errmsg += (j->Quoted?"\"":"");
  160. errmsg += " ";
  161. }
  162. cmSystemTools::Message(errmsg.c_str(), "Warning");
  163. }
  164. //=========================================================================
  165. bool cmIfCommand
  166. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  167. cmExecutionStatus &)
  168. {
  169. char* errorString = 0;
  170. std::vector<std::string> expandedArguments;
  171. this->Makefile->ExpandArguments(args, expandedArguments);
  172. bool isTrue =
  173. cmIfCommand::IsTrue(expandedArguments,&errorString,this->Makefile);
  174. if (errorString)
  175. {
  176. std::string err = "had incorrect arguments: ";
  177. unsigned int i;
  178. for(i =0; i < args.size(); ++i)
  179. {
  180. err += (args[i].Quoted?"\"":"");
  181. err += args[i].Value;
  182. err += (args[i].Quoted?"\"":"");
  183. err += " ";
  184. }
  185. err += "(";
  186. err += errorString;
  187. err += ").";
  188. this->SetError(err.c_str());
  189. delete [] errorString;
  190. return false;
  191. }
  192. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  193. // if is isn't true block the commands
  194. f->ScopeDepth = 1;
  195. f->IsBlocking = !isTrue;
  196. if (isTrue)
  197. {
  198. f->HasRun = true;
  199. }
  200. f->Args = args;
  201. this->Makefile->AddFunctionBlocker(f);
  202. return true;
  203. }
  204. namespace
  205. {
  206. //=========================================================================
  207. void IncrementArguments(std::list<std::string> &newArgs,
  208. std::list<std::string>::iterator &argP1,
  209. std::list<std::string>::iterator &argP2)
  210. {
  211. if (argP1 != newArgs.end())
  212. {
  213. argP1++;
  214. argP2 = argP1;
  215. if (argP1 != newArgs.end())
  216. {
  217. argP2++;
  218. }
  219. }
  220. }
  221. //=========================================================================
  222. // helper function to reduce code duplication
  223. void HandlePredicate(bool value, int &reducible,
  224. std::list<std::string>::iterator &arg,
  225. std::list<std::string> &newArgs,
  226. std::list<std::string>::iterator &argP1,
  227. std::list<std::string>::iterator &argP2)
  228. {
  229. if(value)
  230. {
  231. *arg = "1";
  232. }
  233. else
  234. {
  235. *arg = "0";
  236. }
  237. newArgs.erase(argP1);
  238. argP1 = arg;
  239. IncrementArguments(newArgs,argP1,argP2);
  240. reducible = 1;
  241. }
  242. //=========================================================================
  243. // helper function to reduce code duplication
  244. void HandleBinaryOp(bool value, int &reducible,
  245. std::list<std::string>::iterator &arg,
  246. std::list<std::string> &newArgs,
  247. std::list<std::string>::iterator &argP1,
  248. std::list<std::string>::iterator &argP2)
  249. {
  250. if(value)
  251. {
  252. *arg = "1";
  253. }
  254. else
  255. {
  256. *arg = "0";
  257. }
  258. newArgs.erase(argP2);
  259. newArgs.erase(argP1);
  260. argP1 = arg;
  261. IncrementArguments(newArgs,argP1,argP2);
  262. reducible = 1;
  263. }
  264. //=========================================================================
  265. // level 0 processes parenthetical expressions
  266. bool HandleLevel0(std::list<std::string> &newArgs,
  267. cmMakefile *makefile,
  268. char **errorString)
  269. {
  270. int reducible;
  271. do
  272. {
  273. reducible = 0;
  274. std::list<std::string>::iterator arg = newArgs.begin();
  275. while (arg != newArgs.end())
  276. {
  277. if (*arg == "(")
  278. {
  279. // search for the closing paren for this opening one
  280. std::list<std::string>::iterator argClose;
  281. argClose = arg;
  282. argClose++;
  283. unsigned int depth = 1;
  284. while (argClose != newArgs.end() && depth)
  285. {
  286. if (*argClose == "(")
  287. {
  288. depth++;
  289. }
  290. if (*argClose == ")")
  291. {
  292. depth--;
  293. }
  294. argClose++;
  295. }
  296. if (depth)
  297. {
  298. cmOStringStream error;
  299. error << "mismatched parenthesis in condition";
  300. delete [] *errorString;
  301. *errorString = new char[error.str().size() + 1];
  302. strcpy(*errorString, error.str().c_str());
  303. return false;
  304. }
  305. // store the reduced args in this vector
  306. std::vector<std::string> newArgs2;
  307. // copy to the list structure
  308. std::list<std::string>::iterator argP1 = arg;
  309. argP1++;
  310. for(; argP1 != argClose; argP1++)
  311. {
  312. newArgs2.push_back(*argP1);
  313. }
  314. newArgs2.pop_back();
  315. // now recursively invoke IsTrue to handle the values inside the parenthetical expression
  316. bool value =
  317. cmIfCommand::IsTrue(newArgs2, errorString, makefile);
  318. if(value)
  319. {
  320. *arg = "1";
  321. }
  322. else
  323. {
  324. *arg = "0";
  325. }
  326. argP1 = arg;
  327. argP1++;
  328. // remove the now evaluated parenthetical expression
  329. newArgs.erase(argP1,argClose);
  330. }
  331. ++arg;
  332. }
  333. }
  334. while (reducible);
  335. return true;
  336. }
  337. //=========================================================================
  338. // level one handles most predicates except for NOT
  339. bool HandleLevel1(std::list<std::string> &newArgs,
  340. cmMakefile *makefile,
  341. char **)
  342. {
  343. int reducible;
  344. do
  345. {
  346. reducible = 0;
  347. std::list<std::string>::iterator arg = newArgs.begin();
  348. std::list<std::string>::iterator argP1;
  349. std::list<std::string>::iterator argP2;
  350. while (arg != newArgs.end())
  351. {
  352. argP1 = arg;
  353. IncrementArguments(newArgs,argP1,argP2);
  354. // does a file exist
  355. if (*arg == "EXISTS" && argP1 != newArgs.end())
  356. {
  357. HandlePredicate(
  358. cmSystemTools::FileExists((argP1)->c_str()),
  359. reducible, arg, newArgs, argP1, argP2);
  360. }
  361. // does a directory with this name exist
  362. if (*arg == "IS_DIRECTORY" && argP1 != newArgs.end())
  363. {
  364. HandlePredicate(
  365. cmSystemTools::FileIsDirectory((argP1)->c_str()),
  366. reducible, arg, newArgs, argP1, argP2);
  367. }
  368. // is the given path an absolute path ?
  369. if (*arg == "IS_ABSOLUTE" && argP1 != newArgs.end())
  370. {
  371. HandlePredicate(
  372. cmSystemTools::FileIsFullPath((argP1)->c_str()),
  373. reducible, arg, newArgs, argP1, argP2);
  374. }
  375. // does a command exist
  376. if (*arg == "COMMAND" && argP1 != newArgs.end())
  377. {
  378. HandlePredicate(
  379. makefile->CommandExists((argP1)->c_str()),
  380. reducible, arg, newArgs, argP1, argP2);
  381. }
  382. // does a policy exist
  383. if (*arg == "POLICY" && argP1 != newArgs.end())
  384. {
  385. cmPolicies::PolicyID pid;
  386. HandlePredicate(
  387. makefile->GetPolicies()->GetPolicyID((argP1)->c_str(), pid),
  388. reducible, arg, newArgs, argP1, argP2);
  389. }
  390. // is a variable defined
  391. if (*arg == "DEFINED" && argP1 != newArgs.end())
  392. {
  393. size_t argP1len = argP1->size();
  394. bool bdef = false;
  395. if(argP1len > 4 && argP1->substr(0, 4) == "ENV{" &&
  396. argP1->operator[](argP1len-1) == '}')
  397. {
  398. std::string env = argP1->substr(4, argP1len-5);
  399. bdef = cmSystemTools::GetEnv(env.c_str())?true:false;
  400. }
  401. else
  402. {
  403. bdef = makefile->IsDefinitionSet((argP1)->c_str());
  404. }
  405. HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  406. }
  407. ++arg;
  408. }
  409. }
  410. while (reducible);
  411. return true;
  412. }
  413. //=========================================================================
  414. // level two handles most binary operations except for AND OR
  415. bool HandleLevel2(std::list<std::string> &newArgs,
  416. cmMakefile *makefile,
  417. char **errorString)
  418. {
  419. int reducible;
  420. const char *def;
  421. const char *def2;
  422. do
  423. {
  424. reducible = 0;
  425. std::list<std::string>::iterator arg = newArgs.begin();
  426. std::list<std::string>::iterator argP1;
  427. std::list<std::string>::iterator argP2;
  428. while (arg != newArgs.end())
  429. {
  430. argP1 = arg;
  431. IncrementArguments(newArgs,argP1,argP2);
  432. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  433. *(argP1) == "MATCHES")
  434. {
  435. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  436. const char* rex = (argP2)->c_str();
  437. cmStringCommand::ClearMatches(makefile);
  438. cmsys::RegularExpression regEntry;
  439. if ( !regEntry.compile(rex) )
  440. {
  441. cmOStringStream error;
  442. error << "Regular expression \"" << rex << "\" cannot compile";
  443. delete [] *errorString;
  444. *errorString = new char[error.str().size() + 1];
  445. strcpy(*errorString, error.str().c_str());
  446. return false;
  447. }
  448. if (regEntry.find(def))
  449. {
  450. cmStringCommand::StoreMatches(makefile, regEntry);
  451. *arg = "1";
  452. }
  453. else
  454. {
  455. *arg = "0";
  456. }
  457. newArgs.erase(argP2);
  458. newArgs.erase(argP1);
  459. argP1 = arg;
  460. IncrementArguments(newArgs,argP1,argP2);
  461. reducible = 1;
  462. }
  463. if (argP1 != newArgs.end() && *arg == "MATCHES")
  464. {
  465. *arg = "0";
  466. newArgs.erase(argP1);
  467. argP1 = arg;
  468. IncrementArguments(newArgs,argP1,argP2);
  469. reducible = 1;
  470. }
  471. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  472. (*(argP1) == "LESS" || *(argP1) == "GREATER" ||
  473. *(argP1) == "EQUAL"))
  474. {
  475. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  476. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  477. double lhs;
  478. double rhs;
  479. bool result;
  480. if(sscanf(def, "%lg", &lhs) != 1 ||
  481. sscanf(def2, "%lg", &rhs) != 1)
  482. {
  483. result = false;
  484. }
  485. else if (*(argP1) == "LESS")
  486. {
  487. result = (lhs < rhs);
  488. }
  489. else if (*(argP1) == "GREATER")
  490. {
  491. result = (lhs > rhs);
  492. }
  493. else
  494. {
  495. result = (lhs == rhs);
  496. }
  497. HandleBinaryOp(result,
  498. reducible, arg, newArgs, argP1, argP2);
  499. }
  500. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  501. (*(argP1) == "STRLESS" ||
  502. *(argP1) == "STREQUAL" ||
  503. *(argP1) == "STRGREATER"))
  504. {
  505. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  506. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  507. int val = strcmp(def,def2);
  508. bool result;
  509. if (*(argP1) == "STRLESS")
  510. {
  511. result = (val < 0);
  512. }
  513. else if (*(argP1) == "STRGREATER")
  514. {
  515. result = (val > 0);
  516. }
  517. else // strequal
  518. {
  519. result = (val == 0);
  520. }
  521. HandleBinaryOp(result,
  522. reducible, arg, newArgs, argP1, argP2);
  523. }
  524. // is file A newer than file B
  525. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  526. *(argP1) == "IS_NEWER_THAN")
  527. {
  528. int fileIsNewer=0;
  529. bool success=cmSystemTools::FileTimeCompare(arg->c_str(),
  530. (argP2)->c_str(),
  531. &fileIsNewer);
  532. HandleBinaryOp(
  533. (success==false || fileIsNewer==1 || fileIsNewer==0),
  534. reducible, arg, newArgs, argP1, argP2);
  535. }
  536. ++arg;
  537. }
  538. }
  539. while (reducible);
  540. return true;
  541. }
  542. //=========================================================================
  543. // level 3 handles NOT
  544. bool HandleLevel3(std::list<std::string> &newArgs,
  545. cmMakefile *makefile,
  546. char **)
  547. {
  548. int reducible;
  549. const char *def;
  550. do
  551. {
  552. reducible = 0;
  553. std::list<std::string>::iterator arg = newArgs.begin();
  554. std::list<std::string>::iterator argP1;
  555. std::list<std::string>::iterator argP2;
  556. while (arg != newArgs.end())
  557. {
  558. argP1 = arg;
  559. IncrementArguments(newArgs,argP1,argP2);
  560. if (argP1 != newArgs.end() && *arg == "NOT")
  561. {
  562. def = cmIfCommand::GetVariableOrNumber((argP1)->c_str(), makefile);
  563. HandlePredicate(cmSystemTools::IsOff(def),
  564. reducible, arg, newArgs, argP1, argP2);
  565. }
  566. ++arg;
  567. }
  568. }
  569. while (reducible);
  570. return true;
  571. }
  572. //=========================================================================
  573. // level 4 handles AND OR
  574. bool HandleLevel4(std::list<std::string> &newArgs,
  575. cmMakefile *makefile,
  576. char **)
  577. {
  578. int reducible;
  579. const char *def;
  580. const char *def2;
  581. do
  582. {
  583. reducible = 0;
  584. std::list<std::string>::iterator arg = newArgs.begin();
  585. std::list<std::string>::iterator argP1;
  586. std::list<std::string>::iterator argP2;
  587. while (arg != newArgs.end())
  588. {
  589. argP1 = arg;
  590. IncrementArguments(newArgs,argP1,argP2);
  591. if (argP1 != newArgs.end() && *(argP1) == "AND" &&
  592. argP2 != newArgs.end())
  593. {
  594. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  595. def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
  596. HandleBinaryOp(
  597. !(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2)),
  598. reducible, arg, newArgs, argP1, argP2);
  599. }
  600. if (argP1 != newArgs.end() && *(argP1) == "OR" &&
  601. argP2 != newArgs.end())
  602. {
  603. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  604. def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
  605. HandleBinaryOp(
  606. !(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2)),
  607. reducible, arg, newArgs, argP1, argP2);
  608. }
  609. ++arg;
  610. }
  611. }
  612. while (reducible);
  613. return true;
  614. }
  615. }
  616. //=========================================================================
  617. // order of operations,
  618. // 1. ( ) -- parenthetical groups
  619. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  620. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  621. // 4. NOT
  622. // 5. AND OR
  623. //
  624. // There is an issue on whether the arguments should be values of references,
  625. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  626. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  627. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  628. // take numeric values or variable names. STRLESS and STRGREATER take
  629. // variable names but if the variable name is not found it will use the name
  630. // directly. AND OR take variables or the values 0 or 1.
  631. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  632. char **errorString, cmMakefile *makefile)
  633. {
  634. // check for the different signatures
  635. const char *def;
  636. const char* msg = "Unknown arguments specified";
  637. *errorString = new char[strlen(msg) + 1];
  638. strcpy(*errorString, msg);
  639. // handle empty invocation
  640. if (args.size() < 1)
  641. {
  642. delete [] *errorString;
  643. *errorString = 0;
  644. return false;
  645. }
  646. // store the reduced args in this vector
  647. std::list<std::string> newArgs;
  648. // copy to the list structure
  649. for(unsigned int i = 0; i < args.size(); ++i)
  650. {
  651. newArgs.push_back(args[i]);
  652. }
  653. // now loop through the arguments and see if we can reduce any of them
  654. // we do this multiple times. Once for each level of precedence
  655. if (!HandleLevel0(newArgs, makefile, errorString)) // parens
  656. {
  657. return false;
  658. }
  659. if (!HandleLevel1(newArgs, makefile, errorString)) //predicates
  660. {
  661. return false;
  662. }
  663. if (!HandleLevel2(newArgs, makefile, errorString)) // binary ops
  664. {
  665. return false;
  666. }
  667. if (!HandleLevel3(newArgs, makefile, errorString)) // NOT
  668. {
  669. return false;
  670. }
  671. if (!HandleLevel4(newArgs, makefile, errorString)) // AND OR
  672. {
  673. return false;
  674. }
  675. // now at the end there should only be one argument left
  676. if (newArgs.size() == 1)
  677. {
  678. delete [] *errorString;
  679. *errorString = 0;
  680. if (*newArgs.begin() == "0")
  681. {
  682. return false;
  683. }
  684. if (*newArgs.begin() == "1")
  685. {
  686. return true;
  687. }
  688. def = makefile->GetDefinition(args[0].c_str());
  689. if(cmSystemTools::IsOff(def))
  690. {
  691. return false;
  692. }
  693. }
  694. return true;
  695. }
  696. //=========================================================================
  697. const char* cmIfCommand::GetVariableOrString(const char* str,
  698. const cmMakefile* mf)
  699. {
  700. const char* def = mf->GetDefinition(str);
  701. if(!def)
  702. {
  703. def = str;
  704. }
  705. return def;
  706. }
  707. //=========================================================================
  708. const char* cmIfCommand::GetVariableOrNumber(const char* str,
  709. const cmMakefile* mf)
  710. {
  711. const char* def = mf->GetDefinition(str);
  712. if(!def)
  713. {
  714. if (atoi(str))
  715. {
  716. def = str;
  717. }
  718. }
  719. return def;
  720. }