cmQtAutoGenerators.cxx 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoGenerators.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmFilePathUuid.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmOutputConverter.h"
  9. #include "cmState.h"
  10. #include "cmSystemTools.h"
  11. #include "cm_auto_ptr.hxx"
  12. #include "cmake.h"
  13. #include <algorithm>
  14. #include <assert.h>
  15. #include <cmConfigure.h>
  16. #include <cmsys/FStream.hxx>
  17. #include <cmsys/RegularExpression.hxx>
  18. #include <cmsys/Terminal.h>
  19. #include <iostream>
  20. #include <sstream>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <utility>
  24. #if defined(__APPLE__)
  25. #include <unistd.h>
  26. #endif
  27. static bool requiresMocing(const std::string& text, std::string& macroName)
  28. {
  29. // this simple check is much much faster than the regexp
  30. if (strstr(text.c_str(), "Q_OBJECT") == CM_NULLPTR &&
  31. strstr(text.c_str(), "Q_GADGET") == CM_NULLPTR) {
  32. return false;
  33. }
  34. cmsys::RegularExpression qObjectRegExp("[\n][ \t]*Q_OBJECT[^a-zA-Z0-9_]");
  35. if (qObjectRegExp.find(text)) {
  36. macroName = "Q_OBJECT";
  37. return true;
  38. }
  39. cmsys::RegularExpression qGadgetRegExp("[\n][ \t]*Q_GADGET[^a-zA-Z0-9_]");
  40. if (qGadgetRegExp.find(text)) {
  41. macroName = "Q_GADGET";
  42. return true;
  43. }
  44. return false;
  45. }
  46. static std::string findMatchingHeader(
  47. const std::string& absPath, const std::string& mocSubDir,
  48. const std::string& basename,
  49. const std::vector<std::string>& headerExtensions)
  50. {
  51. std::string header;
  52. for (std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  53. ext != headerExtensions.end(); ++ext) {
  54. std::string sourceFilePath = absPath + basename + "." + (*ext);
  55. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str())) {
  56. header = sourceFilePath;
  57. break;
  58. }
  59. if (!mocSubDir.empty()) {
  60. sourceFilePath = mocSubDir + basename + "." + (*ext);
  61. if (cmsys::SystemTools::FileExists(sourceFilePath.c_str())) {
  62. header = sourceFilePath;
  63. break;
  64. }
  65. }
  66. }
  67. return header;
  68. }
  69. static std::string extractSubDir(const std::string& absPath,
  70. const std::string& currentMoc)
  71. {
  72. std::string subDir;
  73. if (currentMoc.find_first_of('/') != std::string::npos) {
  74. subDir = absPath + cmsys::SystemTools::GetFilenamePath(currentMoc) + '/';
  75. }
  76. return subDir;
  77. }
  78. static bool FileNameIsUnique(const std::string& filePath,
  79. const std::map<std::string, std::string>& fileMap)
  80. {
  81. size_t count(0);
  82. const std::string fileName = cmsys::SystemTools::GetFilenameName(filePath);
  83. for (std::map<std::string, std::string>::const_iterator si = fileMap.begin();
  84. si != fileMap.end(); ++si) {
  85. if (cmsys::SystemTools::GetFilenameName(si->first) == fileName) {
  86. ++count;
  87. if (count > 1) {
  88. return false;
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. cmQtAutoGenerators::cmQtAutoGenerators()
  95. : Verbose(cmsys::SystemTools::HasEnv("VERBOSE"))
  96. , ColorOutput(true)
  97. , RunMocFailed(false)
  98. , RunUicFailed(false)
  99. , RunRccFailed(false)
  100. , GenerateAll(false)
  101. {
  102. std::string colorEnv = "";
  103. cmsys::SystemTools::GetEnv("COLOR", colorEnv);
  104. if (!colorEnv.empty()) {
  105. if (cmSystemTools::IsOn(colorEnv.c_str())) {
  106. this->ColorOutput = true;
  107. } else {
  108. this->ColorOutput = false;
  109. }
  110. }
  111. }
  112. void cmQtAutoGenerators::MergeUicOptions(
  113. std::vector<std::string>& opts, const std::vector<std::string>& fileOpts,
  114. bool isQt5)
  115. {
  116. static const char* valueOptions[] = { "tr", "translate",
  117. "postfix", "generator",
  118. "include", // Since Qt 5.3
  119. "g" };
  120. std::vector<std::string> extraOpts;
  121. for (std::vector<std::string>::const_iterator it = fileOpts.begin();
  122. it != fileOpts.end(); ++it) {
  123. std::vector<std::string>::iterator existingIt =
  124. std::find(opts.begin(), opts.end(), *it);
  125. if (existingIt != opts.end()) {
  126. const char* o = it->c_str();
  127. if (*o == '-') {
  128. ++o;
  129. }
  130. if (isQt5 && *o == '-') {
  131. ++o;
  132. }
  133. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  134. cmStrCmp(*it)) != cmArrayEnd(valueOptions)) {
  135. assert(existingIt + 1 != opts.end());
  136. *(existingIt + 1) = *(it + 1);
  137. ++it;
  138. }
  139. } else {
  140. extraOpts.push_back(*it);
  141. }
  142. }
  143. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  144. }
  145. bool cmQtAutoGenerators::Run(const std::string& targetDirectory,
  146. const std::string& config)
  147. {
  148. bool success = true;
  149. cmake cm;
  150. cm.SetHomeOutputDirectory(targetDirectory);
  151. cm.SetHomeDirectory(targetDirectory);
  152. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  153. cmGlobalGenerator gg(&cm);
  154. cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
  155. snapshot.GetDirectory().SetCurrentBinary(targetDirectory);
  156. snapshot.GetDirectory().SetCurrentSource(targetDirectory);
  157. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, snapshot));
  158. gg.SetCurrentMakefile(mf.get());
  159. this->ReadAutogenInfoFile(mf.get(), targetDirectory, config);
  160. this->ReadOldMocDefinitionsFile(mf.get(), targetDirectory);
  161. this->Init();
  162. if (this->QtMajorVersion == "4" || this->QtMajorVersion == "5") {
  163. success = this->RunAutogen(mf.get());
  164. }
  165. this->WriteOldMocDefinitionsFile(targetDirectory);
  166. return success;
  167. }
  168. bool cmQtAutoGenerators::ReadAutogenInfoFile(
  169. cmMakefile* makefile, const std::string& targetDirectory,
  170. const std::string& config)
  171. {
  172. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  173. cmSystemTools::ConvertToUnixSlashes(filename);
  174. filename += "/AutogenInfo.cmake";
  175. if (!makefile->ReadListFile(filename.c_str())) {
  176. cmSystemTools::Error("Error processing file: ", filename.c_str());
  177. return false;
  178. }
  179. this->QtMajorVersion = makefile->GetSafeDefinition("AM_QT_VERSION_MAJOR");
  180. if (this->QtMajorVersion == "") {
  181. this->QtMajorVersion =
  182. makefile->GetSafeDefinition("AM_Qt5Core_VERSION_MAJOR");
  183. }
  184. this->Sources = makefile->GetSafeDefinition("AM_SOURCES");
  185. {
  186. std::string rccSources = makefile->GetSafeDefinition("AM_RCC_SOURCES");
  187. cmSystemTools::ExpandListArgument(rccSources, this->RccSources);
  188. }
  189. this->SkipMoc = makefile->GetSafeDefinition("AM_SKIP_MOC");
  190. this->SkipUic = makefile->GetSafeDefinition("AM_SKIP_UIC");
  191. this->Headers = makefile->GetSafeDefinition("AM_HEADERS");
  192. this->IncludeProjectDirsBefore =
  193. makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
  194. this->Srcdir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_SOURCE_DIR");
  195. this->Builddir = makefile->GetSafeDefinition("AM_CMAKE_CURRENT_BINARY_DIR");
  196. this->MocExecutable = makefile->GetSafeDefinition("AM_QT_MOC_EXECUTABLE");
  197. this->UicExecutable = makefile->GetSafeDefinition("AM_QT_UIC_EXECUTABLE");
  198. this->RccExecutable = makefile->GetSafeDefinition("AM_QT_RCC_EXECUTABLE");
  199. {
  200. std::string compileDefsPropOrig = "AM_MOC_COMPILE_DEFINITIONS";
  201. std::string compileDefsProp = compileDefsPropOrig;
  202. if (!config.empty()) {
  203. compileDefsProp += "_";
  204. compileDefsProp += config;
  205. }
  206. const char* compileDefs = makefile->GetDefinition(compileDefsProp);
  207. this->MocCompileDefinitionsStr = compileDefs
  208. ? compileDefs
  209. : makefile->GetSafeDefinition(compileDefsPropOrig);
  210. }
  211. {
  212. std::string includesPropOrig = "AM_MOC_INCLUDES";
  213. std::string includesProp = includesPropOrig;
  214. if (!config.empty()) {
  215. includesProp += "_";
  216. includesProp += config;
  217. }
  218. const char* includes = makefile->GetDefinition(includesProp);
  219. this->MocIncludesStr =
  220. includes ? includes : makefile->GetSafeDefinition(includesPropOrig);
  221. }
  222. this->MocOptionsStr = makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  223. this->ProjectBinaryDir = makefile->GetSafeDefinition("AM_CMAKE_BINARY_DIR");
  224. this->ProjectSourceDir = makefile->GetSafeDefinition("AM_CMAKE_SOURCE_DIR");
  225. this->TargetName = makefile->GetSafeDefinition("AM_TARGET_NAME");
  226. this->OriginTargetName =
  227. makefile->GetSafeDefinition("AM_ORIGIN_TARGET_NAME");
  228. {
  229. const char* uicOptionsFiles =
  230. makefile->GetSafeDefinition("AM_UIC_OPTIONS_FILES");
  231. std::string uicOptionsPropOrig = "AM_UIC_TARGET_OPTIONS";
  232. std::string uicOptionsProp = uicOptionsPropOrig;
  233. if (!config.empty()) {
  234. uicOptionsProp += "_";
  235. uicOptionsProp += config;
  236. }
  237. const char* uicTargetOptions = makefile->GetSafeDefinition(uicOptionsProp);
  238. cmSystemTools::ExpandListArgument(
  239. uicTargetOptions ? uicTargetOptions
  240. : makefile->GetSafeDefinition(uicOptionsPropOrig),
  241. this->UicTargetOptions);
  242. const char* uicOptionsOptions =
  243. makefile->GetSafeDefinition("AM_UIC_OPTIONS_OPTIONS");
  244. std::vector<std::string> uicFilesVec;
  245. cmSystemTools::ExpandListArgument(uicOptionsFiles, uicFilesVec);
  246. std::vector<std::string> uicOptionsVec;
  247. cmSystemTools::ExpandListArgument(uicOptionsOptions, uicOptionsVec);
  248. if (uicFilesVec.size() != uicOptionsVec.size()) {
  249. return false;
  250. }
  251. for (std::vector<std::string>::iterator fileIt = uicFilesVec.begin(),
  252. optionIt = uicOptionsVec.begin();
  253. fileIt != uicFilesVec.end(); ++fileIt, ++optionIt) {
  254. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  255. this->UicOptions[*fileIt] = *optionIt;
  256. }
  257. }
  258. {
  259. const char* rccOptionsFiles =
  260. makefile->GetSafeDefinition("AM_RCC_OPTIONS_FILES");
  261. const char* rccOptionsOptions =
  262. makefile->GetSafeDefinition("AM_RCC_OPTIONS_OPTIONS");
  263. std::vector<std::string> rccFilesVec;
  264. cmSystemTools::ExpandListArgument(rccOptionsFiles, rccFilesVec);
  265. std::vector<std::string> rccOptionsVec;
  266. cmSystemTools::ExpandListArgument(rccOptionsOptions, rccOptionsVec);
  267. if (rccFilesVec.size() != rccOptionsVec.size()) {
  268. return false;
  269. }
  270. for (std::vector<std::string>::iterator fileIt = rccFilesVec.begin(),
  271. optionIt = rccOptionsVec.begin();
  272. fileIt != rccFilesVec.end(); ++fileIt, ++optionIt) {
  273. cmSystemTools::ReplaceString(*optionIt, "@list_sep@", ";");
  274. this->RccOptions[*fileIt] = *optionIt;
  275. }
  276. const char* rccInputs = makefile->GetSafeDefinition("AM_RCC_INPUTS");
  277. std::vector<std::string> rccInputLists;
  278. cmSystemTools::ExpandListArgument(rccInputs, rccInputLists);
  279. if (this->RccSources.size() != rccInputLists.size()) {
  280. cmSystemTools::Error("Error processing file: ", filename.c_str());
  281. return false;
  282. }
  283. for (std::vector<std::string>::iterator fileIt = this->RccSources.begin(),
  284. inputIt = rccInputLists.begin();
  285. fileIt != this->RccSources.end(); ++fileIt, ++inputIt) {
  286. cmSystemTools::ReplaceString(*inputIt, "@list_sep@", ";");
  287. std::vector<std::string> rccInputFiles;
  288. cmSystemTools::ExpandListArgument(*inputIt, rccInputFiles);
  289. this->RccInputs[*fileIt] = rccInputFiles;
  290. }
  291. }
  292. this->CurrentCompileSettingsStr = this->MakeCompileSettingsString(makefile);
  293. this->RelaxedMode = makefile->IsOn("AM_RELAXED_MODE");
  294. return true;
  295. }
  296. std::string cmQtAutoGenerators::MakeCompileSettingsString(cmMakefile* makefile)
  297. {
  298. std::string s;
  299. s += makefile->GetSafeDefinition("AM_MOC_COMPILE_DEFINITIONS");
  300. s += " ~~~ ";
  301. s += makefile->GetSafeDefinition("AM_MOC_INCLUDES");
  302. s += " ~~~ ";
  303. s += makefile->GetSafeDefinition("AM_MOC_OPTIONS");
  304. s += " ~~~ ";
  305. s += makefile->IsOn("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE") ? "TRUE"
  306. : "FALSE";
  307. s += " ~~~ ";
  308. return s;
  309. }
  310. bool cmQtAutoGenerators::ReadOldMocDefinitionsFile(
  311. cmMakefile* makefile, const std::string& targetDirectory)
  312. {
  313. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  314. cmSystemTools::ConvertToUnixSlashes(filename);
  315. filename += "/AutomocOldMocDefinitions.cmake";
  316. if (makefile->ReadListFile(filename.c_str())) {
  317. this->OldCompileSettingsStr =
  318. makefile->GetSafeDefinition("AM_OLD_COMPILE_SETTINGS");
  319. }
  320. return true;
  321. }
  322. void cmQtAutoGenerators::WriteOldMocDefinitionsFile(
  323. const std::string& targetDirectory)
  324. {
  325. std::string filename(cmSystemTools::CollapseFullPath(targetDirectory));
  326. cmSystemTools::ConvertToUnixSlashes(filename);
  327. filename += "/AutomocOldMocDefinitions.cmake";
  328. cmsys::ofstream outfile;
  329. outfile.open(filename.c_str(), std::ios::trunc);
  330. outfile << "set(AM_OLD_COMPILE_SETTINGS "
  331. << cmOutputConverter::EscapeForCMake(this->CurrentCompileSettingsStr)
  332. << ")\n";
  333. outfile.close();
  334. }
  335. void cmQtAutoGenerators::Init()
  336. {
  337. this->TargetBuildSubDir = this->TargetName;
  338. this->TargetBuildSubDir += ".dir/";
  339. this->OutMocCppFilenameRel = this->TargetName;
  340. this->OutMocCppFilenameRel += ".cpp";
  341. this->OutMocCppFilenameAbs = this->Builddir + this->OutMocCppFilenameRel;
  342. std::vector<std::string> cdefList;
  343. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  344. for (std::vector<std::string>::const_iterator it = cdefList.begin();
  345. it != cdefList.end(); ++it) {
  346. this->MocDefinitions.push_back("-D" + (*it));
  347. }
  348. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  349. std::vector<std::string> incPaths;
  350. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  351. std::set<std::string> frameworkPaths;
  352. for (std::vector<std::string>::const_iterator it = incPaths.begin();
  353. it != incPaths.end(); ++it) {
  354. const std::string& path = *it;
  355. this->MocIncludes.push_back("-I" + path);
  356. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  357. // Go up twice to get to the framework root
  358. std::vector<std::string> pathComponents;
  359. cmsys::SystemTools::SplitPath(path, pathComponents);
  360. std::string frameworkPath = cmsys::SystemTools::JoinPath(
  361. pathComponents.begin(), pathComponents.end() - 2);
  362. frameworkPaths.insert(frameworkPath);
  363. }
  364. }
  365. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  366. it != frameworkPaths.end(); ++it) {
  367. this->MocIncludes.push_back("-F");
  368. this->MocIncludes.push_back(*it);
  369. }
  370. if (this->IncludeProjectDirsBefore) {
  371. const std::string binDir = "-I" + this->ProjectBinaryDir;
  372. const std::string srcDir = "-I" + this->ProjectSourceDir;
  373. std::list<std::string> sortedMocIncludes;
  374. std::list<std::string>::iterator it = this->MocIncludes.begin();
  375. while (it != this->MocIncludes.end()) {
  376. if (cmsys::SystemTools::StringStartsWith(*it, binDir.c_str())) {
  377. sortedMocIncludes.push_back(*it);
  378. it = this->MocIncludes.erase(it);
  379. } else {
  380. ++it;
  381. }
  382. }
  383. it = this->MocIncludes.begin();
  384. while (it != this->MocIncludes.end()) {
  385. if (cmsys::SystemTools::StringStartsWith(*it, srcDir.c_str())) {
  386. sortedMocIncludes.push_back(*it);
  387. it = this->MocIncludes.erase(it);
  388. } else {
  389. ++it;
  390. }
  391. }
  392. sortedMocIncludes.insert(sortedMocIncludes.end(),
  393. this->MocIncludes.begin(),
  394. this->MocIncludes.end());
  395. this->MocIncludes = sortedMocIncludes;
  396. }
  397. }
  398. static std::string ReadAll(const std::string& filename)
  399. {
  400. cmsys::ifstream file(filename.c_str());
  401. std::ostringstream stream;
  402. stream << file.rdbuf();
  403. file.close();
  404. return stream.str();
  405. }
  406. bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
  407. {
  408. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilenameAbs.c_str()) ||
  409. (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr)) {
  410. this->GenerateAll = true;
  411. }
  412. // the program goes through all .cpp files to see which moc files are
  413. // included. It is not really interesting how the moc file is named, but
  414. // what file the moc is created from. Once a moc is included the same moc
  415. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  416. // header containing Q_OBJECT where no corresponding moc file is included
  417. // anywhere a moc_<filename>.cpp file is created and included in
  418. // the _automoc.cpp file.
  419. // key = moc source filepath, value = moc output filepath
  420. std::map<std::string, std::string> includedMocs;
  421. // collect all headers which may need to be mocced
  422. std::set<std::string> headerFiles;
  423. std::vector<std::string> sourceFiles;
  424. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  425. const std::vector<std::string>& headerExtensions =
  426. makefile->GetCMakeInstance()->GetHeaderExtensions();
  427. std::map<std::string, std::vector<std::string> > includedUis;
  428. std::map<std::string, std::vector<std::string> > skippedUis;
  429. std::vector<std::string> uicSkipped;
  430. cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
  431. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  432. it != sourceFiles.end(); ++it) {
  433. const bool skipUic =
  434. std::find(uicSkipped.begin(), uicSkipped.end(), *it) != uicSkipped.end();
  435. std::map<std::string, std::vector<std::string> >& uiFiles =
  436. skipUic ? skippedUis : includedUis;
  437. const std::string& absFilename = *it;
  438. if (this->Verbose) {
  439. std::ostringstream err;
  440. err << "AUTOGEN: Checking " << absFilename << std::endl;
  441. this->LogInfo(err.str());
  442. }
  443. if (this->RelaxedMode) {
  444. this->ParseCppFile(absFilename, headerExtensions, includedMocs, uiFiles);
  445. } else {
  446. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
  447. uiFiles);
  448. }
  449. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  450. }
  451. {
  452. std::vector<std::string> mocSkipped;
  453. cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
  454. for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
  455. it != mocSkipped.end(); ++it) {
  456. if (std::find(uicSkipped.begin(), uicSkipped.end(), *it) !=
  457. uicSkipped.end()) {
  458. const std::string& absFilename = *it;
  459. if (this->Verbose) {
  460. std::ostringstream err;
  461. err << "AUTOGEN: Checking " << absFilename << std::endl;
  462. this->LogInfo(err.str());
  463. }
  464. this->ParseForUic(absFilename, includedUis);
  465. }
  466. }
  467. }
  468. std::vector<std::string> headerFilesVec;
  469. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  470. headerFiles.insert(headerFilesVec.begin(), headerFilesVec.end());
  471. // key = moc source filepath, value = moc output filename
  472. std::map<std::string, std::string> notIncludedMocs;
  473. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
  474. if (!this->MocExecutable.empty()) {
  475. this->GenerateMocFiles(includedMocs, notIncludedMocs);
  476. }
  477. if (!this->UicExecutable.empty()) {
  478. this->GenerateUiFiles(includedUis);
  479. }
  480. if (!this->RccExecutable.empty()) {
  481. this->GenerateQrcFiles();
  482. }
  483. if (this->RunMocFailed) {
  484. std::ostringstream err;
  485. err << "moc failed..." << std::endl;
  486. this->LogError(err.str());
  487. return false;
  488. }
  489. if (this->RunUicFailed) {
  490. std::ostringstream err;
  491. err << "uic failed..." << std::endl;
  492. this->LogError(err.str());
  493. return false;
  494. }
  495. if (this->RunRccFailed) {
  496. std::ostringstream err;
  497. err << "rcc failed..." << std::endl;
  498. this->LogError(err.str());
  499. return false;
  500. }
  501. return true;
  502. }
  503. void cmQtAutoGenerators::ParseCppFile(
  504. const std::string& absFilename,
  505. const std::vector<std::string>& headerExtensions,
  506. std::map<std::string, std::string>& includedMocs,
  507. std::map<std::string, std::vector<std::string> >& includedUis)
  508. {
  509. cmsys::RegularExpression mocIncludeRegExp(
  510. "[\n][ \t]*#[ \t]*include[ \t]+"
  511. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  512. const std::string contentsString = ReadAll(absFilename);
  513. if (contentsString.empty()) {
  514. std::ostringstream err;
  515. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  516. << std::endl;
  517. this->LogError(err.str());
  518. return;
  519. }
  520. this->ParseForUic(absFilename, contentsString, includedUis);
  521. if (this->MocExecutable.empty()) {
  522. return;
  523. }
  524. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  525. cmsys::SystemTools::GetRealPath(absFilename)) +
  526. '/';
  527. const std::string scannedFileBasename =
  528. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  529. std::string macroName;
  530. const bool requiresMoc = requiresMocing(contentsString, macroName);
  531. bool dotMocIncluded = false;
  532. bool mocUnderscoreIncluded = false;
  533. std::string ownMocUnderscoreFile;
  534. std::string ownDotMocFile;
  535. std::string ownMocHeaderFile;
  536. std::string::size_type matchOffset = 0;
  537. // first a simple string check for "moc" is *much* faster than the regexp,
  538. // and if the string search already fails, we don't have to try the
  539. // expensive regexp
  540. if ((strstr(contentsString.c_str(), "moc") != CM_NULLPTR) &&
  541. (mocIncludeRegExp.find(contentsString))) {
  542. // for every moc include in the file
  543. do {
  544. const std::string currentMoc = mocIncludeRegExp.match(1);
  545. std::string basename =
  546. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentMoc);
  547. const bool moc_style = cmHasLiteralPrefix(basename, "moc_");
  548. // If the moc include is of the moc_foo.cpp style we expect
  549. // the Q_OBJECT class declaration in a header file.
  550. // If the moc include is of the foo.moc style we need to look for
  551. // a Q_OBJECT macro in the current source file, if it contains the
  552. // macro we generate the moc file from the source file.
  553. // Q_OBJECT
  554. if (moc_style) {
  555. // basename should be the part of the moc filename used for
  556. // finding the correct header, so we need to remove the moc_ part
  557. basename = basename.substr(4);
  558. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  559. std::string headerToMoc =
  560. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  561. if (!headerToMoc.empty()) {
  562. includedMocs[headerToMoc] = currentMoc;
  563. if (basename == scannedFileBasename) {
  564. mocUnderscoreIncluded = true;
  565. ownMocUnderscoreFile = currentMoc;
  566. ownMocHeaderFile = headerToMoc;
  567. }
  568. } else {
  569. std::ostringstream err;
  570. err << "AUTOGEN: error: " << absFilename << ": The file "
  571. << "includes the moc file \"" << currentMoc << "\", "
  572. << "but could not find header \"" << basename << '{'
  573. << this->JoinExts(headerExtensions) << "}\" ";
  574. if (mocSubDir.empty()) {
  575. err << "in " << absPath << "\n" << std::endl;
  576. } else {
  577. err << "neither in " << absPath << " nor in " << mocSubDir << "\n"
  578. << std::endl;
  579. }
  580. this->LogError(err.str());
  581. ::exit(EXIT_FAILURE);
  582. }
  583. } else {
  584. std::string fileToMoc = absFilename;
  585. if (!requiresMoc || basename != scannedFileBasename) {
  586. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  587. std::string headerToMoc =
  588. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  589. if (!headerToMoc.empty()) {
  590. // this is for KDE4 compatibility:
  591. fileToMoc = headerToMoc;
  592. if (!requiresMoc && basename == scannedFileBasename) {
  593. std::ostringstream err;
  594. err << "AUTOGEN: warning: " << absFilename
  595. << ": The file "
  596. "includes the moc file \""
  597. << currentMoc << "\", but does not contain a " << macroName
  598. << " macro. Running moc on "
  599. << "\"" << headerToMoc << "\" ! Include \"moc_" << basename
  600. << ".cpp\" for a compatibility with "
  601. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  602. << std::endl;
  603. this->LogError(err.str());
  604. } else {
  605. std::ostringstream err;
  606. err << "AUTOGEN: warning: " << absFilename
  607. << ": The file "
  608. "includes the moc file \""
  609. << currentMoc << "\" instead of \"moc_" << basename
  610. << ".cpp\". "
  611. "Running moc on "
  612. << "\"" << headerToMoc << "\" ! Include \"moc_" << basename
  613. << ".cpp\" for compatibility with "
  614. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  615. << std::endl;
  616. this->LogError(err.str());
  617. }
  618. } else {
  619. std::ostringstream err;
  620. err << "AUTOGEN: error: " << absFilename
  621. << ": The file "
  622. "includes the moc file \""
  623. << currentMoc
  624. << "\", which seems to be the moc file from a different "
  625. "source file. CMake also could not find a matching "
  626. "header.\n"
  627. << std::endl;
  628. this->LogError(err.str());
  629. ::exit(EXIT_FAILURE);
  630. }
  631. } else {
  632. dotMocIncluded = true;
  633. ownDotMocFile = currentMoc;
  634. }
  635. includedMocs[fileToMoc] = currentMoc;
  636. }
  637. matchOffset += mocIncludeRegExp.end();
  638. } while (mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  639. }
  640. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  641. // If this is the case, the moc_foo.cpp should probably be generated from
  642. // foo.cpp instead of foo.h, because otherwise it won't build.
  643. // But warn, since this is not how it is supposed to be used.
  644. if (!dotMocIncluded && requiresMoc) {
  645. if (mocUnderscoreIncluded) {
  646. // this is for KDE4 compatibility:
  647. std::ostringstream err;
  648. err << "AUTOGEN: warning: " << absFilename << ": The file "
  649. << "contains a " << macroName << " macro, but does not "
  650. "include "
  651. << "\"" << scannedFileBasename << ".moc\", but instead "
  652. "includes "
  653. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  654. << "\"" << absFilename << "\" ! Better include \""
  655. << scannedFileBasename
  656. << ".moc\" for compatibility with "
  657. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  658. << std::endl;
  659. this->LogError(err.str());
  660. includedMocs[absFilename] = ownMocUnderscoreFile;
  661. includedMocs.erase(ownMocHeaderFile);
  662. } else {
  663. // otherwise always error out since it will not compile:
  664. std::ostringstream err;
  665. err << "AUTOGEN: error: " << absFilename << ": The file "
  666. << "contains a " << macroName << " macro, but does not "
  667. "include "
  668. << "\"" << scannedFileBasename << ".moc\" !\n"
  669. << std::endl;
  670. this->LogError(err.str());
  671. ::exit(EXIT_FAILURE);
  672. }
  673. }
  674. }
  675. void cmQtAutoGenerators::StrictParseCppFile(
  676. const std::string& absFilename,
  677. const std::vector<std::string>& headerExtensions,
  678. std::map<std::string, std::string>& includedMocs,
  679. std::map<std::string, std::vector<std::string> >& includedUis)
  680. {
  681. cmsys::RegularExpression mocIncludeRegExp(
  682. "[\n][ \t]*#[ \t]*include[ \t]+"
  683. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  684. const std::string contentsString = ReadAll(absFilename);
  685. if (contentsString.empty()) {
  686. std::ostringstream err;
  687. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  688. << std::endl;
  689. this->LogError(err.str());
  690. return;
  691. }
  692. this->ParseForUic(absFilename, contentsString, includedUis);
  693. if (this->MocExecutable.empty()) {
  694. return;
  695. }
  696. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  697. cmsys::SystemTools::GetRealPath(absFilename)) +
  698. '/';
  699. const std::string scannedFileBasename =
  700. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  701. bool dotMocIncluded = false;
  702. std::string::size_type matchOffset = 0;
  703. // first a simple string check for "moc" is *much* faster than the regexp,
  704. // and if the string search already fails, we don't have to try the
  705. // expensive regexp
  706. if ((strstr(contentsString.c_str(), "moc") != CM_NULLPTR) &&
  707. (mocIncludeRegExp.find(contentsString))) {
  708. // for every moc include in the file
  709. do {
  710. const std::string currentMoc = mocIncludeRegExp.match(1);
  711. std::string basename =
  712. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentMoc);
  713. const bool mocUnderscoreStyle = cmHasLiteralPrefix(basename, "moc_");
  714. // If the moc include is of the moc_foo.cpp style we expect
  715. // the Q_OBJECT class declaration in a header file.
  716. // If the moc include is of the foo.moc style we need to look for
  717. // a Q_OBJECT macro in the current source file, if it contains the
  718. // macro we generate the moc file from the source file.
  719. if (mocUnderscoreStyle) {
  720. // basename should be the part of the moc filename used for
  721. // finding the correct header, so we need to remove the moc_ part
  722. basename = basename.substr(4);
  723. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  724. std::string headerToMoc =
  725. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  726. if (!headerToMoc.empty()) {
  727. includedMocs[headerToMoc] = currentMoc;
  728. } else {
  729. std::ostringstream err;
  730. err << "AUTOGEN: error: " << absFilename << " The file "
  731. << "includes the moc file \"" << currentMoc << "\", "
  732. << "but could not find header \"" << basename << '{'
  733. << this->JoinExts(headerExtensions) << "}\" ";
  734. if (mocSubDir.empty()) {
  735. err << "in " << absPath << "\n" << std::endl;
  736. } else {
  737. err << "neither in " << absPath << " nor in " << mocSubDir << "\n"
  738. << std::endl;
  739. }
  740. this->LogError(err.str());
  741. ::exit(EXIT_FAILURE);
  742. }
  743. } else {
  744. if (basename != scannedFileBasename) {
  745. std::ostringstream err;
  746. err << "AUTOGEN: error: " << absFilename
  747. << ": The file "
  748. "includes the moc file \""
  749. << currentMoc
  750. << "\", which seems to be the moc file from a different "
  751. "source file. This is not supported. "
  752. "Include \""
  753. << scannedFileBasename << ".moc\" to run "
  754. "moc on this source file.\n"
  755. << std::endl;
  756. this->LogError(err.str());
  757. ::exit(EXIT_FAILURE);
  758. }
  759. dotMocIncluded = true;
  760. includedMocs[absFilename] = currentMoc;
  761. }
  762. matchOffset += mocIncludeRegExp.end();
  763. } while (mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  764. }
  765. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  766. // If this is the case, the moc_foo.cpp should probably be generated from
  767. // foo.cpp instead of foo.h, because otherwise it won't build.
  768. // But warn, since this is not how it is supposed to be used.
  769. std::string macroName;
  770. if (!dotMocIncluded && requiresMocing(contentsString, macroName)) {
  771. // otherwise always error out since it will not compile:
  772. std::ostringstream err;
  773. err << "AUTOGEN: error: " << absFilename << ": The file "
  774. << "contains a " << macroName << " macro, but does not include "
  775. << "\"" << scannedFileBasename << ".moc\" !\n"
  776. << std::endl;
  777. this->LogError(err.str());
  778. ::exit(EXIT_FAILURE);
  779. }
  780. }
  781. void cmQtAutoGenerators::ParseForUic(
  782. const std::string& absFilename,
  783. std::map<std::string, std::vector<std::string> >& includedUis)
  784. {
  785. if (this->UicExecutable.empty()) {
  786. return;
  787. }
  788. const std::string contentsString = ReadAll(absFilename);
  789. if (contentsString.empty()) {
  790. std::ostringstream err;
  791. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  792. << std::endl;
  793. this->LogError(err.str());
  794. return;
  795. }
  796. this->ParseForUic(absFilename, contentsString, includedUis);
  797. }
  798. void cmQtAutoGenerators::ParseForUic(
  799. const std::string& absFilename, const std::string& contentsString,
  800. std::map<std::string, std::vector<std::string> >& includedUis)
  801. {
  802. if (this->UicExecutable.empty()) {
  803. return;
  804. }
  805. cmsys::RegularExpression uiIncludeRegExp(
  806. "[\n][ \t]*#[ \t]*include[ \t]+"
  807. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  808. std::string::size_type matchOffset = 0;
  809. const std::string realName = cmsys::SystemTools::GetRealPath(absFilename);
  810. matchOffset = 0;
  811. if ((strstr(contentsString.c_str(), "ui_") != CM_NULLPTR) &&
  812. (uiIncludeRegExp.find(contentsString))) {
  813. do {
  814. const std::string currentUi = uiIncludeRegExp.match(1);
  815. std::string basename =
  816. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentUi);
  817. // basename should be the part of the ui filename used for
  818. // finding the correct header, so we need to remove the ui_ part
  819. basename = basename.substr(3);
  820. includedUis[realName].push_back(basename);
  821. matchOffset += uiIncludeRegExp.end();
  822. } while (uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
  823. }
  824. }
  825. void cmQtAutoGenerators::SearchHeadersForCppFile(
  826. const std::string& absFilename,
  827. const std::vector<std::string>& headerExtensions,
  828. std::set<std::string>& absHeaders)
  829. {
  830. // search for header files and private header files we may need to moc:
  831. const std::string basename =
  832. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  833. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  834. cmsys::SystemTools::GetRealPath(absFilename)) +
  835. '/';
  836. for (std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  837. ext != headerExtensions.end(); ++ext) {
  838. const std::string headerName = absPath + basename + "." + (*ext);
  839. if (cmsys::SystemTools::FileExists(headerName.c_str())) {
  840. absHeaders.insert(headerName);
  841. break;
  842. }
  843. }
  844. for (std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  845. ext != headerExtensions.end(); ++ext) {
  846. const std::string privateHeaderName = absPath + basename + "_p." + (*ext);
  847. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str())) {
  848. absHeaders.insert(privateHeaderName);
  849. break;
  850. }
  851. }
  852. }
  853. void cmQtAutoGenerators::ParseHeaders(
  854. const std::set<std::string>& absHeaders,
  855. const std::map<std::string, std::string>& includedMocs,
  856. std::map<std::string, std::string>& notIncludedMocs,
  857. std::map<std::string, std::vector<std::string> >& includedUis)
  858. {
  859. cmFilePathUuid fpathUuid(this->Srcdir, this->Builddir,
  860. this->ProjectSourceDir, this->ProjectBinaryDir);
  861. for (std::set<std::string>::const_iterator hIt = absHeaders.begin();
  862. hIt != absHeaders.end(); ++hIt) {
  863. const std::string& headerName = *hIt;
  864. const std::string contents = ReadAll(headerName);
  865. if (!this->MocExecutable.empty() &&
  866. includedMocs.find(headerName) == includedMocs.end()) {
  867. if (this->Verbose) {
  868. std::ostringstream err;
  869. err << "AUTOGEN: Checking " << headerName << std::endl;
  870. this->LogInfo(err.str());
  871. }
  872. std::string macroName;
  873. if (requiresMocing(contents, macroName)) {
  874. notIncludedMocs[headerName] =
  875. this->TargetBuildSubDir + fpathUuid.get(headerName, "moc_", ".cpp");
  876. }
  877. }
  878. this->ParseForUic(headerName, contents, includedUis);
  879. }
  880. }
  881. bool cmQtAutoGenerators::GenerateMocFiles(
  882. const std::map<std::string, std::string>& includedMocs,
  883. const std::map<std::string, std::string>& notIncludedMocs)
  884. {
  885. // look for name collisions
  886. {
  887. std::multimap<std::string, std::string> collisions;
  888. // Test merged map of included and notIncluded
  889. std::map<std::string, std::string> mergedMocs(includedMocs);
  890. mergedMocs.insert(notIncludedMocs.begin(), notIncludedMocs.end());
  891. if (this->NameCollisionTest(mergedMocs, collisions)) {
  892. std::ostringstream err;
  893. err << "AUTOGEN: error: "
  894. "The same moc file will be generated "
  895. "from different sources."
  896. << std::endl
  897. << "To avoid this error either" << std::endl
  898. << "- rename the source files or" << std::endl
  899. << "- do not include the (moc_NAME.cpp|NAME.moc) file" << std::endl;
  900. this->NameCollisionLog(err.str(), collisions);
  901. ::exit(EXIT_FAILURE);
  902. }
  903. }
  904. // generate moc files that are included by source files.
  905. for (std::map<std::string, std::string>::const_iterator it =
  906. includedMocs.begin();
  907. it != includedMocs.end(); ++it) {
  908. if (!this->GenerateMoc(it->first, it->second)) {
  909. if (this->RunMocFailed) {
  910. return false;
  911. }
  912. }
  913. }
  914. // generate moc files that are _not_ included by source files.
  915. bool automocCppChanged = false;
  916. for (std::map<std::string, std::string>::const_iterator it =
  917. notIncludedMocs.begin();
  918. it != notIncludedMocs.end(); ++it) {
  919. if (this->GenerateMoc(it->first, it->second)) {
  920. automocCppChanged = true;
  921. } else {
  922. if (this->RunMocFailed) {
  923. return false;
  924. }
  925. }
  926. }
  927. // compose _automoc.cpp content
  928. std::string automocSource;
  929. {
  930. std::ostringstream outStream;
  931. outStream << "/* This file is autogenerated, do not edit*/\n";
  932. if (notIncludedMocs.empty()) {
  933. outStream << "enum some_compilers { need_more_than_nothing };\n";
  934. } else {
  935. for (std::map<std::string, std::string>::const_iterator it =
  936. notIncludedMocs.begin();
  937. it != notIncludedMocs.end(); ++it) {
  938. outStream << "#include \"" << it->second << "\"\n";
  939. }
  940. }
  941. outStream.flush();
  942. automocSource = outStream.str();
  943. }
  944. // check if we even need to update _automoc.cpp
  945. if (!automocCppChanged) {
  946. // compare contents of the _automoc.cpp file
  947. const std::string oldContents = ReadAll(this->OutMocCppFilenameAbs);
  948. if (oldContents == automocSource) {
  949. // nothing changed: don't touch the _automoc.cpp file
  950. if (this->Verbose) {
  951. std::ostringstream err;
  952. err << "AUTOGEN: " << this->OutMocCppFilenameRel << " still up to date"
  953. << std::endl;
  954. this->LogInfo(err.str());
  955. }
  956. return true;
  957. }
  958. }
  959. // actually write _automoc.cpp
  960. {
  961. std::string msg = "Generating moc compilation ";
  962. msg += this->OutMocCppFilenameRel;
  963. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  964. cmsysTerminal_Color_ForegroundBold,
  965. msg.c_str(), true, this->ColorOutput);
  966. }
  967. {
  968. cmsys::ofstream outfile;
  969. outfile.open(this->OutMocCppFilenameAbs.c_str(), std::ios::trunc);
  970. outfile << automocSource;
  971. outfile.close();
  972. }
  973. return true;
  974. }
  975. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  976. const std::string& mocFileName)
  977. {
  978. const std::string mocFilePath = this->Builddir + mocFileName;
  979. int sourceNewerThanMoc = 0;
  980. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile, mocFilePath,
  981. &sourceNewerThanMoc);
  982. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0) {
  983. // make sure the directory for the resulting moc file exists
  984. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  985. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false)) {
  986. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  987. }
  988. std::string msg = "Generating moc source ";
  989. msg += mocFileName;
  990. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  991. cmsysTerminal_Color_ForegroundBold,
  992. msg.c_str(), true, this->ColorOutput);
  993. std::vector<std::string> command;
  994. command.push_back(this->MocExecutable);
  995. command.insert(command.end(), this->MocIncludes.begin(),
  996. this->MocIncludes.end());
  997. command.insert(command.end(), this->MocDefinitions.begin(),
  998. this->MocDefinitions.end());
  999. command.insert(command.end(), this->MocOptions.begin(),
  1000. this->MocOptions.end());
  1001. #ifdef _WIN32
  1002. command.push_back("-DWIN32");
  1003. #endif
  1004. command.push_back("-o");
  1005. command.push_back(mocFilePath);
  1006. command.push_back(sourceFile);
  1007. if (this->Verbose) {
  1008. this->LogCommand(command);
  1009. }
  1010. std::string output;
  1011. int retVal = 0;
  1012. bool result =
  1013. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1014. if (!result || retVal) {
  1015. std::ostringstream err;
  1016. err << "AUTOGEN: error: process for " << mocFilePath << " failed:\n"
  1017. << output << std::endl;
  1018. this->LogError(err.str());
  1019. this->RunMocFailed = true;
  1020. cmSystemTools::RemoveFile(mocFilePath);
  1021. }
  1022. return true;
  1023. }
  1024. return false;
  1025. }
  1026. bool cmQtAutoGenerators::GenerateUiFiles(
  1027. const std::map<std::string, std::vector<std::string> >& includedUis)
  1028. {
  1029. // single map with input / output names
  1030. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1031. std::map<std::string, std::string> testMap;
  1032. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1033. includedUis.begin();
  1034. it != includedUis.end(); ++it) {
  1035. // source file path
  1036. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1037. sourcePath += '/';
  1038. // insert new map for source file an use new reference
  1039. uiGenMap[it->first] = std::map<std::string, std::string>();
  1040. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1041. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1042. sit != it->second.end(); ++sit) {
  1043. const std::string& uiFileName = *sit;
  1044. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1045. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1046. sourceMap[uiInputFile] = uiOutputFile;
  1047. testMap[uiInputFile] = uiOutputFile;
  1048. }
  1049. }
  1050. // look for name collisions
  1051. {
  1052. std::multimap<std::string, std::string> collisions;
  1053. if (this->NameCollisionTest(testMap, collisions)) {
  1054. std::ostringstream err;
  1055. err << "AUTOGEN: error: The same ui_NAME.h file will be generated "
  1056. "from different sources."
  1057. << std::endl
  1058. << "To avoid this error rename the source files." << std::endl;
  1059. this->NameCollisionLog(err.str(), collisions);
  1060. ::exit(EXIT_FAILURE);
  1061. }
  1062. }
  1063. testMap.clear();
  1064. // generate ui files
  1065. for (std::map<std::string,
  1066. std::map<std::string, std::string> >::const_iterator it =
  1067. uiGenMap.begin();
  1068. it != uiGenMap.end(); ++it) {
  1069. for (std::map<std::string, std::string>::const_iterator sit =
  1070. it->second.begin();
  1071. sit != it->second.end(); ++sit) {
  1072. if (!this->GenerateUi(it->first, sit->first, sit->second)) {
  1073. if (this->RunUicFailed) {
  1074. return false;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. return true;
  1080. }
  1081. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1082. const std::string& uiInputFile,
  1083. const std::string& uiOutputFile)
  1084. {
  1085. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false)) {
  1086. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1087. }
  1088. const std::string uiBuildFile = this->Builddir + uiOutputFile;
  1089. int sourceNewerThanUi = 0;
  1090. bool success = cmsys::SystemTools::FileTimeCompare(uiInputFile, uiBuildFile,
  1091. &sourceNewerThanUi);
  1092. if (this->GenerateAll || !success || sourceNewerThanUi >= 0) {
  1093. std::string msg = "Generating ui header ";
  1094. msg += uiOutputFile;
  1095. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1096. cmsysTerminal_Color_ForegroundBold,
  1097. msg.c_str(), true, this->ColorOutput);
  1098. std::vector<std::string> command;
  1099. command.push_back(this->UicExecutable);
  1100. std::vector<std::string> opts = this->UicTargetOptions;
  1101. std::map<std::string, std::string>::const_iterator optionIt =
  1102. this->UicOptions.find(uiInputFile);
  1103. if (optionIt != this->UicOptions.end()) {
  1104. std::vector<std::string> fileOpts;
  1105. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1106. cmQtAutoGenerators::MergeUicOptions(opts, fileOpts,
  1107. this->QtMajorVersion == "5");
  1108. }
  1109. command.insert(command.end(), opts.begin(), opts.end());
  1110. command.push_back("-o");
  1111. command.push_back(uiBuildFile);
  1112. command.push_back(uiInputFile);
  1113. if (this->Verbose) {
  1114. this->LogCommand(command);
  1115. }
  1116. std::string output;
  1117. int retVal = 0;
  1118. bool result =
  1119. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1120. if (!result || retVal) {
  1121. std::ostringstream err;
  1122. err << "AUTOUIC: error: process for " << uiOutputFile
  1123. << " needed by\n \"" << realName << "\"\nfailed:\n"
  1124. << output << std::endl;
  1125. this->LogError(err.str());
  1126. this->RunUicFailed = true;
  1127. cmSystemTools::RemoveFile(uiOutputFile);
  1128. return false;
  1129. }
  1130. return true;
  1131. }
  1132. return false;
  1133. }
  1134. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  1135. const std::string& rccOutput)
  1136. {
  1137. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  1138. for (std::vector<std::string>::const_iterator it = files.begin();
  1139. it != files.end(); ++it) {
  1140. int inputNewerThanQrc = 0;
  1141. bool success =
  1142. cmsys::SystemTools::FileTimeCompare(*it, rccOutput, &inputNewerThanQrc);
  1143. if (!success || inputNewerThanQrc >= 0) {
  1144. return true;
  1145. }
  1146. }
  1147. return false;
  1148. }
  1149. bool cmQtAutoGenerators::GenerateQrcFiles()
  1150. {
  1151. // generate single map with input / output names
  1152. std::map<std::string, std::string> qrcGenMap;
  1153. {
  1154. cmFilePathUuid fpathUuid(this->Srcdir, this->Builddir,
  1155. this->ProjectSourceDir, this->ProjectBinaryDir);
  1156. for (std::vector<std::string>::const_iterator si =
  1157. this->RccSources.begin();
  1158. si != this->RccSources.end(); ++si) {
  1159. const std::string ext =
  1160. cmsys::SystemTools::GetFilenameLastExtension(*si);
  1161. if (ext == ".qrc") {
  1162. qrcGenMap[*si] =
  1163. (this->TargetBuildSubDir + fpathUuid.get(*si, "qrc_", ".cpp"));
  1164. }
  1165. }
  1166. }
  1167. // look for name collisions
  1168. {
  1169. std::multimap<std::string, std::string> collisions;
  1170. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1171. std::ostringstream err;
  1172. err << "AUTOGEN: error: The same qrc_NAME.cpp file"
  1173. " will be generated from different sources."
  1174. << std::endl
  1175. << "To avoid this error rename the source .qrc files." << std::endl;
  1176. this->NameCollisionLog(err.str(), collisions);
  1177. ::exit(EXIT_FAILURE);
  1178. }
  1179. }
  1180. // generate qrc files
  1181. for (std::map<std::string, std::string>::const_iterator si =
  1182. qrcGenMap.begin();
  1183. si != qrcGenMap.end(); ++si) {
  1184. bool unique = FileNameIsUnique(si->first, qrcGenMap);
  1185. if (!this->GenerateQrc(si->first, si->second, unique)) {
  1186. if (this->RunRccFailed) {
  1187. return false;
  1188. }
  1189. }
  1190. }
  1191. return true;
  1192. }
  1193. bool cmQtAutoGenerators::GenerateQrc(const std::string& qrcInputFile,
  1194. const std::string& qrcOutputFile,
  1195. bool unique_n)
  1196. {
  1197. std::string symbolName;
  1198. if (unique_n) {
  1199. symbolName =
  1200. cmsys::SystemTools::GetFilenameWithoutLastExtension(qrcInputFile);
  1201. } else {
  1202. symbolName =
  1203. cmsys::SystemTools::GetFilenameWithoutLastExtension(qrcOutputFile);
  1204. // Remove "qrc_" at string begin
  1205. symbolName.erase(0, 4);
  1206. }
  1207. // Replace '-' with '_'. The former is valid for
  1208. // file names but not for symbol names.
  1209. std::replace(symbolName.begin(), symbolName.end(), '-', '_');
  1210. const std::string qrcBuildFile = this->Builddir + qrcOutputFile;
  1211. int sourceNewerThanQrc = 0;
  1212. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(
  1213. qrcInputFile, qrcBuildFile, &sourceNewerThanQrc);
  1214. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  1215. generateQrc =
  1216. generateQrc || this->InputFilesNewerThanQrc(qrcInputFile, qrcBuildFile);
  1217. if (this->GenerateAll || generateQrc) {
  1218. std::string msg = "Generating qrc source ";
  1219. msg += qrcOutputFile;
  1220. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1221. cmsysTerminal_Color_ForegroundBold,
  1222. msg.c_str(), true, this->ColorOutput);
  1223. std::vector<std::string> command;
  1224. command.push_back(this->RccExecutable);
  1225. std::map<std::string, std::string>::const_iterator optionIt =
  1226. this->RccOptions.find(qrcInputFile);
  1227. if (optionIt != this->RccOptions.end()) {
  1228. cmSystemTools::ExpandListArgument(optionIt->second, command);
  1229. }
  1230. command.push_back("-name");
  1231. command.push_back(symbolName);
  1232. command.push_back("-o");
  1233. command.push_back(qrcBuildFile);
  1234. command.push_back(qrcInputFile);
  1235. if (this->Verbose) {
  1236. this->LogCommand(command);
  1237. }
  1238. std::string output;
  1239. int retVal = 0;
  1240. bool result =
  1241. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1242. if (!result || retVal) {
  1243. std::ostringstream err;
  1244. err << "AUTORCC: error: process for " << qrcOutputFile << " failed:\n"
  1245. << output << std::endl;
  1246. this->LogError(err.str());
  1247. this->RunRccFailed = true;
  1248. cmSystemTools::RemoveFile(qrcBuildFile);
  1249. return false;
  1250. }
  1251. }
  1252. return true;
  1253. }
  1254. /**
  1255. * @brief Collects name collisions as output/input pairs
  1256. * @return True if there were collisions
  1257. */
  1258. bool cmQtAutoGenerators::NameCollisionTest(
  1259. const std::map<std::string, std::string>& genFiles,
  1260. std::multimap<std::string, std::string>& collisions)
  1261. {
  1262. typedef std::map<std::string, std::string>::const_iterator Iter;
  1263. typedef std::map<std::string, std::string>::value_type VType;
  1264. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1265. bool first_match(true);
  1266. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1267. if (ait->second == bit->second) {
  1268. if (first_match) {
  1269. if (collisions.find(ait->second) != collisions.end()) {
  1270. // We already know of this collision from before
  1271. break;
  1272. }
  1273. collisions.insert(VType(ait->second, ait->first));
  1274. first_match = false;
  1275. }
  1276. collisions.insert(VType(bit->second, bit->first));
  1277. }
  1278. }
  1279. }
  1280. return !collisions.empty();
  1281. }
  1282. void cmQtAutoGenerators::NameCollisionLog(
  1283. const std::string& message,
  1284. const std::multimap<std::string, std::string>& collisions)
  1285. {
  1286. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1287. std::ostringstream err;
  1288. // Add message
  1289. err << message;
  1290. // Append collision list
  1291. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1292. err << it->first << " : " << it->second << std::endl;
  1293. }
  1294. this->LogError(err.str());
  1295. }
  1296. void cmQtAutoGenerators::LogInfo(const std::string& message)
  1297. {
  1298. std::cout << message;
  1299. }
  1300. void cmQtAutoGenerators::LogError(const std::string& message)
  1301. {
  1302. std::cerr << message;
  1303. }
  1304. void cmQtAutoGenerators::LogCommand(const std::vector<std::string>& command)
  1305. {
  1306. std::ostringstream sbuf;
  1307. for (std::vector<std::string>::const_iterator cmdIt = command.begin();
  1308. cmdIt != command.end(); ++cmdIt) {
  1309. if (cmdIt != command.begin()) {
  1310. sbuf << " ";
  1311. }
  1312. sbuf << *cmdIt;
  1313. }
  1314. if (!sbuf.str().empty()) {
  1315. sbuf << std::endl;
  1316. this->LogInfo(sbuf.str());
  1317. }
  1318. }
  1319. std::string cmQtAutoGenerators::JoinExts(const std::vector<std::string>& lst)
  1320. {
  1321. if (lst.empty()) {
  1322. return "";
  1323. }
  1324. std::string result;
  1325. std::string separator = ",";
  1326. for (std::vector<std::string>::const_iterator it = lst.begin();
  1327. it != lst.end(); ++it) {
  1328. if (it != lst.begin()) {
  1329. result += separator;
  1330. }
  1331. result += '.' + (*it);
  1332. }
  1333. result.erase(result.end() - 1);
  1334. return result;
  1335. }