cmConditionEvaluator.cxx 27 KB

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