PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/shared/proparser/qmakeevaluator.cpp

https://bitbucket.org/kpozn/qt-creator-py-reborn
C++ | 1985 lines | 1831 code | 101 blank | 53 comment | 148 complexity | 08735c5bb8617eb03ef055f18e8430ab MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /**************************************************************************
  2. **
  3. ** This file is part of Qt Creator
  4. **
  5. ** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
  6. **
  7. ** Contact: Nokia Corporation (qt-info@nokia.com)
  8. **
  9. **
  10. ** GNU Lesser General Public License Usage
  11. **
  12. ** This file may be used under the terms of the GNU Lesser General Public
  13. ** License version 2.1 as published by the Free Software Foundation and
  14. ** appearing in the file LICENSE.LGPL included in the packaging of this file.
  15. ** Please review the following information to ensure the GNU Lesser General
  16. ** Public License version 2.1 requirements will be met:
  17. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  18. **
  19. ** In addition, as a special exception, Nokia gives you certain additional
  20. ** rights. These rights are described in the Nokia Qt LGPL Exception
  21. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  22. **
  23. ** Other Usage
  24. **
  25. ** Alternatively, this file may be used in accordance with the terms and
  26. ** conditions contained in a signed written agreement between you and Nokia.
  27. **
  28. ** If you have questions regarding the use of this file, please contact
  29. ** Nokia at qt-info@nokia.com.
  30. **
  31. **************************************************************************/
  32. #include "qmakeevaluator.h"
  33. #include "qmakeglobals.h"
  34. #include "qmakeparser.h"
  35. #include "qmakeevaluator_p.h"
  36. #include "ioutils.h"
  37. #include <QByteArray>
  38. #include <QDateTime>
  39. #include <QDebug>
  40. #include <QDir>
  41. #include <QFile>
  42. #include <QFileInfo>
  43. #include <QList>
  44. #include <QRegExp>
  45. #include <QSet>
  46. #include <QStack>
  47. #include <QString>
  48. #include <QStringList>
  49. #ifdef PROEVALUATOR_THREAD_SAFE
  50. # include <QThreadPool>
  51. #endif
  52. #ifdef Q_OS_UNIX
  53. #include <unistd.h>
  54. #include <sys/utsname.h>
  55. #else
  56. #include <Windows.h>
  57. #endif
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. using namespace ProFileEvaluatorInternal;
  61. QT_BEGIN_NAMESPACE
  62. using namespace ProStringConstants;
  63. #define fL1S(s) QString::fromLatin1(s)
  64. QMakeBaseEnv::QMakeBaseEnv()
  65. : evaluator(0)
  66. {
  67. #ifdef PROEVALUATOR_THREAD_SAFE
  68. inProgress = false;
  69. #endif
  70. }
  71. QMakeBaseEnv::~QMakeBaseEnv()
  72. {
  73. delete evaluator;
  74. }
  75. namespace ProFileEvaluatorInternal {
  76. QMakeStatics statics;
  77. }
  78. void QMakeEvaluator::initStatics()
  79. {
  80. if (!statics.field_sep.isNull())
  81. return;
  82. statics.field_sep = QLatin1String(" ");
  83. statics.strtrue = QLatin1String("true");
  84. statics.strfalse = QLatin1String("false");
  85. statics.strCONFIG = ProString("CONFIG");
  86. statics.strARGS = ProString("ARGS");
  87. statics.strDot = QLatin1String(".");
  88. statics.strDotDot = QLatin1String("..");
  89. statics.strever = QLatin1String("ever");
  90. statics.strforever = QLatin1String("forever");
  91. statics.strTEMPLATE = ProString("TEMPLATE");
  92. statics.fakeValue = ProStringList(ProString("_FAKE_")); // It has to have a unique begin() value
  93. initFunctionStatics();
  94. static const struct {
  95. const char * const oldname, * const newname;
  96. } mapInits[] = {
  97. { "INTERFACES", "FORMS" },
  98. { "QMAKE_POST_BUILD", "QMAKE_POST_LINK" },
  99. { "TARGETDEPS", "POST_TARGETDEPS" },
  100. { "LIBPATH", "QMAKE_LIBDIR" },
  101. { "QMAKE_EXT_MOC", "QMAKE_EXT_CPP_MOC" },
  102. { "QMAKE_MOD_MOC", "QMAKE_H_MOD_MOC" },
  103. { "QMAKE_LFLAGS_SHAPP", "QMAKE_LFLAGS_APP" },
  104. { "PRECOMPH", "PRECOMPILED_HEADER" },
  105. { "PRECOMPCPP", "PRECOMPILED_SOURCE" },
  106. { "INCPATH", "INCLUDEPATH" },
  107. { "QMAKE_EXTRA_WIN_COMPILERS", "QMAKE_EXTRA_COMPILERS" },
  108. { "QMAKE_EXTRA_UNIX_COMPILERS", "QMAKE_EXTRA_COMPILERS" },
  109. { "QMAKE_EXTRA_WIN_TARGETS", "QMAKE_EXTRA_TARGETS" },
  110. { "QMAKE_EXTRA_UNIX_TARGETS", "QMAKE_EXTRA_TARGETS" },
  111. { "QMAKE_EXTRA_UNIX_INCLUDES", "QMAKE_EXTRA_INCLUDES" },
  112. { "QMAKE_EXTRA_UNIX_VARIABLES", "QMAKE_EXTRA_VARIABLES" },
  113. { "QMAKE_RPATH", "QMAKE_LFLAGS_RPATH" },
  114. { "QMAKE_FRAMEWORKDIR", "QMAKE_FRAMEWORKPATH" },
  115. { "QMAKE_FRAMEWORKDIR_FLAGS", "QMAKE_FRAMEWORKPATH_FLAGS" },
  116. { "IN_PWD", "PWD" }
  117. };
  118. for (unsigned i = 0; i < sizeof(mapInits)/sizeof(mapInits[0]); ++i)
  119. statics.varMap.insert(ProString(mapInits[i].oldname),
  120. ProString(mapInits[i].newname));
  121. }
  122. const ProString &QMakeEvaluator::map(const ProString &var)
  123. {
  124. QHash<ProString, ProString>::ConstIterator it = statics.varMap.constFind(var);
  125. return (it != statics.varMap.constEnd()) ? it.value() : var;
  126. }
  127. QMakeEvaluator::QMakeEvaluator(QMakeGlobals *option,
  128. QMakeParser *parser, QMakeHandler *handler)
  129. : m_option(option), m_parser(parser), m_handler(handler)
  130. {
  131. // So that single-threaded apps don't have to call initialize() for now.
  132. initStatics();
  133. // Configuration, more or less
  134. #ifdef PROEVALUATOR_CUMULATIVE
  135. m_cumulative = false;
  136. #endif
  137. // Evaluator state
  138. m_skipLevel = 0;
  139. m_loopLevel = 0;
  140. m_listCount = 0;
  141. m_valuemapStack.push(ProValueMap());
  142. }
  143. QMakeEvaluator::~QMakeEvaluator()
  144. {
  145. }
  146. void QMakeEvaluator::initFrom(const QMakeEvaluator &other)
  147. {
  148. Q_ASSERT_X(&other, "QMakeEvaluator::visitProFile", "Project not prepared");
  149. m_functionDefs = other.m_functionDefs;
  150. m_valuemapStack = other.m_valuemapStack;
  151. m_qmakespec = other.m_qmakespec;
  152. m_qmakespecFull = other.m_qmakespecFull;
  153. m_qmakespecName = other.m_qmakespecName;
  154. m_qmakepath = other.m_qmakepath;
  155. m_qmakefeatures = other.m_qmakefeatures;
  156. m_featureRoots = other.m_featureRoots;
  157. }
  158. //////// Evaluator tools /////////
  159. uint QMakeEvaluator::getBlockLen(const ushort *&tokPtr)
  160. {
  161. uint len = *tokPtr++;
  162. len |= (uint)*tokPtr++ << 16;
  163. return len;
  164. }
  165. ProString QMakeEvaluator::getStr(const ushort *&tokPtr)
  166. {
  167. uint len = *tokPtr++;
  168. ProString ret(m_current.pro->items(), tokPtr - m_current.pro->tokPtr(), len, NoHash);
  169. ret.setSource(m_current.pro);
  170. tokPtr += len;
  171. return ret;
  172. }
  173. ProString QMakeEvaluator::getHashStr(const ushort *&tokPtr)
  174. {
  175. uint hash = getBlockLen(tokPtr);
  176. uint len = *tokPtr++;
  177. ProString ret(m_current.pro->items(), tokPtr - m_current.pro->tokPtr(), len, hash);
  178. tokPtr += len;
  179. return ret;
  180. }
  181. void QMakeEvaluator::skipStr(const ushort *&tokPtr)
  182. {
  183. uint len = *tokPtr++;
  184. tokPtr += len;
  185. }
  186. void QMakeEvaluator::skipHashStr(const ushort *&tokPtr)
  187. {
  188. tokPtr += 2;
  189. uint len = *tokPtr++;
  190. tokPtr += len;
  191. }
  192. // FIXME: this should not build new strings for direct sections.
  193. // Note that the E_SPRINTF and E_LIST implementations rely on the deep copy.
  194. ProStringList QMakeEvaluator::split_value_list(const QString &vals, const ProFile *source)
  195. {
  196. QString build;
  197. ProStringList ret;
  198. QStack<char> quote;
  199. const ushort SPACE = ' ';
  200. const ushort LPAREN = '(';
  201. const ushort RPAREN = ')';
  202. const ushort SINGLEQUOTE = '\'';
  203. const ushort DOUBLEQUOTE = '"';
  204. const ushort BACKSLASH = '\\';
  205. if (!source)
  206. source = currentProFile();
  207. ushort unicode;
  208. const QChar *vals_data = vals.data();
  209. const int vals_len = vals.length();
  210. for (int x = 0, parens = 0; x < vals_len; x++) {
  211. unicode = vals_data[x].unicode();
  212. if (x != (int)vals_len-1 && unicode == BACKSLASH &&
  213. (vals_data[x+1].unicode() == SINGLEQUOTE || vals_data[x+1].unicode() == DOUBLEQUOTE)) {
  214. build += vals_data[x++]; //get that 'escape'
  215. } else if (!quote.isEmpty() && unicode == quote.top()) {
  216. quote.pop();
  217. } else if (unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE) {
  218. quote.push(unicode);
  219. } else if (unicode == RPAREN) {
  220. --parens;
  221. } else if (unicode == LPAREN) {
  222. ++parens;
  223. }
  224. if (!parens && quote.isEmpty() && vals_data[x] == SPACE) {
  225. ret << ProString(build, NoHash).setSource(source);
  226. build.clear();
  227. } else {
  228. build += vals_data[x];
  229. }
  230. }
  231. if (!build.isEmpty())
  232. ret << ProString(build, NoHash).setSource(source);
  233. return ret;
  234. }
  235. static void zipEmpty(ProStringList *value)
  236. {
  237. for (int i = value->size(); --i >= 0;)
  238. if (value->at(i).isEmpty())
  239. value->remove(i);
  240. }
  241. static void insertUnique(ProStringList *varlist, const ProStringList &value)
  242. {
  243. foreach (const ProString &str, value)
  244. if (!str.isEmpty() && !varlist->contains(str))
  245. varlist->append(str);
  246. }
  247. static void removeAll(ProStringList *varlist, const ProString &value)
  248. {
  249. for (int i = varlist->size(); --i >= 0; )
  250. if (varlist->at(i) == value)
  251. varlist->remove(i);
  252. }
  253. static void removeEach(ProStringList *varlist, const ProStringList &value)
  254. {
  255. foreach (const ProString &str, value)
  256. if (!str.isEmpty())
  257. removeAll(varlist, str);
  258. }
  259. static void replaceInList(ProStringList *varlist,
  260. const QRegExp &regexp, const QString &replace, bool global, QString &tmp)
  261. {
  262. for (ProStringList::Iterator varit = varlist->begin(); varit != varlist->end(); ) {
  263. QString val = varit->toQString(tmp);
  264. QString copy = val; // Force detach and have a reference value
  265. val.replace(regexp, replace);
  266. if (!val.isSharedWith(copy)) {
  267. if (val.isEmpty()) {
  268. varit = varlist->erase(varit);
  269. } else {
  270. (*varit).setValue(val, NoHash);
  271. ++varit;
  272. }
  273. if (!global)
  274. break;
  275. } else {
  276. ++varit;
  277. }
  278. }
  279. }
  280. // This is braindead, but we want qmake compat
  281. QString QMakeEvaluator::fixPathToLocalOS(const QString &str) const
  282. {
  283. QString string = m_option->expandEnvVars(str);
  284. if (string.length() > 2 && string.at(0).isLetter() && string.at(1) == QLatin1Char(':'))
  285. string[0] = string[0].toLower();
  286. #if defined(Q_OS_WIN32)
  287. string.replace(QLatin1Char('/'), QLatin1Char('\\'));
  288. #else
  289. string.replace(QLatin1Char('\\'), QLatin1Char('/'));
  290. #endif
  291. return string;
  292. }
  293. //////// Evaluator /////////
  294. static ALWAYS_INLINE void addStr(
  295. const ProString &str, ProStringList *ret, bool &pending, bool joined)
  296. {
  297. if (joined) {
  298. ret->last().append(str, &pending);
  299. } else {
  300. if (!pending) {
  301. pending = true;
  302. *ret << str;
  303. } else {
  304. ret->last().append(str);
  305. }
  306. }
  307. }
  308. static ALWAYS_INLINE void addStrList(
  309. const ProStringList &list, ushort tok, ProStringList *ret, bool &pending, bool joined)
  310. {
  311. if (!list.isEmpty()) {
  312. if (joined) {
  313. ret->last().append(list, &pending, !(tok & TokQuoted));
  314. } else {
  315. if (tok & TokQuoted) {
  316. if (!pending) {
  317. pending = true;
  318. *ret << ProString();
  319. }
  320. ret->last().append(list);
  321. } else {
  322. if (!pending) {
  323. // Another qmake bizzarity: if nothing is pending and the
  324. // first element is empty, it will be eaten
  325. if (!list.at(0).isEmpty()) {
  326. // The common case
  327. pending = true;
  328. *ret += list;
  329. return;
  330. }
  331. } else {
  332. ret->last().append(list.at(0));
  333. }
  334. // This is somewhat slow, but a corner case
  335. for (int j = 1; j < list.size(); ++j) {
  336. pending = true;
  337. *ret << list.at(j);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. void QMakeEvaluator::evaluateExpression(
  344. const ushort *&tokPtr, ProStringList *ret, bool joined)
  345. {
  346. if (joined)
  347. *ret << ProString();
  348. bool pending = false;
  349. forever {
  350. ushort tok = *tokPtr++;
  351. if (tok & TokNewStr)
  352. pending = false;
  353. ushort maskedTok = tok & TokMask;
  354. switch (maskedTok) {
  355. case TokLine:
  356. m_current.line = *tokPtr++;
  357. break;
  358. case TokLiteral:
  359. addStr(getStr(tokPtr), ret, pending, joined);
  360. break;
  361. case TokHashLiteral:
  362. addStr(getHashStr(tokPtr), ret, pending, joined);
  363. break;
  364. case TokVariable:
  365. addStrList(values(map(getHashStr(tokPtr))), tok, ret, pending, joined);
  366. break;
  367. case TokProperty:
  368. addStr(propertyValue(getHashStr(tokPtr)).setSource(currentProFile()),
  369. ret, pending, joined);
  370. break;
  371. case TokEnvVar:
  372. addStrList(split_value_list(m_option->getEnv(getStr(tokPtr).toQString(m_tmp1))),
  373. tok, ret, pending, joined);
  374. break;
  375. case TokFuncName: {
  376. ProString func = getHashStr(tokPtr);
  377. addStrList(evaluateExpandFunction(func, tokPtr), tok, ret, pending, joined);
  378. break; }
  379. default:
  380. tokPtr--;
  381. return;
  382. }
  383. }
  384. }
  385. void QMakeEvaluator::skipExpression(const ushort *&pTokPtr)
  386. {
  387. const ushort *tokPtr = pTokPtr;
  388. forever {
  389. ushort tok = *tokPtr++;
  390. switch (tok) {
  391. case TokLine:
  392. m_current.line = *tokPtr++;
  393. break;
  394. case TokValueTerminator:
  395. case TokFuncTerminator:
  396. pTokPtr = tokPtr;
  397. return;
  398. case TokArgSeparator:
  399. break;
  400. default:
  401. switch (tok & TokMask) {
  402. case TokLiteral:
  403. case TokEnvVar:
  404. skipStr(tokPtr);
  405. break;
  406. case TokHashLiteral:
  407. case TokVariable:
  408. case TokProperty:
  409. skipHashStr(tokPtr);
  410. break;
  411. case TokFuncName:
  412. skipHashStr(tokPtr);
  413. pTokPtr = tokPtr;
  414. skipExpression(pTokPtr);
  415. tokPtr = pTokPtr;
  416. break;
  417. default:
  418. Q_ASSERT_X(false, "skipExpression", "Unrecognized token");
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. QMakeEvaluator::VisitReturn QMakeEvaluator::visitProBlock(
  425. ProFile *pro, const ushort *tokPtr)
  426. {
  427. m_current.pro = pro;
  428. m_current.line = 0;
  429. return visitProBlock(tokPtr);
  430. }
  431. QMakeEvaluator::VisitReturn QMakeEvaluator::visitProBlock(
  432. const ushort *tokPtr)
  433. {
  434. ProStringList curr;
  435. bool okey = true, or_op = false, invert = false;
  436. uint blockLen;
  437. VisitReturn ret = ReturnTrue;
  438. while (ushort tok = *tokPtr++) {
  439. switch (tok) {
  440. case TokLine:
  441. m_current.line = *tokPtr++;
  442. continue;
  443. case TokAssign:
  444. case TokAppend:
  445. case TokAppendUnique:
  446. case TokRemove:
  447. case TokReplace:
  448. visitProVariable(tok, curr, tokPtr);
  449. curr.clear();
  450. continue;
  451. case TokBranch:
  452. blockLen = getBlockLen(tokPtr);
  453. if (m_cumulative) {
  454. if (!okey)
  455. m_skipLevel++;
  456. ret = blockLen ? visitProBlock(tokPtr) : ReturnTrue;
  457. tokPtr += blockLen;
  458. blockLen = getBlockLen(tokPtr);
  459. if (!okey)
  460. m_skipLevel--;
  461. else
  462. m_skipLevel++;
  463. if ((ret == ReturnTrue || ret == ReturnFalse) && blockLen)
  464. ret = visitProBlock(tokPtr);
  465. if (okey)
  466. m_skipLevel--;
  467. } else {
  468. if (okey)
  469. ret = blockLen ? visitProBlock(tokPtr) : ReturnTrue;
  470. tokPtr += blockLen;
  471. blockLen = getBlockLen(tokPtr);
  472. if (!okey)
  473. ret = blockLen ? visitProBlock(tokPtr) : ReturnTrue;
  474. }
  475. tokPtr += blockLen;
  476. okey = true, or_op = false; // force next evaluation
  477. break;
  478. case TokForLoop:
  479. if (m_cumulative) { // This is a no-win situation, so just pretend it's no loop
  480. skipHashStr(tokPtr);
  481. uint exprLen = getBlockLen(tokPtr);
  482. tokPtr += exprLen;
  483. blockLen = getBlockLen(tokPtr);
  484. ret = visitProBlock(tokPtr);
  485. } else if (okey != or_op) {
  486. const ProString &variable = getHashStr(tokPtr);
  487. uint exprLen = getBlockLen(tokPtr);
  488. const ushort *exprPtr = tokPtr;
  489. tokPtr += exprLen;
  490. blockLen = getBlockLen(tokPtr);
  491. ret = visitProLoop(variable, exprPtr, tokPtr);
  492. } else {
  493. skipHashStr(tokPtr);
  494. uint exprLen = getBlockLen(tokPtr);
  495. tokPtr += exprLen;
  496. blockLen = getBlockLen(tokPtr);
  497. ret = ReturnTrue;
  498. }
  499. tokPtr += blockLen;
  500. okey = true, or_op = false; // force next evaluation
  501. break;
  502. case TokTestDef:
  503. case TokReplaceDef:
  504. if (m_cumulative || okey != or_op) {
  505. const ProString &name = getHashStr(tokPtr);
  506. blockLen = getBlockLen(tokPtr);
  507. visitProFunctionDef(tok, name, tokPtr);
  508. } else {
  509. skipHashStr(tokPtr);
  510. blockLen = getBlockLen(tokPtr);
  511. }
  512. tokPtr += blockLen;
  513. okey = true, or_op = false; // force next evaluation
  514. continue;
  515. case TokNot:
  516. invert ^= true;
  517. continue;
  518. case TokAnd:
  519. or_op = false;
  520. continue;
  521. case TokOr:
  522. or_op = true;
  523. continue;
  524. case TokCondition:
  525. if (!m_skipLevel && okey != or_op) {
  526. if (curr.size() != 1) {
  527. if (!m_cumulative || !curr.isEmpty())
  528. evalError(fL1S("Conditional must expand to exactly one word."));
  529. okey = false;
  530. } else {
  531. okey = isActiveConfig(curr.at(0).toQString(m_tmp2), true) ^ invert;
  532. }
  533. }
  534. or_op = !okey; // tentatively force next evaluation
  535. invert = false;
  536. curr.clear();
  537. continue;
  538. case TokTestCall:
  539. if (!m_skipLevel && okey != or_op) {
  540. if (curr.size() != 1) {
  541. if (!m_cumulative || !curr.isEmpty())
  542. evalError(fL1S("Test name must expand to exactly one word."));
  543. skipExpression(tokPtr);
  544. okey = false;
  545. } else {
  546. ret = evaluateConditionalFunction(curr.at(0), tokPtr);
  547. switch (ret) {
  548. case ReturnTrue: okey = true; break;
  549. case ReturnFalse: okey = false; break;
  550. default: return ret;
  551. }
  552. okey ^= invert;
  553. }
  554. } else if (m_cumulative) {
  555. m_skipLevel++;
  556. if (curr.size() != 1)
  557. skipExpression(tokPtr);
  558. else
  559. evaluateConditionalFunction(curr.at(0), tokPtr);
  560. m_skipLevel--;
  561. } else {
  562. skipExpression(tokPtr);
  563. }
  564. or_op = !okey; // tentatively force next evaluation
  565. invert = false;
  566. curr.clear();
  567. continue;
  568. default: {
  569. const ushort *oTokPtr = --tokPtr;
  570. evaluateExpression(tokPtr, &curr, false);
  571. if (tokPtr != oTokPtr)
  572. continue;
  573. }
  574. Q_ASSERT_X(false, "visitProBlock", "unexpected item type");
  575. }
  576. if (ret != ReturnTrue && ret != ReturnFalse)
  577. break;
  578. }
  579. return ret;
  580. }
  581. void QMakeEvaluator::visitProFunctionDef(
  582. ushort tok, const ProString &name, const ushort *tokPtr)
  583. {
  584. QHash<ProString, ProFunctionDef> *hash =
  585. (tok == TokTestDef
  586. ? &m_functionDefs.testFunctions
  587. : &m_functionDefs.replaceFunctions);
  588. hash->insert(name, ProFunctionDef(m_current.pro, tokPtr - m_current.pro->tokPtr()));
  589. }
  590. QMakeEvaluator::VisitReturn QMakeEvaluator::visitProLoop(
  591. const ProString &_variable, const ushort *exprPtr, const ushort *tokPtr)
  592. {
  593. VisitReturn ret = ReturnTrue;
  594. bool infinite = false;
  595. int index = 0;
  596. ProString variable;
  597. ProStringList oldVarVal;
  598. ProString it_list = expandVariableReferences(exprPtr, 0, true).at(0);
  599. if (_variable.isEmpty()) {
  600. if (it_list != statics.strever) {
  601. evalError(fL1S("Invalid loop expression."));
  602. return ReturnFalse;
  603. }
  604. it_list = ProString(statics.strforever);
  605. } else {
  606. variable = map(_variable);
  607. oldVarVal = values(variable);
  608. }
  609. ProStringList list = values(it_list);
  610. if (list.isEmpty()) {
  611. if (it_list == statics.strforever) {
  612. infinite = true;
  613. } else {
  614. const QString &itl = it_list.toQString(m_tmp1);
  615. int dotdot = itl.indexOf(statics.strDotDot);
  616. if (dotdot != -1) {
  617. bool ok;
  618. int start = itl.left(dotdot).toInt(&ok);
  619. if (ok) {
  620. int end = itl.mid(dotdot+2).toInt(&ok);
  621. if (ok) {
  622. if (start < end) {
  623. for (int i = start; i <= end; i++)
  624. list << ProString(QString::number(i), NoHash);
  625. } else {
  626. for (int i = start; i >= end; i--)
  627. list << ProString(QString::number(i), NoHash);
  628. }
  629. }
  630. }
  631. }
  632. }
  633. }
  634. m_loopLevel++;
  635. forever {
  636. if (infinite) {
  637. if (!variable.isEmpty())
  638. m_valuemapStack.top()[variable] = ProStringList(ProString(QString::number(index++), NoHash));
  639. if (index > 1000) {
  640. evalError(fL1S("ran into infinite loop (> 1000 iterations)."));
  641. break;
  642. }
  643. } else {
  644. ProString val;
  645. do {
  646. if (index >= list.count())
  647. goto do_break;
  648. val = list.at(index++);
  649. } while (val.isEmpty()); // stupid, but qmake is like that
  650. m_valuemapStack.top()[variable] = ProStringList(val);
  651. }
  652. ret = visitProBlock(tokPtr);
  653. switch (ret) {
  654. case ReturnTrue:
  655. case ReturnFalse:
  656. break;
  657. case ReturnNext:
  658. ret = ReturnTrue;
  659. break;
  660. case ReturnBreak:
  661. ret = ReturnTrue;
  662. goto do_break;
  663. default:
  664. goto do_break;
  665. }
  666. }
  667. do_break:
  668. m_loopLevel--;
  669. if (!variable.isEmpty())
  670. m_valuemapStack.top()[variable] = oldVarVal;
  671. return ret;
  672. }
  673. void QMakeEvaluator::visitProVariable(
  674. ushort tok, const ProStringList &curr, const ushort *&tokPtr)
  675. {
  676. int sizeHint = *tokPtr++;
  677. if (curr.size() != 1) {
  678. skipExpression(tokPtr);
  679. if (!m_cumulative || !curr.isEmpty())
  680. evalError(fL1S("Left hand side of assignment must expand to exactly one word."));
  681. return;
  682. }
  683. const ProString &varName = map(curr.first());
  684. if (tok == TokReplace) { // ~=
  685. // DEFINES ~= s/a/b/?[gqi]
  686. const ProStringList &varVal = expandVariableReferences(tokPtr, sizeHint, true);
  687. const QString &val = varVal.at(0).toQString(m_tmp1);
  688. if (val.length() < 4 || val.at(0) != QLatin1Char('s')) {
  689. evalError(fL1S("the ~= operator can handle only the s/// function."));
  690. return;
  691. }
  692. QChar sep = val.at(1);
  693. QStringList func = val.split(sep);
  694. if (func.count() < 3 || func.count() > 4) {
  695. evalError(fL1S("the s/// function expects 3 or 4 arguments."));
  696. return;
  697. }
  698. bool global = false, quote = false, case_sense = false;
  699. if (func.count() == 4) {
  700. global = func[3].indexOf(QLatin1Char('g')) != -1;
  701. case_sense = func[3].indexOf(QLatin1Char('i')) == -1;
  702. quote = func[3].indexOf(QLatin1Char('q')) != -1;
  703. }
  704. QString pattern = func[1];
  705. QString replace = func[2];
  706. if (quote)
  707. pattern = QRegExp::escape(pattern);
  708. QRegExp regexp(pattern, case_sense ? Qt::CaseSensitive : Qt::CaseInsensitive);
  709. if (!m_skipLevel || m_cumulative) {
  710. // We could make a union of modified and unmodified values,
  711. // but this will break just as much as it fixes, so leave it as is.
  712. replaceInList(&valuesRef(varName), regexp, replace, global, m_tmp2);
  713. }
  714. } else {
  715. ProStringList varVal = expandVariableReferences(tokPtr, sizeHint);
  716. switch (tok) {
  717. default: // whatever - cannot happen
  718. case TokAssign: // =
  719. if (!m_cumulative) {
  720. if (!m_skipLevel) {
  721. zipEmpty(&varVal);
  722. m_valuemapStack.top()[varName] = varVal;
  723. }
  724. } else {
  725. zipEmpty(&varVal);
  726. if (!varVal.isEmpty()) {
  727. // We are greedy for values. But avoid exponential growth.
  728. ProStringList &v = valuesRef(varName);
  729. if (v.isEmpty()) {
  730. v = varVal;
  731. } else {
  732. ProStringList old = v;
  733. v = varVal;
  734. QSet<ProString> has;
  735. has.reserve(v.size());
  736. foreach (const ProString &s, v)
  737. has.insert(s);
  738. v.reserve(v.size() + old.size());
  739. foreach (const ProString &s, old)
  740. if (!has.contains(s))
  741. v << s;
  742. }
  743. }
  744. }
  745. break;
  746. case TokAppendUnique: // *=
  747. if (!m_skipLevel || m_cumulative)
  748. insertUnique(&valuesRef(varName), varVal);
  749. break;
  750. case TokAppend: // +=
  751. if (!m_skipLevel || m_cumulative) {
  752. zipEmpty(&varVal);
  753. valuesRef(varName) += varVal;
  754. }
  755. break;
  756. case TokRemove: // -=
  757. if (!m_cumulative) {
  758. if (!m_skipLevel)
  759. removeEach(&valuesRef(varName), varVal);
  760. } else {
  761. // We are stingy with our values, too.
  762. }
  763. break;
  764. }
  765. }
  766. if (varName == statics.strTEMPLATE)
  767. setTemplate();
  768. }
  769. void QMakeEvaluator::setTemplate()
  770. {
  771. ProStringList &values = valuesRef(statics.strTEMPLATE);
  772. if (!m_option->user_template.isEmpty()) {
  773. // Don't allow override
  774. values = ProStringList(ProString(m_option->user_template, NoHash));
  775. } else {
  776. if (values.isEmpty())
  777. values.append(ProString("app", NoHash));
  778. else
  779. values.erase(values.begin() + 1, values.end());
  780. }
  781. if (!m_option->user_template_prefix.isEmpty()) {
  782. QString val = values.first().toQString(m_tmp1);
  783. if (!val.startsWith(m_option->user_template_prefix)) {
  784. val.prepend(m_option->user_template_prefix);
  785. values = ProStringList(ProString(val, NoHash));
  786. }
  787. }
  788. }
  789. void QMakeEvaluator::loadDefaults()
  790. {
  791. ProValueMap &vars = m_valuemapStack.top();
  792. vars[ProString("LITERAL_WHITESPACE")] << ProString("\t", NoHash);
  793. vars[ProString("LITERAL_DOLLAR")] << ProString("$", NoHash);
  794. vars[ProString("LITERAL_HASH")] << ProString("#", NoHash);
  795. vars[ProString("DIR_SEPARATOR")] << ProString(m_option->dir_sep, NoHash);
  796. vars[ProString("DIRLIST_SEPARATOR")] << ProString(m_option->dirlist_sep, NoHash);
  797. vars[ProString("_DATE_")] << ProString(QDateTime::currentDateTime().toString(), NoHash);
  798. if (!m_option->qmake_abslocation.isEmpty())
  799. vars[ProString("QMAKE_QMAKE")] << ProString(m_option->qmake_abslocation, NoHash);
  800. #if defined(Q_OS_WIN32)
  801. vars[ProString("QMAKE_HOST.os")] << ProString("Windows", NoHash);
  802. DWORD name_length = 1024;
  803. wchar_t name[1024];
  804. if (GetComputerName(name, &name_length))
  805. vars[ProString("QMAKE_HOST.name")] << ProString(QString::fromWCharArray(name), NoHash);
  806. QSysInfo::WinVersion ver = QSysInfo::WindowsVersion;
  807. vars[ProString("QMAKE_HOST.version")] << ProString(QString::number(ver), NoHash);
  808. ProString verStr;
  809. switch (ver) {
  810. case QSysInfo::WV_Me: verStr = ProString("WinMe", NoHash); break;
  811. case QSysInfo::WV_95: verStr = ProString("Win95", NoHash); break;
  812. case QSysInfo::WV_98: verStr = ProString("Win98", NoHash); break;
  813. case QSysInfo::WV_NT: verStr = ProString("WinNT", NoHash); break;
  814. case QSysInfo::WV_2000: verStr = ProString("Win2000", NoHash); break;
  815. case QSysInfo::WV_2003: verStr = ProString("Win2003", NoHash); break;
  816. case QSysInfo::WV_XP: verStr = ProString("WinXP", NoHash); break;
  817. case QSysInfo::WV_VISTA: verStr = ProString("WinVista", NoHash); break;
  818. default: verStr = ProString("Unknown", NoHash); break;
  819. }
  820. vars[ProString("QMAKE_HOST.version_string")] << verStr;
  821. SYSTEM_INFO info;
  822. GetSystemInfo(&info);
  823. ProString archStr;
  824. switch (info.wProcessorArchitecture) {
  825. # ifdef PROCESSOR_ARCHITECTURE_AMD64
  826. case PROCESSOR_ARCHITECTURE_AMD64:
  827. archStr = ProString("x86_64", NoHash);
  828. break;
  829. # endif
  830. case PROCESSOR_ARCHITECTURE_INTEL:
  831. archStr = ProString("x86", NoHash);
  832. break;
  833. case PROCESSOR_ARCHITECTURE_IA64:
  834. # ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
  835. case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
  836. # endif
  837. archStr = ProString("IA64", NoHash);
  838. break;
  839. default:
  840. archStr = ProString("Unknown", NoHash);
  841. break;
  842. }
  843. vars[ProString("QMAKE_HOST.arch")] << archStr;
  844. # if defined(Q_CC_MSVC) // ### bogus condition, but nobody x-builds for msvc with a different qmake
  845. QLatin1Char backslash('\\');
  846. QString paths = m_option->getEnv(QLatin1String("PATH"));
  847. QString vcBin64 = m_option->getEnv(QLatin1String("VCINSTALLDIR"));
  848. if (!vcBin64.endsWith(backslash))
  849. vcBin64.append(backslash);
  850. vcBin64.append(QLatin1String("bin\\amd64"));
  851. QString vcBinX86_64 = m_option->getEnv(QLatin1String("VCINSTALLDIR"));
  852. if (!vcBinX86_64.endsWith(backslash))
  853. vcBinX86_64.append(backslash);
  854. vcBinX86_64.append(QLatin1String("bin\\x86_amd64"));
  855. if (paths.contains(vcBin64, Qt::CaseInsensitive)
  856. || paths.contains(vcBinX86_64, Qt::CaseInsensitive))
  857. vars[ProString("QMAKE_TARGET.arch")] << ProString("x86_64", NoHash);
  858. else
  859. vars[ProString("QMAKE_TARGET.arch")] << ProString("x86", NoHash);
  860. # endif
  861. #elif defined(Q_OS_UNIX)
  862. struct utsname name;
  863. if (!uname(&name)) {
  864. vars[ProString("QMAKE_HOST.os")] << ProString(name.sysname, NoHash);
  865. vars[ProString("QMAKE_HOST.name")] << ProString(QString::fromLocal8Bit(name.nodename), NoHash);
  866. vars[ProString("QMAKE_HOST.version")] << ProString(name.release, NoHash);
  867. vars[ProString("QMAKE_HOST.version_string")] << ProString(name.version, NoHash);
  868. vars[ProString("QMAKE_HOST.arch")] << ProString(name.machine, NoHash);
  869. }
  870. #endif
  871. }
  872. bool QMakeEvaluator::prepareProject(const QString &inDir)
  873. {
  874. QString superdir;
  875. if (m_option->do_cache) {
  876. QString conffile;
  877. QString cachefile = m_option->cachefile;
  878. if (cachefile.isEmpty()) { //find it as it has not been specified
  879. if (m_outputDir.isEmpty())
  880. goto no_cache;
  881. superdir = m_outputDir;
  882. forever {
  883. QString superfile = superdir + QLatin1String("/.qmake.super");
  884. if (IoUtils::exists(superfile)) {
  885. m_superfile = superfile;
  886. break;
  887. }
  888. QFileInfo qdfi(superdir);
  889. if (qdfi.isRoot()) {
  890. superdir.clear();
  891. break;
  892. }
  893. superdir = qdfi.path();
  894. }
  895. QString sdir = inDir;
  896. QString dir = m_outputDir;
  897. forever {
  898. conffile = sdir + QLatin1String("/.qmake.conf");
  899. if (!IoUtils::exists(conffile))
  900. conffile.clear();
  901. cachefile = dir + QLatin1String("/.qmake.cache");
  902. if (!IoUtils::exists(cachefile))
  903. cachefile.clear();
  904. if (!conffile.isEmpty() || !cachefile.isEmpty()) {
  905. m_sourceRoot = sdir;
  906. m_buildRoot = dir;
  907. break;
  908. }
  909. if (dir == superdir)
  910. goto no_cache;
  911. QFileInfo qsdfi(sdir);
  912. QFileInfo qdfi(dir);
  913. if (qsdfi.isRoot() || qdfi.isRoot())
  914. goto no_cache;
  915. sdir = qsdfi.path();
  916. dir = qdfi.path();
  917. }
  918. } else {
  919. m_buildRoot = QFileInfo(cachefile).path();
  920. }
  921. m_conffile = conffile;
  922. m_cachefile = cachefile;
  923. }
  924. no_cache:
  925. // Look for mkspecs/ in source and build. First to win determines the root.
  926. QString sdir = inDir;
  927. QString dir = m_outputDir;
  928. while (dir != m_buildRoot) {
  929. if ((dir != sdir && QFileInfo(sdir, QLatin1String("mkspecs")).isDir())
  930. || QFileInfo(dir, QLatin1String("mkspecs")).isDir()) {
  931. if (dir != sdir)
  932. m_sourceRoot = sdir;
  933. m_buildRoot = dir;
  934. break;
  935. }
  936. if (dir == superdir)
  937. break;
  938. QFileInfo qsdfi(sdir);
  939. QFileInfo qdfi(dir);
  940. if (qsdfi.isRoot() || qdfi.isRoot())
  941. break;
  942. sdir = qsdfi.path();
  943. dir = qdfi.path();
  944. }
  945. return true;
  946. }
  947. bool QMakeEvaluator::loadSpec()
  948. {
  949. loadDefaults();
  950. QString qmakespec = m_option->expandEnvVars(m_option->qmakespec);
  951. {
  952. QMakeEvaluator evaluator(m_option, m_parser, m_handler);
  953. if (!m_superfile.isEmpty()) {
  954. valuesRef(ProString("_QMAKE_SUPER_CACHE_")) << ProString(m_superfile, NoHash);
  955. if (!evaluator.evaluateFileDirect(m_superfile, QMakeHandler::EvalConfigFile, LoadProOnly))
  956. return false;
  957. }
  958. if (!m_conffile.isEmpty()) {
  959. valuesRef(ProString("_QMAKE_CONF_")) << ProString(m_conffile, NoHash);
  960. if (!evaluator.evaluateFileDirect(m_conffile, QMakeHandler::EvalConfigFile, LoadProOnly))
  961. return false;
  962. }
  963. if (!m_cachefile.isEmpty()) {
  964. valuesRef(ProString("_QMAKE_CACHE_")) << ProString(m_cachefile, NoHash);
  965. if (!evaluator.evaluateFileDirect(m_cachefile, QMakeHandler::EvalConfigFile, LoadProOnly))
  966. return false;
  967. }
  968. if (qmakespec.isEmpty())
  969. qmakespec = evaluator.first(ProString("QMAKESPEC")).toQString();
  970. m_qmakepath = evaluator.values(ProString("QMAKEPATH")).toQStringList();
  971. m_qmakefeatures = evaluator.values(ProString("QMAKEFEATURES")).toQStringList();
  972. }
  973. if (qmakespec.isEmpty())
  974. qmakespec = QLatin1String("default");
  975. if (IoUtils::isRelativePath(qmakespec)) {
  976. foreach (const QString &root, qmakeMkspecPaths()) {
  977. QString mkspec = root + QLatin1Char('/') + qmakespec;
  978. if (IoUtils::exists(mkspec)) {
  979. qmakespec = mkspec;
  980. goto cool;
  981. }
  982. }
  983. m_handler->configError(fL1S("Could not find qmake configuration file"));
  984. return false;
  985. }
  986. cool:
  987. m_qmakespec = QDir::cleanPath(qmakespec);
  988. if (!m_superfile.isEmpty()
  989. && !evaluateFileDirect(m_superfile, QMakeHandler::EvalConfigFile, LoadProOnly)) {
  990. return false;
  991. }
  992. if (!evaluateFeatureFile(QLatin1String("spec_pre.prf")))
  993. return false;
  994. QString spec = m_qmakespec + QLatin1String("/qmake.conf");
  995. if (!evaluateFileDirect(spec, QMakeHandler::EvalConfigFile, LoadProOnly)) {
  996. m_handler->configError(
  997. fL1S("Could not read qmake configuration file %1").arg(spec));
  998. return false;
  999. }
  1000. #ifdef Q_OS_UNIX
  1001. m_qmakespecFull = QFileInfo(m_qmakespec).canonicalFilePath();
  1002. #else
  1003. // We can't resolve symlinks as they do on Unix, so configure.exe puts
  1004. // the source of the qmake.conf at the end of the default/qmake.conf in
  1005. // the QMAKESPEC_ORIGINAL variable.
  1006. const ProString &orig_spec = first(ProString("QMAKESPEC_ORIGINAL"));
  1007. m_qmakespecFull = orig_spec.isEmpty() ? m_qmakespec : orig_spec.toQString();
  1008. #endif
  1009. valuesRef(ProString("QMAKESPEC")) << ProString(m_qmakespecFull, NoHash);
  1010. m_qmakespecName = IoUtils::fileName(m_qmakespecFull).toString();
  1011. if (!evaluateFeatureFile(QLatin1String("spec_post.prf")))
  1012. return false;
  1013. // The spec extends the feature search path, so invalidate the cache.
  1014. m_featureRoots.clear();
  1015. if (!m_conffile.isEmpty()
  1016. && !evaluateFileDirect(m_conffile, QMakeHandler::EvalConfigFile, LoadProOnly)) {
  1017. return false;
  1018. }
  1019. if (!m_cachefile.isEmpty()
  1020. && !evaluateFileDirect(m_cachefile, QMakeHandler::EvalConfigFile, LoadProOnly)) {
  1021. return false;
  1022. }
  1023. return true;
  1024. }
  1025. void QMakeEvaluator::setupProject()
  1026. {
  1027. setTemplate();
  1028. ProValueMap &vars = m_valuemapStack.top();
  1029. vars[ProString("TARGET")] << ProString(QFileInfo(currentFileName()).baseName(), NoHash);
  1030. vars[ProString("_PRO_FILE_")] << ProString(currentFileName(), NoHash);
  1031. vars[ProString("_PRO_FILE_PWD_")] << ProString(currentDirectory(), NoHash);
  1032. vars[ProString("OUT_PWD")] << ProString(m_outputDir, NoHash);
  1033. }
  1034. void QMakeEvaluator::visitCmdLine(const QString &cmds)
  1035. {
  1036. if (!cmds.isEmpty()) {
  1037. if (ProFile *pro = m_parser->parsedProBlock(fL1S("(command line)"), cmds)) {
  1038. m_locationStack.push(m_current);
  1039. visitProBlock(pro, pro->tokPtr());
  1040. m_current = m_locationStack.pop();
  1041. pro->deref();
  1042. }
  1043. }
  1044. }
  1045. QMakeEvaluator::VisitReturn QMakeEvaluator::visitProFile(
  1046. ProFile *pro, QMakeHandler::EvalFileType type, LoadFlags flags)
  1047. {
  1048. if (!m_cumulative && !pro->isOk())
  1049. return ReturnFalse;
  1050. if (flags & LoadPreFiles) {
  1051. if (!prepareProject(pro->directoryName()))
  1052. return ReturnFalse;
  1053. #ifdef PROEVALUATOR_THREAD_SAFE
  1054. m_option->mutex.lock();
  1055. #endif
  1056. QMakeBaseEnv **baseEnvPtr = &m_option->baseEnvs[QMakeBaseKey(m_buildRoot)];
  1057. if (!*baseEnvPtr)
  1058. *baseEnvPtr = new QMakeBaseEnv;
  1059. QMakeBaseEnv *baseEnv = *baseEnvPtr;
  1060. #ifdef PROEVALUATOR_THREAD_SAFE
  1061. {
  1062. QMutexLocker locker(&baseEnv->mutex);
  1063. m_option->mutex.unlock();
  1064. if (baseEnv->inProgress) {
  1065. QThreadPool::globalInstance()->releaseThread();
  1066. baseEnv->cond.wait(&baseEnv->mutex);
  1067. QThreadPool::globalInstance()->reserveThread();
  1068. if (!baseEnv->isOk)
  1069. return ReturnFalse;
  1070. } else
  1071. #endif
  1072. if (!baseEnv->evaluator) {
  1073. #ifdef PROEVALUATOR_THREAD_SAFE
  1074. baseEnv->inProgress = true;
  1075. locker.unlock();
  1076. #endif
  1077. QMakeEvaluator *baseEval = new QMakeEvaluator(m_option, m_parser, m_handler);
  1078. baseEnv->evaluator = baseEval;
  1079. baseEval->m_superfile = m_superfile;
  1080. baseEval->m_conffile = m_conffile;
  1081. baseEval->m_cachefile = m_cachefile;
  1082. baseEval->m_sourceRoot = m_sourceRoot;
  1083. baseEval->m_buildRoot = m_buildRoot;
  1084. bool ok = baseEval->loadSpec();
  1085. #ifdef PROEVALUATOR_THREAD_SAFE
  1086. locker.relock();
  1087. baseEnv->isOk = ok;
  1088. baseEnv->inProgress = false;
  1089. baseEnv->cond.wakeAll();
  1090. #endif
  1091. if (!ok)
  1092. return ReturnFalse;
  1093. }
  1094. #ifdef PROEVALUATOR_THREAD_SAFE
  1095. }
  1096. #endif
  1097. initFrom(*baseEnv->evaluator);
  1098. }
  1099. m_handler->aboutToEval(currentProFile(), pro, type);
  1100. m_profileStack.push(pro);
  1101. valuesRef(ProString("PWD")) = ProStringList(ProString(currentDirectory(), NoHash));
  1102. if (flags & LoadPreFiles) {
  1103. setupProject();
  1104. evaluateFeatureFile(QLatin1String("default_pre.prf"));
  1105. visitCmdLine(m_option->precmds);
  1106. }
  1107. visitProBlock(pro, pro->tokPtr());
  1108. if (flags & LoadPostFiles) {
  1109. visitCmdLine(m_option->postcmds);
  1110. evaluateFeatureFile(QLatin1String("default_post.prf"));
  1111. QSet<QString> processed;
  1112. forever {
  1113. bool finished = true;
  1114. ProStringList configs = values(statics.strCONFIG);
  1115. for (int i = configs.size() - 1; i >= 0; --i) {
  1116. QString config = configs.at(i).toQString(m_tmp1).toLower();
  1117. if (!processed.contains(config)) {
  1118. config.detach();
  1119. processed.insert(config);
  1120. if (evaluateFeatureFile(config)) {
  1121. finished = false;
  1122. break;
  1123. }
  1124. }
  1125. }
  1126. if (finished)
  1127. break;
  1128. }
  1129. }
  1130. m_profileStack.pop();
  1131. valuesRef(ProString("PWD")) = ProStringList(ProString(currentDirectory(), NoHash));
  1132. m_handler->doneWithEval(currentProFile());
  1133. return ReturnTrue;
  1134. }
  1135. QStringList QMakeEvaluator::qmakeMkspecPaths() const
  1136. {
  1137. QStringList ret;
  1138. const QString concat = QLatin1String("/mkspecs");
  1139. foreach (const QString &it, m_option->getPathListEnv(QLatin1String("QMAKEPATH")))
  1140. ret << it + concat;
  1141. foreach (const QString &it, m_qmakepath)
  1142. ret << it + concat;
  1143. if (!m_buildRoot.isEmpty())
  1144. ret << m_buildRoot + concat;
  1145. if (!m_sourceRoot.isEmpty())
  1146. ret << m_sourceRoot + concat;
  1147. ret << m_option->propertyValue(ProString("QT_HOST_DATA")) + concat;
  1148. ret.removeDuplicates();
  1149. return ret;
  1150. }
  1151. QStringList QMakeEvaluator::qmakeFeaturePaths() const
  1152. {
  1153. QString mkspecs_concat = QLatin1String("/mkspecs");
  1154. QString features_concat = QLatin1String("/features/");
  1155. QStringList feature_roots;
  1156. foreach (const QString &f, m_option->getPathListEnv(QLatin1String("QMAKEFEATURES")))
  1157. feature_roots += f;
  1158. feature_roots += m_qmakefeatures;
  1159. feature_roots += m_option->propertyValue(ProString("QMAKEFEATURES")).toQString(m_mtmp).split(
  1160. m_option->dirlist_sep, QString::SkipEmptyParts);
  1161. QStringList feature_bases;
  1162. if (!m_buildRoot.isEmpty())
  1163. feature_bases << m_buildRoot;
  1164. if (!m_sourceRoot.isEmpty())
  1165. feature_bases << m_sourceRoot;
  1166. foreach (const QString &item, m_option->getPathListEnv(QLatin1String("QMAKEPATH")))
  1167. feature_bases << (item + mkspecs_concat);
  1168. foreach (const QString &item, m_qmakepath)
  1169. feature_bases << (item + mkspecs_concat);
  1170. if (!m_qmakespecFull.isEmpty()) {
  1171. // The spec is already platform-dependent, so no subdirs here.
  1172. feature_roots << (m_qmakespecFull + features_concat);
  1173. // Also check directly under the root directory of the mkspecs collection
  1174. QDir specdir(m_qmakespecFull);
  1175. while (!specdir.isRoot() && specdir.cdUp()) {
  1176. const QString specpath = specdir.path();
  1177. if (specpath.endsWith(mkspecs_concat)) {
  1178. if (IoUtils::exists(specpath + features_concat))
  1179. feature_bases << specpath;
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. feature_bases << (m_option->propertyValue(ProString("QT_HOST_DATA")).toQString(m_mtmp)
  1185. + mkspecs_concat);
  1186. foreach (const QString &fb, feature_bases) {
  1187. foreach (const ProString &sfx, values(ProString("QMAKE_PLATFORM")))
  1188. feature_roots << (fb + features_concat + sfx + QLatin1Char('/'));
  1189. feature_roots << (fb + features_concat);
  1190. }
  1191. for (int i = 0; i < feature_roots.count(); ++i)
  1192. if (!feature_roots.at(i).endsWith((ushort)'/'))
  1193. feature_roots[i].append((ushort)'/');
  1194. feature_roots.removeDuplicates();
  1195. QStringList ret;
  1196. foreach (const QString &root, feature_roots)
  1197. if (IoUtils::exists(root))
  1198. ret << root;
  1199. return ret;
  1200. }
  1201. ProString QMakeEvaluator::propertyValue(const ProString &name) const
  1202. {
  1203. if (name == QLatin1String("QMAKE_MKSPECS"))
  1204. return ProString(qmakeMkspecPaths().join(m_option->dirlist_sep), NoHash);
  1205. ProString ret = m_option->propertyValue(name);
  1206. if (ret.isNull())
  1207. evalError(fL1S("Querying unknown property %1").arg(name.toQString(m_mtmp)));
  1208. return ret;
  1209. }
  1210. ProFile *QMakeEvaluator::currentProFile() const
  1211. {
  1212. if (m_profileStack.count() > 0)
  1213. return m_profileStack.top();
  1214. return 0;
  1215. }
  1216. QString QMakeEvaluator::currentFileName() const
  1217. {
  1218. ProFile *pro = currentProFile();
  1219. if (pro)
  1220. return pro->fileName();
  1221. return QString();
  1222. }
  1223. QString QMakeEvaluator::currentDirectory() const
  1224. {
  1225. ProFile *pro = currentProFile();
  1226. if (pro)
  1227. return pro->directoryName();
  1228. return QString();
  1229. }
  1230. // The (QChar*)current->constData() constructs below avoid pointless detach() calls
  1231. // FIXME: This is inefficient. Should not make new string if it is a straight subsegment
  1232. static ALWAYS_INLINE void appendChar(ushort unicode,
  1233. QString *current, QChar **ptr, ProString *pending)
  1234. {
  1235. if (!pending->isEmpty()) {
  1236. int len = pending->size();
  1237. current->resize(current->size() + len);
  1238. ::memcpy((QChar*)current->constData(), pending->constData(), len * 2);
  1239. pending->clear();
  1240. *ptr = (QChar*)current->constData() + len;
  1241. }
  1242. *(*ptr)++ = QChar(unicode);
  1243. }
  1244. static void appendString(const ProString &string,
  1245. QString *current, QChar **ptr, ProString *pending)
  1246. {
  1247. if (string.isEmpty())
  1248. return;
  1249. QChar *uc = (QChar*)current->constData();
  1250. int len;
  1251. if (*ptr != uc) {
  1252. len = *ptr - uc;
  1253. current->resize(current->size() + string.size());
  1254. } else if (!pending->isEmpty()) {
  1255. len = pending->size();
  1256. current->resize(current->size() + len + string.size());
  1257. ::memcpy((QChar*)current->constData(), pending->constData(), len * 2);
  1258. pending->clear();
  1259. } else {
  1260. *pending = string;
  1261. return;
  1262. }
  1263. *ptr = (QChar*)current->constData() + len;
  1264. ::memcpy(*ptr, string.constData(), string.size() * 2);
  1265. *ptr += string.size();
  1266. }
  1267. static void flushCurrent(ProStringList *ret,
  1268. QString *current, QChar **ptr, ProString *pending, bool joined)
  1269. {
  1270. QChar *uc = (QChar*)current->constData();
  1271. int len = *ptr - uc;
  1272. if (len) {
  1273. ret->append(ProString(QString(uc, len), NoHash));
  1274. *ptr = uc;
  1275. } else if (!pending->isEmpty()) {
  1276. ret->append(*pending);
  1277. pending->clear();
  1278. } else if (joined) {
  1279. ret->append(ProString());
  1280. }
  1281. }
  1282. static inline void flushFinal(ProStringList *ret,
  1283. const QString &current, const QChar *ptr, const ProString &pending,
  1284. const ProString &str, bool replaced, bool joined)
  1285. {
  1286. int len = ptr - current.data();
  1287. if (len) {
  1288. if (!replaced && len == str.size())
  1289. ret->append(str);
  1290. else
  1291. ret->append(ProString(QString(current.data(), len), NoHash));
  1292. } else if (!pending.isEmpty()) {
  1293. ret->append(pending);
  1294. } else if (joined) {
  1295. ret->append(ProString());
  1296. }
  1297. }
  1298. ProStringList QMakeEvaluator::expandVariableReferences(
  1299. const ProString &str, int *pos, bool joined)
  1300. {
  1301. ProStringList ret;
  1302. // if (ok)
  1303. // *ok = true;
  1304. if (str.isEmpty() && !pos)
  1305. return ret;
  1306. const ushort LSQUARE = '[';
  1307. const ushort RSQUARE = ']';
  1308. const ushort LCURLY = '{';
  1309. const ushort RCURLY = '}';
  1310. const ushort LPAREN = '(';
  1311. const ushort RPAREN = ')';
  1312. const ushort DOLLAR = '$';
  1313. const ushort BACKSLASH = '\\';
  1314. const ushort UNDERSCORE = '_';
  1315. const ushort DOT = '.';
  1316. const ushort SPACE = ' ';
  1317. const ushort TAB = '\t';
  1318. const ushort COMMA = ',';
  1319. const ushort SINGLEQUOTE = '\'';
  1320. const ushort DOUBLEQUOTE = '"';
  1321. ushort unicode, quote = 0, parens = 0;
  1322. const ushort *str_data = (const ushort *)str.constData();
  1323. const int str_len = str.size();
  1324. ProString var, args;
  1325. bool replaced = false;
  1326. bool putSpace = false;
  1327. QString current; // Buffer for successively assembled string segments
  1328. current.resize(str.size());
  1329. QChar *ptr = current.data();
  1330. ProString pending; // Buffer for string segments from variables
  1331. // Only one of the above buffers can be filled at a given time.
  1332. for (int i = pos ? *pos : 0; i < str_len; ++i) {
  1333. unicode = str_data[i];
  1334. if (unicode == DOLLAR) {
  1335. if (str_len > i+2 && str_data[i+1] == DOLLAR) {
  1336. ++i;
  1337. ushort term = 0;
  1338. enum { VAR, ENVIRON, FUNCTION, PROPERTY } var_type = VAR;
  1339. unicode = str_data[++i];
  1340. if (unicode == LSQUARE) {
  1341. unicode = str_data[++i];
  1342. term = RSQUARE;
  1343. var_type = PROPERTY;
  1344. } else if (unicode == LCURLY) {
  1345. unicode = str_data[++i];
  1346. var_type = VAR;
  1347. term = RCURLY;
  1348. } else if (unicode == LPAREN) {
  1349. unicode = str_data[++i];
  1350. var_type = ENVIRON;
  1351. term = RPAREN;
  1352. }
  1353. int name_start = i;
  1354. forever {
  1355. if (!(unicode & (0xFF<<8)) &&
  1356. unicode != DOT && unicode != UNDERSCORE &&
  1357. //unicode != SINGLEQUOTE && unicode != DOUBLEā€¦

Large files files are truncated, but you can click here to view the full file