cmQtAutoGenerators.cxx 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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. #if defined(_WIN32) && !defined(__CYGWIN__)
  17. # include "cmLocalVisualStudioGenerator.h"
  18. #endif
  19. #include <cmsys/Terminal.h>
  20. #include <cmsys/ios/sstream>
  21. #include <string.h>
  22. #if defined(__APPLE__)
  23. #include <unistd.h>
  24. #endif
  25. #include "cmQtAutoGenerators.h"
  26. static bool requiresMocing(const std::string& text, std::string &macroName)
  27. {
  28. // this simple check is much much faster than the regexp
  29. if (strstr(text.c_str(), "Q_OBJECT") == NULL
  30. && strstr(text.c_str(), "Q_GADGET") == NULL)
  31. {
  32. return false;
  33. }
  34. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  35. if (qObjectRegExp.find(text))
  36. {
  37. macroName = "Q_OBJECT";
  38. return true;
  39. }
  40. cmsys::RegularExpression qGadgetRegExp("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  41. if (qGadgetRegExp.find(text))
  42. {
  43. macroName = "Q_GADGET";
  44. return true;
  45. }
  46. return false;
  47. }
  48. static std::string findMatchingHeader(const std::string& absPath,
  49. const std::string& mocSubDir,
  50. const std::string& basename,
  51. const std::vector<std::string>& headerExtensions)
  52. {
  53. std::string header;
  54. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  55. ext != headerExtensions.end();
  56. ++ext)
  57. {
  58. std::string sourceFilePath = absPath + basename + "." + (*ext);
  59. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  60. {
  61. header = sourceFilePath;
  62. break;
  63. }
  64. if (!mocSubDir.empty())
  65. {
  66. sourceFilePath = mocSubDir + basename + "." + (*ext);
  67. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str()))
  68. {
  69. header = sourceFilePath;
  70. break;
  71. }
  72. }
  73. }
  74. return header;
  75. }
  76. static std::string extractSubDir(const std::string& absPath,
  77. const std::string& currentMoc)
  78. {
  79. std::string subDir;
  80. if (currentMoc.find_first_of('/') != std::string::npos)
  81. {
  82. subDir = absPath
  83. + cmsys::SystemTools::GetFilenamePath(currentMoc) + '/';
  84. }
  85. return subDir;
  86. }
  87. static void copyTargetProperty(cmTarget* destinationTarget,
  88. cmTarget* sourceTarget,
  89. const char* propertyName)
  90. {
  91. const char* propertyValue = sourceTarget->GetProperty(propertyName);
  92. if (propertyValue)
  93. {
  94. destinationTarget->SetProperty(propertyName, propertyValue);
  95. }
  96. }
  97. cmQtAutoGenerators::cmQtAutoGenerators()
  98. :Verbose(cmsys::SystemTools::GetEnv("VERBOSE") != 0)
  99. ,ColorOutput(true)
  100. ,RunMocFailed(false)
  101. ,GenerateAll(false)
  102. {
  103. std::string colorEnv = "";
  104. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  105. if(!colorEnv.empty())
  106. {
  107. if(cmSystemTools::IsOn(colorEnv.c_str()))
  108. {
  109. this->ColorOutput = true;
  110. }
  111. else
  112. {
  113. this->ColorOutput = false;
  114. }
  115. }
  116. }
  117. bool cmQtAutoGenerators::InitializeMocSourceFile(cmTarget* target)
  118. {
  119. cmMakefile* makefile = target->GetMakefile();
  120. // don't do anything if there is no Qt4 or Qt5Core (which contains moc):
  121. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  122. if (qtMajorVersion == "")
  123. {
  124. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  125. }
  126. if (qtMajorVersion != "4" && qtMajorVersion != "5")
  127. {
  128. return false;
  129. }
  130. std::string automocTargetName = target->GetName();
  131. automocTargetName += "_automoc";
  132. std::string mocCppFile = makefile->GetCurrentOutputDirectory();
  133. mocCppFile += "/";
  134. mocCppFile += automocTargetName;
  135. mocCppFile += ".cpp";
  136. cmSourceFile* mocCppSource = makefile->GetOrCreateSource(mocCppFile.c_str(),
  137. true);
  138. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  139. mocCppFile.c_str(), false);
  140. target->AddSourceFile(mocCppSource);
  141. return true;
  142. }
  143. static void GetCompileDefinitionsAndDirectories(cmTarget *target,
  144. const char * config,
  145. std::string &incs,
  146. std::string &defs)
  147. {
  148. cmMakefile* makefile = target->GetMakefile();
  149. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  150. std::vector<std::string> includeDirs;
  151. cmGeneratorTarget gtgt(target);
  152. // Get the include dirs for this target, without stripping the implicit
  153. // include dirs off, see http://public.kitware.com/Bug/view.php?id=13667
  154. localGen->GetIncludeDirectories(includeDirs, &gtgt, "CXX", config, false);
  155. const char* sep = "";
  156. incs = "";
  157. for(std::vector<std::string>::const_iterator incDirIt = includeDirs.begin();
  158. incDirIt != includeDirs.end();
  159. ++incDirIt)
  160. {
  161. incs += sep;
  162. sep = ";";
  163. incs += *incDirIt;
  164. }
  165. std::set<std::string> defines;
  166. localGen->AddCompileDefinitions(defines, target, config);
  167. sep = "";
  168. for(std::set<std::string>::const_iterator defIt = defines.begin();
  169. defIt != defines.end();
  170. ++defIt)
  171. {
  172. defs += sep;
  173. sep = ";";
  174. defs += *defIt;
  175. }
  176. }
  177. void cmQtAutoGenerators::SetupAutoGenerateTarget(cmTarget* target)
  178. {
  179. cmMakefile* makefile = target->GetMakefile();
  180. const char* targetName = target->GetName();
  181. bool relaxedMode = makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE");
  182. // create a custom target for running automoc at buildtime:
  183. std::string automocTargetName = targetName;
  184. automocTargetName += "_automoc";
  185. std::string targetDir = makefile->GetCurrentOutputDirectory();
  186. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  187. targetDir += "/";
  188. targetDir += automocTargetName;
  189. targetDir += ".dir/";
  190. cmCustomCommandLine currentLine;
  191. currentLine.push_back(makefile->GetSafeDefinition("CMAKE_COMMAND"));
  192. currentLine.push_back("-E");
  193. currentLine.push_back("cmake_autogen");
  194. currentLine.push_back(targetDir);
  195. currentLine.push_back("$<CONFIGURATION>");
  196. cmCustomCommandLines commandLines;
  197. commandLines.push_back(currentLine);
  198. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  199. "", makefile->GetCurrentOutputDirectory());
  200. std::vector<std::string> depends;
  201. std::string automocComment = "Automoc for target ";
  202. automocComment += targetName;
  203. #if defined(_WIN32) && !defined(__CYGWIN__)
  204. bool usePRE_BUILD = false;
  205. cmLocalGenerator* localGen = makefile->GetLocalGenerator();
  206. cmGlobalGenerator* gg = localGen->GetGlobalGenerator();
  207. if(strstr(gg->GetName(), "Visual Studio"))
  208. {
  209. cmLocalVisualStudioGenerator* vslg =
  210. static_cast<cmLocalVisualStudioGenerator*>(localGen);
  211. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  212. // reduce the number of targets loaded into the IDE.
  213. // This also works around a VS 11 bug that may skip updating the target:
  214. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  215. usePRE_BUILD = vslg->GetVersion() >= cmLocalVisualStudioGenerator::VS7;
  216. }
  217. if(usePRE_BUILD)
  218. {
  219. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  220. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  221. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  222. std::vector<std::string> no_output;
  223. cmCustomCommand cc(makefile, no_output, depends,
  224. commandLines, automocComment.c_str(),
  225. workingDirectory.c_str());
  226. cc.SetEscapeOldStyle(false);
  227. cc.SetEscapeAllowMakeVars(true);
  228. target->GetPreBuildCommands().push_back(cc);
  229. }
  230. else
  231. #endif
  232. {
  233. cmTarget* automocTarget = makefile->AddUtilityCommand(
  234. automocTargetName.c_str(), true,
  235. workingDirectory.c_str(), depends,
  236. commandLines, false, automocComment.c_str());
  237. // Set target folder
  238. const char* automocFolder = makefile->GetCMakeInstance()->GetProperty(
  239. "AUTOMOC_TARGETS_FOLDER");
  240. if (automocFolder && *automocFolder)
  241. {
  242. automocTarget->SetProperty("FOLDER", automocFolder);
  243. }
  244. else
  245. {
  246. // inherit FOLDER property from target (#13688)
  247. copyTargetProperty(automocTarget, target, "FOLDER");
  248. }
  249. target->AddUtility(automocTargetName.c_str());
  250. }
  251. // configure a file to get all information to automoc at buildtime:
  252. std::string _moc_files;
  253. std::string _moc_headers;
  254. const char* sepFiles = "";
  255. const char* sepHeaders = "";
  256. const std::vector<cmSourceFile*>& srcFiles = target->GetSourceFiles();
  257. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  258. fileIt != srcFiles.end();
  259. ++fileIt)
  260. {
  261. cmSourceFile* sf = *fileIt;
  262. std::string absFile = cmsys::SystemTools::GetRealPath(
  263. sf->GetFullPath().c_str());
  264. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  265. bool generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  266. if ((skip==false) && (generated == false))
  267. {
  268. std::string ext = sf->GetExtension();
  269. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  270. ext.c_str());
  271. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  272. {
  273. _moc_files += sepFiles;
  274. _moc_files += absFile;
  275. sepFiles = ";";
  276. }
  277. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  278. {
  279. _moc_headers += sepHeaders;
  280. _moc_headers += absFile;
  281. sepHeaders = ";";
  282. }
  283. }
  284. }
  285. const char* tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  286. std::string _moc_options = (tmp!=0 ? tmp : "");
  287. // forget the variables added here afterwards again:
  288. cmMakefile::ScopePushPop varScope(makefile);
  289. static_cast<void>(varScope);
  290. makefile->AddDefinition("_moc_target_name",
  291. cmLocalGenerator::EscapeForCMake(automocTargetName.c_str()).c_str());
  292. makefile->AddDefinition("_moc_options",
  293. cmLocalGenerator::EscapeForCMake(_moc_options.c_str()).c_str());
  294. makefile->AddDefinition("_moc_files",
  295. cmLocalGenerator::EscapeForCMake(_moc_files.c_str()).c_str());
  296. makefile->AddDefinition("_moc_headers",
  297. cmLocalGenerator::EscapeForCMake(_moc_headers.c_str()).c_str());
  298. makefile->AddDefinition("_moc_relaxed_mode", relaxedMode ? "TRUE" : "FALSE");
  299. std::string _moc_incs;
  300. std::string _moc_compile_defs;
  301. std::vector<std::string> configs;
  302. const char *config = makefile->GetConfigurations(configs);
  303. GetCompileDefinitionsAndDirectories(target, config,
  304. _moc_incs, _moc_compile_defs);
  305. makefile->AddDefinition("_moc_incs",
  306. cmLocalGenerator::EscapeForCMake(_moc_incs.c_str()).c_str());
  307. makefile->AddDefinition("_moc_compile_defs",
  308. cmLocalGenerator::EscapeForCMake(_moc_compile_defs.c_str()).c_str());
  309. std::map<std::string, std::string> configIncludes;
  310. std::map<std::string, std::string> configDefines;
  311. for (std::vector<std::string>::const_iterator li = configs.begin();
  312. li != configs.end(); ++li)
  313. {
  314. std::string config_moc_incs;
  315. std::string config_moc_compile_defs;
  316. GetCompileDefinitionsAndDirectories(target, li->c_str(),
  317. config_moc_incs,
  318. config_moc_compile_defs);
  319. if (config_moc_incs != _moc_incs)
  320. {
  321. configIncludes["_moc_incs_" + *li] =
  322. cmLocalGenerator::EscapeForCMake(config_moc_incs.c_str());
  323. if(_moc_incs.empty())
  324. {
  325. _moc_incs = config_moc_incs;
  326. }
  327. }
  328. if (config_moc_compile_defs != _moc_compile_defs)
  329. {
  330. configDefines["_moc_compile_defs_" + *li] =
  331. cmLocalGenerator::EscapeForCMake(config_moc_compile_defs.c_str());
  332. if(_moc_compile_defs.empty())
  333. {
  334. _moc_compile_defs = config_moc_compile_defs;
  335. }
  336. }
  337. }
  338. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  339. if (!qtVersion)
  340. {
  341. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  342. }
  343. if (const char *targetQtVersion =
  344. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", 0))
  345. {
  346. qtVersion = targetQtVersion;
  347. }
  348. if (qtVersion)
  349. {
  350. makefile->AddDefinition("_target_qt_version", qtVersion);
  351. }
  352. {
  353. const char *qtMoc = makefile->GetSafeDefinition("QT_MOC_EXECUTABLE");
  354. makefile->AddDefinition("_qt_moc_executable", qtMoc);
  355. }
  356. if (strcmp(qtVersion, "5") == 0)
  357. {
  358. cmTarget *qt5Moc = makefile->FindTargetToUse("Qt5::moc");
  359. if (!qt5Moc)
  360. {
  361. cmSystemTools::Error("Qt5::moc target not found ",
  362. automocTargetName.c_str());
  363. return;
  364. }
  365. makefile->AddDefinition("_qt_moc_executable", qt5Moc->GetLocation(0));
  366. }
  367. else
  368. {
  369. if (strcmp(qtVersion, "4") != 0)
  370. {
  371. cmSystemTools::Error("The CMAKE_AUTOMOC feature supports only Qt 4 and "
  372. "Qt 5 ", automocTargetName.c_str());
  373. }
  374. }
  375. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  376. std::string inputFile = cmakeRoot;
  377. inputFile += "/Modules/AutomocInfo.cmake.in";
  378. std::string outputFile = targetDir;
  379. outputFile += "/AutomocInfo.cmake";
  380. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  381. false, true, false);
  382. if (!configDefines.empty() || !configIncludes.empty())
  383. {
  384. std::ofstream infoFile(outputFile.c_str(), std::ios::app);
  385. if ( !infoFile )
  386. {
  387. std::string error = "Internal CMake error when trying to open file: ";
  388. error += outputFile.c_str();
  389. error += " for writing.";
  390. cmSystemTools::Error(error.c_str());
  391. return;
  392. }
  393. if (!configDefines.empty())
  394. {
  395. for (std::map<std::string, std::string>::iterator
  396. it = configDefines.begin(), end = configDefines.end();
  397. it != end; ++it)
  398. {
  399. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  400. " " << it->second << ")\n";
  401. }
  402. }
  403. if (!configIncludes.empty())
  404. {
  405. for (std::map<std::string, std::string>::iterator
  406. it = configIncludes.begin(), end = configIncludes.end();
  407. it != end; ++it)
  408. {
  409. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  410. " " << it->second << ")\n";
  411. }
  412. }
  413. }
  414. }
  415. bool cmQtAutoGenerators::Run(const char* targetDirectory, const char *config)
  416. {
  417. bool success = true;
  418. cmake cm;
  419. cmGlobalGenerator* gg = this->CreateGlobalGenerator(&cm, targetDirectory);
  420. cmMakefile* makefile = gg->GetCurrentLocalGenerator()->GetMakefile();
  421. this->ReadAutomocInfoFile(makefile, targetDirectory, config);
  422. this->ReadOldMocDefinitionsFile(makefile, targetDirectory);
  423. this->Init();
  424. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5")
  425. {
  426. success = this->RunAutomoc(makefile);
  427. }
  428. this->WriteOldMocDefinitionsFile(targetDirectory);
  429. delete gg;
  430. gg = NULL;
  431. makefile = NULL;
  432. return success;
  433. }
  434. cmGlobalGenerator* cmQtAutoGenerators::CreateGlobalGenerator(cmake* cm,
  435. const char* targetDirectory)
  436. {
  437. cmGlobalGenerator* gg = new cmGlobalGenerator();
  438. gg->SetCMakeInstance(cm);
  439. cmLocalGenerator* lg = gg->CreateLocalGenerator();
  440. lg->GetMakefile()->SetHomeOutputDirectory(targetDirectory);
  441. lg->GetMakefile()->SetStartOutputDirectory(targetDirectory);
  442. lg->GetMakefile()->SetHomeDirectory(targetDirectory);
  443. lg->GetMakefile()->SetStartDirectory(targetDirectory);
  444. gg->SetCurrentLocalGenerator(lg);
  445. return gg;
  446. }
  447. bool cmQtAutoGenerators::ReadAutomocInfoFile(cmMakefile* makefile,
  448. const char* targetDirectory,
  449. const char *config)
  450. {
  451. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  452. cmSystemTools::ConvertToUnixSlashes(filename);
  453. filename += "/AutomocInfo.cmake";
  454. if (!makefile->ReadListFile(0, filename.c_str()))
  455. {
  456. cmSystemTools::Error("Error processing file: ", filename.c_str());
  457. return false;
  458. }
  459. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  460. if (this->QtMajorVersion == "")
  461. {
  462. this->QtMajorVersion = makefile->GetSafeDefinition(
  463. "AM_Qt5Core_VERSION_MAJOR");
  464. }
  465. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  466. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  467. this->IncludeProjectDirsBefore = makefile->IsOn(
  468. "AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  469. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  470. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  471. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  472. std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
  473. std::string compileDefsProp = compileDefsPropOrig;
  474. if(config)
  475. {
  476. compileDefsProp += "_";
  477. compileDefsProp += config;
  478. }
  479. const char *compileDefs = makefile->GetDefinition(compileDefsProp.c_str());
  480. this->MocCompileDefinitionsStr = compileDefs ? compileDefs
  481. : makefile->GetSafeDefinition(compileDefsPropOrig.c_str());
  482. std::string includesPropOrig = "AM_MOC_INCLUDES";
  483. std::string includesProp = includesPropOrig;
  484. if(config)
  485. {
  486. includesProp += "_";
  487. includesProp += config;
  488. }
  489. const char *includes = makefile->GetDefinition(includesProp.c_str());
  490. this->MocIncludesStr = includes ? includes
  491. : makefile->GetSafeDefinition(includesPropOrig.c_str());
  492. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  493. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  494. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  495. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  496. this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
  497. this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
  498. return true;
  499. }
  500. std::string cmQtAutoGenerators::MakeCompileSettingsString(cmMakefile* makefile)
  501. {
  502. std::string s;
  503. s += makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  504. s += " ~~~ ";
  505. s += makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  506. s += " ~~~ ";
  507. s += makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  508. s += " ~~~ ";
  509. s += makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE") ? "TRUE"
  510. : "FALSE";
  511. s += " ~~~ ";
  512. return s;
  513. }
  514. bool cmQtAutoGenerators::ReadOldMocDefinitionsFile(cmMakefile* makefile,
  515. const char* targetDirectory)
  516. {
  517. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  518. cmSystemTools::ConvertToUnixSlashes(filename);
  519. filename += "/AutomocOldMocDefinitions.cmake";
  520. if (makefile->ReadListFile(0, filename.c_str()))
  521. {
  522. this->OldCompileSettingsStr =
  523. makefile->GetSafeDefinition("AM_OLD_COMPILE_SETTINGS");
  524. }
  525. return true;
  526. }
  527. void
  528. cmQtAutoGenerators::WriteOldMocDefinitionsFile(const char* targetDirectory)
  529. {
  530. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  531. cmSystemTools::ConvertToUnixSlashes(filename);
  532. filename += "/AutomocOldMocDefinitions.cmake";
  533. std::fstream outfile;
  534. outfile.open(filename.c_str(),
  535. std::ios::out | std::ios::trunc);
  536. outfile << "set(AM_OLD_COMPILE_SETTINGS "
  537. << cmLocalGenerator::EscapeForCMake(
  538. this->CurrentCompileSettingsStr.c_str()) << ")\n";
  539. outfile.close();
  540. }
  541. void cmQtAutoGenerators::Init()
  542. {
  543. this->OutMocCppFilename = this->Builddir;
  544. this->OutMocCppFilename += this->TargetName;
  545. this->OutMocCppFilename += ".cpp";
  546. std::vector<std::string> cdefList;
  547. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  548. for(std::vector<std::string>::const_iterator it = cdefList.begin();
  549. it != cdefList.end();
  550. ++it)
  551. {
  552. this->MocDefinitions.push_back("-D" + (*it));
  553. }
  554. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  555. std::vector<std::string> incPaths;
  556. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  557. std::set<std::string> frameworkPaths;
  558. for(std::vector<std::string>::const_iterator it = incPaths.begin();
  559. it != incPaths.end();
  560. ++it)
  561. {
  562. const std::string &path = *it;
  563. this->MocIncludes.push_back("-I" + path);
  564. if (this->EndsWith(path, ".framework/Headers"))
  565. {
  566. // Go up twice to get to the framework root
  567. std::vector<std::string> pathComponents;
  568. cmsys::SystemTools::SplitPath(path.c_str(), pathComponents);
  569. std::string frameworkPath =cmsys::SystemTools::JoinPath(
  570. pathComponents.begin(), pathComponents.end() - 2);
  571. frameworkPaths.insert(frameworkPath);
  572. }
  573. }
  574. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  575. it != frameworkPaths.end(); ++it)
  576. {
  577. this->MocIncludes.push_back("-F");
  578. this->MocIncludes.push_back(*it);
  579. }
  580. if (this->IncludeProjectDirsBefore)
  581. {
  582. const std::string &binDir = "-I" + this->ProjectBinaryDir;
  583. const std::string srcDir = "-I" + this->ProjectSourceDir;
  584. std::list<std::string> sortedMocIncludes;
  585. std::list<std::string>::iterator it = this->MocIncludes.begin();
  586. while (it != this->MocIncludes.end())
  587. {
  588. if (this->StartsWith(*it, binDir))
  589. {
  590. sortedMocIncludes.push_back(*it);
  591. it = this->MocIncludes.erase(it);
  592. }
  593. else
  594. {
  595. ++it;
  596. }
  597. }
  598. it = this->MocIncludes.begin();
  599. while (it != this->MocIncludes.end())
  600. {
  601. if (this->StartsWith(*it, srcDir))
  602. {
  603. sortedMocIncludes.push_back(*it);
  604. it = this->MocIncludes.erase(it);
  605. }
  606. else
  607. {
  608. ++it;
  609. }
  610. }
  611. sortedMocIncludes.insert(sortedMocIncludes.end(),
  612. this->MocIncludes.begin(), this->MocIncludes.end());
  613. this->MocIncludes = sortedMocIncludes;
  614. }
  615. }
  616. bool cmQtAutoGenerators::RunAutomoc(cmMakefile* makefile)
  617. {
  618. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilename.c_str())
  619. || (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr))
  620. {
  621. this->GenerateAll = true;
  622. }
  623. // the program goes through all .cpp files to see which moc files are
  624. // included. It is not really interesting how the moc file is named, but
  625. // what file the moc is created from. Once a moc is included the same moc
  626. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  627. // header containing Q_OBJECT where no corresponding moc file is included
  628. // anywhere a moc_<filename>.cpp file is created and included in
  629. // the _automoc.cpp file.
  630. // key = moc source filepath, value = moc output filepath
  631. std::map<std::string, std::string> includedMocs;
  632. // collect all headers which may need to be mocced
  633. std::set<std::string> headerFiles;
  634. std::vector<std::string> sourceFiles;
  635. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  636. const std::vector<std::string>& headerExtensions =
  637. makefile->GetHeaderExtensions();
  638. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  639. it != sourceFiles.end();
  640. ++it)
  641. {
  642. const std::string &absFilename = *it;
  643. if (this->Verbose)
  644. {
  645. std::cout << "AUTOMOC: Checking " << absFilename << std::endl;
  646. }
  647. if (this->RelaxedMode)
  648. {
  649. this->ParseCppFile(absFilename, headerExtensions, includedMocs);
  650. }
  651. else
  652. {
  653. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs);
  654. }
  655. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  656. }
  657. std::vector<std::string> headerFilesVec;
  658. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  659. for (std::vector<std::string>::const_iterator it = headerFilesVec.begin();
  660. it != headerFilesVec.end();
  661. ++it)
  662. {
  663. headerFiles.insert(*it);
  664. }
  665. // key = moc source filepath, value = moc output filename
  666. std::map<std::string, std::string> notIncludedMocs;
  667. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs);
  668. // run moc on all the moc's that are #included in source files
  669. for(std::map<std::string, std::string>::const_iterator
  670. it = includedMocs.begin();
  671. it != includedMocs.end();
  672. ++it)
  673. {
  674. this->GenerateMoc(it->first, it->second);
  675. }
  676. cmsys_ios::stringstream outStream;
  677. outStream << "/* This file is autogenerated, do not edit*/\n";
  678. bool automocCppChanged = false;
  679. if (notIncludedMocs.empty())
  680. {
  681. outStream << "enum some_compilers { need_more_than_nothing };\n";
  682. }
  683. else
  684. {
  685. // run moc on the remaining headers and include them in
  686. // the _automoc.cpp file
  687. for(std::map<std::string, std::string>::const_iterator
  688. it = notIncludedMocs.begin();
  689. it != notIncludedMocs.end();
  690. ++it)
  691. {
  692. bool mocSuccess = this->GenerateMoc(it->first, it->second);
  693. if (mocSuccess)
  694. {
  695. automocCppChanged = true;
  696. }
  697. outStream << "#include \"" << it->second << "\"\n";
  698. }
  699. }
  700. if (this->RunMocFailed)
  701. {
  702. std::cerr << "moc failed..."<< std::endl;
  703. return false;
  704. }
  705. outStream.flush();
  706. std::string automocSource = outStream.str();
  707. if (!automocCppChanged)
  708. {
  709. // compare contents of the _automoc.cpp file
  710. const std::string oldContents = this->ReadAll(this->OutMocCppFilename);
  711. if (oldContents == automocSource)
  712. {
  713. // nothing changed: don't touch the _automoc.cpp file
  714. return true;
  715. }
  716. }
  717. // source file that includes all remaining moc files (_automoc.cpp file)
  718. std::fstream outfile;
  719. outfile.open(this->OutMocCppFilename.c_str(),
  720. std::ios::out | std::ios::trunc);
  721. outfile << automocSource;
  722. outfile.close();
  723. return true;
  724. }
  725. void cmQtAutoGenerators::ParseCppFile(const std::string& absFilename,
  726. const std::vector<std::string>& headerExtensions,
  727. std::map<std::string, std::string>& includedMocs)
  728. {
  729. cmsys::RegularExpression mocIncludeRegExp(
  730. "[\n][ \t]*#[ \t]*include[ \t]+"
  731. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  732. const std::string contentsString = this->ReadAll(absFilename);
  733. if (contentsString.empty())
  734. {
  735. std::cerr << "AUTOMOC: warning: " << absFilename << ": file is empty\n"
  736. << std::endl;
  737. return;
  738. }
  739. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  740. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  741. const std::string scannedFileBasename = cmsys::SystemTools::
  742. GetFilenameWithoutLastExtension(absFilename);
  743. std::string macroName;
  744. const bool requiresMoc = requiresMocing(contentsString, macroName);
  745. bool dotMocIncluded = false;
  746. bool mocUnderscoreIncluded = false;
  747. std::string ownMocUnderscoreFile;
  748. std::string ownDotMocFile;
  749. std::string ownMocHeaderFile;
  750. std::string::size_type matchOffset = 0;
  751. // first a simple string check for "moc" is *much* faster than the regexp,
  752. // and if the string search already fails, we don't have to try the
  753. // expensive regexp
  754. if ((strstr(contentsString.c_str(), "moc") != NULL)
  755. && (mocIncludeRegExp.find(contentsString)))
  756. {
  757. // for every moc include in the file
  758. do
  759. {
  760. const std::string currentMoc = mocIncludeRegExp.match(1);
  761. //std::cout << "found moc include: " << currentMoc << std::endl;
  762. std::string basename = cmsys::SystemTools::
  763. GetFilenameWithoutLastExtension(currentMoc);
  764. const bool moc_style = this->StartsWith(basename, "moc_");
  765. // If the moc include is of the moc_foo.cpp style we expect
  766. // the Q_OBJECT class declaration in a header file.
  767. // If the moc include is of the foo.moc style we need to look for
  768. // a Q_OBJECT macro in the current source file, if it contains the
  769. // macro we generate the moc file from the source file.
  770. // Q_OBJECT
  771. if (moc_style)
  772. {
  773. // basename should be the part of the moc filename used for
  774. // finding the correct header, so we need to remove the moc_ part
  775. basename = basename.substr(4);
  776. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  777. std::string headerToMoc = findMatchingHeader(
  778. absPath, mocSubDir, basename, headerExtensions);
  779. if (!headerToMoc.empty())
  780. {
  781. includedMocs[headerToMoc] = currentMoc;
  782. if (basename == scannedFileBasename)
  783. {
  784. mocUnderscoreIncluded = true;
  785. ownMocUnderscoreFile = currentMoc;
  786. ownMocHeaderFile = headerToMoc;
  787. }
  788. }
  789. else
  790. {
  791. std::cerr << "AUTOMOC: error: " << absFilename << " The file "
  792. << "includes the moc file \"" << currentMoc << "\", "
  793. << "but could not find header \"" << basename
  794. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  795. if (mocSubDir.empty())
  796. {
  797. std::cerr << "in " << absPath << "\n" << std::endl;
  798. }
  799. else
  800. {
  801. std::cerr << "neither in " << absPath
  802. << " nor in " << mocSubDir << "\n" << std::endl;
  803. }
  804. ::exit(EXIT_FAILURE);
  805. }
  806. }
  807. else
  808. {
  809. std::string fileToMoc = absFilename;
  810. if ((basename != scannedFileBasename) || (requiresMoc==false))
  811. {
  812. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  813. std::string headerToMoc = findMatchingHeader(
  814. absPath, mocSubDir, basename, headerExtensions);
  815. if (!headerToMoc.empty())
  816. {
  817. // this is for KDE4 compatibility:
  818. fileToMoc = headerToMoc;
  819. if ((requiresMoc==false) &&(basename==scannedFileBasename))
  820. {
  821. std::cerr << "AUTOMOC: warning: " << absFilename << ": The file "
  822. "includes the moc file \"" << currentMoc <<
  823. "\", but does not contain a " << macroName
  824. << " macro. Running moc on "
  825. << "\"" << headerToMoc << "\" ! Include \"moc_"
  826. << basename << ".cpp\" for a compatiblity with "
  827. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  828. << std::endl;
  829. }
  830. else
  831. {
  832. std::cerr << "AUTOMOC: warning: " << absFilename << ": The file "
  833. "includes the moc file \"" << currentMoc <<
  834. "\" instead of \"moc_" << basename << ".cpp\". "
  835. "Running moc on "
  836. << "\"" << headerToMoc << "\" ! Include \"moc_"
  837. << basename << ".cpp\" for compatiblity with "
  838. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  839. << std::endl;
  840. }
  841. }
  842. else
  843. {
  844. std::cerr <<"AUTOMOC: error: " << absFilename << ": The file "
  845. "includes the moc file \"" << currentMoc <<
  846. "\", which seems to be the moc file from a different "
  847. "source file. CMake also could not find a matching "
  848. "header.\n" << std::endl;
  849. ::exit(EXIT_FAILURE);
  850. }
  851. }
  852. else
  853. {
  854. dotMocIncluded = true;
  855. ownDotMocFile = currentMoc;
  856. }
  857. includedMocs[fileToMoc] = currentMoc;
  858. }
  859. matchOffset += mocIncludeRegExp.end();
  860. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  861. }
  862. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  863. // If this is the case, the moc_foo.cpp should probably be generated from
  864. // foo.cpp instead of foo.h, because otherwise it won't build.
  865. // But warn, since this is not how it is supposed to be used.
  866. if ((dotMocIncluded == false) && (requiresMoc == true))
  867. {
  868. if (mocUnderscoreIncluded == true)
  869. {
  870. // this is for KDE4 compatibility:
  871. std::cerr << "AUTOMOC: warning: " << absFilename << ": The file "
  872. << "contains a " << macroName << " macro, but does not "
  873. "include "
  874. << "\"" << scannedFileBasename << ".moc\", but instead "
  875. "includes "
  876. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  877. << "\"" << absFilename << "\" ! Better include \""
  878. << scannedFileBasename << ".moc\" for compatiblity with "
  879. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  880. << std::endl;
  881. includedMocs[absFilename] = ownMocUnderscoreFile;
  882. includedMocs.erase(ownMocHeaderFile);
  883. }
  884. else
  885. {
  886. // otherwise always error out since it will not compile:
  887. std::cerr << "AUTOMOC: error: " << absFilename << ": The file "
  888. << "contains a " << macroName << " macro, but does not "
  889. "include "
  890. << "\"" << scannedFileBasename << ".moc\" !\n"
  891. << std::endl;
  892. ::exit(EXIT_FAILURE);
  893. }
  894. }
  895. }
  896. void cmQtAutoGenerators::StrictParseCppFile(const std::string& absFilename,
  897. const std::vector<std::string>& headerExtensions,
  898. std::map<std::string, std::string>& includedMocs)
  899. {
  900. cmsys::RegularExpression mocIncludeRegExp(
  901. "[\n][ \t]*#[ \t]*include[ \t]+"
  902. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  903. const std::string contentsString = this->ReadAll(absFilename);
  904. if (contentsString.empty())
  905. {
  906. std::cerr << "AUTOMOC: warning: " << absFilename << ": file is empty\n"
  907. << std::endl;
  908. return;
  909. }
  910. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  911. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  912. const std::string scannedFileBasename = cmsys::SystemTools::
  913. GetFilenameWithoutLastExtension(absFilename);
  914. bool dotMocIncluded = false;
  915. std::string::size_type matchOffset = 0;
  916. // first a simple string check for "moc" is *much* faster than the regexp,
  917. // and if the string search already fails, we don't have to try the
  918. // expensive regexp
  919. if ((strstr(contentsString.c_str(), "moc") != NULL)
  920. && (mocIncludeRegExp.find(contentsString)))
  921. {
  922. // for every moc include in the file
  923. do
  924. {
  925. const std::string currentMoc = mocIncludeRegExp.match(1);
  926. std::string basename = cmsys::SystemTools::
  927. GetFilenameWithoutLastExtension(currentMoc);
  928. const bool mocUnderscoreStyle = this->StartsWith(basename, "moc_");
  929. // If the moc include is of the moc_foo.cpp style we expect
  930. // the Q_OBJECT class declaration in a header file.
  931. // If the moc include is of the foo.moc style we need to look for
  932. // a Q_OBJECT macro in the current source file, if it contains the
  933. // macro we generate the moc file from the source file.
  934. if (mocUnderscoreStyle)
  935. {
  936. // basename should be the part of the moc filename used for
  937. // finding the correct header, so we need to remove the moc_ part
  938. basename = basename.substr(4);
  939. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  940. std::string headerToMoc = findMatchingHeader(
  941. absPath, mocSubDir, basename, headerExtensions);
  942. if (!headerToMoc.empty())
  943. {
  944. includedMocs[headerToMoc] = currentMoc;
  945. }
  946. else
  947. {
  948. std::cerr << "AUTOMOC: error: " << absFilename << " The file "
  949. << "includes the moc file \"" << currentMoc << "\", "
  950. << "but could not find header \"" << basename
  951. << '{' << this->Join(headerExtensions, ',') << "}\" ";
  952. if (mocSubDir.empty())
  953. {
  954. std::cerr << "in " << absPath << "\n" << std::endl;
  955. }
  956. else
  957. {
  958. std::cerr << "neither in " << absPath
  959. << " nor in " << mocSubDir << "\n" << std::endl;
  960. }
  961. ::exit(EXIT_FAILURE);
  962. }
  963. }
  964. else
  965. {
  966. if (basename != scannedFileBasename)
  967. {
  968. std::cerr <<"AUTOMOC: error: " << absFilename << ": The file "
  969. "includes the moc file \"" << currentMoc <<
  970. "\", which seems to be the moc file from a different "
  971. "source file. This is not supported. "
  972. "Include \"" << scannedFileBasename << ".moc\" to run "
  973. "moc on this source file.\n" << std::endl;
  974. ::exit(EXIT_FAILURE);
  975. }
  976. dotMocIncluded = true;
  977. includedMocs[absFilename] = currentMoc;
  978. }
  979. matchOffset += mocIncludeRegExp.end();
  980. } while(mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  981. }
  982. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  983. // If this is the case, the moc_foo.cpp should probably be generated from
  984. // foo.cpp instead of foo.h, because otherwise it won't build.
  985. // But warn, since this is not how it is supposed to be used.
  986. std::string macroName;
  987. if ((dotMocIncluded == false) && (requiresMocing(contentsString,
  988. macroName)))
  989. {
  990. // otherwise always error out since it will not compile:
  991. std::cerr << "AUTOMOC: error: " << absFilename << ": The file "
  992. << "contains a " << macroName << " macro, but does not include "
  993. << "\"" << scannedFileBasename << ".moc\" !\n"
  994. << std::endl;
  995. ::exit(EXIT_FAILURE);
  996. }
  997. }
  998. void
  999. cmQtAutoGenerators::SearchHeadersForCppFile(const std::string& absFilename,
  1000. const std::vector<std::string>& headerExtensions,
  1001. std::set<std::string>& absHeaders)
  1002. {
  1003. // search for header files and private header files we may need to moc:
  1004. const std::string basename =
  1005. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  1006. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  1007. cmsys::SystemTools::GetRealPath(absFilename.c_str())) + '/';
  1008. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1009. ext != headerExtensions.end();
  1010. ++ext)
  1011. {
  1012. const std::string headerName = absPath + basename + "." + (*ext);
  1013. if (cmsys::SystemTools::FileExists(headerName.c_str()))
  1014. {
  1015. absHeaders.insert(headerName);
  1016. break;
  1017. }
  1018. }
  1019. for(std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  1020. ext != headerExtensions.end();
  1021. ++ext)
  1022. {
  1023. const std::string privateHeaderName = absPath+basename+"_p."+(*ext);
  1024. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str()))
  1025. {
  1026. absHeaders.insert(privateHeaderName);
  1027. break;
  1028. }
  1029. }
  1030. }
  1031. void cmQtAutoGenerators::ParseHeaders(const std::set<std::string>& absHeaders,
  1032. const std::map<std::string, std::string>& includedMocs,
  1033. std::map<std::string, std::string>& notIncludedMocs)
  1034. {
  1035. for(std::set<std::string>::const_iterator hIt=absHeaders.begin();
  1036. hIt!=absHeaders.end();
  1037. ++hIt)
  1038. {
  1039. const std::string& headerName = *hIt;
  1040. if (includedMocs.find(headerName) == includedMocs.end())
  1041. {
  1042. if (this->Verbose)
  1043. {
  1044. std::cout << "AUTOMOC: Checking " << headerName << std::endl;
  1045. }
  1046. const std::string basename = cmsys::SystemTools::
  1047. GetFilenameWithoutLastExtension(headerName);
  1048. const std::string currentMoc = "moc_" + basename + ".cpp";
  1049. const std::string contents = this->ReadAll(headerName);
  1050. std::string macroName;
  1051. if (requiresMocing(contents, macroName))
  1052. {
  1053. //std::cout << "header contains Q_OBJECT macro";
  1054. notIncludedMocs[headerName] = currentMoc;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  1060. const std::string& mocFileName)
  1061. {
  1062. const std::string mocFilePath = this->Builddir + mocFileName;
  1063. int sourceNewerThanMoc = 0;
  1064. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile.c_str(),
  1065. mocFilePath.c_str(),
  1066. &sourceNewerThanMoc);
  1067. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0)
  1068. {
  1069. // make sure the directory for the resulting moc file exists
  1070. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  1071. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false))
  1072. {
  1073. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  1074. }
  1075. std::string msg = "Generating ";
  1076. msg += mocFileName;
  1077. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue
  1078. |cmsysTerminal_Color_ForegroundBold,
  1079. msg.c_str(), true, this->ColorOutput);
  1080. std::vector<cmStdString> command;
  1081. command.push_back(this->MocExecutable);
  1082. for (std::list<std::string>::const_iterator it = this->MocIncludes.begin();
  1083. it != this->MocIncludes.end();
  1084. ++it)
  1085. {
  1086. command.push_back(*it);
  1087. }
  1088. for(std::list<std::string>::const_iterator it=this->MocDefinitions.begin();
  1089. it != this->MocDefinitions.end();
  1090. ++it)
  1091. {
  1092. command.push_back(*it);
  1093. }
  1094. for(std::vector<std::string>::const_iterator it=this->MocOptions.begin();
  1095. it != this->MocOptions.end();
  1096. ++it)
  1097. {
  1098. command.push_back(*it);
  1099. }
  1100. #ifdef _WIN32
  1101. command.push_back("-DWIN32");
  1102. #endif
  1103. command.push_back("-o");
  1104. command.push_back(mocFilePath);
  1105. command.push_back(sourceFile);
  1106. if (this->Verbose)
  1107. {
  1108. for(std::vector<cmStdString>::const_iterator cmdIt = command.begin();
  1109. cmdIt != command.end();
  1110. ++cmdIt)
  1111. {
  1112. std::cout << *cmdIt << " ";
  1113. }
  1114. std::cout << std::endl;
  1115. }
  1116. std::string output;
  1117. int retVal = 0;
  1118. bool result = cmSystemTools::RunSingleCommand(command, &output, &retVal);
  1119. if (!result || retVal)
  1120. {
  1121. std::cerr << "AUTOMOC: error: process for " << mocFilePath <<" failed:\n"
  1122. << output << std::endl;
  1123. this->RunMocFailed = true;
  1124. cmSystemTools::RemoveFile(mocFilePath.c_str());
  1125. }
  1126. return true;
  1127. }
  1128. return false;
  1129. }
  1130. std::string cmQtAutoGenerators::Join(const std::vector<std::string>& lst,
  1131. char separator)
  1132. {
  1133. if (lst.empty())
  1134. {
  1135. return "";
  1136. }
  1137. std::string result;
  1138. for (std::vector<std::string>::const_iterator it = lst.begin();
  1139. it != lst.end();
  1140. ++it)
  1141. {
  1142. result += "." + (*it) + separator;
  1143. }
  1144. result.erase(result.end() - 1);
  1145. return result;
  1146. }
  1147. bool cmQtAutoGenerators::StartsWith(const std::string& str,
  1148. const std::string& with)
  1149. {
  1150. return (str.substr(0, with.length()) == with);
  1151. }
  1152. bool cmQtAutoGenerators::EndsWith(const std::string& str,
  1153. const std::string& with)
  1154. {
  1155. if (with.length() > (str.length()))
  1156. {
  1157. return false;
  1158. }
  1159. return (str.substr(str.length() - with.length(), with.length()) == with);
  1160. }
  1161. std::string cmQtAutoGenerators::ReadAll(const std::string& filename)
  1162. {
  1163. std::ifstream file(filename.c_str());
  1164. cmsys_ios::stringstream stream;
  1165. stream << file.rdbuf();
  1166. file.close();
  1167. return stream.str();
  1168. }