cmIfCommand.cxx 14 KB

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