cmConditionEvaluator.cxx 26 KB

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