cmQtAutomoc.cxx 27 KB

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