cmQtAutomoc.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #include "cmGlobalGenerator.h"
  2. #include "cmLocalGenerator.h"
  3. #include "cmMakefile.h"
  4. #include "cmSourceFile.h"
  5. #include "cmSystemTools.h"
  6. #include "cmQtAutomoc.h"
  7. cmQtAutomoc::cmQtAutomoc()
  8. :Verbose(true)
  9. ,RunMocFailed(false)
  10. ,GenerateAll(false)
  11. {
  12. }
  13. void cmQtAutomoc::SetupAutomocTarget(cmMakefile* makefile,
  14. const char* targetName,
  15. std::vector<std::string>& srcs)
  16. {
  17. // don't do anything if there is no Qt4:
  18. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  19. if (qtMajorVersion != "4")
  20. {
  21. return;
  22. }
  23. std::string automocTargetName = targetName;
  24. automocTargetName += "_automoc";
  25. std::string targetDir = makefile->GetCurrentOutputDirectory();
  26. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  27. targetDir += "/";
  28. targetDir += automocTargetName;
  29. targetDir += ".dir/";
  30. cmCustomCommandLine currentLine;
  31. currentLine.push_back(makefile->GetCMakeInstance()->GetCMakeCommand());
  32. currentLine.push_back("-E");
  33. currentLine.push_back("cmake_automoc");
  34. currentLine.push_back(targetDir);
  35. cmCustomCommandLines commandLines;
  36. commandLines.push_back(currentLine);
  37. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  38. "", makefile->GetCurrentOutputDirectory());
  39. std::vector<std::string> depends;
  40. cmTarget* target = makefile->AddUtilityCommand(automocTargetName.c_str(),
  41. true,
  42. workingDirectory.c_str(), depends,
  43. commandLines, false, "Automoc target");
  44. std::string _moc_files;
  45. std::string _moc_headers;
  46. const char* sepFiles = "";
  47. const char* sepHeaders = "";
  48. for(std::vector<std::string>::const_iterator fileIt = srcs.begin();
  49. fileIt != srcs.end();
  50. ++fileIt)
  51. {
  52. std::string absFile = cmSystemTools::CollapseFullPath(
  53. fileIt->c_str(), makefile->GetCurrentDirectory());
  54. bool skip = false;
  55. bool generated = false;
  56. cmSourceFile* sf = makefile->GetSource(absFile.c_str());
  57. if (sf)
  58. {
  59. skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  60. generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  61. }
  62. if ((skip==false) && (generated == false))
  63. {
  64. std::string ext = cmSystemTools::GetFilenameExtension(fileIt->c_str());
  65. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  66. ext.c_str());
  67. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  68. {
  69. _moc_files += sepFiles;
  70. _moc_files += absFile;
  71. sepFiles = ";";
  72. }
  73. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  74. {
  75. _moc_headers += sepHeaders;
  76. _moc_headers += absFile;
  77. sepHeaders = ";";
  78. }
  79. }
  80. }
  81. std::string _moc_incs = makefile->GetProperty("INCLUDE_DIRECTORIES");
  82. std::string _moc_defs = makefile->GetProperty("DEFINITIONS");
  83. std::string _moc_compile_defs = makefile->GetProperty("COMPILE_DEFINITIONS");
  84. // forget the variables added here afterwards again:
  85. cmMakefile::ScopePushPop varScope(makefile);
  86. static_cast<void>(varScope);
  87. makefile->AddDefinition("_moc_target_name", automocTargetName.c_str());
  88. makefile->AddDefinition("_moc_incs", _moc_incs.c_str());
  89. makefile->AddDefinition("_moc_defs", _moc_defs.c_str());
  90. makefile->AddDefinition("_moc_compile_defs", _moc_compile_defs.c_str());
  91. makefile->AddDefinition("_moc_files", _moc_files.c_str());
  92. makefile->AddDefinition("_moc_headers", _moc_headers.c_str());
  93. const char* cmakeRoot = makefile->GetDefinition("CMAKE_ROOT");
  94. std::string inputFile = cmakeRoot;
  95. inputFile += "/Modules/AutomocInfo.cmake.in";
  96. std::string outputFile = targetDir;
  97. outputFile += "/AutomocInfo.cmake";
  98. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  99. false, true, false);
  100. std::string mocCppFile = makefile->GetCurrentOutputDirectory();
  101. mocCppFile += "/";
  102. mocCppFile += automocTargetName;
  103. mocCppFile += ".cpp";
  104. makefile->GetOrCreateSource(mocCppFile.c_str(), true);
  105. srcs.push_back(mocCppFile);
  106. }
  107. void cmQtAutomoc::AddTargetDependency(cmMakefile* makefile, cmTarget* target)
  108. {
  109. // don't do anything if there is no Qt4:
  110. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  111. if (qtMajorVersion != "4")
  112. {
  113. return;
  114. }
  115. std::string automocTargetName = target->GetName();
  116. automocTargetName += "_automoc";
  117. target->AddUtility(automocTargetName.c_str());
  118. }
  119. bool cmQtAutomoc::Run(const char* targetDirectory)
  120. {
  121. cmake cm;
  122. cmGlobalGenerator* gg = this->CreateGlobalGenerator(&cm, targetDirectory);
  123. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  124. this->ReadAutomocInfoFile(makefile, targetDirectory);
  125. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  126. this->Init();
  127. if (this->QtMajorVersion == "4")
  128. {
  129. this->RunAutomocQt4();
  130. }
  131. this->WriteOldMocDefinitionsFile(targetDirectory);
  132. delete gg;
  133. gg = NULL;
  134. makefile = NULL;
  135. }
  136. cmGlobalGenerator* cmQtAutomoc::CreateGlobalGenerator(cmake* cm,
  137. const char* targetDirectory)
  138. {
  139. cmGlobalGenerator* gg = new cmGlobalGenerator();
  140. gg->SetCMakeInstance(cm);
  141. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  142. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  143. lg->GetMakefile()->SetStartOutputDirectory(targetDirectory);
  144. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  145. lg->GetMakefile()->SetStartDirectory(targetDirectory);
  146. gg->SetCurrentLocalGenerator(lg);
  147. return gg;
  148. }
  149. bool cmQtAutomoc::ReadAutomocInfoFile(cmMakefile* makefile,
  150. const char* targetDirectory)
  151. {
  152. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  153. cmSystemTools::ConvertToUnixSlashes(filename);
  154. filename += "/AutomocInfo.cmake";
  155. if (!makefile->ReadListFile(0, filename.c_str()))
  156. {
  157. cmSystemTools::Error("Error processing file:", filename.c_str());
  158. return false;
  159. }
  160. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  161. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  162. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  163. this->IncludeProjectDirsBefore = makefile->IsOn(
  164. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  165. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  166. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  167. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  168. this->MocCompileDefinitionsStr = makefile->GetSafeDefinition(
  169. "AM_MOC_COMPILE_DEFINITIONS");
  170. this->MocDefinitionsStr = makefile->GetSafeDefinition("AM_MOC_DEFINITIONS");
  171. this->MocIncludesStr = makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  172. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  173. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  174. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  175. return true;
  176. }
  177. bool cmQtAutomoc::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  178. const char* targetDirectory)
  179. {
  180. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  181. cmSystemTools::ConvertToUnixSlashes(filename);
  182. filename += "/AutomocOldMocDefinitions.cmake";
  183. if (makefile->ReadListFile(0, filename.c_str()))
  184. {
  185. this->OldMocDefinitionsStr =
  186. makefile->GetSafeDefinition("AM_OLD_MOC_DEFINITIONS");
  187. }
  188. return true;
  189. }
  190. void cmQtAutomoc::WriteOldMocDefinitionsFile(const char* targetDirectory)
  191. {
  192. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  193. cmSystemTools::ConvertToUnixSlashes(filename);
  194. filename += "/AutomocOldMocDefinitions.cmake";
  195. std::fstream outfile;
  196. outfile.open(filename.c_str(),
  197. std::ios_base::out | std::ios_base::trunc);
  198. outfile << "set(AM_OLD_MOC_DEFINITIONS \""
  199. << this->Join(this->MocDefinitions, ' ') << "\")\n";
  200. outfile.close();
  201. }
  202. void cmQtAutomoc::Init()
  203. {
  204. this->OutMocCppFilename = this->Builddir;
  205. this->OutMocCppFilename += this->TargetName;
  206. this->OutMocCppFilename += ".cpp";
  207. std::vector<std::string> cdefList;
  208. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  209. if (!cdefList.empty())
  210. {
  211. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  212. it != cdefList.end();
  213. ++it)
  214. {
  215. this->MocDefinitions.push_back("-D" + (*it));
  216. }
  217. }
  218. else
  219. {
  220. std::string tmpMocDefs = this->MocDefinitionsStr;
  221. cmSystemTools::ReplaceString(tmpMocDefs, " ", ";");
  222. std::vector<std::string> defList;
  223. cmSystemTools::ExpandListArgument(tmpMocDefs, defList);
  224. for(std::vector<std::string>::const_iterator it = defList.begin();
  225. it != defList.end();
  226. ++it)
  227. {
  228. if (this->StartsWith(*it, "-D"))
  229. {
  230. this->MocDefinitions.push_back(*it);
  231. }
  232. }
  233. }
  234. std::vector<std::string> incPaths;
  235. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  236. std::set<std::string> frameworkPaths;
  237. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  238. it != incPaths.end();
  239. ++it)
  240. {
  241. const std::string &path = *it;
  242. this->MocIncludes.push_back("-I" + path);
  243. if (this->EndsWith(path, ".framework/Headers"))
  244. {
  245. // Go up twice to get to the framework root
  246. std::vector<std::string> pathComponents;
  247. cmsys::SystemTools::SplitPath(path.c_str(), pathComponents);
  248. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  249. pathComponents.begin(), pathComponents.end() - 2);
  250. frameworkPaths.insert(frameworkPath);
  251. }
  252. }
  253. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  254. it != frameworkPaths.end(); ++it)
  255. {
  256. this->MocIncludes.push_back("-F");
  257. this->MocIncludes.push_back(*it);
  258. }
  259. if (this->IncludeProjectDirsBefore)
  260. {
  261. const std::string &binDir = "-I" + this->ProjectBinaryDir;
  262. const std::string srcDir = "-I" + this->ProjectSourceDir;
  263. std::list<std::string> sortedMocIncludes;
  264. std::list<std::string>::iterator it = this->MocIncludes.begin();
  265. while (it != this->MocIncludes.end())
  266. {
  267. if (this->StartsWith(*it, binDir))
  268. {
  269. sortedMocIncludes.push_back(*it);
  270. it = this->MocIncludes.erase(it);
  271. }
  272. else
  273. {
  274. ++it;
  275. }
  276. }
  277. it = this->MocIncludes.begin();
  278. while (it != this->MocIncludes.end())
  279. {
  280. if (this->StartsWith(*it, srcDir))
  281. {
  282. sortedMocIncludes.push_back(*it);
  283. it = this->MocIncludes.erase(it);
  284. }
  285. else
  286. {
  287. ++it;
  288. }
  289. }
  290. sortedMocIncludes.insert(sortedMocIncludes.end(),
  291. this->MocIncludes.begin(), this->MocIncludes.end());
  292. this->MocIncludes = sortedMocIncludes;
  293. }
  294. }
  295. bool cmQtAutomoc::RunAutomocQt4()
  296. {
  297. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  298. || (this->OldMocDefinitionsStr != this->Join(this->MocDefinitions, ' ')))
  299. {
  300. this->GenerateAll = true;
  301. }
  302. // the program goes through all .cpp files to see which moc files are
  303. // included. It is not really interesting how the moc file is named, but
  304. // what file the moc is created from. Once a moc is included the same moc
  305. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  306. // header containing Q_OBJECT where no corresponding moc file is included
  307. // anywhere a moc_<filename>.cpp file is created and included in
  308. // the _automoc.cpp file.
  309. // key = moc source filepath, value = moc output filepath
  310. std::map<std::string, std::string> includedMocs;
  311. // key = moc source filepath, value = moc output filename
  312. std::map<std::string, std::string> notIncludedMocs;
  313. cmsys::RegularExpression mocIncludeRegExp(
  314. "[\n][ \t]*#[ \t]*include[ \t]+"
  315. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  316. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  317. std::list<std::string> headerExtensions;
  318. #if defined(_WIN32)
  319. // not case sensitive
  320. headerExtensions.push_back(".h");
  321. headerExtensions.push_back(".hpp");
  322. headerExtensions.push_back(".hxx");
  323. #elif defined(__APPLE__)
  324. headerExtensions.push_back(".h");
  325. headerExtensions.push_back(".hpp");
  326. headerExtensions.push_back(".hxx");
  327. // detect case-sensitive filesystem
  328. long caseSensitive = pathconf(this->Srcdir.c_str(), _PC_CASE_SENSITIVE);
  329. if (caseSensitive == 1)
  330. {
  331. headerExtensions.push_back(".H");
  332. }
  333. #else
  334. headerExtensions.push_back(".h");
  335. headerExtensions.push_back(".hpp");
  336. headerExtensions.push_back(".hxx");
  337. headerExtensions.push_back(".H");
  338. #endif
  339. std::vector<std::string> sourceFiles;
  340. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  341. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  342. it != sourceFiles.end();
  343. ++it)
  344. {
  345. const std::string &absFilename = *it;
  346. if (this->Verbose)
  347. {
  348. printf("Checking -%s-\n", absFilename.c_str());
  349. }
  350. const std::string contentsString = this->ReadAll(absFilename);
  351. if (contentsString.empty())
  352. {
  353. std::cerr << "automoc4: empty source file: " << absFilename << std::endl;
  354. continue;
  355. }
  356. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  357. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  358. int matchOffset = 0;
  359. if (!mocIncludeRegExp.find(contentsString.c_str()))
  360. {
  361. // no moc #include, look whether we need to create a moc from
  362. // the .h nevertheless
  363. const std::string basename =
  364. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  365. for(std::list<std::string>::const_iterator ext =
  366. headerExtensions.begin();
  367. ext != headerExtensions.end();
  368. ++ext)
  369. {
  370. const std::string headername = absPath + basename + (*ext);
  371. if (cmsys::SystemTools::FileExists(headername.c_str())
  372. && includedMocs.find(headername) == includedMocs.end()
  373. && notIncludedMocs.find(headername) == notIncludedMocs.end())
  374. {
  375. const std::string currentMoc = "moc_" + basename + ".cpp";
  376. const std::string contents = this->ReadAll(headername);
  377. if (qObjectRegExp.find(contents))
  378. {
  379. //std::cout << "header contains Q_OBJECT macro";
  380. notIncludedMocs[headername] = currentMoc;
  381. }
  382. break;
  383. }
  384. }
  385. for(std::list<std::string>::const_iterator ext =
  386. headerExtensions.begin();
  387. ext != headerExtensions.end();
  388. ++ext)
  389. {
  390. const std::string privateHeaderName = absPath+basename+"_p"+(*ext);
  391. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str())
  392. && includedMocs.find(privateHeaderName) == includedMocs.end()
  393. && notIncludedMocs.find(privateHeaderName) == notIncludedMocs.end())
  394. {
  395. const std::string currentMoc = "moc_" + basename + "_p.cpp";
  396. const std::string contents = this->ReadAll(privateHeaderName);
  397. if (qObjectRegExp.find(contents))
  398. {
  399. //std::cout << "header contains Q_OBJECT macro";
  400. notIncludedMocs[privateHeaderName] = currentMoc;
  401. }
  402. break;
  403. }
  404. }
  405. }
  406. else
  407. {
  408. // for every moc include in the file
  409. do
  410. {
  411. const std::string currentMoc = mocIncludeRegExp.match(1);
  412. //std::cout << "found moc include: " << currentMoc << std::endl;
  413. std::string basename = cmsys::SystemTools::
  414. GetFilenameWithoutLastExtension(currentMoc);
  415. const bool moc_style = this->StartsWith(basename, "moc_");
  416. // If the moc include is of the moc_foo.cpp style we expect
  417. // the Q_OBJECT class declaration in a header file.
  418. // If the moc include is of the foo.moc style we need to look for
  419. // a Q_OBJECT macro in the current source file, if it contains the
  420. // macro we generate the moc file from the source file, else from the
  421. // header.
  422. // Q_OBJECT
  423. if (moc_style || !qObjectRegExp.find(contentsString))
  424. {
  425. if (moc_style)
  426. {
  427. // basename should be the part of the moc filename used for
  428. // finding the correct header, so we need to remove the moc_ part
  429. basename = basename.substr(4);
  430. }
  431. bool headerFound = false;
  432. for(std::list<std::string>::const_iterator ext =
  433. headerExtensions.begin();
  434. ext != headerExtensions.end();
  435. ++ext)
  436. {
  437. const std::string &sourceFilePath = absPath + basename + (*ext);
  438. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  439. {
  440. headerFound = true;
  441. includedMocs[sourceFilePath] = currentMoc;
  442. notIncludedMocs.erase(sourceFilePath);
  443. break;
  444. }
  445. }
  446. if (!headerFound)
  447. {
  448. // the moc file is in a subdir => look for the header in the
  449. // same subdir
  450. if (currentMoc.find_first_of('/') != std::string::npos)
  451. {
  452. const std::string &filepath = absPath
  453. + cmsys::SystemTools::GetFilenamePath(currentMoc)
  454. + '/' + basename;
  455. for(std::list<std::string>::const_iterator ext =
  456. headerExtensions.begin();
  457. ext != headerExtensions.end();
  458. ++ext)
  459. {
  460. const std::string &sourceFilePath = filepath + (*ext);
  461. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  462. {
  463. headerFound = true;
  464. includedMocs[sourceFilePath] = currentMoc;
  465. notIncludedMocs.erase(sourceFilePath);
  466. break;
  467. }
  468. }
  469. if (!headerFound)
  470. {
  471. std::cerr << "automoc4: The file \"" << absFilename
  472. << "\" includes the moc file \"" << currentMoc
  473. << "\", but neither \"" << absPath << basename
  474. << '{' << this->Join(headerExtensions, ',')
  475. << "}\" nor \"" << filepath << '{'
  476. << this->Join(headerExtensions, ',') << '}'
  477. << "\" exist." << std::endl;
  478. ::exit(EXIT_FAILURE);
  479. }
  480. }
  481. else
  482. {
  483. std::cerr << "automoc4: The file \"" << absFilename
  484. << "\" includes the moc file \"" << currentMoc
  485. << "\", but \"" << absPath << basename << '{'
  486. << this->Join(headerExtensions, ',') << '}'
  487. << "\" does not exist." << std::endl;
  488. ::exit(EXIT_FAILURE);
  489. }
  490. }
  491. }
  492. else
  493. {
  494. includedMocs[absFilename] = currentMoc;
  495. notIncludedMocs.erase(absFilename);
  496. }
  497. matchOffset += mocIncludeRegExp.end();
  498. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  499. }
  500. }
  501. std::vector<std::string> headerFiles;
  502. cmSystemTools::ExpandListArgument(this->Headers, headerFiles);
  503. for (std::vector<std::string>::const_iterator it = headerFiles.begin();
  504. it != headerFiles.end();
  505. ++it)
  506. {
  507. const std::string &absFilename = *it;
  508. if (this->Verbose)
  509. {
  510. printf("Checking -%s-\n", absFilename.c_str());
  511. }
  512. if (includedMocs.find(absFilename) == includedMocs.end()
  513. && notIncludedMocs.find(absFilename) == notIncludedMocs.end())
  514. {
  515. // if this header is not getting processed yet and is explicitly
  516. // mentioned for the automoc the moc is run unconditionally on the
  517. // header and the resulting file is included in the _automoc.cpp file
  518. // (unless there's a .cpp file later on that includes the moc from
  519. // this header)
  520. const std::string currentMoc = "moc_" + cmsys::SystemTools::
  521. GetFilenameWithoutLastExtension(absFilename) + ".cpp";
  522. notIncludedMocs[absFilename] = currentMoc;
  523. }
  524. }
  525. // run moc on all the moc's that are #included in source files
  526. for(std::map<std::string, std::string>::const_iterator
  527. it = includedMocs.begin();
  528. it != includedMocs.end();
  529. ++it)
  530. {
  531. this->GenerateMoc(it->first, it->second);
  532. }
  533. std::stringstream outStream(std::stringstream::out);
  534. outStream << "/* This file is autogenerated, do not edit*/\n";
  535. bool automocCppChanged = false;
  536. if (notIncludedMocs.empty())
  537. {
  538. outStream << "enum some_compilers { need_more_than_nothing };\n";
  539. }
  540. else
  541. {
  542. // run moc on the remaining headers and include them in
  543. // the _automoc.cpp file
  544. for(std::map<std::string, std::string>::const_iterator
  545. it = notIncludedMocs.begin();
  546. it != notIncludedMocs.end();
  547. ++it)
  548. {
  549. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  550. if (mocSuccess)
  551. {
  552. automocCppChanged = true;
  553. }
  554. outStream << "#include \"" << it->second << "\"\n";
  555. }
  556. }
  557. if (this->RunMocFailed)
  558. {
  559. std::cerr << "returning failed.."<< std::endl;
  560. return false;
  561. }
  562. outStream.flush();
  563. std::string automocSource = outStream.str();
  564. if (!automocCppChanged)
  565. {
  566. // compare contents of the _automoc.cpp file
  567. const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
  568. if (oldContents == automocSource)
  569. {
  570. // nothing changed: don't touch the _automoc.cpp file
  571. return true;
  572. }
  573. }
  574. // source file that includes all remaining moc files (_automoc.cpp file)
  575. std::fstream outfile;
  576. outfile.open(this->OutMocCppFilename.c_str(),
  577. std::ios_base::out | std::ios_base::trunc);
  578. outfile << automocSource;
  579. outfile.close();
  580. return true;
  581. }
  582. bool cmQtAutomoc::GenerateMoc(const std::string& sourceFile,
  583. const std::string& mocFileName)
  584. {
  585. const std::string mocFilePath = this->Builddir + mocFileName;
  586. int sourceNewerThanMoc = 0;
  587. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile.c_str(),
  588. mocFilePath.c_str(),
  589. &sourceNewerThanMoc);
  590. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  591. {
  592. // make sure the directory for the resulting moc file exists
  593. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  594. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  595. {
  596. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  597. }
  598. /* if (this->Verbose)
  599. {
  600. echoColor("Generating " + mocFilePath + " from " + sourceFile);
  601. }
  602. else
  603. {
  604. echoColor("Generating " + mocFileName);
  605. }*/
  606. std::vector<cmStdString> command;
  607. command.push_back(this->MocExecutable);
  608. for (std::list<std::string>::const_iterator it = this->MocIncludes.begin();
  609. it != this->MocIncludes.end();
  610. ++it)
  611. {
  612. command.push_back(*it);
  613. }
  614. for(std::list<std::string>::const_iterator it=this->MocDefinitions.begin();
  615. it != this->MocDefinitions.end();
  616. ++it)
  617. {
  618. command.push_back(*it);
  619. }
  620. #ifdef _WIN32
  621. command.push_back("-DWIN32");
  622. #endif
  623. command.push_back("-o");
  624. command.push_back(mocFilePath);
  625. command.push_back(sourceFile);
  626. if (this->Verbose)
  627. {
  628. for(int i=0; i<command.size(); i++)
  629. {
  630. printf("%s ", command[i].c_str());
  631. }
  632. printf("\n");
  633. }
  634. std::string output;
  635. int retVal = 0;
  636. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  637. if (!result || retVal)
  638. {
  639. std::cerr << "automoc4: process for " << mocFilePath << " failed:\n"
  640. << output << std::endl;
  641. this->RunMocFailed = true;
  642. cmSystemTools::RemoveFile(mocFilePath.c_str());
  643. }
  644. return true;
  645. }
  646. return false;
  647. }
  648. std::string cmQtAutomoc::Join(const std::list<std::string>& lst,char separator)
  649. {
  650. if (lst.empty())
  651. {
  652. return "";
  653. }
  654. std::string result;
  655. for (std::list<std::string>::const_iterator it = lst.begin();
  656. it != lst.end();
  657. ++it)
  658. {
  659. result += (*it) + separator;
  660. }
  661. result.erase(result.end() - 1);
  662. return result;
  663. }
  664. bool cmQtAutomoc::StartsWith(const std::string& str, const std::string& with)
  665. {
  666. return (str.substr(0, with.length()) == with);
  667. }
  668. bool cmQtAutomoc::EndsWith(const std::string& str, const std::string& with)
  669. {
  670. if (with.length() > (str.length()))
  671. {
  672. return false;
  673. }
  674. return (str.substr(str.length() - with.length(), with.length()) == with);
  675. }
  676. std::string cmQtAutomoc::ReadAll(const std::string& filename)
  677. {
  678. std::ifstream file(filename.c_str());
  679. std::stringstream stream;
  680. stream << file.rdbuf();
  681. file.close();
  682. return stream.str();
  683. }