cmConditionEvaluator.cxx 26 KB

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