cmConditionEvaluator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 <algorithm>
  5. #include <cmConfigure.h>
  6. #include <cmsys/RegularExpression.hxx>
  7. #include <sstream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "cmAlgorithms.h"
  12. #include "cmMakefile.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. class cmCommand;
  16. class cmTest;
  17. static std::string const keyAND = "AND";
  18. static std::string const keyCOMMAND = "COMMAND";
  19. static std::string const keyDEFINED = "DEFINED";
  20. static std::string const keyEQUAL = "EQUAL";
  21. static std::string const keyEXISTS = "EXISTS";
  22. static std::string const keyGREATER = "GREATER";
  23. static std::string const keyGREATER_EQUAL = "GREATER_EQUAL";
  24. static std::string const keyIN_LIST = "IN_LIST";
  25. static std::string const keyIS_ABSOLUTE = "IS_ABSOLUTE";
  26. static std::string const keyIS_DIRECTORY = "IS_DIRECTORY";
  27. static std::string const keyIS_NEWER_THAN = "IS_NEWER_THAN";
  28. static std::string const keyIS_SYMLINK = "IS_SYMLINK";
  29. static std::string const keyLESS = "LESS";
  30. static std::string const keyLESS_EQUAL = "LESS_EQUAL";
  31. static std::string const keyMATCHES = "MATCHES";
  32. static std::string const keyNOT = "NOT";
  33. static std::string const keyOR = "OR";
  34. static std::string const keyParenL = "(";
  35. static std::string const keyParenR = ")";
  36. static std::string const keyPOLICY = "POLICY";
  37. static std::string const keySTREQUAL = "STREQUAL";
  38. static std::string const keySTRGREATER = "STRGREATER";
  39. static std::string const keySTRGREATER_EQUAL = "STRGREATER_EQUAL";
  40. static std::string const keySTRLESS = "STRLESS";
  41. static std::string const keySTRLESS_EQUAL = "STRLESS_EQUAL";
  42. static std::string const keyTARGET = "TARGET";
  43. static std::string const keyTEST = "TEST";
  44. static std::string const keyVERSION_EQUAL = "VERSION_EQUAL";
  45. static std::string const keyVERSION_GREATER = "VERSION_GREATER";
  46. static std::string const keyVERSION_GREATER_EQUAL = "VERSION_GREATER_EQUAL";
  47. static std::string const keyVERSION_LESS = "VERSION_LESS";
  48. static std::string const keyVERSION_LESS_EQUAL = "VERSION_LESS_EQUAL";
  49. cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile,
  50. const cmListFileContext& context,
  51. const cmListFileBacktrace& bt)
  52. : Makefile(makefile)
  53. , ExecutionContext(context)
  54. , Backtrace(bt)
  55. , Policy12Status(makefile.GetPolicyStatus(cmPolicies::CMP0012))
  56. , Policy54Status(makefile.GetPolicyStatus(cmPolicies::CMP0054))
  57. , Policy57Status(makefile.GetPolicyStatus(cmPolicies::CMP0057))
  58. , Policy64Status(makefile.GetPolicyStatus(cmPolicies::CMP0064))
  59. {
  60. }
  61. //=========================================================================
  62. // order of operations,
  63. // 1. ( ) -- parenthetical groups
  64. // 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
  65. // 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
  66. // 4. NOT
  67. // 5. AND OR
  68. //
  69. // There is an issue on whether the arguments should be values of references,
  70. // for example IF (FOO AND BAR) should that compare the strings FOO and BAR
  71. // or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
  72. // EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
  73. // take numeric values or variable names. STRLESS and STRGREATER take
  74. // variable names but if the variable name is not found it will use the name
  75. // directly. AND OR take variables or the values 0 or 1.
  76. bool cmConditionEvaluator::IsTrue(
  77. const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
  78. cmake::MessageType& status)
  79. {
  80. errorString = "";
  81. // handle empty invocation
  82. if (args.empty()) {
  83. return false;
  84. }
  85. // store the reduced args in this vector
  86. cmArgumentList newArgs;
  87. // copy to the list structure
  88. newArgs.insert(newArgs.end(), 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 = cmake::FATAL_ERROR;
  115. return false;
  116. }
  117. return this->GetBooleanValueWithAutoDereference(*(newArgs.begin()),
  118. errorString, 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 CM_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. cmake::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. cmake::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. cmake::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 = cmake::AUTHOR_WARNING;
  259. }
  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 = cmake::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. cmake::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 = cmake::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. newArgs2.insert(newArgs2.end(), 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. cmake::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 != CM_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()) != CM_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 != CM_NULLPTR, reducible, arg,
  439. newArgs, 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(cmake::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.c_str());
  458. } else {
  459. bdef = this->Makefile.IsDefinitionSet(argP1->GetValue());
  460. }
  461. this->HandlePredicate(bdef, reducible, arg, newArgs, argP1, argP2);
  462. }
  463. ++arg;
  464. }
  465. } while (reducible);
  466. return true;
  467. }
  468. //=========================================================================
  469. // level two handles most binary operations except for AND OR
  470. bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
  471. std::string& errorString,
  472. cmake::MessageType& status)
  473. {
  474. int reducible;
  475. std::string def_buf;
  476. const char* def;
  477. const char* def2;
  478. do {
  479. reducible = 0;
  480. cmArgumentList::iterator arg = newArgs.begin();
  481. cmArgumentList::iterator argP1;
  482. cmArgumentList::iterator argP2;
  483. while (arg != newArgs.end()) {
  484. argP1 = arg;
  485. this->IncrementArguments(newArgs, argP1, argP2);
  486. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  487. IsKeyword(keyMATCHES, *argP1)) {
  488. def = this->GetVariableOrString(*arg);
  489. if (def != arg->c_str() // yes, we compare the pointer value
  490. && cmHasLiteralPrefix(arg->GetValue(), "CMAKE_MATCH_")) {
  491. // The string to match is owned by our match result variables.
  492. // Move it to our own buffer before clearing them.
  493. def_buf = def;
  494. def = def_buf.c_str();
  495. }
  496. const char* rex = argP2->c_str();
  497. this->Makefile.ClearMatches();
  498. cmsys::RegularExpression regEntry;
  499. if (!regEntry.compile(rex)) {
  500. std::ostringstream error;
  501. error << "Regular expression \"" << rex << "\" cannot compile";
  502. errorString = error.str();
  503. status = cmake::FATAL_ERROR;
  504. return false;
  505. }
  506. if (regEntry.find(def)) {
  507. this->Makefile.StoreMatches(regEntry);
  508. *arg = cmExpandedCommandArgument("1", true);
  509. } else {
  510. *arg = cmExpandedCommandArgument("0", true);
  511. }
  512. newArgs.erase(argP2);
  513. newArgs.erase(argP1);
  514. argP1 = arg;
  515. this->IncrementArguments(newArgs, argP1, argP2);
  516. reducible = 1;
  517. }
  518. if (argP1 != newArgs.end() && this->IsKeyword(keyMATCHES, *arg)) {
  519. *arg = cmExpandedCommandArgument("0", true);
  520. newArgs.erase(argP1);
  521. argP1 = arg;
  522. this->IncrementArguments(newArgs, argP1, argP2);
  523. reducible = 1;
  524. }
  525. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  526. (this->IsKeyword(keyLESS, *argP1) ||
  527. this->IsKeyword(keyLESS_EQUAL, *argP1) ||
  528. this->IsKeyword(keyGREATER, *argP1) ||
  529. this->IsKeyword(keyGREATER_EQUAL, *argP1) ||
  530. this->IsKeyword(keyEQUAL, *argP1))) {
  531. def = this->GetVariableOrString(*arg);
  532. def2 = this->GetVariableOrString(*argP2);
  533. double lhs;
  534. double rhs;
  535. bool result;
  536. if (sscanf(def, "%lg", &lhs) != 1 || sscanf(def2, "%lg", &rhs) != 1) {
  537. result = false;
  538. } else if (*(argP1) == keyLESS) {
  539. result = (lhs < rhs);
  540. } else if (*(argP1) == keyLESS_EQUAL) {
  541. result = (lhs <= rhs);
  542. } else if (*(argP1) == keyGREATER) {
  543. result = (lhs > rhs);
  544. } else if (*(argP1) == keyGREATER_EQUAL) {
  545. result = (lhs >= rhs);
  546. } else {
  547. result = (lhs == rhs);
  548. }
  549. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  550. }
  551. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  552. (this->IsKeyword(keySTRLESS, *argP1) ||
  553. this->IsKeyword(keySTRLESS_EQUAL, *argP1) ||
  554. this->IsKeyword(keySTRGREATER, *argP1) ||
  555. this->IsKeyword(keySTRGREATER_EQUAL, *argP1) ||
  556. this->IsKeyword(keySTREQUAL, *argP1))) {
  557. def = this->GetVariableOrString(*arg);
  558. def2 = this->GetVariableOrString(*argP2);
  559. int val = strcmp(def, def2);
  560. bool result;
  561. if (*(argP1) == keySTRLESS) {
  562. result = (val < 0);
  563. } else if (*(argP1) == keySTRLESS_EQUAL) {
  564. result = (val <= 0);
  565. } else if (*(argP1) == keySTRGREATER) {
  566. result = (val > 0);
  567. } else if (*(argP1) == keySTRGREATER_EQUAL) {
  568. result = (val >= 0);
  569. } else // strequal
  570. {
  571. result = (val == 0);
  572. }
  573. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  574. }
  575. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  576. (this->IsKeyword(keyVERSION_LESS, *argP1) ||
  577. this->IsKeyword(keyVERSION_LESS_EQUAL, *argP1) ||
  578. this->IsKeyword(keyVERSION_GREATER, *argP1) ||
  579. this->IsKeyword(keyVERSION_GREATER_EQUAL, *argP1) ||
  580. this->IsKeyword(keyVERSION_EQUAL, *argP1))) {
  581. def = this->GetVariableOrString(*arg);
  582. def2 = this->GetVariableOrString(*argP2);
  583. cmSystemTools::CompareOp op;
  584. if (*argP1 == keyVERSION_LESS) {
  585. op = cmSystemTools::OP_LESS;
  586. } else if (*argP1 == keyVERSION_LESS_EQUAL) {
  587. op = cmSystemTools::OP_LESS_EQUAL;
  588. } else if (*argP1 == keyVERSION_GREATER) {
  589. op = cmSystemTools::OP_GREATER;
  590. } else if (*argP1 == keyVERSION_GREATER_EQUAL) {
  591. op = cmSystemTools::OP_GREATER_EQUAL;
  592. } else { // version_equal
  593. op = cmSystemTools::OP_EQUAL;
  594. }
  595. bool result = cmSystemTools::VersionCompare(op, def, def2);
  596. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  597. }
  598. // is file A newer than file B
  599. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  600. this->IsKeyword(keyIS_NEWER_THAN, *argP1)) {
  601. int fileIsNewer = 0;
  602. bool success = cmSystemTools::FileTimeCompare(
  603. arg->GetValue(), (argP2)->GetValue(), &fileIsNewer);
  604. this->HandleBinaryOp(
  605. (!success || fileIsNewer == 1 || fileIsNewer == 0), reducible, arg,
  606. newArgs, argP1, argP2);
  607. }
  608. if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
  609. this->IsKeyword(keyIN_LIST, *argP1)) {
  610. if (this->Policy57Status != cmPolicies::OLD &&
  611. this->Policy57Status != cmPolicies::WARN) {
  612. bool result = false;
  613. def = this->GetVariableOrString(*arg);
  614. def2 = this->Makefile.GetDefinition(argP2->GetValue());
  615. if (def2) {
  616. std::vector<std::string> list;
  617. cmSystemTools::ExpandListArgument(def2, list, true);
  618. result = std::find(list.begin(), list.end(), def) != list.end();
  619. }
  620. this->HandleBinaryOp(result, reducible, arg, newArgs, argP1, argP2);
  621. } else if (this->Policy57Status == cmPolicies::WARN) {
  622. std::ostringstream e;
  623. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0057) << "\n";
  624. e << "IN_LIST will be interpreted as an operator "
  625. "when the policy is set to NEW. "
  626. "Since the policy is not set the OLD behavior will be used.";
  627. this->Makefile.IssueMessage(cmake::AUTHOR_WARNING, e.str());
  628. }
  629. }
  630. ++arg;
  631. }
  632. } while (reducible);
  633. return true;
  634. }
  635. //=========================================================================
  636. // level 3 handles NOT
  637. bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs,
  638. std::string& errorString,
  639. cmake::MessageType& status)
  640. {
  641. int reducible;
  642. do {
  643. reducible = 0;
  644. cmArgumentList::iterator arg = newArgs.begin();
  645. cmArgumentList::iterator argP1;
  646. cmArgumentList::iterator argP2;
  647. while (arg != newArgs.end()) {
  648. argP1 = arg;
  649. IncrementArguments(newArgs, argP1, argP2);
  650. if (argP1 != newArgs.end() && IsKeyword(keyNOT, *arg)) {
  651. bool rhs = this->GetBooleanValueWithAutoDereference(
  652. *argP1, errorString, status);
  653. this->HandlePredicate(!rhs, reducible, arg, newArgs, argP1, argP2);
  654. }
  655. ++arg;
  656. }
  657. } while (reducible);
  658. return true;
  659. }
  660. //=========================================================================
  661. // level 4 handles AND OR
  662. bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs,
  663. std::string& errorString,
  664. cmake::MessageType& status)
  665. {
  666. int reducible;
  667. bool lhs;
  668. bool rhs;
  669. do {
  670. reducible = 0;
  671. cmArgumentList::iterator arg = newArgs.begin();
  672. cmArgumentList::iterator argP1;
  673. cmArgumentList::iterator argP2;
  674. while (arg != newArgs.end()) {
  675. argP1 = arg;
  676. IncrementArguments(newArgs, argP1, argP2);
  677. if (argP1 != newArgs.end() && IsKeyword(keyAND, *argP1) &&
  678. argP2 != newArgs.end()) {
  679. lhs =
  680. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  681. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  682. status);
  683. this->HandleBinaryOp((lhs && rhs), reducible, arg, newArgs, argP1,
  684. argP2);
  685. }
  686. if (argP1 != newArgs.end() && this->IsKeyword(keyOR, *argP1) &&
  687. argP2 != newArgs.end()) {
  688. lhs =
  689. this->GetBooleanValueWithAutoDereference(*arg, errorString, status);
  690. rhs = this->GetBooleanValueWithAutoDereference(*argP2, errorString,
  691. status);
  692. this->HandleBinaryOp((lhs || rhs), reducible, arg, newArgs, argP1,
  693. argP2);
  694. }
  695. ++arg;
  696. }
  697. } while (reducible);
  698. return true;
  699. }