cmQtAutoGenerators.cxx 51 KB

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