cmConditionEvaluator.cxx 27 KB

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