cmConditionEvaluator.cxx 27 KB

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