cmConditionEvaluator.cxx 25 KB

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