cmIfCommand.cxx 15 KB

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