cmIfCommand.cxx 18 KB

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