cmQtAutomoc.cxx 26 KB

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