1
0

cmConditionEvaluator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 <cm/string_view>
  11. #include <cmext/algorithm>
  12. #include "cmsys/RegularExpression.hxx"
  13. #include "cmMakefile.h"
  14. #include "cmMessageType.h"
  15. #include "cmProperty.h"
  16. #include "cmState.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.h"
  19. #include "cmake.h"
  20. class cmTest;
  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. } // anonymous namespace
  56. cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile,
  57. cmListFileBacktrace bt)
  58. : Makefile(makefile)
  59. , Backtrace(std::move(bt))
  60. , Policy12Status(makefile.GetPolicyStatus(cmPolicies::CMP0012))
  61. , Policy54Status(makefile.GetPolicyStatus(cmPolicies::CMP0054))
  62. , Policy57Status(makefile.GetPolicyStatus(cmPolicies::CMP0057))
  63. , Policy64Status(makefile.GetPolicyStatus(cmPolicies::CMP0064))
  64. {
  65. }
  66. //=========================================================================
  67. // order of operations,
  68. // 1. ( ) -- parenthetical groups
  69. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  70. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  71. // 4. NOT
  72. // 5. AND OR
  73. //
  74. // There is an issue on whether the arguments should be values of references,
  75. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  76. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  77. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  78. // take numeric values or variable names. STRLESS and STRGREATER take
  79. // variable names but if the variable name is not found it will use the name
  80. // directly. AND OR take variables or the values 0 or 1.
  81. bool cmConditionEvaluator::IsTrue(
  82. const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
  83. MessageType& status)
  84. {
  85. errorString.clear();
  86. // handle empty invocation
  87. if (args.empty()) {
  88. return false;
  89. }
  90. // store the reduced args in this vector
  91. cmArgumentList newArgs(args.begin(), args.end());
  92. // now loop through the arguments and see if we can reduce any of them
  93. // we do this multiple times. Once for each level of precedence
  94. // parens
  95. if (!this->HandleLevel0(newArgs, errorString, status)) {
  96. return false;
  97. }
  98. // predicates
  99. if (!this->HandleLevel1(newArgs, errorString, status)) {
  100. return false;
  101. }
  102. // binary ops
  103. if (!this->HandleLevel2(newArgs, errorString, status)) {
  104. return false;
  105. }
  106. // NOT
  107. if (!this->HandleLevel3(newArgs, errorString, status)) {
  108. return false;
  109. }
  110. // AND OR
  111. if (!this->HandleLevel4(newArgs, errorString, status)) {
  112. return false;
  113. }
  114. // now at the end there should only be one argument left
  115. if (newArgs.size() != 1) {
  116. errorString = "Unknown arguments specified";
  117. status = MessageType::FATAL_ERROR;
  118. return false;
  119. }
  120. return this->GetBooleanValueWithAutoDereference(newArgs.front(), errorString,
  121. status, true);
  122. }
  123. //=========================================================================
  124. cmProp cmConditionEvaluator::GetDefinitionIfUnquoted(
  125. cmExpandedCommandArgument const& argument) const
  126. {
  127. if ((this->Policy54Status != cmPolicies::WARN &&
  128. this->Policy54Status != cmPolicies::OLD) &&
  129. argument.WasQuoted()) {
  130. return nullptr;
  131. }
  132. cmProp def = this->Makefile.GetDefinition(argument.GetValue());
  133. if (def && argument.WasQuoted() &&
  134. this->Policy54Status == cmPolicies::WARN) {
  135. if (!this->Makefile.HasCMP0054AlreadyBeenReported(this->Backtrace.Top())) {
  136. std::ostringstream e;
  137. e << (cmPolicies::GetPolicyWarning(cmPolicies::CMP0054)) << "\n";
  138. e << "Quoted variables like \"" << argument.GetValue()
  139. << "\" will no longer be dereferenced "
  140. "when the policy is set to NEW. "
  141. "Since the policy is not set the OLD behavior will be used.";
  142. this->Makefile.GetCMakeInstance()->IssueMessage(
  143. MessageType::AUTHOR_WARNING, e.str(), this->Backtrace);
  144. }
  145. }
  146. return def;
  147. }
  148. //=========================================================================
  149. cmProp cmConditionEvaluator::GetVariableOrString(
  150. const cmExpandedCommandArgument& argument) const
  151. {
  152. cmProp def = this->GetDefinitionIfUnquoted(argument);
  153. if (!def) {
  154. def = &argument.GetValue();
  155. }
  156. return def;
  157. }
  158. //=========================================================================
  159. bool cmConditionEvaluator::IsKeyword(cm::string_view keyword,
  160. cmExpandedCommandArgument& argument) const
  161. {
  162. if ((this->Policy54Status != cmPolicies::WARN &&
  163. this->Policy54Status != cmPolicies::OLD) &&
  164. argument.WasQuoted()) {
  165. return false;
  166. }
  167. const bool isKeyword = argument.GetValue() == keyword;
  168. if (isKeyword && argument.WasQuoted() &&
  169. this->Policy54Status == cmPolicies::WARN) {
  170. if (!this->Makefile.HasCMP0054AlreadyBeenReported(this->Backtrace.Top())) {
  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 (cmIsOn(arg.GetValue())) {
  196. return true;
  197. }
  198. if (cmIsOff(arg.GetValue())) {
  199. return false;
  200. }
  201. // Check for numbers.
  202. if (!arg.empty()) {
  203. char* end;
  204. const double d = std::strtod(arg.GetValue().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. cmProp def = this->GetDefinitionIfUnquoted(arg);
  212. return !cmIsOff(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. cmProp def = this->GetDefinitionIfUnquoted(arg);
  228. return !cmIsOff(def);
  229. }
  230. // Old GetVariableOrNumber behavior.
  231. cmProp def = this->GetDefinitionIfUnquoted(arg);
  232. if (!def && atoi(arg.GetValue().c_str())) {
  233. def = &arg.GetValue();
  234. }
  235. return !cmIsOff(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 this->GetBooleanValue(newArg);
  246. }
  247. if (this->Policy12Status == cmPolicies::OLD) {
  248. return this->GetBooleanValueOld(newArg, oneArg);
  249. }
  250. // Check policy only if old and new results differ.
  251. const bool newResult = this->GetBooleanValue(newArg);
  252. const 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. 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, 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. this->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. this->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. unsigned int depth = 1;
  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 = arg;
  350. argP1++;
  351. cm::append(newArgs2, argP1, argClose);
  352. newArgs2.pop_back();
  353. // now recursively invoke IsTrue to handle the values inside the
  354. // parenthetical expression
  355. const bool value = this->IsTrue(newArgs2, errorString, status);
  356. *arg = cmExpandedCommandArgument(ZERO_ONE_XLAT[value], true);
  357. argP1 = arg;
  358. argP1++;
  359. // remove the now evaluated parenthetical expression
  360. newArgs.erase(argP1, argClose);
  361. }
  362. }
  363. } while (reducible);
  364. return true;
  365. }
  366. //=========================================================================
  367. // level one handles most predicates except for NOT
  368. bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
  369. MessageType&)
  370. {
  371. bool reducible;
  372. do {
  373. reducible = false;
  374. for (auto arg = newArgs.begin(), argP1 = arg, argP2 = arg;
  375. arg != newArgs.end(); argP1 = ++arg) {
  376. this->IncrementArguments(newArgs, argP1, argP2);
  377. // does a file exist
  378. if (this->IsKeyword(keyEXISTS, *arg) && argP1 != newArgs.end()) {
  379. this->HandlePredicate(cmSystemTools::FileExists(argP1->GetValue()),
  380. reducible, arg, newArgs, argP1, argP2);
  381. }
  382. // does a directory with this name exist
  383. if (this->IsKeyword(keyIS_DIRECTORY, *arg) && argP1 != newArgs.end()) {
  384. this->HandlePredicate(
  385. cmSystemTools::FileIsDirectory(argP1->GetValue()), reducible, arg,
  386. newArgs, argP1, argP2);
  387. }
  388. // does a symlink with this name exist
  389. if (this->IsKeyword(keyIS_SYMLINK, *arg) && argP1 != newArgs.end()) {
  390. this->HandlePredicate(cmSystemTools::FileIsSymlink(argP1->GetValue()),
  391. reducible, arg, newArgs, argP1, argP2);
  392. }
  393. // is the given path an absolute path ?
  394. if (this->IsKeyword(keyIS_ABSOLUTE, *arg) && argP1 != newArgs.end()) {
  395. this->HandlePredicate(cmSystemTools::FileIsFullPath(argP1->GetValue()),
  396. reducible, arg, newArgs, argP1, argP2);
  397. }
  398. // does a command exist
  399. if (this->IsKeyword(keyCOMMAND, *arg) && argP1 != newArgs.end()) {
  400. cmState::Command command =
  401. this->Makefile.GetState()->GetCommand(argP1->GetValue());
  402. this->HandlePredicate(command != nullptr, reducible, arg, newArgs,
  403. argP1, argP2);
  404. }
  405. // does a policy exist
  406. if (this->IsKeyword(keyPOLICY, *arg) && argP1 != newArgs.end()) {
  407. cmPolicies::PolicyID pid;
  408. this->HandlePredicate(
  409. cmPolicies::GetPolicyID(argP1->GetValue().c_str(), pid), reducible,
  410. arg, newArgs, argP1, argP2);
  411. }
  412. // does a target exist
  413. if (this->IsKeyword(keyTARGET, *arg) && argP1 != newArgs.end()) {
  414. this->HandlePredicate(
  415. this->Makefile.FindTargetToUse(argP1->GetValue()) != nullptr,
  416. reducible, arg, newArgs, argP1, argP2);
  417. }
  418. // does a test exist
  419. if (this->Policy64Status != cmPolicies::OLD &&
  420. this->Policy64Status != cmPolicies::WARN) {
  421. if (this->IsKeyword(keyTEST, *arg) && argP1 != newArgs.end()) {
  422. const cmTest* haveTest = this->Makefile.GetTest(argP1->GetValue());
  423. this->HandlePredicate(haveTest != nullptr, reducible, arg, newArgs,
  424. argP1, argP2);
  425. }
  426. } else if (this->Policy64Status == cmPolicies::WARN &&
  427. this->IsKeyword(keyTEST, *arg)) {
  428. std::ostringstream e;
  429. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0064) << "\n";
  430. e << "TEST will be interpreted as an operator "
  431. "when the policy is set to NEW. "
  432. "Since the policy is not set the OLD behavior will be used.";
  433. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  434. }
  435. // is a variable defined
  436. if (this->IsKeyword(keyDEFINED, *arg) && argP1 != newArgs.end()) {
  437. const size_t argP1len = argP1->GetValue().size();
  438. bool bdef = false;
  439. if (argP1len > 4 && cmHasLiteralPrefix(argP1->GetValue(), "ENV{") &&
  440. argP1->GetValue().operator[](argP1len - 1) == '}') {
  441. std::string env = argP1->GetValue().substr(4, argP1len - 5);
  442. bdef = cmSystemTools::HasEnv(env);
  443. } else if (argP1len > 6 &&
  444. cmHasLiteralPrefix(argP1->GetValue(), "CACHE{") &&
  445. argP1->GetValue().operator[](argP1len - 1) == '}') {
  446. std::string cache = argP1->GetValue().substr(6, argP1len - 7);
  447. bdef =
  448. this->Makefile.GetState()->GetCacheEntryValue(cache) != nullptr;
  449. } else {
  450. bdef = this->Makefile.IsDefinitionSet(argP1->GetValue());
  451. }
  452. this->HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  453. }
  454. }
  455. } while (reducible);
  456. return true;
  457. }
  458. //=========================================================================
  459. // level two handles most binary operations except for AND OR
  460. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  461. std::string& errorString,
  462. MessageType& status)
  463. {
  464. bool reducible;
  465. std::string def_buf;
  466. cmProp def;
  467. cmProp def2;
  468. do {
  469. reducible = false;
  470. for (auto arg = newArgs.begin(), argP1 = arg, argP2 = arg;
  471. arg != newArgs.end(); argP1 = ++arg) {
  472. this->IncrementArguments(newArgs, argP1, argP2);
  473. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  474. this->IsKeyword(keyMATCHES, *argP1)) {
  475. def = this->GetDefinitionIfUnquoted(*arg);
  476. if (!def) {
  477. def = &arg->GetValue();
  478. } else if (cmHasLiteralPrefix(arg->GetValue(), "CMAKE_MATCH_")) {
  479. // The string to match is owned by our match result variables.
  480. // Move it to our own buffer before clearing them.
  481. def_buf = *def;
  482. def = &def_buf;
  483. }
  484. const std::string& rex = argP2->GetValue();
  485. this->Makefile.ClearMatches();
  486. cmsys::RegularExpression regEntry;
  487. if (!regEntry.compile(rex)) {
  488. std::ostringstream error;
  489. error << "Regular expression \"" << rex << "\" cannot compile";
  490. errorString = error.str();
  491. status = MessageType::FATAL_ERROR;
  492. return false;
  493. }
  494. if (regEntry.find(*def)) {
  495. this->Makefile.StoreMatches(regEntry);
  496. *arg = cmExpandedCommandArgument("1", true);
  497. } else {
  498. *arg = cmExpandedCommandArgument("0", true);
  499. }
  500. newArgs.erase(argP2);
  501. newArgs.erase(argP1);
  502. argP1 = arg;
  503. this->IncrementArguments(newArgs, argP1, argP2);
  504. reducible = true;
  505. }
  506. if (argP1 != newArgs.end() && this->IsKeyword(keyMATCHES, *arg)) {
  507. *arg = cmExpandedCommandArgument("0", true);
  508. newArgs.erase(argP1);
  509. argP1 = arg;
  510. this->IncrementArguments(newArgs, argP1, argP2);
  511. reducible = true;
  512. }
  513. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  514. (this->IsKeyword(keyLESS, *argP1) ||
  515. this->IsKeyword(keyLESS_EQUAL, *argP1) ||
  516. this->IsKeyword(keyGREATER, *argP1) ||
  517. this->IsKeyword(keyGREATER_EQUAL, *argP1) ||
  518. this->IsKeyword(keyEQUAL, *argP1))) {
  519. def = this->GetVariableOrString(*arg);
  520. def2 = this->GetVariableOrString(*argP2);
  521. double lhs;
  522. double rhs;
  523. bool result;
  524. if (std::sscanf(def->c_str(), "%lg", &lhs) != 1 ||
  525. std::sscanf(def2->c_str(), "%lg", &rhs) != 1) {
  526. result = false;
  527. } else if (argP1->GetValue() == keyLESS) {
  528. result = (lhs < rhs);
  529. } else if (argP1->GetValue() == keyLESS_EQUAL) {
  530. result = (lhs <= rhs);
  531. } else if (argP1->GetValue() == keyGREATER) {
  532. result = (lhs > rhs);
  533. } else if (argP1->GetValue() == keyGREATER_EQUAL) {
  534. result = (lhs >= rhs);
  535. } else {
  536. result = (lhs == rhs);
  537. }
  538. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  539. }
  540. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  541. (this->IsKeyword(keySTRLESS, *argP1) ||
  542. this->IsKeyword(keySTRLESS_EQUAL, *argP1) ||
  543. this->IsKeyword(keySTRGREATER, *argP1) ||
  544. this->IsKeyword(keySTRGREATER_EQUAL, *argP1) ||
  545. this->IsKeyword(keySTREQUAL, *argP1))) {
  546. def = this->GetVariableOrString(*arg);
  547. def2 = this->GetVariableOrString(*argP2);
  548. const int val = (*def).compare(*def2);
  549. bool result;
  550. if (argP1->GetValue() == keySTRLESS) {
  551. result = (val < 0);
  552. } else if (argP1->GetValue() == keySTRLESS_EQUAL) {
  553. result = (val <= 0);
  554. } else if (argP1->GetValue() == keySTRGREATER) {
  555. result = (val > 0);
  556. } else if (argP1->GetValue() == keySTRGREATER_EQUAL) {
  557. result = (val >= 0);
  558. } else // strequal
  559. {
  560. result = (val == 0);
  561. }
  562. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  563. }
  564. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  565. (this->IsKeyword(keyVERSION_LESS, *argP1) ||
  566. this->IsKeyword(keyVERSION_LESS_EQUAL, *argP1) ||
  567. this->IsKeyword(keyVERSION_GREATER, *argP1) ||
  568. this->IsKeyword(keyVERSION_GREATER_EQUAL, *argP1) ||
  569. this->IsKeyword(keyVERSION_EQUAL, *argP1))) {
  570. def = this->GetVariableOrString(*arg);
  571. def2 = this->GetVariableOrString(*argP2);
  572. cmSystemTools::CompareOp op;
  573. if (argP1->GetValue() == keyVERSION_LESS) {
  574. op = cmSystemTools::OP_LESS;
  575. } else if (argP1->GetValue() == keyVERSION_LESS_EQUAL) {
  576. op = cmSystemTools::OP_LESS_EQUAL;
  577. } else if (argP1->GetValue() == keyVERSION_GREATER) {
  578. op = cmSystemTools::OP_GREATER;
  579. } else if (argP1->GetValue() == keyVERSION_GREATER_EQUAL) {
  580. op = cmSystemTools::OP_GREATER_EQUAL;
  581. } else { // version_equal
  582. op = cmSystemTools::OP_EQUAL;
  583. }
  584. const bool result =
  585. cmSystemTools::VersionCompare(op, def->c_str(), def2->c_str());
  586. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  587. }
  588. // is file A newer than file B
  589. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  590. this->IsKeyword(keyIS_NEWER_THAN, *argP1)) {
  591. int fileIsNewer = 0;
  592. cmsys::Status ftcStatus = cmSystemTools::FileTimeCompare(
  593. arg->GetValue(), (argP2)->GetValue(), &fileIsNewer);
  594. this->HandleBinaryOp(
  595. (!ftcStatus || fileIsNewer == 1 || fileIsNewer == 0), reducible, arg,
  596. newArgs, argP1, argP2);
  597. }
  598. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  599. this->IsKeyword(keyIN_LIST, *argP1)) {
  600. if (this->Policy57Status != cmPolicies::OLD &&
  601. this->Policy57Status != cmPolicies::WARN) {
  602. bool result = false;
  603. def = this->GetVariableOrString(*arg);
  604. def2 = this->Makefile.GetDefinition(argP2->GetValue());
  605. if (def2) {
  606. std::vector<std::string> list = cmExpandedList(*def2, true);
  607. result = cm::contains(list, *def);
  608. }
  609. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  610. } else if (this->Policy57Status == cmPolicies::WARN) {
  611. std::ostringstream e;
  612. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0057) << "\n";
  613. e << "IN_LIST will be interpreted as an operator "
  614. "when the policy is set to NEW. "
  615. "Since the policy is not set the OLD behavior will be used.";
  616. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  617. }
  618. }
  619. }
  620. } while (reducible);
  621. return true;
  622. }
  623. //=========================================================================
  624. // level 3 handles NOT
  625. bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs,
  626. std::string& errorString,
  627. MessageType& status)
  628. {
  629. bool reducible;
  630. do {
  631. reducible = false;
  632. for (auto arg = newArgs.begin(), argP1 = arg, argP2 = arg;
  633. arg != newArgs.end(); argP1 = ++arg) {
  634. this->IncrementArguments(newArgs, argP1, argP2);
  635. if (argP1 != newArgs.end() && this->IsKeyword(keyNOT, *arg)) {
  636. bool rhs = this->GetBooleanValueWithAutoDereference(
  637. *argP1, errorString, status);
  638. this->HandlePredicate(!rhs, reducible, arg, newArgs, argP1, argP2);
  639. }
  640. }
  641. } while (reducible);
  642. return true;
  643. }
  644. //=========================================================================
  645. // level 4 handles AND OR
  646. bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs,
  647. std::string& errorString,
  648. MessageType& status)
  649. {
  650. bool reducible;
  651. bool lhs;
  652. bool rhs;
  653. do {
  654. reducible = false;
  655. for (auto arg = newArgs.begin(), argP1 = arg, argP2 = arg;
  656. arg != newArgs.end(); argP1 = ++arg) {
  657. this->IncrementArguments(newArgs, argP1, argP2);
  658. if (argP1 != newArgs.end() && this->IsKeyword(keyAND, *argP1) &&
  659. argP2 != newArgs.end()) {
  660. lhs =
  661. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  662. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  663. status);
  664. this->HandleBinaryOp((lhs && rhs), reducible, arg, newArgs, argP1,
  665. argP2);
  666. }
  667. if (argP1 != newArgs.end() && this->IsKeyword(keyOR, *argP1) &&
  668. argP2 != newArgs.end()) {
  669. lhs =
  670. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  671. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  672. status);
  673. this->HandleBinaryOp((lhs || rhs), reducible, arg, newArgs, argP1,
  674. argP2);
  675. }
  676. }
  677. } while (reducible);
  678. return true;
  679. }