cmConditionEvaluator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmConditionEvaluator.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmOutputConverter.h"
  13. static std::string const keyAND = "AND";
  14. static std::string const keyCOMMAND = "COMMAND";
  15. static std::string const keyDEFINED = "DEFINED";
  16. static std::string const keyEQUAL = "EQUAL";
  17. static std::string const keyEXISTS = "EXISTS";
  18. static std::string const keyGREATER = "GREATER";
  19. static std::string const keyIN_LIST = "IN_LIST";
  20. static std::string const keyIS_ABSOLUTE = "IS_ABSOLUTE";
  21. static std::string const keyIS_DIRECTORY = "IS_DIRECTORY";
  22. static std::string const keyIS_NEWER_THAN = "IS_NEWER_THAN";
  23. static std::string const keyIS_SYMLINK = "IS_SYMLINK";
  24. static std::string const keyLESS = "LESS";
  25. static std::string const keyMATCHES = "MATCHES";
  26. static std::string const keyNOT = "NOT";
  27. static std::string const keyOR = "OR";
  28. static std::string const keyParenL = "(";
  29. static std::string const keyParenR = ")";
  30. static std::string const keyPOLICY = "POLICY";
  31. static std::string const keySTREQUAL = "STREQUAL";
  32. static std::string const keySTRGREATER = "STRGREATER";
  33. static std::string const keySTRLESS = "STRLESS";
  34. static std::string const keyTARGET = "TARGET";
  35. static std::string const keyTEST = "TEST";
  36. static std::string const keyVERSION_EQUAL = "VERSION_EQUAL";
  37. static std::string const keyVERSION_GREATER = "VERSION_GREATER";
  38. static std::string const keyVERSION_LESS = "VERSION_LESS";
  39. cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile,
  40. const cmListFileContext& context,
  41. const cmListFileBacktrace& bt)
  42. : Makefile(makefile)
  43. , ExecutionContext(context)
  44. , Backtrace(bt)
  45. , Policy12Status(makefile.GetPolicyStatus(cmPolicies::CMP0012))
  46. , Policy54Status(makefile.GetPolicyStatus(cmPolicies::CMP0054))
  47. , Policy57Status(makefile.GetPolicyStatus(cmPolicies::CMP0057))
  48. , Policy64Status(makefile.GetPolicyStatus(cmPolicies::CMP0064))
  49. {
  50. }
  51. //=========================================================================
  52. // order of operations,
  53. // 1. ( ) -- parenthetical groups
  54. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  55. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  56. // 4. NOT
  57. // 5. AND OR
  58. //
  59. // There is an issue on whether the arguments should be values of references,
  60. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  61. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  62. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  63. // take numeric values or variable names. STRLESS and STRGREATER take
  64. // variable names but if the variable name is not found it will use the name
  65. // directly. AND OR take variables or the values 0 or 1.
  66. bool cmConditionEvaluator::IsTrue(
  67. const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
  68. cmake::MessageType& status)
  69. {
  70. errorString = "";
  71. // handle empty invocation
  72. if (args.size() < 1) {
  73. return false;
  74. }
  75. // store the reduced args in this vector
  76. cmArgumentList newArgs;
  77. // copy to the list structure
  78. newArgs.insert(newArgs.end(), args.begin(), args.end());
  79. // now loop through the arguments and see if we can reduce any of them
  80. // we do this multiple times. Once for each level of precedence
  81. // parens
  82. if (!this->HandleLevel0(newArgs, errorString, status)) {
  83. return false;
  84. }
  85. // predicates
  86. if (!this->HandleLevel1(newArgs, errorString, status)) {
  87. return false;
  88. }
  89. // binary ops
  90. if (!this->HandleLevel2(newArgs, errorString, status)) {
  91. return false;
  92. }
  93. // NOT
  94. if (!this->HandleLevel3(newArgs, errorString, status)) {
  95. return false;
  96. }
  97. // AND OR
  98. if (!this->HandleLevel4(newArgs, errorString, status)) {
  99. return false;
  100. }
  101. // now at the end there should only be one argument left
  102. if (newArgs.size() != 1) {
  103. errorString = "Unknown arguments specified";
  104. status = cmake::FATAL_ERROR;
  105. return false;
  106. }
  107. return this->GetBooleanValueWithAutoDereference(*(newArgs.begin()),
  108. errorString, status, true);
  109. }
  110. //=========================================================================
  111. const char* cmConditionEvaluator::GetDefinitionIfUnquoted(
  112. cmExpandedCommandArgument const& argument) const
  113. {
  114. if ((this->Policy54Status != cmPolicies::WARN &&
  115. this->Policy54Status != cmPolicies::OLD) &&
  116. argument.WasQuoted()) {
  117. return CM_NULLPTR;
  118. }
  119. const char* def = this->Makefile.GetDefinition(argument.GetValue());
  120. if (def && argument.WasQuoted() &&
  121. this->Policy54Status == cmPolicies::WARN) {
  122. if (!this->Makefile.HasCMP0054AlreadyBeenReported(
  123. this->ExecutionContext)) {
  124. std::ostringstream e;
  125. e << (cmPolicies::GetPolicyWarning(cmPolicies::CMP0054)) << "\n";
  126. e << "Quoted variables like \"" << argument.GetValue()
  127. << "\" will no longer be dereferenced "
  128. "when the policy is set to NEW. "
  129. "Since the policy is not set the OLD behavior will be used.";
  130. this->Makefile.GetCMakeInstance()->IssueMessage(
  131. cmake::AUTHOR_WARNING, e.str(), this->Backtrace);
  132. }
  133. }
  134. return def;
  135. }
  136. //=========================================================================
  137. const char* cmConditionEvaluator::GetVariableOrString(
  138. const cmExpandedCommandArgument& argument) const
  139. {
  140. const char* def = this->GetDefinitionIfUnquoted(argument);
  141. if (!def) {
  142. def = argument.c_str();
  143. }
  144. return def;
  145. }
  146. //=========================================================================
  147. bool cmConditionEvaluator::IsKeyword(std::string const& keyword,
  148. cmExpandedCommandArgument& argument) const
  149. {
  150. if ((this->Policy54Status != cmPolicies::WARN &&
  151. this->Policy54Status != cmPolicies::OLD) &&
  152. argument.WasQuoted()) {
  153. return false;
  154. }
  155. bool isKeyword = argument.GetValue() == keyword;
  156. if (isKeyword && argument.WasQuoted() &&
  157. this->Policy54Status == cmPolicies::WARN) {
  158. if (!this->Makefile.HasCMP0054AlreadyBeenReported(
  159. this->ExecutionContext)) {
  160. std::ostringstream e;
  161. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0054) << "\n";
  162. e << "Quoted keywords like \"" << argument.GetValue()
  163. << "\" will no longer be interpreted as keywords "
  164. "when the policy is set to NEW. "
  165. "Since the policy is not set the OLD behavior will be used.";
  166. this->Makefile.GetCMakeInstance()->IssueMessage(
  167. cmake::AUTHOR_WARNING, e.str(), this->Backtrace);
  168. }
  169. }
  170. return isKeyword;
  171. }
  172. //=========================================================================
  173. bool cmConditionEvaluator::GetBooleanValue(
  174. cmExpandedCommandArgument& arg) const
  175. {
  176. // Check basic constants.
  177. if (arg == "0") {
  178. return false;
  179. }
  180. if (arg == "1") {
  181. return true;
  182. }
  183. // Check named constants.
  184. if (cmSystemTools::IsOn(arg.c_str())) {
  185. return true;
  186. }
  187. if (cmSystemTools::IsOff(arg.c_str())) {
  188. return false;
  189. }
  190. // Check for numbers.
  191. if (!arg.empty()) {
  192. char* end;
  193. double d = strtod(arg.c_str(), &end);
  194. if (*end == '\0') {
  195. // The whole string is a number. Use C conversion to bool.
  196. return d ? true : false;
  197. }
  198. }
  199. // Check definition.
  200. const char* def = this->GetDefinitionIfUnquoted(arg);
  201. return !cmSystemTools::IsOff(def);
  202. }
  203. //=========================================================================
  204. // Boolean value behavior from CMake 2.6.4 and below.
  205. bool cmConditionEvaluator::GetBooleanValueOld(
  206. cmExpandedCommandArgument const& arg, bool one) const
  207. {
  208. if (one) {
  209. // Old IsTrue behavior for single argument.
  210. if (arg == "0") {
  211. return false;
  212. } else if (arg == "1") {
  213. return true;
  214. } else {
  215. const char* def = this->GetDefinitionIfUnquoted(arg);
  216. return !cmSystemTools::IsOff(def);
  217. }
  218. } else {
  219. // Old GetVariableOrNumber behavior.
  220. const char* def = this->GetDefinitionIfUnquoted(arg);
  221. if (!def && atoi(arg.c_str())) {
  222. def = arg.c_str();
  223. }
  224. return !cmSystemTools::IsOff(def);
  225. }
  226. }
  227. //=========================================================================
  228. // returns the resulting boolean value
  229. bool cmConditionEvaluator::GetBooleanValueWithAutoDereference(
  230. cmExpandedCommandArgument& newArg, std::string& errorString,
  231. cmake::MessageType& status, bool oneArg) const
  232. {
  233. // Use the policy if it is set.
  234. if (this->Policy12Status == cmPolicies::NEW) {
  235. return GetBooleanValue(newArg);
  236. } else if (this->Policy12Status == cmPolicies::OLD) {
  237. return GetBooleanValueOld(newArg, oneArg);
  238. }
  239. // Check policy only if old and new results differ.
  240. bool newResult = this->GetBooleanValue(newArg);
  241. bool oldResult = this->GetBooleanValueOld(newArg, oneArg);
  242. if (newResult != oldResult) {
  243. switch (this->Policy12Status) {
  244. case cmPolicies::WARN: {
  245. errorString = "An argument named \"" + newArg.GetValue() +
  246. "\" appears in a conditional statement. " +
  247. cmPolicies::GetPolicyWarning(cmPolicies::CMP0012);
  248. status = cmake::AUTHOR_WARNING;
  249. }
  250. case cmPolicies::OLD:
  251. return oldResult;
  252. case cmPolicies::REQUIRED_IF_USED:
  253. case cmPolicies::REQUIRED_ALWAYS: {
  254. errorString = "An argument named \"" + newArg.GetValue() +
  255. "\" appears in a conditional statement. " +
  256. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0012);
  257. status = cmake::FATAL_ERROR;
  258. }
  259. case cmPolicies::NEW:
  260. break;
  261. }
  262. }
  263. return newResult;
  264. }
  265. //=========================================================================
  266. void cmConditionEvaluator::IncrementArguments(
  267. cmArgumentList& newArgs, cmArgumentList::iterator& argP1,
  268. cmArgumentList::iterator& argP2) const
  269. {
  270. if (argP1 != newArgs.end()) {
  271. argP1++;
  272. argP2 = argP1;
  273. if (argP1 != newArgs.end()) {
  274. argP2++;
  275. }
  276. }
  277. }
  278. //=========================================================================
  279. // helper function to reduce code duplication
  280. void cmConditionEvaluator::HandlePredicate(
  281. bool value, int& reducible, cmArgumentList::iterator& arg,
  282. cmArgumentList& newArgs, cmArgumentList::iterator& argP1,
  283. cmArgumentList::iterator& argP2) const
  284. {
  285. if (value) {
  286. *arg = cmExpandedCommandArgument("1", true);
  287. } else {
  288. *arg = cmExpandedCommandArgument("0", true);
  289. }
  290. newArgs.erase(argP1);
  291. argP1 = arg;
  292. this->IncrementArguments(newArgs, argP1, argP2);
  293. reducible = 1;
  294. }
  295. //=========================================================================
  296. // helper function to reduce code duplication
  297. void cmConditionEvaluator::HandleBinaryOp(bool value, int& reducible,
  298. cmArgumentList::iterator& arg,
  299. cmArgumentList& newArgs,
  300. cmArgumentList::iterator& argP1,
  301. cmArgumentList::iterator& argP2)
  302. {
  303. if (value) {
  304. *arg = cmExpandedCommandArgument("1", true);
  305. } else {
  306. *arg = cmExpandedCommandArgument("0", true);
  307. }
  308. newArgs.erase(argP2);
  309. newArgs.erase(argP1);
  310. argP1 = arg;
  311. this->IncrementArguments(newArgs, argP1, argP2);
  312. reducible = 1;
  313. }
  314. //=========================================================================
  315. // level 0 processes parenthetical expressions
  316. bool cmConditionEvaluator::HandleLevel0(cmArgumentList& newArgs,
  317. std::string& errorString,
  318. cmake::MessageType& status)
  319. {
  320. int reducible;
  321. do {
  322. reducible = 0;
  323. cmArgumentList::iterator arg = newArgs.begin();
  324. while (arg != newArgs.end()) {
  325. if (IsKeyword(keyParenL, *arg)) {
  326. // search for the closing paren for this opening one
  327. cmArgumentList::iterator argClose;
  328. argClose = arg;
  329. argClose++;
  330. unsigned int depth = 1;
  331. while (argClose != newArgs.end() && depth) {
  332. if (this->IsKeyword(keyParenL, *argClose)) {
  333. depth++;
  334. }
  335. if (this->IsKeyword(keyParenR, *argClose)) {
  336. depth--;
  337. }
  338. argClose++;
  339. }
  340. if (depth) {
  341. errorString = "mismatched parenthesis in condition";
  342. status = cmake::FATAL_ERROR;
  343. return false;
  344. }
  345. // store the reduced args in this vector
  346. std::vector<cmExpandedCommandArgument> newArgs2;
  347. // copy to the list structure
  348. cmArgumentList::iterator argP1 = arg;
  349. argP1++;
  350. newArgs2.insert(newArgs2.end(), argP1, argClose);
  351. newArgs2.pop_back();
  352. // now recursively invoke IsTrue to handle the values inside the
  353. // parenthetical expression
  354. bool value = this->IsTrue(newArgs2, errorString, status);
  355. if (value) {
  356. *arg = cmExpandedCommandArgument("1", true);
  357. } else {
  358. *arg = cmExpandedCommandArgument("0", true);
  359. }
  360. argP1 = arg;
  361. argP1++;
  362. // remove the now evaluated parenthetical expression
  363. newArgs.erase(argP1, argClose);
  364. }
  365. ++arg;
  366. }
  367. } while (reducible);
  368. return true;
  369. }
  370. //=========================================================================
  371. // level one handles most predicates except for NOT
  372. bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
  373. cmake::MessageType&)
  374. {
  375. int reducible;
  376. do {
  377. reducible = 0;
  378. cmArgumentList::iterator arg = newArgs.begin();
  379. cmArgumentList::iterator argP1;
  380. cmArgumentList::iterator argP2;
  381. while (arg != newArgs.end()) {
  382. argP1 = arg;
  383. this->IncrementArguments(newArgs, argP1, argP2);
  384. // does a file exist
  385. if (this->IsKeyword(keyEXISTS, *arg) && argP1 != newArgs.end()) {
  386. this->HandlePredicate(cmSystemTools::FileExists(argP1->c_str()),
  387. reducible, arg, newArgs, argP1, argP2);
  388. }
  389. // does a directory with this name exist
  390. if (this->IsKeyword(keyIS_DIRECTORY, *arg) && argP1 != newArgs.end()) {
  391. this->HandlePredicate(cmSystemTools::FileIsDirectory(argP1->c_str()),
  392. reducible, arg, newArgs, argP1, argP2);
  393. }
  394. // does a symlink with this name exist
  395. if (this->IsKeyword(keyIS_SYMLINK, *arg) && argP1 != newArgs.end()) {
  396. this->HandlePredicate(cmSystemTools::FileIsSymlink(argP1->c_str()),
  397. reducible, arg, newArgs, argP1, argP2);
  398. }
  399. // is the given path an absolute path ?
  400. if (this->IsKeyword(keyIS_ABSOLUTE, *arg) && argP1 != newArgs.end()) {
  401. this->HandlePredicate(cmSystemTools::FileIsFullPath(argP1->c_str()),
  402. reducible, arg, newArgs, argP1, argP2);
  403. }
  404. // does a command exist
  405. if (this->IsKeyword(keyCOMMAND, *arg) && argP1 != newArgs.end()) {
  406. cmCommand* command =
  407. this->Makefile.GetState()->GetCommand(argP1->c_str());
  408. this->HandlePredicate(command ? true : false, reducible, arg, newArgs,
  409. argP1, argP2);
  410. }
  411. // does a policy exist
  412. if (this->IsKeyword(keyPOLICY, *arg) && argP1 != newArgs.end()) {
  413. cmPolicies::PolicyID pid;
  414. this->HandlePredicate(cmPolicies::GetPolicyID(argP1->c_str(), pid),
  415. reducible, arg, newArgs, argP1, argP2);
  416. }
  417. // does a target exist
  418. if (this->IsKeyword(keyTARGET, *arg) && argP1 != newArgs.end()) {
  419. this->HandlePredicate(
  420. this->Makefile.FindTargetToUse(argP1->GetValue()) ? true : false,
  421. reducible, arg, newArgs, argP1, argP2);
  422. }
  423. // does a test exist
  424. if (this->Policy64Status != cmPolicies::OLD &&
  425. this->Policy64Status != cmPolicies::WARN) {
  426. if (this->IsKeyword(keyTEST, *arg) && argP1 != newArgs.end()) {
  427. const cmTest* haveTest = this->Makefile.GetTest(argP1->c_str());
  428. this->HandlePredicate(haveTest ? true : false, reducible, arg,
  429. newArgs, argP1, argP2);
  430. }
  431. } else if (this->Policy64Status == cmPolicies::WARN &&
  432. this->IsKeyword(keyTEST, *arg)) {
  433. std::ostringstream e;
  434. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0064) << "\n";
  435. e << "TEST will be interpreted as an operator "
  436. "when the policy is set to NEW. "
  437. "Since the policy is not set the OLD behavior will be used.";
  438. this->Makefile.IssueMessage(cmake::AUTHOR_WARNING, e.str());
  439. }
  440. // is a variable defined
  441. if (this->IsKeyword(keyDEFINED, *arg) && argP1 != newArgs.end()) {
  442. size_t argP1len = argP1->GetValue().size();
  443. bool bdef = false;
  444. if (argP1len > 4 && argP1->GetValue().substr(0, 4) == "ENV{" &&
  445. argP1->GetValue().operator[](argP1len - 1) == '}') {
  446. std::string env = argP1->GetValue().substr(4, argP1len - 5);
  447. bdef = cmSystemTools::GetEnv(env.c_str()) ? true : false;
  448. } else {
  449. bdef = this->Makefile.IsDefinitionSet(argP1->GetValue());
  450. }
  451. this->HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  452. }
  453. ++arg;
  454. }
  455. } while (reducible);
  456. return true;
  457. }
  458. //=========================================================================
  459. // level two handles most binary operations except for AND OR
  460. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  461. std::string& errorString,
  462. cmake::MessageType& status)
  463. {
  464. int reducible;
  465. std::string def_buf;
  466. const char* def;
  467. const char* def2;
  468. do {
  469. reducible = 0;
  470. cmArgumentList::iterator arg = newArgs.begin();
  471. cmArgumentList::iterator argP1;
  472. cmArgumentList::iterator argP2;
  473. while (arg != newArgs.end()) {
  474. argP1 = arg;
  475. this->IncrementArguments(newArgs, argP1, argP2);
  476. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  477. IsKeyword(keyMATCHES, *argP1)) {
  478. def = this->GetVariableOrString(*arg);
  479. if (def != arg->c_str() // yes, we compare the pointer value
  480. && cmHasLiteralPrefix(arg->GetValue(), "CMAKE_MATCH_")) {
  481. // The string to match is owned by our match result variables.
  482. // Move it to our own buffer before clearing them.
  483. def_buf = def;
  484. def = def_buf.c_str();
  485. }
  486. const char* rex = argP2->c_str();
  487. this->Makefile.ClearMatches();
  488. cmsys::RegularExpression regEntry;
  489. if (!regEntry.compile(rex)) {
  490. std::ostringstream error;
  491. error << "Regular expression \"" << rex << "\" cannot compile";
  492. errorString = error.str();
  493. status = cmake::FATAL_ERROR;
  494. return false;
  495. }
  496. if (regEntry.find(def)) {
  497. this->Makefile.StoreMatches(regEntry);
  498. *arg = cmExpandedCommandArgument("1", true);
  499. } else {
  500. *arg = cmExpandedCommandArgument("0", true);
  501. }
  502. newArgs.erase(argP2);
  503. newArgs.erase(argP1);
  504. argP1 = arg;
  505. this->IncrementArguments(newArgs, argP1, argP2);
  506. reducible = 1;
  507. }
  508. if (argP1 != newArgs.end() && this->IsKeyword(keyMATCHES, *arg)) {
  509. *arg = cmExpandedCommandArgument("0", true);
  510. newArgs.erase(argP1);
  511. argP1 = arg;
  512. this->IncrementArguments(newArgs, argP1, argP2);
  513. reducible = 1;
  514. }
  515. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  516. (this->IsKeyword(keyLESS, *argP1) ||
  517. this->IsKeyword(keyGREATER, *argP1) ||
  518. this->IsKeyword(keyEQUAL, *argP1))) {
  519. def = this->GetVariableOrString(*arg);
  520. def2 = this->GetVariableOrString(*argP2);
  521. double lhs;
  522. double rhs;
  523. bool result;
  524. if (sscanf(def, "%lg", &lhs) != 1 || sscanf(def2, "%lg", &rhs) != 1) {
  525. result = false;
  526. } else if (*(argP1) == keyLESS) {
  527. result = (lhs < rhs);
  528. } else if (*(argP1) == keyGREATER) {
  529. result = (lhs > rhs);
  530. } else {
  531. result = (lhs == rhs);
  532. }
  533. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  534. }
  535. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  536. (this->IsKeyword(keySTRLESS, *argP1) ||
  537. this->IsKeyword(keySTREQUAL, *argP1) ||
  538. this->IsKeyword(keySTRGREATER, *argP1))) {
  539. def = this->GetVariableOrString(*arg);
  540. def2 = this->GetVariableOrString(*argP2);
  541. int val = strcmp(def, def2);
  542. bool result;
  543. if (*(argP1) == keySTRLESS) {
  544. result = (val < 0);
  545. } else if (*(argP1) == keySTRGREATER) {
  546. result = (val > 0);
  547. } else // strequal
  548. {
  549. result = (val == 0);
  550. }
  551. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  552. }
  553. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  554. (this->IsKeyword(keyVERSION_LESS, *argP1) ||
  555. this->IsKeyword(keyVERSION_GREATER, *argP1) ||
  556. this->IsKeyword(keyVERSION_EQUAL, *argP1))) {
  557. def = this->GetVariableOrString(*arg);
  558. def2 = this->GetVariableOrString(*argP2);
  559. cmSystemTools::CompareOp op = cmSystemTools::OP_EQUAL;
  560. if (*argP1 == keyVERSION_LESS) {
  561. op = cmSystemTools::OP_LESS;
  562. } else if (*argP1 == keyVERSION_GREATER) {
  563. op = cmSystemTools::OP_GREATER;
  564. }
  565. bool result = cmSystemTools::VersionCompare(op, def, def2);
  566. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  567. }
  568. // is file A newer than file B
  569. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  570. this->IsKeyword(keyIS_NEWER_THAN, *argP1)) {
  571. int fileIsNewer = 0;
  572. bool success = cmSystemTools::FileTimeCompare(
  573. arg->GetValue(), (argP2)->GetValue(), &fileIsNewer);
  574. this->HandleBinaryOp(
  575. (success == false || fileIsNewer == 1 || fileIsNewer == 0),
  576. reducible, arg, newArgs, argP1, argP2);
  577. }
  578. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  579. this->IsKeyword(keyIN_LIST, *argP1)) {
  580. if (this->Policy57Status != cmPolicies::OLD &&
  581. this->Policy57Status != cmPolicies::WARN) {
  582. bool result = false;
  583. def = this->GetVariableOrString(*arg);
  584. def2 = this->Makefile.GetDefinition(argP2->GetValue());
  585. if (def2) {
  586. std::vector<std::string> list;
  587. cmSystemTools::ExpandListArgument(def2, list, true);
  588. result = std::find(list.begin(), list.end(), def) != list.end();
  589. }
  590. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  591. } else if (this->Policy57Status == cmPolicies::WARN) {
  592. std::ostringstream e;
  593. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0057) << "\n";
  594. e << "IN_LIST will be interpreted as an operator "
  595. "when the policy is set to NEW. "
  596. "Since the policy is not set the OLD behavior will be used.";
  597. this->Makefile.IssueMessage(cmake::AUTHOR_WARNING, e.str());
  598. }
  599. }
  600. ++arg;
  601. }
  602. } while (reducible);
  603. return true;
  604. }
  605. //=========================================================================
  606. // level 3 handles NOT
  607. bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs,
  608. std::string& errorString,
  609. cmake::MessageType& status)
  610. {
  611. int reducible;
  612. do {
  613. reducible = 0;
  614. cmArgumentList::iterator arg = newArgs.begin();
  615. cmArgumentList::iterator argP1;
  616. cmArgumentList::iterator argP2;
  617. while (arg != newArgs.end()) {
  618. argP1 = arg;
  619. IncrementArguments(newArgs, argP1, argP2);
  620. if (argP1 != newArgs.end() && IsKeyword(keyNOT, *arg)) {
  621. bool rhs = this->GetBooleanValueWithAutoDereference(
  622. *argP1, errorString, status);
  623. this->HandlePredicate(!rhs, reducible, arg, newArgs, argP1, argP2);
  624. }
  625. ++arg;
  626. }
  627. } while (reducible);
  628. return true;
  629. }
  630. //=========================================================================
  631. // level 4 handles AND OR
  632. bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs,
  633. std::string& errorString,
  634. cmake::MessageType& status)
  635. {
  636. int reducible;
  637. bool lhs;
  638. bool rhs;
  639. do {
  640. reducible = 0;
  641. cmArgumentList::iterator arg = newArgs.begin();
  642. cmArgumentList::iterator argP1;
  643. cmArgumentList::iterator argP2;
  644. while (arg != newArgs.end()) {
  645. argP1 = arg;
  646. IncrementArguments(newArgs, argP1, argP2);
  647. if (argP1 != newArgs.end() && IsKeyword(keyAND, *argP1) &&
  648. argP2 != newArgs.end()) {
  649. lhs =
  650. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  651. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  652. status);
  653. this->HandleBinaryOp((lhs && rhs), reducible, arg, newArgs, argP1,
  654. argP2);
  655. }
  656. if (argP1 != newArgs.end() && this->IsKeyword(keyOR, *argP1) &&
  657. argP2 != newArgs.end()) {
  658. lhs =
  659. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  660. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  661. status);
  662. this->HandleBinaryOp((lhs || rhs), reducible, arg, newArgs, argP1,
  663. argP2);
  664. }
  665. ++arg;
  666. }
  667. } while (reducible);
  668. return true;
  669. }