cmConditionEvaluator.cxx 27 KB

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