cmQtAutomoc.cxx 27 KB

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