cmConditionEvaluator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <sstream>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <utility>
  11. #include "cmAlgorithms.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmState.h"
  15. #include "cmSystemTools.h"
  16. #include "cmake.h"
  17. class cmCommand;
  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. const char* 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. const char* 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. const char* cmConditionEvaluator::GetVariableOrString(
  148. const cmExpandedCommandArgument& argument) const
  149. {
  150. const char* def = this->GetDefinitionIfUnquoted(argument);
  151. if (!def) {
  152. def = argument.c_str();
  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 (cmSystemTools::IsOn(arg.c_str())) {
  195. return true;
  196. }
  197. if (cmSystemTools::IsOff(arg.c_str())) {
  198. return false;
  199. }
  200. // Check for numbers.
  201. if (!arg.empty()) {
  202. char* end;
  203. double d = strtod(arg.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. const char* def = this->GetDefinitionIfUnquoted(arg);
  211. return !cmSystemTools::IsOff(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. const char* def = this->GetDefinitionIfUnquoted(arg);
  227. return !cmSystemTools::IsOff(def);
  228. }
  229. // Old GetVariableOrNumber behavior.
  230. const char* def = this->GetDefinitionIfUnquoted(arg);
  231. if (!def && atoi(arg.c_str())) {
  232. def = arg.c_str();
  233. }
  234. return !cmSystemTools::IsOff(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. cmArgumentList::iterator 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. cmArgumentList::iterator argP1 = arg;
  359. argP1++;
  360. cmAppend(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. cmArgumentList::iterator 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->c_str()),
  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(cmSystemTools::FileIsDirectory(argP1->c_str()),
  402. reducible, arg, newArgs, argP1, argP2);
  403. }
  404. // does a symlink with this name exist
  405. if (this->IsKeyword(keyIS_SYMLINK, *arg) && argP1 != newArgs.end()) {
  406. this->HandlePredicate(cmSystemTools::FileIsSymlink(argP1->c_str()),
  407. reducible, arg, newArgs, argP1, argP2);
  408. }
  409. // is the given path an absolute path ?
  410. if (this->IsKeyword(keyIS_ABSOLUTE, *arg) && argP1 != newArgs.end()) {
  411. this->HandlePredicate(cmSystemTools::FileIsFullPath(argP1->c_str()),
  412. reducible, arg, newArgs, argP1, argP2);
  413. }
  414. // does a command exist
  415. if (this->IsKeyword(keyCOMMAND, *arg) && argP1 != newArgs.end()) {
  416. cmCommand* command =
  417. this->Makefile.GetState()->GetCommand(argP1->c_str());
  418. this->HandlePredicate(command != nullptr, reducible, arg, newArgs,
  419. argP1, argP2);
  420. }
  421. // does a policy exist
  422. if (this->IsKeyword(keyPOLICY, *arg) && argP1 != newArgs.end()) {
  423. cmPolicies::PolicyID pid;
  424. this->HandlePredicate(cmPolicies::GetPolicyID(argP1->c_str(), pid),
  425. reducible, arg, newArgs, argP1, argP2);
  426. }
  427. // does a target exist
  428. if (this->IsKeyword(keyTARGET, *arg) && argP1 != newArgs.end()) {
  429. this->HandlePredicate(
  430. this->Makefile.FindTargetToUse(argP1->GetValue()) != nullptr,
  431. reducible, arg, newArgs, argP1, argP2);
  432. }
  433. // does a test exist
  434. if (this->Policy64Status != cmPolicies::OLD &&
  435. this->Policy64Status != cmPolicies::WARN) {
  436. if (this->IsKeyword(keyTEST, *arg) && argP1 != newArgs.end()) {
  437. const cmTest* haveTest = this->Makefile.GetTest(argP1->c_str());
  438. this->HandlePredicate(haveTest != nullptr, reducible, arg, newArgs,
  439. argP1, argP2);
  440. }
  441. } else if (this->Policy64Status == cmPolicies::WARN &&
  442. this->IsKeyword(keyTEST, *arg)) {
  443. std::ostringstream e;
  444. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0064) << "\n";
  445. e << "TEST will be interpreted as an operator "
  446. "when the policy is set to NEW. "
  447. "Since the policy is not set the OLD behavior will be used.";
  448. this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  449. }
  450. // is a variable defined
  451. if (this->IsKeyword(keyDEFINED, *arg) && argP1 != newArgs.end()) {
  452. size_t argP1len = argP1->GetValue().size();
  453. bool bdef = false;
  454. if (argP1len > 4 && argP1->GetValue().substr(0, 4) == "ENV{" &&
  455. argP1->GetValue().operator[](argP1len - 1) == '}') {
  456. std::string env = argP1->GetValue().substr(4, argP1len - 5);
  457. bdef = cmSystemTools::HasEnv(env);
  458. } else if (argP1len > 6 &&
  459. argP1->GetValue().substr(0, 6) == "CACHE{" &&
  460. argP1->GetValue().operator[](argP1len - 1) == '}') {
  461. std::string cache = argP1->GetValue().substr(6, argP1len - 7);
  462. bdef =
  463. this->Makefile.GetState()->GetCacheEntryValue(cache) != nullptr;
  464. } else {
  465. bdef = this->Makefile.IsDefinitionSet(argP1->GetValue());
  466. }
  467. this->HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  468. }
  469. ++arg;
  470. }
  471. } while (reducible);
  472. return true;
  473. }
  474. //=========================================================================
  475. // level two handles most binary operations except for AND OR
  476. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  477. std::string& errorString,
  478. MessageType& status)
  479. {
  480. int reducible;
  481. std::string def_buf;
  482. const char* def;
  483. const char* def2;
  484. do {
  485. reducible = 0;
  486. cmArgumentList::iterator arg = newArgs.begin();
  487. cmArgumentList::iterator argP1;
  488. cmArgumentList::iterator argP2;
  489. while (arg != newArgs.end()) {
  490. argP1 = arg;
  491. this->IncrementArguments(newArgs, argP1, argP2);
  492. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  493. IsKeyword(keyMATCHES, *argP1)) {
  494. def = this->GetVariableOrString(*arg);
  495. if (def != arg->c_str() // yes, we compare the pointer value
  496. && cmHasLiteralPrefix(arg->GetValue(), "CMAKE_MATCH_")) {
  497. // The string to match is owned by our match result variables.
  498. // Move it to our own buffer before clearing them.
  499. def_buf = def;
  500. def = def_buf.c_str();
  501. }
  502. const char* rex = argP2->c_str();
  503. this->Makefile.ClearMatches();
  504. cmsys::RegularExpression regEntry;
  505. if (!regEntry.compile(rex)) {
  506. std::ostringstream error;
  507. error << "Regular expression \"" << rex << "\" cannot compile";
  508. errorString = error.str();
  509. status = MessageType::FATAL_ERROR;
  510. return false;
  511. }
  512. if (regEntry.find(def)) {
  513. this->Makefile.StoreMatches(regEntry);
  514. *arg = cmExpandedCommandArgument("1", true);
  515. } else {
  516. *arg = cmExpandedCommandArgument("0", true);
  517. }
  518. newArgs.erase(argP2);
  519. newArgs.erase(argP1);
  520. argP1 = arg;
  521. this->IncrementArguments(newArgs, argP1, argP2);
  522. reducible = 1;
  523. }
  524. if (argP1 != newArgs.end() && this->IsKeyword(keyMATCHES, *arg)) {
  525. *arg = cmExpandedCommandArgument("0", true);
  526. newArgs.erase(argP1);
  527. argP1 = arg;
  528. this->IncrementArguments(newArgs, argP1, argP2);
  529. reducible = 1;
  530. }
  531. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  532. (this->IsKeyword(keyLESS, *argP1) ||
  533. this->IsKeyword(keyLESS_EQUAL, *argP1) ||
  534. this->IsKeyword(keyGREATER, *argP1) ||
  535. this->IsKeyword(keyGREATER_EQUAL, *argP1) ||
  536. this->IsKeyword(keyEQUAL, *argP1))) {
  537. def = this->GetVariableOrString(*arg);
  538. def2 = this->GetVariableOrString(*argP2);
  539. double lhs;
  540. double rhs;
  541. bool result;
  542. if (sscanf(def, "%lg", &lhs) != 1 || sscanf(def2, "%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 = strcmp(def, 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 = cmSystemTools::VersionCompare(op, def, def2);
  602. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  603. }
  604. // is file A newer than file B
  605. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  606. this->IsKeyword(keyIS_NEWER_THAN, *argP1)) {
  607. int fileIsNewer = 0;
  608. bool success = cmSystemTools::FileTimeCompare(
  609. arg->GetValue(), (argP2)->GetValue(), &fileIsNewer);
  610. this->HandleBinaryOp(
  611. (!success || fileIsNewer == 1 || fileIsNewer == 0), reducible, arg,
  612. newArgs, argP1, argP2);
  613. }
  614. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  615. this->IsKeyword(keyIN_LIST, *argP1)) {
  616. if (this->Policy57Status != cmPolicies::OLD &&
  617. this->Policy57Status != cmPolicies::WARN) {
  618. bool result = false;
  619. def = this->GetVariableOrString(*arg);
  620. def2 = this->Makefile.GetDefinition(argP2->GetValue());
  621. if (def2) {
  622. std::vector<std::string> list;
  623. cmSystemTools::ExpandListArgument(def2, list, true);
  624. result = std::find(list.begin(), list.end(), def) != list.end();
  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. cmArgumentList::iterator arg = newArgs.begin();
  651. cmArgumentList::iterator argP1;
  652. cmArgumentList::iterator argP2;
  653. while (arg != newArgs.end()) {
  654. argP1 = arg;
  655. IncrementArguments(newArgs, argP1, argP2);
  656. if (argP1 != newArgs.end() && 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. cmArgumentList::iterator arg = newArgs.begin();
  678. cmArgumentList::iterator argP1;
  679. cmArgumentList::iterator argP2;
  680. while (arg != newArgs.end()) {
  681. argP1 = arg;
  682. IncrementArguments(newArgs, argP1, argP2);
  683. if (argP1 != newArgs.end() && 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. }