cmQtAutomoc.cxx 26 KB

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