cmQtAutomoc.cxx 25 KB

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