cmConditionEvaluator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. , Policy64Status(makefile.GetPolicyStatus(cmPolicies::CMP0064))
  196. , Policy139Status(makefile.GetPolicyStatus(cmPolicies::CMP0139))
  197. {
  198. }
  199. //=========================================================================
  200. // order of operations,
  201. // 1. ( ) -- parenthetical groups
  202. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  203. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  204. // 4. NOT
  205. // 5. AND OR
  206. //
  207. // There is an issue on whether the arguments should be values of references,
  208. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  209. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  210. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  211. // take numeric values or variable names. STRLESS and STRGREATER take
  212. // variable names but if the variable name is not found it will use the name
  213. // directly. AND OR take variables or the values 0 or 1.
  214. bool cmConditionEvaluator::IsTrue(
  215. const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
  216. MessageType& status)
  217. {
  218. errorString.clear();
  219. // handle empty invocation
  220. if (args.empty()) {
  221. return false;
  222. }
  223. // store the reduced args in this vector
  224. cmArgumentList newArgs(args.begin(), args.end());
  225. // now loop through the arguments and see if we can reduce any of them
  226. // we do this multiple times. Once for each level of precedence
  227. // parens
  228. using handlerFn_t = bool (cmConditionEvaluator::*)(
  229. cmArgumentList&, std::string&, MessageType&);
  230. const std::array<handlerFn_t, 5> handlers = { {
  231. &cmConditionEvaluator::HandleLevel0, // parenthesis
  232. &cmConditionEvaluator::HandleLevel1, // predicates
  233. &cmConditionEvaluator::HandleLevel2, // binary ops
  234. &cmConditionEvaluator::HandleLevel3, // NOT
  235. &cmConditionEvaluator::HandleLevel4 // AND OR
  236. } };
  237. for (auto fn : handlers) {
  238. // Call the reducer 'till there is anything to reduce...
  239. // (i.e., if after an iteration the size becomes smaller)
  240. auto levelResult = true;
  241. for (auto beginSize = newArgs.size();
  242. (levelResult = (this->*fn)(newArgs, errorString, status)) &&
  243. newArgs.size() < beginSize;
  244. beginSize = newArgs.size()) {
  245. }
  246. if (!levelResult) {
  247. // NOTE `errorString` supposed to be set already
  248. return false;
  249. }
  250. }
  251. // now at the end there should only be one argument left
  252. if (newArgs.size() != 1) {
  253. errorString = "Unknown arguments specified";
  254. status = MessageType::FATAL_ERROR;
  255. return false;
  256. }
  257. return this->GetBooleanValue(newArgs.front());
  258. }
  259. //=========================================================================
  260. cmValue cmConditionEvaluator::GetDefinitionIfUnquoted(
  261. cmExpandedCommandArgument const& argument) const
  262. {
  263. if (argument.WasQuoted()) {
  264. return nullptr;
  265. }
  266. return this->Makefile.GetDefinition(argument.GetValue());
  267. }
  268. //=========================================================================
  269. cmValue cmConditionEvaluator::GetVariableOrString(
  270. const cmExpandedCommandArgument& argument) const
  271. {
  272. cmValue def = this->GetDefinitionIfUnquoted(argument);
  273. if (!def) {
  274. def = cmValue(argument.GetValue());
  275. }
  276. return def;
  277. }
  278. //=========================================================================
  279. bool cmConditionEvaluator::IsKeyword(
  280. cm::static_string_view keyword,
  281. const cmExpandedCommandArgument& argument) const
  282. {
  283. if (argument.WasQuoted()) {
  284. return false;
  285. }
  286. return argument.GetValue() == keyword;
  287. }
  288. //=========================================================================
  289. bool cmConditionEvaluator::GetBooleanValue(
  290. cmExpandedCommandArgument& arg) const
  291. {
  292. // Check basic and named constants.
  293. if (cmIsOn(arg.GetValue())) {
  294. return true;
  295. }
  296. if (cmIsOff(arg.GetValue())) {
  297. return false;
  298. }
  299. // Check for numbers.
  300. if (!arg.empty()) {
  301. char* end;
  302. const double d = std::strtod(arg.GetValue().c_str(), &end);
  303. if (*end == '\0') {
  304. // The whole string is a number. Use C conversion to bool.
  305. return static_cast<bool>(d);
  306. }
  307. }
  308. // Check definition.
  309. cmValue def = this->GetDefinitionIfUnquoted(arg);
  310. return !def.IsOff();
  311. }
  312. template <int N>
  313. inline int cmConditionEvaluator::matchKeysImpl(
  314. const cmExpandedCommandArgument&)
  315. {
  316. // Zero means "not found"
  317. return 0;
  318. }
  319. template <int N, typename T, typename... Keys>
  320. inline int cmConditionEvaluator::matchKeysImpl(
  321. const cmExpandedCommandArgument& arg, T current, Keys... key)
  322. {
  323. if (this->IsKeyword(current, arg)) {
  324. // Stop searching as soon as smth has found
  325. return N;
  326. }
  327. return matchKeysImpl<N + 1>(arg, key...);
  328. }
  329. template <typename... Keys>
  330. inline int cmConditionEvaluator::matchKeys(
  331. const cmExpandedCommandArgument& arg, Keys... key)
  332. {
  333. // Get index of the matched key (1-based)
  334. return matchKeysImpl<1>(arg, key...);
  335. }
  336. //=========================================================================
  337. // level 0 processes parenthetical expressions
  338. bool cmConditionEvaluator::HandleLevel0(cmArgumentList& newArgs,
  339. std::string& errorString,
  340. MessageType& status)
  341. {
  342. for (auto arg = newArgs.begin(); arg != newArgs.end(); ++arg) {
  343. if (this->IsKeyword(keyParenL, *arg)) {
  344. // search for the closing paren for this opening one
  345. auto depth = 1;
  346. auto argClose = std::next(arg);
  347. for (; argClose != newArgs.end() && depth; ++argClose) {
  348. depth += int(this->IsKeyword(keyParenL, *argClose)) -
  349. int(this->IsKeyword(keyParenR, *argClose));
  350. }
  351. if (depth) {
  352. errorString = "mismatched parenthesis in condition";
  353. status = MessageType::FATAL_ERROR;
  354. return false;
  355. }
  356. // store the reduced args in this vector
  357. auto argOpen = std::next(arg);
  358. const std::vector<cmExpandedCommandArgument> subExpr(
  359. argOpen, std::prev(argClose));
  360. // now recursively invoke IsTrue to handle the values inside the
  361. // parenthetical expression
  362. const auto value = this->IsTrue(subExpr, errorString, status);
  363. *arg = cmExpandedCommandArgument(bool2string(value), true);
  364. argOpen = std::next(arg);
  365. // remove the now evaluated parenthetical expression
  366. newArgs.erase(argOpen, argClose);
  367. }
  368. }
  369. return true;
  370. }
  371. //=========================================================================
  372. // level one handles most predicates except for NOT
  373. bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
  374. MessageType&)
  375. {
  376. for (auto args = newArgs.make2ArgsIterator(); args.current != newArgs.end();
  377. args.advance(newArgs)) {
  378. auto policyCheck = [&, this](const cmPolicies::PolicyID id,
  379. const cmPolicies::PolicyStatus status,
  380. const cm::static_string_view kw) {
  381. if (status == cmPolicies::WARN && this->IsKeyword(kw, *args.current)) {
  382. std::ostringstream e;
  383. e << cmPolicies::GetPolicyWarning(id) << "\n"
  384. << kw
  385. << " will be interpreted as an operator "
  386. "when the policy is set to NEW. "
  387. "Since the policy is not set the OLD behavior will be used.";
  388. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  389. }
  390. };
  391. // NOTE Checking policies for warnings are not require an access to the
  392. // next arg. Check them first!
  393. policyCheck(cmPolicies::CMP0064, this->Policy64Status, keyTEST);
  394. // NOTE Fail fast: All the predicates below require the next arg to be
  395. // valid
  396. if (args.next == newArgs.end()) {
  397. continue;
  398. }
  399. // does a file exist
  400. if (this->IsKeyword(keyEXISTS, *args.current)) {
  401. newArgs.ReduceOneArg(cmSystemTools::FileExists(args.next->GetValue()),
  402. args);
  403. }
  404. // check if a file is readable
  405. else if (this->IsKeyword(keyIS_READABLE, *args.current)) {
  406. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  407. args.next->GetValue(), cmsys::TEST_FILE_READ),
  408. args);
  409. }
  410. // check if a file is writable
  411. else if (this->IsKeyword(keyIS_WRITABLE, *args.current)) {
  412. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  413. args.next->GetValue(), cmsys::TEST_FILE_WRITE),
  414. args);
  415. }
  416. // check if a file is executable
  417. else if (this->IsKeyword(keyIS_EXECUTABLE, *args.current)) {
  418. newArgs.ReduceOneArg(cmSystemTools::TestFileAccess(
  419. args.next->GetValue(), cmsys::TEST_FILE_EXECUTE),
  420. args);
  421. }
  422. // does a directory with this name exist
  423. else if (this->IsKeyword(keyIS_DIRECTORY, *args.current)) {
  424. newArgs.ReduceOneArg(
  425. cmSystemTools::FileIsDirectory(args.next->GetValue()), args);
  426. }
  427. // does a symlink with this name exist
  428. else if (this->IsKeyword(keyIS_SYMLINK, *args.current)) {
  429. newArgs.ReduceOneArg(cmSystemTools::FileIsSymlink(args.next->GetValue()),
  430. args);
  431. }
  432. // is the given path an absolute path ?
  433. else if (this->IsKeyword(keyIS_ABSOLUTE, *args.current)) {
  434. newArgs.ReduceOneArg(
  435. cmSystemTools::FileIsFullPath(args.next->GetValue()), args);
  436. }
  437. // does a command exist
  438. else if (this->IsKeyword(keyCOMMAND, *args.current)) {
  439. newArgs.ReduceOneArg(
  440. static_cast<bool>(
  441. this->Makefile.GetState()->GetCommand(args.next->GetValue())),
  442. args);
  443. }
  444. // does a policy exist
  445. else if (this->IsKeyword(keyPOLICY, *args.current)) {
  446. cmPolicies::PolicyID pid;
  447. newArgs.ReduceOneArg(
  448. cmPolicies::GetPolicyID(args.next->GetValue().c_str(), pid), args);
  449. }
  450. // does a target exist
  451. else if (this->IsKeyword(keyTARGET, *args.current)) {
  452. newArgs.ReduceOneArg(static_cast<bool>(this->Makefile.FindTargetToUse(
  453. args.next->GetValue())),
  454. args);
  455. }
  456. // is a variable defined
  457. else if (this->IsKeyword(keyDEFINED, *args.current)) {
  458. const auto& var = args.next->GetValue();
  459. const auto varNameLen = var.size();
  460. auto result = false;
  461. if (looksLikeSpecialVariable(var, "ENV"_s, varNameLen)) {
  462. const auto env = args.next->GetValue().substr(4, varNameLen - 5);
  463. result = cmSystemTools::HasEnv(env);
  464. }
  465. else if (looksLikeSpecialVariable(var, "CACHE"_s, varNameLen)) {
  466. const auto cache = args.next->GetValue().substr(6, varNameLen - 7);
  467. result = static_cast<bool>(
  468. this->Makefile.GetState()->GetCacheEntryValue(cache));
  469. }
  470. else {
  471. result = this->Makefile.IsDefinitionSet(args.next->GetValue());
  472. }
  473. newArgs.ReduceOneArg(result, args);
  474. }
  475. // does a test exist
  476. else if (this->IsKeyword(keyTEST, *args.current)) {
  477. if (this->Policy64Status == cmPolicies::OLD ||
  478. this->Policy64Status == cmPolicies::WARN) {
  479. continue;
  480. }
  481. newArgs.ReduceOneArg(
  482. static_cast<bool>(this->Makefile.GetTest(args.next->GetValue())),
  483. args);
  484. }
  485. }
  486. return true;
  487. }
  488. //=========================================================================
  489. // level two handles most binary operations except for AND OR
  490. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  491. std::string& errorString,
  492. MessageType& status)
  493. {
  494. for (auto args = newArgs.make3ArgsIterator(); args.current != newArgs.end();
  495. args.advance(newArgs)) {
  496. int matchNo;
  497. // NOTE Handle special case `if(... BLAH_BLAH MATCHES)`
  498. // (i.e., w/o regex to match which is possibly result of
  499. // variable expansion to an empty string)
  500. if (args.next != newArgs.end() &&
  501. this->IsKeyword(keyMATCHES, *args.current)) {
  502. newArgs.ReduceOneArg(false, args);
  503. }
  504. // NOTE Fail fast: All the binary ops below require 2 arguments.
  505. else if (args.next == newArgs.end() || args.nextnext == newArgs.end()) {
  506. continue;
  507. }
  508. else if (this->IsKeyword(keyMATCHES, *args.next)) {
  509. cmValue def = this->GetDefinitionIfUnquoted(*args.current);
  510. std::string def_buf;
  511. if (!def) {
  512. def = cmValue(args.current->GetValue());
  513. } else if (cmHasLiteralPrefix(args.current->GetValue(),
  514. "CMAKE_MATCH_")) {
  515. // The string to match is owned by our match result variables.
  516. // Move it to our own buffer before clearing them.
  517. def_buf = *def;
  518. def = cmValue(def_buf);
  519. }
  520. this->Makefile.ClearMatches();
  521. const auto& rex = args.nextnext->GetValue();
  522. cmsys::RegularExpression regEntry;
  523. if (!regEntry.compile(rex)) {
  524. std::ostringstream error;
  525. error << "Regular expression \"" << rex << "\" cannot compile";
  526. errorString = error.str();
  527. status = MessageType::FATAL_ERROR;
  528. return false;
  529. }
  530. const auto match = regEntry.find(*def);
  531. if (match) {
  532. this->Makefile.StoreMatches(regEntry);
  533. }
  534. newArgs.ReduceTwoArgs(match, args);
  535. }
  536. else if ((matchNo =
  537. this->matchKeys(*args.next, keyLESS, keyLESS_EQUAL, keyGREATER,
  538. keyGREATER_EQUAL, keyEQUAL))) {
  539. cmValue ldef = this->GetVariableOrString(*args.current);
  540. cmValue rdef = this->GetVariableOrString(*args.nextnext);
  541. double lhs;
  542. double rhs;
  543. auto parseDoubles = [&]() {
  544. return std::sscanf(ldef->c_str(), "%lg", &lhs) == 1 &&
  545. std::sscanf(rdef->c_str(), "%lg", &rhs) == 1;
  546. };
  547. // clang-format off
  548. const auto result = parseDoubles() &&
  549. cmRt2CtSelector<
  550. std::less, std::less_equal,
  551. std::greater, std::greater_equal,
  552. std::equal_to
  553. >::eval(matchNo, lhs, rhs);
  554. // clang-format on
  555. newArgs.ReduceTwoArgs(result, args);
  556. }
  557. else if ((matchNo = this->matchKeys(*args.next, keySTRLESS,
  558. keySTRLESS_EQUAL, keySTRGREATER,
  559. keySTRGREATER_EQUAL, keySTREQUAL))) {
  560. const cmValue lhs = this->GetVariableOrString(*args.current);
  561. const cmValue rhs = this->GetVariableOrString(*args.nextnext);
  562. const auto val = (*lhs).compare(*rhs);
  563. // clang-format off
  564. const auto result = cmRt2CtSelector<
  565. std::less, std::less_equal,
  566. std::greater, std::greater_equal,
  567. std::equal_to
  568. >::eval(matchNo, val, 0);
  569. // clang-format on
  570. newArgs.ReduceTwoArgs(result, args);
  571. }
  572. else if ((matchNo =
  573. this->matchKeys(*args.next, keyVERSION_LESS,
  574. keyVERSION_LESS_EQUAL, keyVERSION_GREATER,
  575. keyVERSION_GREATER_EQUAL, keyVERSION_EQUAL))) {
  576. const auto op = MATCH2CMPOP[matchNo - 1];
  577. const cmValue lhs = this->GetVariableOrString(*args.current);
  578. const cmValue rhs = this->GetVariableOrString(*args.nextnext);
  579. const auto result = cmSystemTools::VersionCompare(op, lhs, rhs);
  580. newArgs.ReduceTwoArgs(result, args);
  581. }
  582. // is file A newer than file B
  583. else if (this->IsKeyword(keyIS_NEWER_THAN, *args.next)) {
  584. auto fileIsNewer = 0;
  585. cmsys::Status ftcStatus = cmSystemTools::FileTimeCompare(
  586. args.current->GetValue(), args.nextnext->GetValue(), &fileIsNewer);
  587. newArgs.ReduceTwoArgs(
  588. (!ftcStatus || fileIsNewer == 1 || fileIsNewer == 0), args);
  589. }
  590. else if (this->IsKeyword(keyIN_LIST, *args.next)) {
  591. cmValue lhs = this->GetVariableOrString(*args.current);
  592. cmValue rhs = this->Makefile.GetDefinition(args.nextnext->GetValue());
  593. newArgs.ReduceTwoArgs(
  594. rhs && cm::contains(cmList{ *rhs, cmList::EmptyElements::Yes }, *lhs),
  595. args);
  596. }
  597. else if (this->IsKeyword(keyPATH_EQUAL, *args.next)) {
  598. if (this->Policy139Status != cmPolicies::OLD &&
  599. this->Policy139Status != cmPolicies::WARN) {
  600. cmValue lhs = this->GetVariableOrString(*args.current);
  601. cmValue rhs = this->GetVariableOrString(*args.nextnext);
  602. const auto result = cmCMakePath{ *lhs } == cmCMakePath{ *rhs };
  603. newArgs.ReduceTwoArgs(result, args);
  604. }
  605. else if (this->Policy139Status == cmPolicies::WARN) {
  606. std::ostringstream e;
  607. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0139)
  608. << "\n"
  609. "PATH_EQUAL will be interpreted as an operator "
  610. "when the policy is set to NEW. "
  611. "Since the policy is not set the OLD behavior will be used.";
  612. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  613. }
  614. }
  615. }
  616. return true;
  617. }
  618. //=========================================================================
  619. // level 3 handles NOT
  620. bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs, std::string&,
  621. MessageType&)
  622. {
  623. for (auto args = newArgs.make2ArgsIterator(); args.next != newArgs.end();
  624. args.advance(newArgs)) {
  625. if (this->IsKeyword(keyNOT, *args.current)) {
  626. const auto rhs = this->GetBooleanValue(*args.next);
  627. newArgs.ReduceOneArg(!rhs, args);
  628. }
  629. }
  630. return true;
  631. }
  632. //=========================================================================
  633. // level 4 handles AND OR
  634. bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs, std::string&,
  635. MessageType&)
  636. {
  637. for (auto args = newArgs.make3ArgsIterator(); args.nextnext != newArgs.end();
  638. args.advance(newArgs)) {
  639. int matchNo;
  640. if ((matchNo = this->matchKeys(*args.next, keyAND, keyOR))) {
  641. const auto lhs = this->GetBooleanValue(*args.current);
  642. const auto rhs = this->GetBooleanValue(*args.nextnext);
  643. // clang-format off
  644. const auto result =
  645. cmRt2CtSelector<
  646. std::logical_and, std::logical_or
  647. >::eval(matchNo, lhs, rhs);
  648. // clang-format on
  649. newArgs.ReduceTwoArgs(result, args);
  650. }
  651. }
  652. return true;
  653. }