cmQtAutomoc.cxx 26 KB

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