cmConditionEvaluator.cxx 27 KB

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