cmIfCommand.cxx 15 KB

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