kde4automoc.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  12. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  13. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  14. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  15. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  16. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  17. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  18. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <QtCore/QCoreApplication>
  23. #include <QtCore/QDateTime>
  24. #include <QtCore/QDir>
  25. #include <QtCore/QFile>
  26. #include <QtCore/QFileInfo>
  27. #include <QtCore/QHash>
  28. #include <QtCore/QProcess>
  29. #include <QtCore/QQueue>
  30. #include <QtCore/QRegExp>
  31. #include <QtCore/QStringList>
  32. #include <QtCore/QTextStream>
  33. #include <QtCore/QtDebug>
  34. #include <cstdlib>
  35. #include <sys/types.h>
  36. #include <time.h>
  37. #include <errno.h>
  38. #ifdef Q_OS_WIN
  39. #include <windows.h>
  40. #include <sys/utime.h>
  41. #else
  42. #include <utime.h>
  43. #endif
  44. #if defined(Q_OS_DARWIN) || defined(Q_OS_MAC)
  45. #include <unistd.h>
  46. #endif
  47. // currently this is only used for the version number, Alex
  48. #include "automoc4_config.h"
  49. class AutoMoc
  50. {
  51. public:
  52. AutoMoc();
  53. bool run();
  54. private:
  55. void dotFilesCheck(bool);
  56. void lazyInitMocDefinitions();
  57. void lazyInit();
  58. bool touch(const QString &filename);
  59. bool generateMoc(const QString &sourceFile, const QString &mocFileName);
  60. void printUsage(const QString &);
  61. void printVersion();
  62. void echoColor(const QString &msg)
  63. {
  64. QProcess cmakeEcho;
  65. cmakeEcho.setProcessChannelMode(QProcess::ForwardedChannels);
  66. QStringList args(cmakeEchoColorArgs);
  67. args << msg;
  68. cmakeEcho.start(cmakeExecutable, args, QIODevice::NotOpen);
  69. cmakeEcho.waitForFinished(-1);
  70. }
  71. QString builddir;
  72. QString mocExe;
  73. QStringList mocIncludes;
  74. QStringList mocDefinitions;
  75. QStringList cmakeEchoColorArgs;
  76. QString cmakeExecutable;
  77. QFile dotFiles;
  78. const bool verbose;
  79. QTextStream cerr;
  80. QTextStream cout;
  81. bool failed;
  82. bool automocCppChanged;
  83. bool generateAll;
  84. bool doTouch;
  85. };
  86. void AutoMoc::printUsage(const QString &path)
  87. {
  88. cout << "Usage: " << path << " <outfile> <srcdir> <builddir> <moc executable> <cmake executable> [--touch]" << endl;
  89. }
  90. void AutoMoc::printVersion()
  91. {
  92. cout << "automoc4 " << AUTOMOC4_VERSION << endl;
  93. }
  94. void AutoMoc::dotFilesCheck(bool x)
  95. {
  96. if (!x) {
  97. cerr << "Error: syntax error in " << dotFiles.fileName() << endl;
  98. ::exit(EXIT_FAILURE);
  99. }
  100. }
  101. int main(int argc, char **argv)
  102. {
  103. QCoreApplication app(argc, argv);
  104. if (!AutoMoc().run()) {
  105. return EXIT_FAILURE;
  106. }
  107. return 0;
  108. }
  109. AutoMoc::AutoMoc()
  110. : verbose(!qgetenv("VERBOSE").isEmpty()), cerr(stderr), cout(stdout), failed(false),
  111. automocCppChanged(false), generateAll(false), doTouch(false)
  112. {
  113. const QByteArray colorEnv = qgetenv("COLOR");
  114. cmakeEchoColorArgs << QLatin1String("-E") << QLatin1String("cmake_echo_color")
  115. << QLatin1String("--switch=") + colorEnv << QLatin1String("--blue")
  116. << QLatin1String("--bold");
  117. }
  118. void AutoMoc::lazyInitMocDefinitions()
  119. {
  120. static bool done = false;
  121. if (done) {
  122. return;
  123. }
  124. done = true;
  125. QByteArray line = dotFiles.readLine();
  126. dotFilesCheck(line == "MOC_COMPILE_DEFINITIONS:\n");
  127. line = dotFiles.readLine().trimmed();
  128. const QStringList &cdefList = QString::fromUtf8(line).split(';', QString::SkipEmptyParts);
  129. line = dotFiles.readLine();
  130. dotFilesCheck(line == "MOC_DEFINITIONS:\n");
  131. line = dotFiles.readLine().trimmed();
  132. if (!cdefList.isEmpty()) {
  133. foreach (const QString &def, cdefList) {
  134. Q_ASSERT(!def.isEmpty());
  135. mocDefinitions << QLatin1String("-D") + def;
  136. }
  137. } else {
  138. const QStringList &defList = QString::fromUtf8(line).split(' ', QString::SkipEmptyParts);
  139. foreach (const QString &def, defList) {
  140. Q_ASSERT(!def.isEmpty());
  141. if (def.startsWith(QLatin1String("-D"))) {
  142. mocDefinitions << def;
  143. }
  144. }
  145. }
  146. }
  147. void AutoMoc::lazyInit()
  148. {
  149. const QStringList &args = QCoreApplication::arguments();
  150. mocExe = args[4];
  151. cmakeExecutable = args[5];
  152. if (args.size() > 6) {
  153. if (args[6] == QLatin1String("--touch")) {
  154. doTouch = true;
  155. }
  156. }
  157. lazyInitMocDefinitions();
  158. QByteArray line = dotFiles.readLine();
  159. dotFilesCheck(line == "MOC_INCLUDES:\n");
  160. line = dotFiles.readLine().trimmed();
  161. const QStringList &incPaths = QString::fromUtf8(line).split(';', QString::SkipEmptyParts);
  162. QSet<QString> frameworkPaths;
  163. foreach (const QString &path, incPaths) {
  164. Q_ASSERT(!path.isEmpty());
  165. mocIncludes << "-I" + path;
  166. if (path.endsWith(QLatin1String(".framework/Headers"))) {
  167. QDir framework(path);
  168. // Go up twice to get to the framework root
  169. framework.cdUp();
  170. framework.cdUp();
  171. frameworkPaths << framework.path();
  172. }
  173. }
  174. foreach (const QString &path, frameworkPaths) {
  175. mocIncludes << "-F" << path;
  176. }
  177. line = dotFiles.readLine();
  178. dotFilesCheck(line == "CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE:\n");
  179. line = dotFiles.readLine();
  180. if (line == "ON\n") {
  181. line = dotFiles.readLine();
  182. dotFilesCheck(line == "CMAKE_BINARY_DIR:\n");
  183. const QString &binDir = QLatin1String("-I") + QString::fromUtf8(dotFiles.readLine().trimmed());
  184. line = dotFiles.readLine();
  185. dotFilesCheck(line == "CMAKE_SOURCE_DIR:\n");
  186. const QString &srcDir = QLatin1String("-I") + QString::fromUtf8(dotFiles.readLine().trimmed());
  187. QStringList sortedMocIncludes;
  188. QMutableListIterator<QString> it(mocIncludes);
  189. while (it.hasNext()) {
  190. if (it.next().startsWith(binDir)) {
  191. sortedMocIncludes << it.value();
  192. it.remove();
  193. }
  194. }
  195. it.toFront();
  196. while (it.hasNext()) {
  197. if (it.next().startsWith(srcDir)) {
  198. sortedMocIncludes << it.value();
  199. it.remove();
  200. }
  201. }
  202. sortedMocIncludes += mocIncludes;
  203. mocIncludes = sortedMocIncludes;
  204. }
  205. }
  206. bool AutoMoc::run()
  207. {
  208. const QStringList &args = QCoreApplication::arguments();
  209. Q_ASSERT(args.size() > 0);
  210. if (args.size() == 2) {
  211. if ((args[1]=="--help") || (args[1]=="-h")) {
  212. printUsage(args[0]);
  213. ::exit(0);
  214. }
  215. else if (args[1]=="--version") {
  216. printVersion();
  217. ::exit(0);
  218. }
  219. else {
  220. printUsage(args[0]);
  221. ::exit(EXIT_FAILURE);
  222. }
  223. }
  224. else if (args.size() < 6) {
  225. printUsage(args[0]);
  226. ::exit(EXIT_FAILURE);
  227. }
  228. QFile outfile(args[1]);
  229. const QFileInfo outfileInfo(outfile);
  230. QString srcdir(args[2]);
  231. if (!srcdir.endsWith('/')) {
  232. srcdir += '/';
  233. }
  234. builddir = args[3];
  235. if (!builddir.endsWith('/')) {
  236. builddir += '/';
  237. }
  238. dotFiles.setFileName(args[1] + QLatin1String(".files"));
  239. dotFiles.open(QIODevice::ReadOnly | QIODevice::Text);
  240. const QByteArray &line = dotFiles.readLine();
  241. dotFilesCheck(line == "SOURCES:\n");
  242. const QStringList &sourceFiles = QString::fromUtf8(dotFiles.readLine().trimmed()).split(';', QString::SkipEmptyParts);
  243. if (outfile.exists()) {
  244. // set generateAll = true if MOC_COMPILE_DEFINITIONS changed
  245. outfile.open(QIODevice::ReadOnly | QIODevice::Text);
  246. QByteArray buf = outfile.readLine();
  247. // the second line contains the joined mocDefinitions
  248. buf = outfile.readLine();
  249. buf.chop(1); // remove trailing \n
  250. lazyInitMocDefinitions();
  251. generateAll = (buf != mocDefinitions.join(QString(QLatin1Char(' '))).toUtf8());
  252. outfile.close();
  253. } else {
  254. generateAll = true;
  255. }
  256. // the program goes through all .cpp files to see which moc files are included. It is not really
  257. // interesting how the moc file is named, but what file the moc is created from. Once a moc is
  258. // included the same moc may not be included in the _automoc.cpp file anymore. OTOH if there's a
  259. // header containing Q_OBJECT where no corresponding moc file is included anywhere a
  260. // moc_<filename>.cpp file is created and included in the _automoc.cpp file.
  261. QHash<QString, QString> includedMocs; // key = moc source filepath, value = moc output filepath
  262. QHash<QString, QString> notIncludedMocs; // key = moc source filepath, value = moc output filename
  263. QRegExp mocIncludeRegExp(QLatin1String("[\n]\\s*#\\s*include\\s+[\"<]((?:[^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]"));
  264. QRegExp qObjectRegExp(QLatin1String("[\n]\\s*Q_OBJECT\\b"));
  265. QStringList headerExtensions;
  266. #if defined(Q_OS_WIN)
  267. // not case sensitive
  268. headerExtensions << ".h" << ".hpp" << ".hxx";
  269. #elif defined(Q_OS_DARWIN) || defined(Q_OS_MAC)
  270. headerExtensions << ".h" << ".hpp" << ".hxx";
  271. // detect case-sensitive filesystem
  272. long caseSensitive = pathconf(srcdir.toLocal8Bit(), _PC_CASE_SENSITIVE);
  273. if (caseSensitive == 1) {
  274. headerExtensions << ".H";
  275. }
  276. #else
  277. headerExtensions << ".h" << ".hpp" << ".hxx" << ".H";
  278. #endif
  279. /* not safe: if a moc file is missing it's hard to get it generated if this check is "active"
  280. const QDateTime &lastRun = QFileInfo(dotFiles).lastModified();
  281. if (!generateAll) {
  282. bool dirty = false;
  283. foreach (const QString &absFilename, sourceFiles) {
  284. const QFileInfo sourceFileInfo(absFilename);
  285. if (sourceFileInfo.lastModified() >= lastRun) {
  286. dirty = true;
  287. break;
  288. }
  289. const QString &absPathBaseName = sourceFileInfo.absolutePath() + QLatin1Char('/') + sourceFileInfo.completeBaseName();
  290. foreach (const QString &ext, headerExtensions) {
  291. const QFileInfo header(absPathBaseName + ext);
  292. if (header.exists() && header.lastModified() >= lastRun) {
  293. dirty = true;
  294. break;
  295. }
  296. const QFileInfo pheader(absPathBaseName + QLatin1String("_p") + ext);
  297. if (pheader.exists() && pheader.lastModified() >= lastRun) {
  298. dirty = true;
  299. break;
  300. }
  301. }
  302. if (dirty) {
  303. break;
  304. }
  305. }
  306. if (!dirty) {
  307. return true;
  308. }
  309. }
  310. */
  311. foreach (const QString &absFilename, sourceFiles) {
  312. //qDebug() << absFilename;
  313. const QFileInfo sourceFileInfo(absFilename);
  314. if (absFilename.endsWith(QLatin1String(".cpp")) || absFilename.endsWith(QLatin1String(".cc")) ||
  315. absFilename.endsWith(QLatin1String(".mm")) || absFilename.endsWith(QLatin1String(".cxx")) ||
  316. absFilename.endsWith(QLatin1String(".C"))) {
  317. //qDebug() << "check .cpp file";
  318. QFile sourceFile(absFilename);
  319. sourceFile.open(QIODevice::ReadOnly);
  320. const QByteArray contents = sourceFile.readAll();
  321. if (contents.isEmpty()) {
  322. cerr << "automoc4: empty source file: " << absFilename << endl;
  323. continue;
  324. }
  325. const QString contentsString = QString::fromUtf8(contents);
  326. const QString absPath = sourceFileInfo.absolutePath() + '/';
  327. Q_ASSERT(absPath.endsWith('/'));
  328. int matchOffset = mocIncludeRegExp.indexIn(contentsString);
  329. if (matchOffset < 0) {
  330. // no moc #include, look whether we need to create a moc from the .h nevertheless
  331. //qDebug() << "no moc #include in the .cpp file";
  332. const QString basename = sourceFileInfo.completeBaseName();
  333. foreach (const QString &ext, headerExtensions) {
  334. const QString headername = absPath + basename + ext;
  335. if (QFile::exists(headername) && !includedMocs.contains(headername) &&
  336. !notIncludedMocs.contains(headername)) {
  337. const QString currentMoc = "moc_" + basename + ".cpp";
  338. QFile header(headername);
  339. header.open(QIODevice::ReadOnly);
  340. const QByteArray contents = header.readAll();
  341. if (qObjectRegExp.indexIn(QString::fromUtf8(contents)) >= 0) {
  342. //qDebug() << "header contains Q_OBJECT macro";
  343. notIncludedMocs.insert(headername, currentMoc);
  344. }
  345. break;
  346. }
  347. }
  348. foreach (const QString &ext, headerExtensions) {
  349. const QString privateHeaderName = absPath + basename + "_p" + ext;
  350. if (QFile::exists(privateHeaderName) && !includedMocs.contains(privateHeaderName) &&
  351. !notIncludedMocs.contains(privateHeaderName)) {
  352. const QString currentMoc = "moc_" + basename + "_p.cpp";
  353. QFile header(privateHeaderName);
  354. header.open(QIODevice::ReadOnly);
  355. const QByteArray contents = header.readAll();
  356. if (qObjectRegExp.indexIn(QString::fromUtf8(contents)) >= 0) {
  357. //qDebug() << "header contains Q_OBJECT macro";
  358. notIncludedMocs.insert(privateHeaderName, currentMoc);
  359. }
  360. break;
  361. }
  362. }
  363. } else {
  364. do { // call this for every moc include in the file
  365. const QString currentMoc = mocIncludeRegExp.cap(1);
  366. //qDebug() << "found moc include: " << currentMoc << " at offset " << matchOffset;
  367. const QFileInfo currentMocInfo(currentMoc);
  368. QString basename = currentMocInfo.completeBaseName();
  369. const bool moc_style = basename.startsWith(QLatin1String("moc_"));
  370. // If the moc include is of the moc_foo.cpp style we expect the Q_OBJECT class
  371. // declaration in a header file.
  372. // If the moc include is of the foo.moc style we need to look for a Q_OBJECT
  373. // macro in the current source file, if it contains the macro we generate the
  374. // moc file from the source file, else from the header.
  375. //
  376. // TODO: currently any .moc file name will be used if the source contains
  377. // Q_OBJECT
  378. if (moc_style || qObjectRegExp.indexIn(contentsString) < 0) {
  379. if (moc_style) {
  380. // basename should be the part of the moc filename used for finding the
  381. // correct header, so we need to remove the moc_ part
  382. basename = basename.right(basename.length() - 4);
  383. }
  384. bool headerFound = false;
  385. foreach (const QString &ext, headerExtensions) {
  386. const QString &sourceFilePath = absPath + basename + ext;
  387. if (QFile::exists(sourceFilePath)) {
  388. headerFound = true;
  389. includedMocs.insert(sourceFilePath, currentMoc);
  390. notIncludedMocs.remove(sourceFilePath);
  391. break;
  392. }
  393. }
  394. if (!headerFound) {
  395. // the moc file is in a subdir => look for the header in the same subdir
  396. if (currentMoc.indexOf('/') != -1) {
  397. const QString &filepath = absPath + currentMocInfo.path() + QLatin1Char('/') + basename;
  398. foreach (const QString &ext, headerExtensions) {
  399. const QString &sourceFilePath = filepath + ext;
  400. if (QFile::exists(sourceFilePath)) {
  401. headerFound = true;
  402. includedMocs.insert(sourceFilePath, currentMoc);
  403. notIncludedMocs.remove(sourceFilePath);
  404. break;
  405. }
  406. }
  407. if (!headerFound) {
  408. cerr << "automoc4: The file \"" << absFilename <<
  409. "\" includes the moc file \"" << currentMoc << "\", but neither \"" <<
  410. absPath + basename + '{' + headerExtensions.join(",") + "}\" nor \"" <<
  411. filepath + '{' + headerExtensions.join(",") + '}' <<
  412. "\" exist." << endl;
  413. ::exit(EXIT_FAILURE);
  414. }
  415. } else {
  416. cerr << "automoc4: The file \"" << absFilename <<
  417. "\" includes the moc file \"" << currentMoc << "\", but \"" <<
  418. absPath + basename + '{' + headerExtensions.join(",") + '}' <<
  419. "\" does not exist." << endl;
  420. ::exit(EXIT_FAILURE);
  421. }
  422. }
  423. } else {
  424. includedMocs.insert(absFilename, currentMoc);
  425. notIncludedMocs.remove(absFilename);
  426. }
  427. matchOffset = mocIncludeRegExp.indexIn(contentsString,
  428. matchOffset + currentMoc.length());
  429. } while(matchOffset >= 0);
  430. }
  431. } else if (absFilename.endsWith(QLatin1String(".h")) || absFilename.endsWith(QLatin1String(".hpp")) ||
  432. absFilename.endsWith(QLatin1String(".hxx")) || absFilename.endsWith(QLatin1String(".H"))) {
  433. if (!includedMocs.contains(absFilename) && !notIncludedMocs.contains(absFilename)) {
  434. // if this header is not getting processed yet and is explicitly mentioned for the
  435. // automoc the moc is run unconditionally on the header and the resulting file is
  436. // included in the _automoc.cpp file (unless there's a .cpp file later on that
  437. // includes the moc from this header)
  438. const QString currentMoc = "moc_" + sourceFileInfo.completeBaseName() + ".cpp";
  439. notIncludedMocs.insert(absFilename, currentMoc);
  440. }
  441. } else {
  442. if (verbose) {
  443. cout << "automoc4: ignoring file '" << absFilename << "' with unknown suffix" << endl;
  444. }
  445. }
  446. }
  447. // run moc on all the moc's that are #included in source files
  448. QHash<QString, QString>::ConstIterator end = includedMocs.constEnd();
  449. QHash<QString, QString>::ConstIterator it = includedMocs.constBegin();
  450. for (; it != end; ++it) {
  451. generateMoc(it.key(), it.value());
  452. }
  453. QByteArray automocSource;
  454. QTextStream outStream(&automocSource, QIODevice::WriteOnly);
  455. outStream << "/* This file is autogenerated, do not edit\n"
  456. << mocDefinitions.join(QString(QLatin1Char(' '))) << "\n*/\n";
  457. if (notIncludedMocs.isEmpty()) {
  458. outStream << "enum some_compilers { need_more_than_nothing };\n";
  459. } else {
  460. // run moc on the remaining headers and include them in the _automoc.cpp file
  461. end = notIncludedMocs.constEnd();
  462. it = notIncludedMocs.constBegin();
  463. for (; it != end; ++it) {
  464. if (generateMoc(it.key(), it.value())) {
  465. automocCppChanged = true;
  466. }
  467. outStream << "#include \"" << it.value() << "\"\n";
  468. }
  469. }
  470. if (failed) {
  471. // if any moc process failed we don't want to touch the _automoc.cpp file so that
  472. // automoc4 is rerun until the issue is fixed
  473. cerr << "returning failed.."<< endl;
  474. return false;
  475. }
  476. outStream.flush();
  477. if (!automocCppChanged) {
  478. // compare contents of the _automoc.cpp file
  479. outfile.open(QIODevice::ReadOnly | QIODevice::Text);
  480. const QByteArray oldContents = outfile.readAll();
  481. outfile.close();
  482. if (oldContents == automocSource) {
  483. // nothing changed: don't touch the _automoc.cpp file
  484. return true;
  485. }
  486. }
  487. // either the contents of the _automoc.cpp file or one of the mocs included by it have changed
  488. // source file that includes all remaining moc files (_automoc.cpp file)
  489. outfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
  490. outfile.write(automocSource);
  491. outfile.close();
  492. // update the timestamp on the _automoc.cpp.files file to make sure we get called again
  493. dotFiles.close();
  494. if (doTouch && !touch(dotFiles.fileName())) {
  495. return false;
  496. }
  497. return true;
  498. }
  499. bool AutoMoc::touch(const QString &_filename)
  500. {
  501. // sleep for 1s in order to make the modification time greater than the modification time of
  502. // the files written before. Equal modification time is not good enough. Just using utime with
  503. // time(NULL) + 1 is also not a good solution as then make will complain about clock skew.
  504. #ifdef Q_OS_WIN
  505. Sleep(1000);
  506. _wutime(reinterpret_cast<const wchar_t *>(_filename.utf16()), 0);
  507. #else
  508. const QByteArray &filename = QFile::encodeName(_filename);
  509. const struct timespec sleepDuration = { 1, 0 };
  510. nanosleep(&sleepDuration, NULL);
  511. int err = utime(filename.constData(), NULL);
  512. if (err == -1) {
  513. err = errno;
  514. cerr << strerror(err) << "\n";
  515. return false;
  516. }
  517. #endif
  518. return true;
  519. }
  520. bool AutoMoc::generateMoc(const QString &sourceFile, const QString &mocFileName)
  521. {
  522. //qDebug() << Q_FUNC_INFO << sourceFile << mocFileName;
  523. const QString mocFilePath = builddir + mocFileName;
  524. QFileInfo mocInfo(mocFilePath);
  525. if (generateAll || mocInfo.lastModified() <= QFileInfo(sourceFile).lastModified()) {
  526. QDir mocDir = mocInfo.dir();
  527. // make sure the directory for the resulting moc file exists
  528. if (!mocDir.exists()) {
  529. mocDir.mkpath(mocDir.path());
  530. }
  531. static bool initialized = false;
  532. if (!initialized) {
  533. initialized = true;
  534. lazyInit();
  535. }
  536. if (verbose) {
  537. echoColor("Generating " + mocFilePath + " from " + sourceFile);
  538. } else {
  539. echoColor("Generating " + mocFileName);
  540. }
  541. QProcess mocProc;
  542. mocProc.setProcessChannelMode(QProcess::ForwardedChannels);
  543. QStringList args(mocIncludes + mocDefinitions);
  544. #ifdef Q_OS_WIN
  545. args << "-DWIN32";
  546. #endif
  547. args << QLatin1String("-o") << mocFilePath << sourceFile;
  548. //qDebug() << "executing: " << mocExe << args;
  549. if (verbose) {
  550. cout << mocExe << " " << args.join(QLatin1String(" ")) << endl;
  551. }
  552. mocProc.start(mocExe, args, QIODevice::NotOpen);
  553. if (mocProc.waitForStarted()) {
  554. const bool result = mocProc.waitForFinished(-1);
  555. if (!result || mocProc.exitCode()) {
  556. cerr << "automoc4: process for " << mocFilePath
  557. << " failed: " << mocProc.errorString() << endl;
  558. cerr << "pid to wait for: " << mocProc.pid() << endl;
  559. failed = true;
  560. QFile::remove(mocFilePath);
  561. }
  562. return true;
  563. } else {
  564. cerr << "automoc4: process for " << mocFilePath << "failed to start: "
  565. << mocProc.errorString() << endl;
  566. failed = true;
  567. }
  568. }
  569. return false;
  570. }