cmQtAutoGenerators.cxx 52 KB

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