cmConditionEvaluator.cxx 27 KB

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