cmConditionEvaluator.cxx 27 KB

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