cmIfCommand.cxx 15 KB

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