cmQtAutoGenerators.cxx 50 KB

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