cmConditionEvaluator.cxx 26 KB

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