cmConditionEvaluator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 <list>
  10. #include <sstream>
  11. #include <utility>
  12. #include <cm/string_view>
  13. #include <cmext/algorithm>
  14. #include "cmsys/RegularExpression.hxx"
  15. #include "cmCMakePath.h"
  16. #include "cmExpandedCommandArgument.h"
  17. #include "cmList.h"
  18. #include "cmMakefile.h"
  19. #include "cmMessageType.h"
  20. #include "cmState.h"
  21. #include "cmStringAlgorithms.h"
  22. #include "cmSystemTools.h"
  23. #include "cmValue.h"
  24. namespace {
  25. auto const keyAND = "AND"_s;
  26. auto const keyCOMMAND = "COMMAND"_s;
  27. auto const keyDEFINED = "DEFINED"_s;
  28. auto const keyEQUAL = "EQUAL"_s;
  29. auto const keyEXISTS = "EXISTS"_s;
  30. auto const keyIS_READABLE = "IS_READABLE"_s;
  31. auto const keyIS_WRITABLE = "IS_WRITABLE"_s;
  32. auto const keyIS_EXECUTABLE = "IS_EXECUTABLE"_s;
  33. auto const keyGREATER = "GREATER"_s;
  34. auto const keyGREATER_EQUAL = "GREATER_EQUAL"_s;
  35. auto const keyIN_LIST = "IN_LIST"_s;
  36. auto const keyIS_ABSOLUTE = "IS_ABSOLUTE"_s;
  37. auto const keyIS_DIRECTORY = "IS_DIRECTORY"_s;
  38. auto const keyIS_NEWER_THAN = "IS_NEWER_THAN"_s;
  39. auto const keyIS_SYMLINK = "IS_SYMLINK"_s;
  40. auto const keyLESS = "LESS"_s;
  41. auto const keyLESS_EQUAL = "LESS_EQUAL"_s;
  42. auto const keyMATCHES = "MATCHES"_s;
  43. auto const keyNOT = "NOT"_s;
  44. auto const keyOR = "OR"_s;
  45. auto const keyParenL = "("_s;
  46. auto const keyParenR = ")"_s;
  47. auto const keyPOLICY = "POLICY"_s;
  48. auto const keySTREQUAL = "STREQUAL"_s;
  49. auto const keySTRGREATER = "STRGREATER"_s;
  50. auto const keySTRGREATER_EQUAL = "STRGREATER_EQUAL"_s;
  51. auto const keySTRLESS = "STRLESS"_s;
  52. auto const keySTRLESS_EQUAL = "STRLESS_EQUAL"_s;
  53. auto const keyTARGET = "TARGET"_s;
  54. auto const keyTEST = "TEST"_s;
  55. auto const keyVERSION_EQUAL = "VERSION_EQUAL"_s;
  56. auto const keyVERSION_GREATER = "VERSION_GREATER"_s;
  57. auto const keyVERSION_GREATER_EQUAL = "VERSION_GREATER_EQUAL"_s;
  58. auto const keyVERSION_LESS = "VERSION_LESS"_s;
  59. auto const keyVERSION_LESS_EQUAL = "VERSION_LESS_EQUAL"_s;
  60. auto const keyPATH_EQUAL = "PATH_EQUAL"_s;
  61. cmSystemTools::CompareOp const MATCH2CMPOP[5] = {
  62. cmSystemTools::OP_LESS, cmSystemTools::OP_LESS_EQUAL,
  63. cmSystemTools::OP_GREATER, cmSystemTools::OP_GREATER_EQUAL,
  64. cmSystemTools::OP_EQUAL
  65. };
  66. // Run-Time to Compile-Time template selector
  67. template <template <typename> class Comp, template <typename> class... Ops>
  68. struct cmRt2CtSelector
  69. {
  70. template <typename T>
  71. static bool eval(int r, T lhs, T rhs)
  72. {
  73. switch (r) {
  74. case 0:
  75. return false;
  76. case 1:
  77. return Comp<T>()(lhs, rhs);
  78. default:
  79. return cmRt2CtSelector<Ops...>::eval(r - 1, lhs, rhs);
  80. }
  81. }
  82. };
  83. template <template <typename> class Comp>
  84. struct cmRt2CtSelector<Comp>
  85. {
  86. template <typename T>
  87. static bool eval(int r, T lhs, T rhs)
  88. {
  89. return r == 1 && Comp<T>()(lhs, rhs);
  90. }
  91. };
  92. std::string bool2string(bool const value)
  93. {
  94. return std::string(static_cast<std::size_t>(1),
  95. static_cast<char>('0' + static_cast<int>(value)));
  96. }
  97. bool looksLikeSpecialVariable(const std::string& var,
  98. cm::static_string_view prefix,
  99. const std::size_t varNameLen)
  100. {
  101. // NOTE Expecting a variable name at least 1 char length:
  102. // <prefix> + `{` + <varname> + `}`
  103. return ((prefix.size() + 3) <= varNameLen) &&
  104. cmHasPrefix(var, cmStrCat(prefix, '{')) && var[varNameLen - 1] == '}';
  105. }
  106. } // anonymous namespace
  107. #if defined(__SUNPRO_CC)
  108. # define CM_INHERIT_CTOR(Class, Base, Tpl) \
  109. template <typename... Args> \
  110. Class(Args&&... args) \
  111. : Base Tpl(std::forward<Args>(args)...) \
  112. { \
  113. }
  114. #else
  115. # define CM_INHERIT_CTOR(Class, Base, Tpl) using Base Tpl ::Base
  116. #endif
  117. // BEGIN cmConditionEvaluator::cmArgumentList
  118. class cmConditionEvaluator::cmArgumentList
  119. : public std::list<cmExpandedCommandArgument>
  120. {
  121. using base_t = std::list<cmExpandedCommandArgument>;
  122. public:
  123. CM_INHERIT_CTOR(cmArgumentList, list, <cmExpandedCommandArgument>);
  124. class CurrentAndNextIter
  125. {
  126. friend class cmConditionEvaluator::cmArgumentList;
  127. public:
  128. base_t::iterator current;
  129. base_t::iterator next;
  130. CurrentAndNextIter advance(base_t& args)
  131. {
  132. this->current = std::next(this->current);
  133. this->next =
  134. std::next(this->current,
  135. static_cast<difference_type>(this->current != args.end()));
  136. return *this;
  137. }
  138. private:
  139. CurrentAndNextIter(base_t& args)
  140. : current(args.begin())
  141. , next(
  142. std::next(this->current,
  143. static_cast<difference_type>(this->current != args.end())))
  144. {
  145. }
  146. };
  147. class CurrentAndTwoMoreIter
  148. {
  149. friend class cmConditionEvaluator::cmArgumentList;
  150. public:
  151. base_t::iterator current;
  152. base_t::iterator next;
  153. base_t::iterator nextnext;
  154. CurrentAndTwoMoreIter advance(base_t& args)
  155. {
  156. this->current = std::next(this->current);
  157. this->next =
  158. std::next(this->current,
  159. static_cast<difference_type>(this->current != args.end()));
  160. this->nextnext = std::next(
  161. this->next, static_cast<difference_type>(this->next != args.end()));
  162. return *this;
  163. }
  164. private:
  165. CurrentAndTwoMoreIter(base_t& args)
  166. : current(args.begin())
  167. , next(
  168. std::next(this->current,
  169. static_cast<difference_type>(this->current != args.end())))
  170. , nextnext(std::next(
  171. this->next, static_cast<difference_type>(this->next != args.end())))
  172. {
  173. }
  174. };
  175. CurrentAndNextIter make2ArgsIterator() { return *this; }
  176. CurrentAndTwoMoreIter make3ArgsIterator() { return *this; }
  177. template <typename Iter>
  178. void ReduceOneArg(const bool value, Iter args)
  179. {
  180. *args.current = cmExpandedCommandArgument(bool2string(value), true);
  181. this->erase(args.next);
  182. }
  183. void ReduceTwoArgs(const bool value, CurrentAndTwoMoreIter args)
  184. {
  185. *args.current = cmExpandedCommandArgument(bool2string(value), true);
  186. this->erase(args.nextnext);
  187. this->erase(args.next);
  188. }
  189. };
  190. // END cmConditionEvaluator::cmArgumentList
  191. cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile,
  192. cmListFileBacktrace bt)
  193. : Makefile(makefile)
  194. , Backtrace(std::move(bt))
  195. , Policy139Status(makefile.GetPolicyStatus(cmPolicies::CMP0139))
  196. {
  197. }
  198. //=========================================================================
  199. // order of operations,
  200. // 1. ( ) -- parenthetical groups
  201. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  202. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  203. // 4. NOT
  204. // 5. AND OR
  205. //
  206. // There is an issue on whether the arguments should be values of references,
  207. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  208. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  209. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  210. // take numeric values or variable names. STRLESS and STRGREATER take
  211. // variable names but if the variable name is not found it will use the name
  212. // directly. AND OR take variables or the values 0 or 1.
  213. bool cmConditionEvaluator::IsTrue(
  214. const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
  215. MessageType& status)
  216. {
  217. errorString.clear();
  218. // handle empty invocation
  219. if (args.empty()) {
  220. return false;
  221. }
  222. // store the reduced args in this vector
  223. cmArgumentList newArgs(args.begin(), args.end());
  224. // now loop through the arguments and see if we can reduce any of them
  225. // we do this multiple times. Once for each level of precedence
  226. // parens
  227. using handlerFn_t = bool (cmConditionEvaluator::*)(
  228. cmArgumentList&, std::string&, MessageType&);
  229. const std::array<handlerFn_t, 5> handlers = { {
  230. &cmConditionEvaluator::HandleLevel0, // parenthesis
  231. &cmConditionEvaluator::HandleLevel1, // predicates
  232. &cmConditionEvaluator::HandleLevel2, // binary ops
  233. &cmConditionEvaluator::HandleLevel3, // NOT
  234. &cmConditionEvaluator::HandleLevel4 // AND OR
  235. } };
  236. for (auto fn : handlers) {
  237. // Call the reducer 'till there is anything to reduce...
  238. // (i.e., if after an iteration the size becomes smaller)
  239. auto levelResult = true;
  240. for (auto beginSize = newArgs.size();
  241. (levelResult = (this->*fn)(newArgs, errorString, status)) &&
  242. newArgs.size() < beginSize;
  243. beginSize = newArgs.size()) {
  244. }
  245. if (!levelResult) {
  246. // NOTE `errorString` supposed to be set already
  247. return false;
  248. }
  249. }
  250. // now at the end there should only be one argument left
  251. if (newArgs.size() != 1) {
  252. errorString = "Unknown arguments specified";
  253. status = MessageType::FATAL_ERROR;
  254. return false;
  255. }
  256. return this->GetBooleanValue(newArgs.front());
  257. }
  258. //=========================================================================
  259. cmValue cmConditionEvaluator::GetDefinitionIfUnquoted(
  260. cmExpandedCommandArgument const& argument) const
  261. {
  262. if (argument.WasQuoted()) {
  263. return nullptr;
  264. }
  265. return this->Makefile.GetDefinition(argument.GetValue());
  266. }
  267. //=========================================================================
  268. cmValue cmConditionEvaluator::GetVariableOrString(
  269. const cmExpandedCommandArgument& argument) const
  270. {
  271. cmValue def = this->GetDefinitionIfUnquoted(argument);
  272. if (!def) {
  273. def = cmValue(argument.GetValue());
  274. }
  275. return def;
  276. }
  277. //=========================================================================
  278. bool cmConditionEvaluator::IsKeyword(
  279. cm::static_string_view keyword,
  280. const cmExpandedCommandArgument& argument) const
  281. {
  282. if (argument.WasQuoted()) {
  283. return false;
  284. }
  285. return argument.GetValue() == keyword;
  286. }
  287. //=========================================================================
  288. bool cmConditionEvaluator::GetBooleanValue(
  289. cmExpandedCommandArgument& arg) const
  290. {
  291. // Check basic and named constants.
  292. if (cmIsOn(arg.GetValue())) {
  293. return true;
  294. }
  295. if (cmIsOff(arg.GetValue())) {
  296. return false;
  297. }
  298. // Check for numbers.
  299. if (!arg.empty()) {
  300. char* end;
  301. const double d = std::strtod(arg.GetValue().c_str(), &end);
  302. if (*end == '\0') {
  303. // The whole string is a number. Use C conversion to bool.
  304. return static_cast<bool>(d);
  305. }
  306. }
  307. // Check definition.
  308. cmValue def = this->GetDefinitionIfUnquoted(arg);
  309. return !def.IsOff();
  310. }
  311. template <int N>
  312. inline int cmConditionEvaluator::matchKeysImpl(
  313. const cmExpandedCommandArgument&)
  314. {
  315. // Zero means "not found"
  316. return 0;
  317. }
  318. template <int N, typename T, typename... Keys>
  319. inline int cmConditionEvaluator::matchKeysImpl(
  320. const cmExpandedCommandArgument& arg, T current, Keys... key)
  321. {
  322. if (this->IsKeyword(current, arg)) {
  323. // Stop searching as soon as smth has found
  324. return N;
  325. }
  326. return matchKeysImpl<N + 1>(arg, key...);
  327. }
  328. template <typename... Keys>
  329. inline int cmConditionEvaluator::matchKeys(
  330. const cmExpandedCommandArgument& arg, Keys... key)
  331. {
  332. // Get index of the matched key (1-based)
  333. return matchKeysImpl<1>(arg, key...);
  334. }
  335. //=========================================================================
  336. // level 0 processes parenthetical expressions
  337. bool cmConditionEvaluator::HandleLevel0(cmArgumentList& newArgs,
  338. std::string& errorString,
  339. MessageType& status)
  340. {
  341. for (auto arg = newArgs.begin(); arg != newArgs.end(); ++arg) {
  342. if (this->IsKeyword(keyParenL, *arg)) {
  343. // search for the closing paren for this opening one
  344. auto depth = 1;
  345. auto argClose = std::next(arg);
  346. for (; argClose != newArgs.end() && depth; ++argClose) {
  347. depth += int(this->IsKeyword(keyParenL, *argClose)) -
  348. int(this->IsKeyword(keyParenR, *argClose));
  349. }
  350. if (depth) {
  351. errorString = "mismatched parenthesis in condition";
  352. status = MessageType::FATAL_ERROR;
  353. return false;
  354. }
  355. // store the reduced args in this vector
  356. auto argOpen = std::next(arg);
  357. const std::vector<cmExpandedCommandArgument> subExpr(
  358. argOpen, std::prev(argClose));
  359. // now recursively invoke IsTrue to handle the values inside the
  360. // parenthetical expression
  361. const auto value = this->IsTrue(subExpr, errorString, status);
  362. *arg = cmExpandedCommandArgument(bool2string(value), true);
  363. argOpen = std::next(arg);
  364. // remove the now evaluated parenthetical expression
  365. newArgs.erase(argOpen, argClose);
  366. }
  367. }
  368. return true;
  369. }
  370. //=========================================================================
  371. // level one handles most predicates except for NOT
  372. bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
  373. MessageType&)
  374. {
  375. for (auto args = newArgs.make2ArgsIterator(); args.current != newArgs.end();
  376. args.advance(newArgs)) {
  377. // NOTE Fail fast: All the predicates below require the next arg to be
  378. // valid
  379. if (args.next == newArgs.end()) {
  380. continue;
  381. }
  382. // does a file exist
  383. if (this->IsKeyword(keyEXISTS, *args.current)) {
  384. newArgs.ReduceOneArg(cmSystemTools::FileExists(args.next->GetValue()),
  385. args);
  386. }
  387. // check if a file is readable
  388. else if (this->IsKeyword(keyIS_READABLE, *args.current)) {
  389. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  390. args.next->GetValue(), cmsys::TEST_FILE_READ),
  391. args);
  392. }
  393. // check if a file is writable
  394. else if (this->IsKeyword(keyIS_WRITABLE, *args.current)) {
  395. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  396. args.next->GetValue(), cmsys::TEST_FILE_WRITE),
  397. args);
  398. }
  399. // check if a file is executable
  400. else if (this->IsKeyword(keyIS_EXECUTABLE, *args.current)) {
  401. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  402. args.next->GetValue(), cmsys::TEST_FILE_EXECUTE),
  403. args);
  404. }
  405. // does a directory with this name exist
  406. else if (this->IsKeyword(keyIS_DIRECTORY, *args.current)) {
  407. newArgs.ReduceOneArg(
  408. cmSystemTools::FileIsDirectory(args.next->GetValue()), args);
  409. }
  410. // does a symlink with this name exist
  411. else if (this->IsKeyword(keyIS_SYMLINK, *args.current)) {
  412. newArgs.ReduceOneArg(cmSystemTools::FileIsSymlink(args.next->GetValue()),
  413. args);
  414. }
  415. // is the given path an absolute path ?
  416. else if (this->IsKeyword(keyIS_ABSOLUTE, *args.current)) {
  417. newArgs.ReduceOneArg(
  418. cmSystemTools::FileIsFullPath(args.next->GetValue()), args);
  419. }
  420. // does a command exist
  421. else if (this->IsKeyword(keyCOMMAND, *args.current)) {
  422. newArgs.ReduceOneArg(
  423. static_cast<bool>(
  424. this->Makefile.GetState()->GetCommand(args.next->GetValue())),
  425. args);
  426. }
  427. // does a policy exist
  428. else if (this->IsKeyword(keyPOLICY, *args.current)) {
  429. cmPolicies::PolicyID pid;
  430. newArgs.ReduceOneArg(
  431. cmPolicies::GetPolicyID(args.next->GetValue().c_str(), pid), args);
  432. }
  433. // does a target exist
  434. else if (this->IsKeyword(keyTARGET, *args.current)) {
  435. newArgs.ReduceOneArg(static_cast<bool>(this->Makefile.FindTargetToUse(
  436. args.next->GetValue())),
  437. args);
  438. }
  439. // is a variable defined
  440. else if (this->IsKeyword(keyDEFINED, *args.current)) {
  441. const auto& var = args.next->GetValue();
  442. const auto varNameLen = var.size();
  443. auto result = false;
  444. if (looksLikeSpecialVariable(var, "ENV"_s, varNameLen)) {
  445. const auto env = args.next->GetValue().substr(4, varNameLen - 5);
  446. result = cmSystemTools::HasEnv(env);
  447. }
  448. else if (looksLikeSpecialVariable(var, "CACHE"_s, varNameLen)) {
  449. const auto cache = args.next->GetValue().substr(6, varNameLen - 7);
  450. result = static_cast<bool>(
  451. this->Makefile.GetState()->GetCacheEntryValue(cache));
  452. }
  453. else {
  454. result = this->Makefile.IsDefinitionSet(args.next->GetValue());
  455. }
  456. newArgs.ReduceOneArg(result, args);
  457. }
  458. // does a test exist
  459. else if (this->IsKeyword(keyTEST, *args.current)) {
  460. newArgs.ReduceOneArg(
  461. static_cast<bool>(this->Makefile.GetTest(args.next->GetValue())),
  462. args);
  463. }
  464. }
  465. return true;
  466. }
  467. //=========================================================================
  468. // level two handles most binary operations except for AND OR
  469. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  470. std::string& errorString,
  471. MessageType& status)
  472. {
  473. for (auto args = newArgs.make3ArgsIterator(); args.current != newArgs.end();
  474. args.advance(newArgs)) {
  475. int matchNo;
  476. // NOTE Handle special case `if(... BLAH_BLAH MATCHES)`
  477. // (i.e., w/o regex to match which is possibly result of
  478. // variable expansion to an empty string)
  479. if (args.next != newArgs.end() &&
  480. this->IsKeyword(keyMATCHES, *args.current)) {
  481. newArgs.ReduceOneArg(false, args);
  482. }
  483. // NOTE Fail fast: All the binary ops below require 2 arguments.
  484. else if (args.next == newArgs.end() || args.nextnext == newArgs.end()) {
  485. continue;
  486. }
  487. else if (this->IsKeyword(keyMATCHES, *args.next)) {
  488. cmValue def = this->GetDefinitionIfUnquoted(*args.current);
  489. std::string def_buf;
  490. if (!def) {
  491. def = cmValue(args.current->GetValue());
  492. } else if (cmHasLiteralPrefix(args.current->GetValue(),
  493. "CMAKE_MATCH_")) {
  494. // The string to match is owned by our match result variables.
  495. // Move it to our own buffer before clearing them.
  496. def_buf = *def;
  497. def = cmValue(def_buf);
  498. }
  499. this->Makefile.ClearMatches();
  500. const auto& rex = args.nextnext->GetValue();
  501. cmsys::RegularExpression regEntry;
  502. if (!regEntry.compile(rex)) {
  503. std::ostringstream error;
  504. error << "Regular expression \"" << rex << "\" cannot compile";
  505. errorString = error.str();
  506. status = MessageType::FATAL_ERROR;
  507. return false;
  508. }
  509. const auto match = regEntry.find(*def);
  510. if (match) {
  511. this->Makefile.StoreMatches(regEntry);
  512. }
  513. newArgs.ReduceTwoArgs(match, args);
  514. }
  515. else if ((matchNo =
  516. this->matchKeys(*args.next, keyLESS, keyLESS_EQUAL, keyGREATER,
  517. keyGREATER_EQUAL, keyEQUAL))) {
  518. cmValue ldef = this->GetVariableOrString(*args.current);
  519. cmValue rdef = this->GetVariableOrString(*args.nextnext);
  520. double lhs;
  521. double rhs;
  522. auto parseDoubles = [&]() {
  523. return std::sscanf(ldef->c_str(), "%lg", &lhs) == 1 &&
  524. std::sscanf(rdef->c_str(), "%lg", &rhs) == 1;
  525. };
  526. // clang-format off
  527. const auto result = parseDoubles() &&
  528. cmRt2CtSelector<
  529. std::less, std::less_equal,
  530. std::greater, std::greater_equal,
  531. std::equal_to
  532. >::eval(matchNo, lhs, rhs);
  533. // clang-format on
  534. newArgs.ReduceTwoArgs(result, args);
  535. }
  536. else if ((matchNo = this->matchKeys(*args.next, keySTRLESS,
  537. keySTRLESS_EQUAL, keySTRGREATER,
  538. keySTRGREATER_EQUAL, keySTREQUAL))) {
  539. const cmValue lhs = this->GetVariableOrString(*args.current);
  540. const cmValue rhs = this->GetVariableOrString(*args.nextnext);
  541. const auto val = (*lhs).compare(*rhs);
  542. // clang-format off
  543. const auto result = cmRt2CtSelector<
  544. std::less, std::less_equal,
  545. std::greater, std::greater_equal,
  546. std::equal_to
  547. >::eval(matchNo, val, 0);
  548. // clang-format on
  549. newArgs.ReduceTwoArgs(result, args);
  550. }
  551. else if ((matchNo =
  552. this->matchKeys(*args.next, keyVERSION_LESS,
  553. keyVERSION_LESS_EQUAL, keyVERSION_GREATER,
  554. keyVERSION_GREATER_EQUAL, keyVERSION_EQUAL))) {
  555. const auto op = MATCH2CMPOP[matchNo - 1];
  556. const cmValue lhs = this->GetVariableOrString(*args.current);
  557. const cmValue rhs = this->GetVariableOrString(*args.nextnext);
  558. const auto result = cmSystemTools::VersionCompare(op, lhs, rhs);
  559. newArgs.ReduceTwoArgs(result, args);
  560. }
  561. // is file A newer than file B
  562. else if (this->IsKeyword(keyIS_NEWER_THAN, *args.next)) {
  563. auto fileIsNewer = 0;
  564. cmsys::Status ftcStatus = cmSystemTools::FileTimeCompare(
  565. args.current->GetValue(), args.nextnext->GetValue(), &fileIsNewer);
  566. newArgs.ReduceTwoArgs(
  567. (!ftcStatus || fileIsNewer == 1 || fileIsNewer == 0), args);
  568. }
  569. else if (this->IsKeyword(keyIN_LIST, *args.next)) {
  570. cmValue lhs = this->GetVariableOrString(*args.current);
  571. cmValue rhs = this->Makefile.GetDefinition(args.nextnext->GetValue());
  572. newArgs.ReduceTwoArgs(
  573. rhs && cm::contains(cmList{ *rhs, cmList::EmptyElements::Yes }, *lhs),
  574. args);
  575. }
  576. else if (this->IsKeyword(keyPATH_EQUAL, *args.next)) {
  577. if (this->Policy139Status != cmPolicies::OLD &&
  578. this->Policy139Status != cmPolicies::WARN) {
  579. cmValue lhs = this->GetVariableOrString(*args.current);
  580. cmValue rhs = this->GetVariableOrString(*args.nextnext);
  581. const auto result = cmCMakePath{ *lhs } == cmCMakePath{ *rhs };
  582. newArgs.ReduceTwoArgs(result, args);
  583. }
  584. else if (this->Policy139Status == cmPolicies::WARN) {
  585. std::ostringstream e;
  586. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0139)
  587. << "\n"
  588. "PATH_EQUAL will be interpreted as an operator "
  589. "when the policy is set to NEW. "
  590. "Since the policy is not set the OLD behavior will be used.";
  591. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  592. }
  593. }
  594. }
  595. return true;
  596. }
  597. //=========================================================================
  598. // level 3 handles NOT
  599. bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs, std::string&,
  600. MessageType&)
  601. {
  602. for (auto args = newArgs.make2ArgsIterator(); args.next != newArgs.end();
  603. args.advance(newArgs)) {
  604. if (this->IsKeyword(keyNOT, *args.current)) {
  605. const auto rhs = this->GetBooleanValue(*args.next);
  606. newArgs.ReduceOneArg(!rhs, args);
  607. }
  608. }
  609. return true;
  610. }
  611. //=========================================================================
  612. // level 4 handles AND OR
  613. bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs, std::string&,
  614. MessageType&)
  615. {
  616. for (auto args = newArgs.make3ArgsIterator(); args.nextnext != newArgs.end();
  617. args.advance(newArgs)) {
  618. int matchNo;
  619. if ((matchNo = this->matchKeys(*args.next, keyAND, keyOR))) {
  620. const auto lhs = this->GetBooleanValue(*args.current);
  621. const auto rhs = this->GetBooleanValue(*args.nextnext);
  622. // clang-format off
  623. const auto result =
  624. cmRt2CtSelector<
  625. std::logical_and, std::logical_or
  626. >::eval(matchNo, lhs, rhs);
  627. // clang-format on
  628. newArgs.ReduceTwoArgs(result, args);
  629. }
  630. }
  631. return true;
  632. }