cmQtAutomoc.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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" && qtMajorVersion != "5")
  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->GetSafeDefinition("CMAKE_COMMAND"));
  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. const char* tmp = makefile->GetProperty("INCLUDE_DIRECTORIES");
  106. std::string _moc_incs = (tmp!=0 ? tmp : "");
  107. tmp = makefile->GetProperty("DEFINITIONS");
  108. std::string _moc_defs = (tmp!=0 ? tmp : "");
  109. tmp = makefile->GetProperty("COMPILE_DEFINITIONS");
  110. std::string _moc_compile_defs = (tmp!=0 ? tmp : "");
  111. tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  112. std::string _moc_options = (tmp!=0 ? tmp : "");
  113. // forget the variables added here afterwards again:
  114. cmMakefile::ScopePushPop varScope(makefile);
  115. static_cast<void>(varScope);
  116. makefile->AddDefinition("_moc_target_name", automocTargetName.c_str());
  117. makefile->AddDefinition("_moc_incs", _moc_incs.c_str());
  118. makefile->AddDefinition("_moc_defs", _moc_defs.c_str());
  119. makefile->AddDefinition("_moc_compile_defs", _moc_compile_defs.c_str());
  120. makefile->AddDefinition("_moc_options", _moc_options.c_str());
  121. makefile->AddDefinition("_moc_files", _moc_files.c_str());
  122. makefile->AddDefinition("_moc_headers", _moc_headers.c_str());
  123. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  124. std::string inputFile = cmakeRoot;
  125. inputFile += "/Modules/AutomocInfo.cmake.in";
  126. std::string outputFile = targetDir;
  127. outputFile += "/AutomocInfo.cmake";
  128. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  129. false, true, false);
  130. std::string mocCppFile = makefile->GetCurrentOutputDirectory();
  131. mocCppFile += "/";
  132. mocCppFile += automocTargetName;
  133. mocCppFile += ".cpp";
  134. cmSourceFile* mocCppSource = makefile->GetOrCreateSource(mocCppFile.c_str(),
  135. true);
  136. target->AddSourceFile(mocCppSource);
  137. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  138. mocCppFile.c_str(), false);
  139. }
  140. bool cmQtAutomoc::Run(const char* targetDirectory)
  141. {
  142. cmake cm;
  143. cmGlobalGenerator* gg = this->CreateGlobalGenerator(&cm, targetDirectory);
  144. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  145. this->ReadAutomocInfoFile(makefile, targetDirectory);
  146. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  147. this->Init();
  148. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  149. {
  150. this->RunAutomocQt4();
  151. }
  152. this->WriteOldMocDefinitionsFile(targetDirectory);
  153. delete gg;
  154. gg = NULL;
  155. makefile = NULL;
  156. return true;
  157. }
  158. cmGlobalGenerator* cmQtAutomoc::CreateGlobalGenerator(cmake* cm,
  159. const char* targetDirectory)
  160. {
  161. cmGlobalGenerator* gg = new cmGlobalGenerator();
  162. gg->SetCMakeInstance(cm);
  163. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  164. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  165. lg->GetMakefile()->SetStartOutputDirectory(targetDirectory);
  166. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  167. lg->GetMakefile()->SetStartDirectory(targetDirectory);
  168. gg->SetCurrentLocalGenerator(lg);
  169. return gg;
  170. }
  171. bool cmQtAutomoc::ReadAutomocInfoFile(cmMakefile* makefile,
  172. const char* targetDirectory)
  173. {
  174. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  175. cmSystemTools::ConvertToUnixSlashes(filename);
  176. filename += "/AutomocInfo.cmake";
  177. if (!makefile->ReadListFile(0, filename.c_str()))
  178. {
  179. cmSystemTools::Error("Error processing file:", filename.c_str());
  180. return false;
  181. }
  182. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  183. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  184. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  185. this->IncludeProjectDirsBefore = makefile->IsOn(
  186. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  187. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  188. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  189. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  190. this->MocCompileDefinitionsStr = makefile->GetSafeDefinition(
  191. "AM_MOC_COMPILE_DEFINITIONS");
  192. this->MocDefinitionsStr = makefile->GetSafeDefinition("AM_MOC_DEFINITIONS");
  193. this->MocIncludesStr = makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  194. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  195. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  196. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  197. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  198. return true;
  199. }
  200. bool cmQtAutomoc::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  201. const char* targetDirectory)
  202. {
  203. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  204. cmSystemTools::ConvertToUnixSlashes(filename);
  205. filename += "/AutomocOldMocDefinitions.cmake";
  206. if (makefile->ReadListFile(0, filename.c_str()))
  207. {
  208. this->OldMocDefinitionsStr =
  209. makefile->GetSafeDefinition("AM_OLD_MOC_DEFINITIONS");
  210. }
  211. return true;
  212. }
  213. void cmQtAutomoc::WriteOldMocDefinitionsFile(const char* targetDirectory)
  214. {
  215. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  216. cmSystemTools::ConvertToUnixSlashes(filename);
  217. filename += "/AutomocOldMocDefinitions.cmake";
  218. std::fstream outfile;
  219. outfile.open(filename.c_str(),
  220. std::ios::out | std::ios::trunc);
  221. outfile << "set(AM_OLD_MOC_DEFINITIONS \""
  222. << this->Join(this->MocDefinitions, ' ') << "\")\n";
  223. outfile.close();
  224. }
  225. void cmQtAutomoc::Init()
  226. {
  227. this->OutMocCppFilename = this->Builddir;
  228. this->OutMocCppFilename += this->TargetName;
  229. this->OutMocCppFilename += ".cpp";
  230. std::vector<std::string> cdefList;
  231. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  232. if (!cdefList.empty())
  233. {
  234. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  235. it != cdefList.end();
  236. ++it)
  237. {
  238. this->MocDefinitions.push_back("-D" + (*it));
  239. }
  240. }
  241. else
  242. {
  243. std::string tmpMocDefs = this->MocDefinitionsStr;
  244. cmSystemTools::ReplaceString(tmpMocDefs, " ", ";");
  245. std::vector<std::string> defList;
  246. cmSystemTools::ExpandListArgument(tmpMocDefs, defList);
  247. for(std::vector<std::string>::const_iterator it = defList.begin();
  248. it != defList.end();
  249. ++it)
  250. {
  251. if (this->StartsWith(*it, "-D"))
  252. {
  253. this->MocDefinitions.push_back(*it);
  254. }
  255. }
  256. }
  257. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  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. for(std::vector<std::string>::const_iterator it=this->MocOptions.begin();
  627. it != this->MocOptions.end();
  628. ++it)
  629. {
  630. command.push_back(*it);
  631. }
  632. #ifdef _WIN32
  633. command.push_back("-DWIN32");
  634. #endif
  635. command.push_back("-o");
  636. command.push_back(mocFilePath);
  637. command.push_back(sourceFile);
  638. if (this->Verbose)
  639. {
  640. for(std::vector<cmStdString>::const_iterator cmdIt = command.begin();
  641. cmdIt != command.end();
  642. ++cmdIt)
  643. {
  644. std::cout << *cmdIt << " ";
  645. }
  646. std::cout << std::endl;
  647. }
  648. std::string output;
  649. int retVal = 0;
  650. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  651. if (!result || retVal)
  652. {
  653. std::cerr << "AUTOMOC: process for " << mocFilePath << " failed:\n"
  654. << output << std::endl;
  655. this->RunMocFailed = true;
  656. cmSystemTools::RemoveFile(mocFilePath.c_str());
  657. }
  658. return true;
  659. }
  660. return false;
  661. }
  662. std::string cmQtAutomoc::Join(const std::list<std::string>& lst,char separator)
  663. {
  664. if (lst.empty())
  665. {
  666. return "";
  667. }
  668. std::string result;
  669. for (std::list<std::string>::const_iterator it = lst.begin();
  670. it != lst.end();
  671. ++it)
  672. {
  673. result += (*it) + separator;
  674. }
  675. result.erase(result.end() - 1);
  676. return result;
  677. }
  678. bool cmQtAutomoc::StartsWith(const std::string& str, const std::string& with)
  679. {
  680. return (str.substr(0, with.length()) == with);
  681. }
  682. bool cmQtAutomoc::EndsWith(const std::string& str, const std::string& with)
  683. {
  684. if (with.length() > (str.length()))
  685. {
  686. return false;
  687. }
  688. return (str.substr(str.length() - with.length(), with.length()) == with);
  689. }
  690. std::string cmQtAutomoc::ReadAll(const std::string& filename)
  691. {
  692. std::ifstream file(filename.c_str());
  693. std::stringstream stream;
  694. stream << file.rdbuf();
  695. file.close();
  696. return stream.str();
  697. }