cmConditionEvaluator.cxx 27 KB

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