cmQtAutoGenerators.cxx 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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") == NULL &&
  30. strstr(text.c_str(), "Q_GADGET") == NULL) {
  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::GetEnv("VERBOSE") != 0)
  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. cmsys::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::stringstream 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::stringstream 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::stringstream 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::stringstream err;
  467. err << "moc failed..." << std::endl;
  468. this->LogError(err.str());
  469. return false;
  470. }
  471. if (this->RunUicFailed) {
  472. std::stringstream err;
  473. err << "uic failed..." << std::endl;
  474. this->LogError(err.str());
  475. return false;
  476. }
  477. if (this->RunRccFailed) {
  478. std::stringstream 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::stringstream 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") != NULL) &&
  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::stringstream 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 ((basename != scannedFileBasename) || (requiresMoc == false)) {
  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 == false) && (basename == scannedFileBasename)) {
  575. std::stringstream 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::stringstream 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::stringstream 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 == false) && (requiresMoc == true)) {
  627. if (mocUnderscoreIncluded == true) {
  628. // this is for KDE4 compatibility:
  629. std::stringstream 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::stringstream 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::stringstream 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") != NULL) &&
  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::stringstream 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::stringstream 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 == false) &&
  753. (requiresMocing(contentsString, macroName))) {
  754. // otherwise always error out since it will not compile:
  755. std::stringstream err;
  756. err << "AUTOGEN: error: " << absFilename << ": The file "
  757. << "contains a " << macroName << " macro, but does not include "
  758. << "\"" << scannedFileBasename << ".moc\" !\n"
  759. << std::endl;
  760. this->LogError(err.str());
  761. ::exit(EXIT_FAILURE);
  762. }
  763. }
  764. void cmQtAutoGenerators::ParseForUic(
  765. const std::string& absFilename,
  766. std::map<std::string, std::vector<std::string> >& includedUis)
  767. {
  768. if (this->UicExecutable.empty()) {
  769. return;
  770. }
  771. const std::string contentsString = ReadAll(absFilename);
  772. if (contentsString.empty()) {
  773. std::stringstream err;
  774. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  775. << std::endl;
  776. this->LogError(err.str());
  777. return;
  778. }
  779. this->ParseForUic(absFilename, contentsString, includedUis);
  780. }
  781. void cmQtAutoGenerators::ParseForUic(
  782. const std::string& absFilename, const std::string& contentsString,
  783. std::map<std::string, std::vector<std::string> >& includedUis)
  784. {
  785. if (this->UicExecutable.empty()) {
  786. return;
  787. }
  788. cmsys::RegularExpression uiIncludeRegExp(
  789. "[\n][ \t]*#[ \t]*include[ \t]+"
  790. "[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
  791. std::string::size_type matchOffset = 0;
  792. const std::string realName = cmsys::SystemTools::GetRealPath(absFilename);
  793. matchOffset = 0;
  794. if ((strstr(contentsString.c_str(), "ui_") != NULL) &&
  795. (uiIncludeRegExp.find(contentsString))) {
  796. do {
  797. const std::string currentUi = uiIncludeRegExp.match(1);
  798. std::string basename =
  799. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentUi);
  800. // basename should be the part of the ui filename used for
  801. // finding the correct header, so we need to remove the ui_ part
  802. basename = basename.substr(3);
  803. includedUis[realName].push_back(basename);
  804. matchOffset += uiIncludeRegExp.end();
  805. } while (uiIncludeRegExp.find(contentsString.c_str() + matchOffset));
  806. }
  807. }
  808. void cmQtAutoGenerators::SearchHeadersForCppFile(
  809. const std::string& absFilename,
  810. const std::vector<std::string>& headerExtensions,
  811. std::set<std::string>& absHeaders)
  812. {
  813. // search for header files and private header files we may need to moc:
  814. const std::string basename =
  815. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  816. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  817. cmsys::SystemTools::GetRealPath(absFilename)) +
  818. '/';
  819. for (std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  820. ext != headerExtensions.end(); ++ext) {
  821. const std::string headerName = absPath + basename + "." + (*ext);
  822. if (cmsys::SystemTools::FileExists(headerName.c_str())) {
  823. absHeaders.insert(headerName);
  824. break;
  825. }
  826. }
  827. for (std::vector<std::string>::const_iterator ext = headerExtensions.begin();
  828. ext != headerExtensions.end(); ++ext) {
  829. const std::string privateHeaderName = absPath + basename + "_p." + (*ext);
  830. if (cmsys::SystemTools::FileExists(privateHeaderName.c_str())) {
  831. absHeaders.insert(privateHeaderName);
  832. break;
  833. }
  834. }
  835. }
  836. void cmQtAutoGenerators::ParseHeaders(
  837. const std::set<std::string>& absHeaders,
  838. const std::map<std::string, std::string>& includedMocs,
  839. std::map<std::string, std::string>& notIncludedMocs,
  840. std::map<std::string, std::vector<std::string> >& includedUis)
  841. {
  842. for (std::set<std::string>::const_iterator hIt = absHeaders.begin();
  843. hIt != absHeaders.end(); ++hIt) {
  844. const std::string& headerName = *hIt;
  845. const std::string contents = ReadAll(headerName);
  846. if (!this->MocExecutable.empty() &&
  847. includedMocs.find(headerName) == includedMocs.end()) {
  848. if (this->Verbose) {
  849. std::stringstream err;
  850. err << "AUTOGEN: Checking " << headerName << std::endl;
  851. this->LogInfo(err.str());
  852. }
  853. const std::string basename =
  854. cmsys::SystemTools::GetFilenameWithoutLastExtension(headerName);
  855. const std::string currentMoc = "moc_" + basename + ".cpp";
  856. std::string macroName;
  857. if (requiresMocing(contents, macroName)) {
  858. notIncludedMocs[headerName] = currentMoc;
  859. }
  860. }
  861. this->ParseForUic(headerName, contents, includedUis);
  862. }
  863. }
  864. bool cmQtAutoGenerators::GenerateMocFiles(
  865. const std::map<std::string, std::string>& includedMocs,
  866. const std::map<std::string, std::string>& notIncludedMocs)
  867. {
  868. // look for name collisions
  869. {
  870. std::multimap<std::string, std::string> collisions;
  871. // Test merged map of included and notIncluded
  872. std::map<std::string, std::string> mergedMocs(includedMocs);
  873. mergedMocs.insert(notIncludedMocs.begin(), notIncludedMocs.end());
  874. if (this->NameCollisionTest(mergedMocs, collisions)) {
  875. std::stringstream err;
  876. err << "AUTOGEN: error: "
  877. "The same moc file will be generated "
  878. "from different sources."
  879. << std::endl
  880. << "To avoid this error either" << std::endl
  881. << "- rename the source files or" << std::endl
  882. << "- do not include the (moc_NAME.cpp|NAME.moc) file" << std::endl;
  883. this->NameCollisionLog(err.str(), collisions);
  884. ::exit(EXIT_FAILURE);
  885. }
  886. }
  887. // generate moc files that are included by source files.
  888. for (std::map<std::string, std::string>::const_iterator it =
  889. includedMocs.begin();
  890. it != includedMocs.end(); ++it) {
  891. if (!this->GenerateMoc(it->first, it->second)) {
  892. if (this->RunMocFailed) {
  893. return false;
  894. }
  895. }
  896. }
  897. // generate moc files that are _not_ included by source files.
  898. bool automocCppChanged = false;
  899. for (std::map<std::string, std::string>::const_iterator it =
  900. notIncludedMocs.begin();
  901. it != notIncludedMocs.end(); ++it) {
  902. if (this->GenerateMoc(it->first, it->second)) {
  903. automocCppChanged = true;
  904. } else {
  905. if (this->RunMocFailed) {
  906. return false;
  907. }
  908. }
  909. }
  910. // compose _automoc.cpp content
  911. std::string automocSource;
  912. {
  913. std::stringstream outStream;
  914. outStream << "/* This file is autogenerated, do not edit*/\n";
  915. if (notIncludedMocs.empty()) {
  916. outStream << "enum some_compilers { need_more_than_nothing };\n";
  917. } else {
  918. for (std::map<std::string, std::string>::const_iterator it =
  919. notIncludedMocs.begin();
  920. it != notIncludedMocs.end(); ++it) {
  921. outStream << "#include \"" << it->second << "\"\n";
  922. }
  923. }
  924. outStream.flush();
  925. automocSource = outStream.str();
  926. }
  927. // check if we even need to update _automoc.cpp
  928. if (!automocCppChanged) {
  929. // compare contents of the _automoc.cpp file
  930. const std::string oldContents = ReadAll(this->OutMocCppFilename);
  931. if (oldContents == automocSource) {
  932. // nothing changed: don't touch the _automoc.cpp file
  933. if (this->Verbose) {
  934. std::stringstream err;
  935. err << "AUTOGEN: " << this->OutMocCppFilenameRel << " still up to date"
  936. << std::endl;
  937. this->LogInfo(err.str());
  938. }
  939. return true;
  940. }
  941. }
  942. // actually write _automoc.cpp
  943. {
  944. std::string msg = "Generating moc compilation ";
  945. msg += this->OutMocCppFilenameRel;
  946. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  947. cmsysTerminal_Color_ForegroundBold,
  948. msg.c_str(), true, this->ColorOutput);
  949. }
  950. {
  951. cmsys::ofstream outfile;
  952. outfile.open(this->OutMocCppFilename.c_str(), std::ios::trunc);
  953. outfile << automocSource;
  954. outfile.close();
  955. }
  956. return true;
  957. }
  958. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  959. const std::string& mocFileName)
  960. {
  961. const std::string mocFilePath = this->Builddir + mocFileName;
  962. int sourceNewerThanMoc = 0;
  963. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile, mocFilePath,
  964. &sourceNewerThanMoc);
  965. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0) {
  966. // make sure the directory for the resulting moc file exists
  967. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  968. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false)) {
  969. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  970. }
  971. std::string msg = "Generating moc source ";
  972. msg += mocFileName;
  973. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  974. cmsysTerminal_Color_ForegroundBold,
  975. msg.c_str(), true, this->ColorOutput);
  976. std::vector<std::string> command;
  977. command.push_back(this->MocExecutable);
  978. command.insert(command.end(), this->MocIncludes.begin(),
  979. this->MocIncludes.end());
  980. command.insert(command.end(), this->MocDefinitions.begin(),
  981. this->MocDefinitions.end());
  982. command.insert(command.end(), this->MocOptions.begin(),
  983. this->MocOptions.end());
  984. #ifdef _WIN32
  985. command.push_back("-DWIN32");
  986. #endif
  987. command.push_back("-o");
  988. command.push_back(mocFilePath);
  989. command.push_back(sourceFile);
  990. if (this->Verbose) {
  991. this->LogCommand(command);
  992. }
  993. std::string output;
  994. int retVal = 0;
  995. bool result =
  996. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  997. if (!result || retVal) {
  998. std::stringstream err;
  999. err << "AUTOGEN: error: process for " << mocFilePath << " failed:\n"
  1000. << output << std::endl;
  1001. this->LogError(err.str());
  1002. this->RunMocFailed = true;
  1003. cmSystemTools::RemoveFile(mocFilePath);
  1004. }
  1005. return true;
  1006. }
  1007. return false;
  1008. }
  1009. bool cmQtAutoGenerators::GenerateUiFiles(
  1010. const std::map<std::string, std::vector<std::string> >& includedUis)
  1011. {
  1012. // single map with input / output names
  1013. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1014. std::map<std::string, std::string> testMap;
  1015. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1016. includedUis.begin();
  1017. it != includedUis.end(); ++it) {
  1018. // source file path
  1019. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1020. sourcePath += '/';
  1021. // insert new map for source file an use new reference
  1022. uiGenMap[it->first] = std::map<std::string, std::string>();
  1023. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1024. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1025. sit != it->second.end(); ++sit) {
  1026. const std::string& uiFileName = *sit;
  1027. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1028. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1029. sourceMap[uiInputFile] = uiOutputFile;
  1030. testMap[uiInputFile] = uiOutputFile;
  1031. }
  1032. }
  1033. // look for name collisions
  1034. {
  1035. std::multimap<std::string, std::string> collisions;
  1036. if (this->NameCollisionTest(testMap, collisions)) {
  1037. std::stringstream err;
  1038. err << "AUTOGEN: error: The same ui_NAME.h file will be generated "
  1039. "from different sources."
  1040. << std::endl
  1041. << "To avoid this error rename the source files." << std::endl;
  1042. this->NameCollisionLog(err.str(), collisions);
  1043. ::exit(EXIT_FAILURE);
  1044. }
  1045. }
  1046. testMap.clear();
  1047. // generate ui files
  1048. for (std::map<std::string,
  1049. std::map<std::string, std::string> >::const_iterator it =
  1050. uiGenMap.begin();
  1051. it != uiGenMap.end(); ++it) {
  1052. for (std::map<std::string, std::string>::const_iterator sit =
  1053. it->second.begin();
  1054. sit != it->second.end(); ++sit) {
  1055. if (!this->GenerateUi(it->first, sit->first, sit->second)) {
  1056. if (this->RunUicFailed) {
  1057. return false;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. return true;
  1063. }
  1064. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1065. const std::string& uiInputFile,
  1066. const std::string& uiOutputFile)
  1067. {
  1068. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false)) {
  1069. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1070. }
  1071. const ::std::string uiBuildFile = this->Builddir + uiOutputFile;
  1072. int sourceNewerThanUi = 0;
  1073. bool success = cmsys::SystemTools::FileTimeCompare(uiInputFile, uiBuildFile,
  1074. &sourceNewerThanUi);
  1075. if (this->GenerateAll || !success || sourceNewerThanUi >= 0) {
  1076. std::string msg = "Generating ui header ";
  1077. msg += uiOutputFile;
  1078. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1079. cmsysTerminal_Color_ForegroundBold,
  1080. msg.c_str(), true, this->ColorOutput);
  1081. std::vector<std::string> command;
  1082. command.push_back(this->UicExecutable);
  1083. std::vector<std::string> opts = this->UicTargetOptions;
  1084. std::map<std::string, std::string>::const_iterator optionIt =
  1085. this->UicOptions.find(uiInputFile);
  1086. if (optionIt != this->UicOptions.end()) {
  1087. std::vector<std::string> fileOpts;
  1088. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1089. cmQtAutoGenerators::MergeUicOptions(opts, fileOpts,
  1090. this->QtMajorVersion == "5");
  1091. }
  1092. command.insert(command.end(), opts.begin(), opts.end());
  1093. command.push_back("-o");
  1094. command.push_back(uiBuildFile);
  1095. command.push_back(uiInputFile);
  1096. if (this->Verbose) {
  1097. this->LogCommand(command);
  1098. }
  1099. std::string output;
  1100. int retVal = 0;
  1101. bool result =
  1102. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1103. if (!result || retVal) {
  1104. std::stringstream err;
  1105. err << "AUTOUIC: error: process for " << uiOutputFile
  1106. << " needed by\n \"" << realName << "\"\nfailed:\n"
  1107. << output << std::endl;
  1108. this->LogError(err.str());
  1109. this->RunUicFailed = true;
  1110. cmSystemTools::RemoveFile(uiOutputFile);
  1111. return false;
  1112. }
  1113. return true;
  1114. }
  1115. return false;
  1116. }
  1117. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  1118. const std::string& rccOutput)
  1119. {
  1120. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  1121. for (std::vector<std::string>::const_iterator it = files.begin();
  1122. it != files.end(); ++it) {
  1123. int inputNewerThanQrc = 0;
  1124. bool success =
  1125. cmsys::SystemTools::FileTimeCompare(*it, rccOutput, &inputNewerThanQrc);
  1126. if (!success || inputNewerThanQrc >= 0) {
  1127. return true;
  1128. }
  1129. }
  1130. return false;
  1131. }
  1132. bool cmQtAutoGenerators::GenerateQrcFiles()
  1133. {
  1134. // generate single map with input / output names
  1135. std::map<std::string, std::string> qrcGenMap;
  1136. for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
  1137. si != this->RccSources.end(); ++si) {
  1138. const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  1139. if (ext == ".qrc") {
  1140. std::string basename =
  1141. cmsys::SystemTools::GetFilenameWithoutLastExtension(*si);
  1142. std::string qrcOutputFile = "CMakeFiles/" + this->OriginTargetName +
  1143. ".dir/qrc_" + basename + ".cpp";
  1144. qrcGenMap[*si] = qrcOutputFile;
  1145. }
  1146. }
  1147. // look for name collisions
  1148. {
  1149. std::multimap<std::string, std::string> collisions;
  1150. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1151. std::stringstream err;
  1152. err << "AUTOGEN: error: The same qrc_NAME.cpp file"
  1153. " will be generated from different sources."
  1154. << std::endl
  1155. << "To avoid this error rename the source .qrc files." << std::endl;
  1156. this->NameCollisionLog(err.str(), collisions);
  1157. ::exit(EXIT_FAILURE);
  1158. }
  1159. }
  1160. // generate qrc files
  1161. for (std::map<std::string, std::string>::const_iterator si =
  1162. qrcGenMap.begin();
  1163. si != qrcGenMap.end(); ++si) {
  1164. if (!this->GenerateQrc(si->first, si->second)) {
  1165. if (this->RunRccFailed) {
  1166. return false;
  1167. }
  1168. }
  1169. }
  1170. return true;
  1171. }
  1172. bool cmQtAutoGenerators::GenerateQrc(const std::string& qrcInputFile,
  1173. const std::string& qrcOutputFile)
  1174. {
  1175. const std::string basename =
  1176. cmsys::SystemTools::GetFilenameWithoutLastExtension(qrcInputFile);
  1177. const ::std::string qrcBuildFile = this->Builddir + qrcOutputFile;
  1178. int sourceNewerThanQrc = 0;
  1179. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(
  1180. qrcInputFile, qrcBuildFile, &sourceNewerThanQrc);
  1181. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  1182. generateQrc =
  1183. generateQrc || this->InputFilesNewerThanQrc(qrcInputFile, qrcBuildFile);
  1184. if (this->GenerateAll || generateQrc) {
  1185. std::string msg = "Generating qrc source ";
  1186. msg += qrcOutputFile;
  1187. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1188. cmsysTerminal_Color_ForegroundBold,
  1189. msg.c_str(), true, this->ColorOutput);
  1190. std::vector<std::string> command;
  1191. command.push_back(this->RccExecutable);
  1192. std::map<std::string, std::string>::const_iterator optionIt =
  1193. this->RccOptions.find(qrcInputFile);
  1194. if (optionIt != this->RccOptions.end()) {
  1195. cmSystemTools::ExpandListArgument(optionIt->second, command);
  1196. }
  1197. command.push_back("-name");
  1198. command.push_back(basename);
  1199. command.push_back("-o");
  1200. command.push_back(qrcBuildFile);
  1201. command.push_back(qrcInputFile);
  1202. if (this->Verbose) {
  1203. this->LogCommand(command);
  1204. }
  1205. std::string output;
  1206. int retVal = 0;
  1207. bool result =
  1208. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1209. if (!result || retVal) {
  1210. std::stringstream err;
  1211. err << "AUTORCC: error: process for " << qrcOutputFile << " failed:\n"
  1212. << output << std::endl;
  1213. this->LogError(err.str());
  1214. this->RunRccFailed = true;
  1215. cmSystemTools::RemoveFile(qrcBuildFile);
  1216. return false;
  1217. }
  1218. }
  1219. return true;
  1220. }
  1221. /**
  1222. * @brief Collects name collisions as output/input pairs
  1223. * @return True if there were collisions
  1224. */
  1225. bool cmQtAutoGenerators::NameCollisionTest(
  1226. const std::map<std::string, std::string>& genFiles,
  1227. std::multimap<std::string, std::string>& collisions)
  1228. {
  1229. typedef std::map<std::string, std::string>::const_iterator Iter;
  1230. typedef std::map<std::string, std::string>::value_type VType;
  1231. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1232. bool first_match(true);
  1233. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1234. if (ait->second == bit->second) {
  1235. if (first_match) {
  1236. if (collisions.find(ait->second) != collisions.end()) {
  1237. // We already know of this collision from before
  1238. break;
  1239. }
  1240. collisions.insert(VType(ait->second, ait->first));
  1241. first_match = false;
  1242. }
  1243. collisions.insert(VType(bit->second, bit->first));
  1244. }
  1245. }
  1246. }
  1247. return !collisions.empty();
  1248. }
  1249. void cmQtAutoGenerators::NameCollisionLog(
  1250. const std::string& message,
  1251. const std::multimap<std::string, std::string>& collisions)
  1252. {
  1253. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1254. std::stringstream err;
  1255. // Add message
  1256. err << message;
  1257. // Append collision list
  1258. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1259. err << it->first << " : " << it->second << std::endl;
  1260. }
  1261. this->LogError(err.str());
  1262. }
  1263. void cmQtAutoGenerators::LogInfo(const std::string& message)
  1264. {
  1265. std::cout << message;
  1266. }
  1267. void cmQtAutoGenerators::LogError(const std::string& message)
  1268. {
  1269. std::cerr << message;
  1270. }
  1271. void cmQtAutoGenerators::LogCommand(const std::vector<std::string>& command)
  1272. {
  1273. std::stringstream sbuf;
  1274. for (std::vector<std::string>::const_iterator cmdIt = command.begin();
  1275. cmdIt != command.end(); ++cmdIt) {
  1276. if (cmdIt != command.begin()) {
  1277. sbuf << " ";
  1278. }
  1279. sbuf << *cmdIt;
  1280. }
  1281. if (!sbuf.str().empty()) {
  1282. sbuf << std::endl;
  1283. this->LogInfo(sbuf.str());
  1284. }
  1285. }
  1286. std::string cmQtAutoGenerators::JoinExts(const std::vector<std::string>& lst)
  1287. {
  1288. if (lst.empty()) {
  1289. return "";
  1290. }
  1291. std::string result;
  1292. std::string separator = ",";
  1293. for (std::vector<std::string>::const_iterator it = lst.begin();
  1294. it != lst.end(); ++it) {
  1295. if (it != lst.begin()) {
  1296. result += separator;
  1297. }
  1298. result += '.' + (*it);
  1299. }
  1300. result.erase(result.end() - 1);
  1301. return result;
  1302. }