cmIfCommand.cxx 15 KB

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