cmIfCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <stdlib.h> // required for atof
  15. #include <list>
  16. #include <cmsys/RegularExpression.hxx>
  17. bool cmIfFunctionBlocker::
  18. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  19. {
  20. // always let if statements through
  21. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
  22. {
  23. return false;
  24. }
  25. // watch for our ELSE or ENDIF
  26. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"else") ||
  27. !cmSystemTools::Strucmp(lff.Name.c_str(),"elseif") ||
  28. !cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  29. {
  30. // if it was an else statement then we should change state
  31. // and block this Else Command
  32. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"else"))
  33. {
  34. this->IsBlocking = this->HasRun;
  35. return true;
  36. }
  37. // if it was an elseif statement then we should check state
  38. // and possibly block this Else Command
  39. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"elseif"))
  40. {
  41. if (!this->HasRun)
  42. {
  43. char* errorString = 0;
  44. std::vector<std::string> expandedArguments;
  45. mf.ExpandArguments(lff.Arguments, expandedArguments);
  46. bool isTrue =
  47. cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
  48. if (errorString)
  49. {
  50. std::string err = "had incorrect arguments: ";
  51. unsigned int i;
  52. for(i =0; i < lff.Arguments.size(); ++i)
  53. {
  54. err += (lff.Arguments[i].Quoted?"\"":"");
  55. err += lff.Arguments[i].Value;
  56. err += (lff.Arguments[i].Quoted?"\"":"");
  57. err += " ";
  58. }
  59. err += "(";
  60. err += errorString;
  61. err += ").";
  62. cmSystemTools::Error(err.c_str());
  63. delete [] errorString;
  64. return false;
  65. }
  66. if (isTrue)
  67. {
  68. this->IsBlocking = false;
  69. this->HasRun = true;
  70. return true;
  71. }
  72. }
  73. this->IsBlocking = true;
  74. return true;
  75. }
  76. // otherwise it must be an ENDIF statement, in that case remove the
  77. // function blocker
  78. mf.RemoveFunctionBlocker(lff);
  79. return true;
  80. }
  81. return this->IsBlocking;
  82. }
  83. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  84. cmMakefile& mf)
  85. {
  86. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  87. {
  88. if (mf.IsOn("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS")
  89. || lff.Arguments == this->Args)
  90. {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. void cmIfFunctionBlocker::
  97. ScopeEnded(cmMakefile &mf)
  98. {
  99. std::string errmsg = "The end of a CMakeLists file was reached with an "
  100. "IF statement that was not closed properly.\nWithin the directory: ";
  101. errmsg += mf.GetCurrentDirectory();
  102. errmsg += "\nThe arguments are: ";
  103. for(std::vector<cmListFileArgument>::const_iterator j = this->Args.begin();
  104. j != this->Args.end(); ++j)
  105. {
  106. errmsg += (j->Quoted?"\"":"");
  107. errmsg += j->Value;
  108. errmsg += (j->Quoted?"\"":"");
  109. errmsg += " ";
  110. }
  111. cmSystemTools::Message(errmsg.c_str(), "Warning");
  112. }
  113. bool cmIfCommand
  114. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args)
  115. {
  116. char* errorString = 0;
  117. std::vector<std::string> expandedArguments;
  118. this->Makefile->ExpandArguments(args, expandedArguments);
  119. bool isTrue =
  120. cmIfCommand::IsTrue(expandedArguments,&errorString,this->Makefile);
  121. if (errorString)
  122. {
  123. std::string err = "had incorrect arguments: ";
  124. unsigned int i;
  125. for(i =0; i < args.size(); ++i)
  126. {
  127. err += (args[i].Quoted?"\"":"");
  128. err += args[i].Value;
  129. err += (args[i].Quoted?"\"":"");
  130. err += " ";
  131. }
  132. err += "(";
  133. err += errorString;
  134. err += ").";
  135. this->SetError(err.c_str());
  136. delete [] errorString;
  137. return false;
  138. }
  139. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  140. // if is isn't true block the commands
  141. f->IsBlocking = !isTrue;
  142. if (isTrue)
  143. {
  144. f->HasRun = true;
  145. }
  146. f->Args = args;
  147. this->Makefile->AddFunctionBlocker(f);
  148. return true;
  149. }
  150. namespace
  151. {
  152. void IncrementArguments(std::list<std::string> &newArgs,
  153. std::list<std::string>::iterator &argP1,
  154. std::list<std::string>::iterator &argP2)
  155. {
  156. if (argP1 != newArgs.end())
  157. {
  158. argP1++;
  159. argP2 = argP1;
  160. if (argP1 != newArgs.end())
  161. {
  162. argP2++;
  163. }
  164. }
  165. }
  166. }
  167. // order of operations,
  168. // IS_DIRECTORY EXISTS COMMAND DEFINED
  169. // MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL
  170. // AND OR
  171. //
  172. // There is an issue on whether the arguments should be values of references,
  173. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  174. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  175. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  176. // take numeric values or variable names. STRLESS and STRGREATER take
  177. // variable names but if the variable name is not found it will use the name
  178. // directly. AND OR take variables or the values 0 or 1.
  179. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  180. char **errorString, const cmMakefile *makefile)
  181. {
  182. // check for the different signatures
  183. const char *def;
  184. const char *def2;
  185. const char* msg = "Unknown arguments specified";
  186. *errorString = new char[strlen(msg) + 1];
  187. strcpy(*errorString, msg);
  188. // handle empty invocation
  189. if (args.size() < 1)
  190. {
  191. delete [] *errorString;
  192. *errorString = 0;
  193. return false;
  194. }
  195. // this is a super ugly hack. Basically old versiosn of VTK and ITK have a
  196. // bad test to check for more recent versions of CMake in the
  197. // CMakeLists.txt file for libtiff. So when we reved CMake up to 2.0 the
  198. // test started failing because the minor version went to zero this causes
  199. // the test to pass
  200. if (args.size() == 3 &&
  201. (makefile->GetDefinition("VTKTIFF_SOURCE_DIR") ||
  202. makefile->GetDefinition("ITKTIFF_SOURCE_DIR")) &&
  203. args[0] == "CMAKE_MINOR_VERSION" &&
  204. args[1] == "MATCHES")
  205. {
  206. delete [] *errorString;
  207. *errorString = 0;
  208. return true;
  209. }
  210. // store the reduced args in this vector
  211. std::list<std::string> newArgs;
  212. int reducible;
  213. unsigned int i;
  214. // copy to the list structure
  215. for(i = 0; i < args.size(); ++i)
  216. {
  217. newArgs.push_back(args[i]);
  218. }
  219. std::list<std::string>::iterator argP1;
  220. std::list<std::string>::iterator argP2;
  221. // now loop through the arguments and see if we can reduce any of them
  222. // we do this multiple times. Once for each level of precedence
  223. do
  224. {
  225. reducible = 0;
  226. std::list<std::string>::iterator arg = newArgs.begin();
  227. while (arg != newArgs.end())
  228. {
  229. argP1 = arg;
  230. IncrementArguments(newArgs,argP1,argP2);
  231. // does a file exist
  232. if (*arg == "EXISTS" && argP1 != newArgs.end())
  233. {
  234. if(cmSystemTools::FileExists((argP1)->c_str()))
  235. {
  236. *arg = "1";
  237. }
  238. else
  239. {
  240. *arg = "0";
  241. }
  242. newArgs.erase(argP1);
  243. argP1 = arg;
  244. IncrementArguments(newArgs,argP1,argP2);
  245. reducible = 1;
  246. }
  247. // does a directory with this name exist
  248. if (*arg == "IS_DIRECTORY" && argP1 != newArgs.end())
  249. {
  250. if(cmSystemTools::FileIsDirectory((argP1)->c_str()))
  251. {
  252. *arg = "1";
  253. }
  254. else
  255. {
  256. *arg = "0";
  257. }
  258. newArgs.erase(argP1);
  259. argP1 = arg;
  260. IncrementArguments(newArgs,argP1,argP2);
  261. reducible = 1;
  262. }
  263. // is file A newer than file B
  264. if (*arg == "FILE_IS_NEWER" &&
  265. argP1 != newArgs.end() && argP2 != newArgs.end())
  266. {
  267. int fileIsNewer=0;
  268. bool success=cmSystemTools::FileTimeCompare((argP1)->c_str(),
  269. (argP2)->c_str(),
  270. &fileIsNewer);
  271. if(success==false || fileIsNewer==1 || fileIsNewer==0)
  272. {
  273. *arg = "1";
  274. }
  275. else
  276. {
  277. *arg = "0";
  278. }
  279. newArgs.erase(argP2);
  280. newArgs.erase(argP1);
  281. argP1 = arg;
  282. IncrementArguments(newArgs,argP1,argP2);
  283. reducible = 1;
  284. }
  285. // does a command exist
  286. if (*arg == "COMMAND" && argP1 != newArgs.end())
  287. {
  288. if(makefile->CommandExists((argP1)->c_str()))
  289. {
  290. *arg = "1";
  291. }
  292. else
  293. {
  294. *arg = "0";
  295. }
  296. newArgs.erase(argP1);
  297. argP1 = arg;
  298. IncrementArguments(newArgs,argP1,argP2);
  299. reducible = 1;
  300. }
  301. // is a variable defined
  302. if (*arg == "DEFINED" && argP1 != newArgs.end())
  303. {
  304. size_t argP1len = argP1->size();
  305. if(argP1len > 4 && argP1->substr(0, 4) == "ENV{" &&
  306. argP1->operator[](argP1len-1) == '}')
  307. {
  308. std::string env = argP1->substr(4, argP1len-5);
  309. def = cmSystemTools::GetEnv(env.c_str());
  310. }
  311. else
  312. {
  313. def = makefile->GetDefinition((argP1)->c_str());
  314. }
  315. if(def)
  316. {
  317. *arg = "1";
  318. }
  319. else
  320. {
  321. *arg = "0";
  322. }
  323. newArgs.erase(argP1);
  324. argP1 = arg;
  325. IncrementArguments(newArgs,argP1,argP2);
  326. reducible = 1;
  327. }
  328. ++arg;
  329. }
  330. }
  331. while (reducible);
  332. // now loop through the arguments and see if we can reduce any of them
  333. // we do this multiple times. Once for each level of precedence
  334. do
  335. {
  336. reducible = 0;
  337. std::list<std::string>::iterator arg = newArgs.begin();
  338. while (arg != newArgs.end())
  339. {
  340. argP1 = arg;
  341. IncrementArguments(newArgs,argP1,argP2);
  342. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  343. *(argP1) == "MATCHES")
  344. {
  345. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  346. const char* rex = (argP2)->c_str();
  347. cmsys::RegularExpression regEntry;
  348. if ( !regEntry.compile(rex) )
  349. {
  350. cmOStringStream error;
  351. error << "Regular expression \"" << rex << "\" cannot compile";
  352. delete [] *errorString;
  353. *errorString = new char[error.str().size() + 1];
  354. strcpy(*errorString, error.str().c_str());
  355. return false;
  356. }
  357. if (regEntry.find(def))
  358. {
  359. *arg = "1";
  360. }
  361. else
  362. {
  363. *arg = "0";
  364. }
  365. newArgs.erase(argP2);
  366. newArgs.erase(argP1);
  367. argP1 = arg;
  368. IncrementArguments(newArgs,argP1,argP2);
  369. reducible = 1;
  370. }
  371. if (argP1 != newArgs.end() && *arg == "MATCHES")
  372. {
  373. *arg = "0";
  374. newArgs.erase(argP1);
  375. argP1 = arg;
  376. IncrementArguments(newArgs,argP1,argP2);
  377. reducible = 1;
  378. }
  379. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  380. (*(argP1) == "LESS" || *(argP1) == "GREATER" ||
  381. *(argP1) == "EQUAL"))
  382. {
  383. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  384. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  385. if (*(argP1) == "LESS")
  386. {
  387. if(atof(def) < atof(def2))
  388. {
  389. *arg = "1";
  390. }
  391. else
  392. {
  393. *arg = "0";
  394. }
  395. }
  396. else if (*(argP1) == "GREATER")
  397. {
  398. if(atof(def) > atof(def2))
  399. {
  400. *arg = "1";
  401. }
  402. else
  403. {
  404. *arg = "0";
  405. }
  406. }
  407. else
  408. {
  409. if(atof(def) == atof(def2))
  410. {
  411. *arg = "1";
  412. }
  413. else
  414. {
  415. *arg = "0";
  416. }
  417. }
  418. newArgs.erase(argP2);
  419. newArgs.erase(argP1);
  420. argP1 = arg;
  421. IncrementArguments(newArgs,argP1,argP2);
  422. reducible = 1;
  423. }
  424. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  425. (*(argP1) == "STRLESS" ||
  426. *(argP1) == "STREQUAL" ||
  427. *(argP1) == "STRGREATER"))
  428. {
  429. def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
  430. def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
  431. int val = strcmp(def,def2);
  432. int result;
  433. if (*(argP1) == "STRLESS")
  434. {
  435. result = (val < 0);
  436. }
  437. else if (*(argP1) == "STRGREATER")
  438. {
  439. result = (val > 0);
  440. }
  441. else // strequal
  442. {
  443. result = (val == 0);
  444. }
  445. if(result)
  446. {
  447. *arg = "1";
  448. }
  449. else
  450. {
  451. *arg = "0";
  452. }
  453. newArgs.erase(argP2);
  454. newArgs.erase(argP1);
  455. argP1 = arg;
  456. IncrementArguments(newArgs,argP1,argP2);
  457. reducible = 1;
  458. }
  459. ++arg;
  460. }
  461. }
  462. while (reducible);
  463. // now loop through the arguments and see if we can reduce any of them
  464. // we do this multiple times. Once for each level of precedence
  465. do
  466. {
  467. reducible = 0;
  468. std::list<std::string>::iterator arg = newArgs.begin();
  469. while (arg != newArgs.end())
  470. {
  471. argP1 = arg;
  472. IncrementArguments(newArgs,argP1,argP2);
  473. if (argP1 != newArgs.end() && *arg == "NOT")
  474. {
  475. def = cmIfCommand::GetVariableOrNumber((argP1)->c_str(), makefile);
  476. if(!cmSystemTools::IsOff(def))
  477. {
  478. *arg = "0";
  479. }
  480. else
  481. {
  482. *arg = "1";
  483. }
  484. newArgs.erase(argP1);
  485. argP1 = arg;
  486. IncrementArguments(newArgs,argP1,argP2);
  487. reducible = 1;
  488. }
  489. ++arg;
  490. }
  491. }
  492. while (reducible);
  493. // now loop through the arguments and see if we can reduce any of them
  494. // we do this multiple times. Once for each level of precedence
  495. do
  496. {
  497. reducible = 0;
  498. std::list<std::string>::iterator arg = newArgs.begin();
  499. while (arg != newArgs.end())
  500. {
  501. argP1 = arg;
  502. IncrementArguments(newArgs,argP1,argP2);
  503. if (argP1 != newArgs.end() && *(argP1) == "AND" &&
  504. argP2 != newArgs.end())
  505. {
  506. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  507. def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
  508. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  509. {
  510. *arg = "0";
  511. }
  512. else
  513. {
  514. *arg = "1";
  515. }
  516. newArgs.erase(argP2);
  517. newArgs.erase(argP1);
  518. argP1 = arg;
  519. IncrementArguments(newArgs,argP1,argP2);
  520. reducible = 1;
  521. }
  522. if (argP1 != newArgs.end() && *(argP1) == "OR" &&
  523. argP2 != newArgs.end())
  524. {
  525. def = cmIfCommand::GetVariableOrNumber(arg->c_str(), makefile);
  526. def2 = cmIfCommand::GetVariableOrNumber((argP2)->c_str(), makefile);
  527. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  528. {
  529. *arg = "0";
  530. }
  531. else
  532. {
  533. *arg = "1";
  534. }
  535. newArgs.erase(argP2);
  536. newArgs.erase(argP1);
  537. argP1 = arg;
  538. IncrementArguments(newArgs,argP1,argP2);
  539. reducible = 1;
  540. }
  541. ++arg;
  542. }
  543. }
  544. while (reducible);
  545. // now at the end there should only be one argument left
  546. if (newArgs.size() == 1)
  547. {
  548. delete [] *errorString;
  549. *errorString = 0;
  550. if (*newArgs.begin() == "0")
  551. {
  552. return false;
  553. }
  554. if (*newArgs.begin() == "1")
  555. {
  556. return true;
  557. }
  558. def = makefile->GetDefinition(args[0].c_str());
  559. if(cmSystemTools::IsOff(def))
  560. {
  561. return false;
  562. }
  563. }
  564. return true;
  565. }
  566. const char* cmIfCommand::GetVariableOrString(const char* str,
  567. const cmMakefile* mf)
  568. {
  569. const char* def = mf->GetDefinition(str);
  570. if(!def)
  571. {
  572. def = str;
  573. }
  574. return def;
  575. }
  576. const char* cmIfCommand::GetVariableOrNumber(const char* str,
  577. const cmMakefile* mf)
  578. {
  579. const char* def = mf->GetDefinition(str);
  580. if(!def)
  581. {
  582. if (atoi(str))
  583. {
  584. def = str;
  585. }
  586. }
  587. return def;
  588. }