cmConditionEvaluator.cxx 27 KB

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