cmQtAutomoc.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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->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. 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: empty source file: " << absFilename << std::endl;
  437. return;
  438. }
  439. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  440. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  441. std::string::size_type matchOffset = 0;
  442. if (mocIncludeRegExp.find(contentsString.c_str()))
  443. {
  444. // for every moc include in the file
  445. do
  446. {
  447. const std::string currentMoc = mocIncludeRegExp.match(1);
  448. //std::cout << "found moc include: " << currentMoc << std::endl;
  449. std::string basename = cmsys::SystemTools::
  450. GetFilenameWithoutLastExtension(currentMoc);
  451. const bool moc_style = this->StartsWith(basename, "moc_");
  452. // If the moc include is of the moc_foo.cpp style we expect
  453. // the Q_OBJECT class declaration in a header file.
  454. // If the moc include is of the foo.moc style we need to look for
  455. // a Q_OBJECT macro in the current source file, if it contains the
  456. // macro we generate the moc file from the source file.
  457. // Q_OBJECT
  458. if (moc_style)
  459. {
  460. // basename should be the part of the moc filename used for
  461. // finding the correct header, so we need to remove the moc_ part
  462. basename = basename.substr(4);
  463. bool headerFound = false;
  464. for(std::list<std::string>::const_iterator ext =
  465. headerExtensions.begin();
  466. ext != headerExtensions.end();
  467. ++ext)
  468. {
  469. const std::string &sourceFilePath = absPath + basename + (*ext);
  470. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  471. {
  472. headerFound = true;
  473. includedMocs[sourceFilePath] = currentMoc;
  474. break;
  475. }
  476. }
  477. if (!headerFound)
  478. {
  479. // the moc file is in a subdir => look for the header in the
  480. // same subdir
  481. if (currentMoc.find_first_of('/') != std::string::npos)
  482. {
  483. const std::string &filepath = absPath
  484. + cmsys::SystemTools::GetFilenamePath(currentMoc)
  485. + '/' + basename;
  486. for(std::list<std::string>::const_iterator ext =
  487. headerExtensions.begin();
  488. ext != headerExtensions.end();
  489. ++ext)
  490. {
  491. const std::string &sourceFilePath = filepath + (*ext);
  492. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  493. {
  494. headerFound = true;
  495. includedMocs[sourceFilePath] = currentMoc;
  496. break;
  497. }
  498. }
  499. if (!headerFound)
  500. {
  501. std::cerr << "AUTOMOC: The file \"" << absFilename
  502. << "\" includes the moc file \"" << currentMoc
  503. << "\", but neither \"" << absPath << basename
  504. << '{' << this->Join(headerExtensions, ',')
  505. << "}\" nor \"" << filepath << '{'
  506. << this->Join(headerExtensions, ',') << '}'
  507. << "\" exist." << std::endl;
  508. ::exit(EXIT_FAILURE);
  509. }
  510. }
  511. else
  512. {
  513. std::cerr << "AUTOMOC: The file \"" << absFilename
  514. << "\" includes the moc file \"" << currentMoc
  515. << "\", but \"" << absPath << basename << '{'
  516. << this->Join(headerExtensions, ',') << '}'
  517. << "\" does not exist." << std::endl;
  518. ::exit(EXIT_FAILURE);
  519. }
  520. }
  521. }
  522. else
  523. {
  524. includedMocs[absFilename] = currentMoc;
  525. }
  526. matchOffset += mocIncludeRegExp.end();
  527. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  528. }
  529. // search for header files and private header files we may need to moc:
  530. const std::string basename =
  531. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  532. for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
  533. ext != headerExtensions.end();
  534. ++ext)
  535. {
  536. const std::string headerName = absPath + basename + (*ext);
  537. if (cmsys::SystemTools::FileExists(headerName.c_str()))
  538. {
  539. absHeaders.insert(headerName);
  540. break;
  541. }
  542. }
  543. for(std::list<std::string>::const_iterator ext = headerExtensions.begin();
  544. ext != headerExtensions.end();
  545. ++ext)
  546. {
  547. const std::string privateHeaderName = absPath+basename+"_p"+(*ext);
  548. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str()))
  549. {
  550. absHeaders.insert(privateHeaderName);
  551. break;
  552. }
  553. }
  554. }
  555. void cmQtAutomoc::ParseHeaders(const std::set<std::string>& absHeaders,
  556. const std::map<std::string, std::string>& includedMocs,
  557. std::map<std::string, std::string>& notIncludedMocs)
  558. {
  559. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  560. for(std::set<std::string>::const_iterator hIt=absHeaders.begin();
  561. hIt!=absHeaders.end();
  562. ++hIt)
  563. {
  564. const std::string& headerName = *hIt;
  565. if (includedMocs.find(headerName) == includedMocs.end())
  566. {
  567. if (this->Verbose)
  568. {
  569. std::cout << "AUTOMOC: Checking " << headerName << std::endl;
  570. }
  571. const std::string basename = cmsys::SystemTools::
  572. GetFilenameWithoutLastExtension(headerName);
  573. const std::string currentMoc = "moc_" + basename + ".cpp";
  574. const std::string contents = this->ReadAll(headerName);
  575. if (qObjectRegExp.find(contents))
  576. {
  577. //std::cout << "header contains Q_OBJECT macro";
  578. notIncludedMocs[headerName] = currentMoc;
  579. }
  580. }
  581. }
  582. }
  583. bool cmQtAutomoc::GenerateMoc(const std::string& sourceFile,
  584. const std::string& mocFileName)
  585. {
  586. const std::string mocFilePath = this->Builddir + mocFileName;
  587. int sourceNewerThanMoc = 0;
  588. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile.c_str(),
  589. mocFilePath.c_str(),
  590. &sourceNewerThanMoc);
  591. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  592. {
  593. // make sure the directory for the resulting moc file exists
  594. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  595. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  596. {
  597. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  598. }
  599. std::string msg = "Generating ";
  600. msg += mocFileName;
  601. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  602. |cmsysTerminal_Color_ForegroundBold,
  603. msg.c_str(), true, this->ColorOutput);
  604. std::vector<cmStdString> command;
  605. command.push_back(this->MocExecutable);
  606. for (std::list<std::string>::const_iterator it = this->MocIncludes.begin();
  607. it != this->MocIncludes.end();
  608. ++it)
  609. {
  610. command.push_back(*it);
  611. }
  612. for(std::list<std::string>::const_iterator it=this->MocDefinitions.begin();
  613. it != this->MocDefinitions.end();
  614. ++it)
  615. {
  616. command.push_back(*it);
  617. }
  618. #ifdef _WIN32
  619. command.push_back("-DWIN32");
  620. #endif
  621. command.push_back("-o");
  622. command.push_back(mocFilePath);
  623. command.push_back(sourceFile);
  624. if (this->Verbose)
  625. {
  626. for(std::vector<cmStdString>::const_iterator cmdIt = command.begin();
  627. cmdIt != command.end();
  628. ++cmdIt)
  629. {
  630. std::cout << *cmdIt << " ";
  631. }
  632. std::cout << std::endl;
  633. }
  634. std::string output;
  635. int retVal = 0;
  636. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  637. if (!result || retVal)
  638. {
  639. std::cerr << "AUTOMOC: process for " << mocFilePath << " failed:\n"
  640. << output << std::endl;
  641. this->RunMocFailed = true;
  642. cmSystemTools::RemoveFile(mocFilePath.c_str());
  643. }
  644. return true;
  645. }
  646. return false;
  647. }
  648. std::string cmQtAutomoc::Join(const std::list<std::string>& lst,char separator)
  649. {
  650. if (lst.empty())
  651. {
  652. return "";
  653. }
  654. std::string result;
  655. for (std::list<std::string>::const_iterator it = lst.begin();
  656. it != lst.end();
  657. ++it)
  658. {
  659. result += (*it) + separator;
  660. }
  661. result.erase(result.end() - 1);
  662. return result;
  663. }
  664. bool cmQtAutomoc::StartsWith(const std::string& str, const std::string& with)
  665. {
  666. return (str.substr(0, with.length()) == with);
  667. }
  668. bool cmQtAutomoc::EndsWith(const std::string& str, const std::string& with)
  669. {
  670. if (with.length() > (str.length()))
  671. {
  672. return false;
  673. }
  674. return (str.substr(str.length() - with.length(), with.length()) == with);
  675. }
  676. std::string cmQtAutomoc::ReadAll(const std::string& filename)
  677. {
  678. std::ifstream file(filename.c_str());
  679. std::stringstream stream;
  680. stream << file.rdbuf();
  681. file.close();
  682. return stream.str();
  683. }