cmConditionEvaluator.cxx 27 KB

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