cmConditionEvaluator.cxx 27 KB

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