cmQtAutoGenerators.cxx 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  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->TargetBuildSubDir = this->TargetName;
  321. this->TargetBuildSubDir += ".dir/";
  322. this->OutMocCppFilenameRel = this->TargetName;
  323. this->OutMocCppFilenameRel += ".cpp";
  324. this->OutMocCppFilenameAbs = this->Builddir + this->OutMocCppFilenameRel;
  325. std::vector<std::string> cdefList;
  326. cmSystemTools::ExpandListArgument(this->MocCompileDefinitionsStr, cdefList);
  327. for (std::vector<std::string>::const_iterator it = cdefList.begin();
  328. it != cdefList.end(); ++it) {
  329. this->MocDefinitions.push_back("-D" + (*it));
  330. }
  331. cmSystemTools::ExpandListArgument(this->MocOptionsStr, this->MocOptions);
  332. std::vector<std::string> incPaths;
  333. cmSystemTools::ExpandListArgument(this->MocIncludesStr, incPaths);
  334. std::set<std::string> frameworkPaths;
  335. for (std::vector<std::string>::const_iterator it = incPaths.begin();
  336. it != incPaths.end(); ++it) {
  337. const std::string& path = *it;
  338. this->MocIncludes.push_back("-I" + path);
  339. if (cmHasLiteralSuffix(path, ".framework/Headers")) {
  340. // Go up twice to get to the framework root
  341. std::vector<std::string> pathComponents;
  342. cmsys::SystemTools::SplitPath(path, pathComponents);
  343. std::string frameworkPath = cmsys::SystemTools::JoinPath(
  344. pathComponents.begin(), pathComponents.end() - 2);
  345. frameworkPaths.insert(frameworkPath);
  346. }
  347. }
  348. for (std::set<std::string>::const_iterator it = frameworkPaths.begin();
  349. it != frameworkPaths.end(); ++it) {
  350. this->MocIncludes.push_back("-F");
  351. this->MocIncludes.push_back(*it);
  352. }
  353. if (this->IncludeProjectDirsBefore) {
  354. const std::string binDir = "-I" + this->ProjectBinaryDir;
  355. const std::string srcDir = "-I" + this->ProjectSourceDir;
  356. std::list<std::string> sortedMocIncludes;
  357. std::list<std::string>::iterator it = this->MocIncludes.begin();
  358. while (it != this->MocIncludes.end()) {
  359. if (cmsys::SystemTools::StringStartsWith(*it, binDir.c_str())) {
  360. sortedMocIncludes.push_back(*it);
  361. it = this->MocIncludes.erase(it);
  362. } else {
  363. ++it;
  364. }
  365. }
  366. it = this->MocIncludes.begin();
  367. while (it != this->MocIncludes.end()) {
  368. if (cmsys::SystemTools::StringStartsWith(*it, srcDir.c_str())) {
  369. sortedMocIncludes.push_back(*it);
  370. it = this->MocIncludes.erase(it);
  371. } else {
  372. ++it;
  373. }
  374. }
  375. sortedMocIncludes.insert(sortedMocIncludes.end(),
  376. this->MocIncludes.begin(),
  377. this->MocIncludes.end());
  378. this->MocIncludes = sortedMocIncludes;
  379. }
  380. }
  381. static std::string ReadAll(const std::string& filename)
  382. {
  383. cmsys::ifstream file(filename.c_str());
  384. std::stringstream stream;
  385. stream << file.rdbuf();
  386. file.close();
  387. return stream.str();
  388. }
  389. bool cmQtAutoGenerators::RunAutogen(cmMakefile* makefile)
  390. {
  391. if (!cmsys::SystemTools::FileExists(this->OutMocCppFilenameAbs.c_str()) ||
  392. (this->OldCompileSettingsStr != this->CurrentCompileSettingsStr)) {
  393. this->GenerateAll = true;
  394. }
  395. // the program goes through all .cpp files to see which moc files are
  396. // included. It is not really interesting how the moc file is named, but
  397. // what file the moc is created from. Once a moc is included the same moc
  398. // may not be included in the _automoc.cpp file anymore. OTOH if there's a
  399. // header containing Q_OBJECT where no corresponding moc file is included
  400. // anywhere a moc_<filename>.cpp file is created and included in
  401. // the _automoc.cpp file.
  402. // key = moc source filepath, value = moc output filepath
  403. std::map<std::string, std::string> includedMocs;
  404. // collect all headers which may need to be mocced
  405. std::set<std::string> headerFiles;
  406. std::vector<std::string> sourceFiles;
  407. cmSystemTools::ExpandListArgument(this->Sources, sourceFiles);
  408. const std::vector<std::string>& headerExtensions =
  409. makefile->GetCMakeInstance()->GetHeaderExtensions();
  410. std::map<std::string, std::vector<std::string> > includedUis;
  411. std::map<std::string, std::vector<std::string> > skippedUis;
  412. std::vector<std::string> uicSkipped;
  413. cmSystemTools::ExpandListArgument(this->SkipUic, uicSkipped);
  414. for (std::vector<std::string>::const_iterator it = sourceFiles.begin();
  415. it != sourceFiles.end(); ++it) {
  416. const bool skipUic =
  417. std::find(uicSkipped.begin(), uicSkipped.end(), *it) != uicSkipped.end();
  418. std::map<std::string, std::vector<std::string> >& uiFiles =
  419. skipUic ? skippedUis : includedUis;
  420. const std::string& absFilename = *it;
  421. if (this->Verbose) {
  422. std::stringstream err;
  423. err << "AUTOGEN: Checking " << absFilename << std::endl;
  424. this->LogInfo(err.str());
  425. }
  426. if (this->RelaxedMode) {
  427. this->ParseCppFile(absFilename, headerExtensions, includedMocs, uiFiles);
  428. } else {
  429. this->StrictParseCppFile(absFilename, headerExtensions, includedMocs,
  430. uiFiles);
  431. }
  432. this->SearchHeadersForCppFile(absFilename, headerExtensions, headerFiles);
  433. }
  434. {
  435. std::vector<std::string> mocSkipped;
  436. cmSystemTools::ExpandListArgument(this->SkipMoc, mocSkipped);
  437. for (std::vector<std::string>::const_iterator it = mocSkipped.begin();
  438. it != mocSkipped.end(); ++it) {
  439. if (std::find(uicSkipped.begin(), uicSkipped.end(), *it) !=
  440. uicSkipped.end()) {
  441. const std::string& absFilename = *it;
  442. if (this->Verbose) {
  443. std::stringstream err;
  444. err << "AUTOGEN: Checking " << absFilename << std::endl;
  445. this->LogInfo(err.str());
  446. }
  447. this->ParseForUic(absFilename, includedUis);
  448. }
  449. }
  450. }
  451. std::vector<std::string> headerFilesVec;
  452. cmSystemTools::ExpandListArgument(this->Headers, headerFilesVec);
  453. headerFiles.insert(headerFilesVec.begin(), headerFilesVec.end());
  454. // key = moc source filepath, value = moc output filename
  455. std::map<std::string, std::string> notIncludedMocs;
  456. this->ParseHeaders(headerFiles, includedMocs, notIncludedMocs, includedUis);
  457. if (!this->MocExecutable.empty()) {
  458. this->GenerateMocFiles(includedMocs, notIncludedMocs);
  459. }
  460. if (!this->UicExecutable.empty()) {
  461. this->GenerateUiFiles(includedUis);
  462. }
  463. if (!this->RccExecutable.empty()) {
  464. this->GenerateQrcFiles();
  465. }
  466. if (this->RunMocFailed) {
  467. std::stringstream err;
  468. err << "moc failed..." << std::endl;
  469. this->LogError(err.str());
  470. return false;
  471. }
  472. if (this->RunUicFailed) {
  473. std::stringstream err;
  474. err << "uic failed..." << std::endl;
  475. this->LogError(err.str());
  476. return false;
  477. }
  478. if (this->RunRccFailed) {
  479. std::stringstream err;
  480. err << "rcc failed..." << std::endl;
  481. this->LogError(err.str());
  482. return false;
  483. }
  484. return true;
  485. }
  486. void cmQtAutoGenerators::ParseCppFile(
  487. const std::string& absFilename,
  488. const std::vector<std::string>& headerExtensions,
  489. std::map<std::string, std::string>& includedMocs,
  490. std::map<std::string, std::vector<std::string> >& includedUis)
  491. {
  492. cmsys::RegularExpression mocIncludeRegExp(
  493. "[\n][ \t]*#[ \t]*include[ \t]+"
  494. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  495. const std::string contentsString = ReadAll(absFilename);
  496. if (contentsString.empty()) {
  497. std::stringstream err;
  498. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  499. << std::endl;
  500. this->LogError(err.str());
  501. return;
  502. }
  503. this->ParseForUic(absFilename, contentsString, includedUis);
  504. if (this->MocExecutable.empty()) {
  505. return;
  506. }
  507. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  508. cmsys::SystemTools::GetRealPath(absFilename)) +
  509. '/';
  510. const std::string scannedFileBasename =
  511. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  512. std::string macroName;
  513. const bool requiresMoc = requiresMocing(contentsString, macroName);
  514. bool dotMocIncluded = false;
  515. bool mocUnderscoreIncluded = false;
  516. std::string ownMocUnderscoreFile;
  517. std::string ownDotMocFile;
  518. std::string ownMocHeaderFile;
  519. std::string::size_type matchOffset = 0;
  520. // first a simple string check for "moc" is *much* faster than the regexp,
  521. // and if the string search already fails, we don't have to try the
  522. // expensive regexp
  523. if ((strstr(contentsString.c_str(), "moc") != NULL) &&
  524. (mocIncludeRegExp.find(contentsString))) {
  525. // for every moc include in the file
  526. do {
  527. const std::string currentMoc = mocIncludeRegExp.match(1);
  528. std::string basename =
  529. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentMoc);
  530. const bool moc_style = cmHasLiteralPrefix(basename, "moc_");
  531. // If the moc include is of the moc_foo.cpp style we expect
  532. // the Q_OBJECT class declaration in a header file.
  533. // If the moc include is of the foo.moc style we need to look for
  534. // a Q_OBJECT macro in the current source file, if it contains the
  535. // macro we generate the moc file from the source file.
  536. // Q_OBJECT
  537. if (moc_style) {
  538. // basename should be the part of the moc filename used for
  539. // finding the correct header, so we need to remove the moc_ part
  540. basename = basename.substr(4);
  541. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  542. std::string headerToMoc =
  543. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  544. if (!headerToMoc.empty()) {
  545. includedMocs[headerToMoc] = currentMoc;
  546. if (basename == scannedFileBasename) {
  547. mocUnderscoreIncluded = true;
  548. ownMocUnderscoreFile = currentMoc;
  549. ownMocHeaderFile = headerToMoc;
  550. }
  551. } else {
  552. std::stringstream err;
  553. err << "AUTOGEN: error: " << absFilename << ": The file "
  554. << "includes the moc file \"" << currentMoc << "\", "
  555. << "but could not find header \"" << basename << '{'
  556. << this->JoinExts(headerExtensions) << "}\" ";
  557. if (mocSubDir.empty()) {
  558. err << "in " << absPath << "\n" << std::endl;
  559. } else {
  560. err << "neither in " << absPath << " nor in " << mocSubDir << "\n"
  561. << std::endl;
  562. }
  563. this->LogError(err.str());
  564. ::exit(EXIT_FAILURE);
  565. }
  566. } else {
  567. std::string fileToMoc = absFilename;
  568. if (!requiresMoc || basename != scannedFileBasename) {
  569. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  570. std::string headerToMoc =
  571. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  572. if (!headerToMoc.empty()) {
  573. // this is for KDE4 compatibility:
  574. fileToMoc = headerToMoc;
  575. if (!requiresMoc && basename == scannedFileBasename) {
  576. std::stringstream err;
  577. err << "AUTOGEN: warning: " << absFilename
  578. << ": The file "
  579. "includes the moc file \""
  580. << currentMoc << "\", but does not contain a " << macroName
  581. << " macro. Running moc on "
  582. << "\"" << headerToMoc << "\" ! Include \"moc_" << basename
  583. << ".cpp\" for a compatibility with "
  584. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  585. << std::endl;
  586. this->LogError(err.str());
  587. } else {
  588. std::stringstream err;
  589. err << "AUTOGEN: warning: " << absFilename
  590. << ": The file "
  591. "includes the moc file \""
  592. << currentMoc << "\" instead of \"moc_" << basename
  593. << ".cpp\". "
  594. "Running moc on "
  595. << "\"" << headerToMoc << "\" ! Include \"moc_" << basename
  596. << ".cpp\" for compatibility with "
  597. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  598. << std::endl;
  599. this->LogError(err.str());
  600. }
  601. } else {
  602. std::stringstream err;
  603. err << "AUTOGEN: error: " << absFilename
  604. << ": The file "
  605. "includes the moc file \""
  606. << currentMoc
  607. << "\", which seems to be the moc file from a different "
  608. "source file. CMake also could not find a matching "
  609. "header.\n"
  610. << std::endl;
  611. this->LogError(err.str());
  612. ::exit(EXIT_FAILURE);
  613. }
  614. } else {
  615. dotMocIncluded = true;
  616. ownDotMocFile = currentMoc;
  617. }
  618. includedMocs[fileToMoc] = currentMoc;
  619. }
  620. matchOffset += mocIncludeRegExp.end();
  621. } while (mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  622. }
  623. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  624. // If this is the case, the moc_foo.cpp should probably be generated from
  625. // foo.cpp instead of foo.h, because otherwise it won't build.
  626. // But warn, since this is not how it is supposed to be used.
  627. if (!dotMocIncluded && requiresMoc) {
  628. if (mocUnderscoreIncluded) {
  629. // this is for KDE4 compatibility:
  630. std::stringstream err;
  631. err << "AUTOGEN: warning: " << absFilename << ": The file "
  632. << "contains a " << macroName << " macro, but does not "
  633. "include "
  634. << "\"" << scannedFileBasename << ".moc\", but instead "
  635. "includes "
  636. << "\"" << ownMocUnderscoreFile << "\". Running moc on "
  637. << "\"" << absFilename << "\" ! Better include \""
  638. << scannedFileBasename
  639. << ".moc\" for compatibility with "
  640. "strict mode (see CMAKE_AUTOMOC_RELAXED_MODE).\n"
  641. << std::endl;
  642. this->LogError(err.str());
  643. includedMocs[absFilename] = ownMocUnderscoreFile;
  644. includedMocs.erase(ownMocHeaderFile);
  645. } else {
  646. // otherwise always error out since it will not compile:
  647. std::stringstream err;
  648. err << "AUTOGEN: error: " << absFilename << ": The file "
  649. << "contains a " << macroName << " macro, but does not "
  650. "include "
  651. << "\"" << scannedFileBasename << ".moc\" !\n"
  652. << std::endl;
  653. this->LogError(err.str());
  654. ::exit(EXIT_FAILURE);
  655. }
  656. }
  657. }
  658. void cmQtAutoGenerators::StrictParseCppFile(
  659. const std::string& absFilename,
  660. const std::vector<std::string>& headerExtensions,
  661. std::map<std::string, std::string>& includedMocs,
  662. std::map<std::string, std::vector<std::string> >& includedUis)
  663. {
  664. cmsys::RegularExpression mocIncludeRegExp(
  665. "[\n][ \t]*#[ \t]*include[ \t]+"
  666. "[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
  667. const std::string contentsString = ReadAll(absFilename);
  668. if (contentsString.empty()) {
  669. std::stringstream err;
  670. err << "AUTOGEN: warning: " << absFilename << ": file is empty\n"
  671. << std::endl;
  672. this->LogError(err.str());
  673. return;
  674. }
  675. this->ParseForUic(absFilename, contentsString, includedUis);
  676. if (this->MocExecutable.empty()) {
  677. return;
  678. }
  679. const std::string absPath = cmsys::SystemTools::GetFilenamePath(
  680. cmsys::SystemTools::GetRealPath(absFilename)) +
  681. '/';
  682. const std::string scannedFileBasename =
  683. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFilename);
  684. bool dotMocIncluded = false;
  685. std::string::size_type matchOffset = 0;
  686. // first a simple string check for "moc" is *much* faster than the regexp,
  687. // and if the string search already fails, we don't have to try the
  688. // expensive regexp
  689. if ((strstr(contentsString.c_str(), "moc") != NULL) &&
  690. (mocIncludeRegExp.find(contentsString))) {
  691. // for every moc include in the file
  692. do {
  693. const std::string currentMoc = mocIncludeRegExp.match(1);
  694. std::string basename =
  695. cmsys::SystemTools::GetFilenameWithoutLastExtension(currentMoc);
  696. const bool mocUnderscoreStyle = cmHasLiteralPrefix(basename, "moc_");
  697. // If the moc include is of the moc_foo.cpp style we expect
  698. // the Q_OBJECT class declaration in a header file.
  699. // If the moc include is of the foo.moc style we need to look for
  700. // a Q_OBJECT macro in the current source file, if it contains the
  701. // macro we generate the moc file from the source file.
  702. if (mocUnderscoreStyle) {
  703. // basename should be the part of the moc filename used for
  704. // finding the correct header, so we need to remove the moc_ part
  705. basename = basename.substr(4);
  706. std::string mocSubDir = extractSubDir(absPath, currentMoc);
  707. std::string headerToMoc =
  708. findMatchingHeader(absPath, mocSubDir, basename, headerExtensions);
  709. if (!headerToMoc.empty()) {
  710. includedMocs[headerToMoc] = currentMoc;
  711. } else {
  712. std::stringstream err;
  713. err << "AUTOGEN: error: " << absFilename << " The file "
  714. << "includes the moc file \"" << currentMoc << "\", "
  715. << "but could not find header \"" << basename << '{'
  716. << this->JoinExts(headerExtensions) << "}\" ";
  717. if (mocSubDir.empty()) {
  718. err << "in " << absPath << "\n" << std::endl;
  719. } else {
  720. err << "neither in " << absPath << " nor in " << mocSubDir << "\n"
  721. << std::endl;
  722. }
  723. this->LogError(err.str());
  724. ::exit(EXIT_FAILURE);
  725. }
  726. } else {
  727. if (basename != scannedFileBasename) {
  728. std::stringstream err;
  729. err << "AUTOGEN: error: " << absFilename
  730. << ": The file "
  731. "includes the moc file \""
  732. << currentMoc
  733. << "\", which seems to be the moc file from a different "
  734. "source file. This is not supported. "
  735. "Include \""
  736. << scannedFileBasename << ".moc\" to run "
  737. "moc on this source file.\n"
  738. << std::endl;
  739. this->LogError(err.str());
  740. ::exit(EXIT_FAILURE);
  741. }
  742. dotMocIncluded = true;
  743. includedMocs[absFilename] = currentMoc;
  744. }
  745. matchOffset += mocIncludeRegExp.end();
  746. } while (mocIncludeRegExp.find(contentsString.c_str() + matchOffset));
  747. }
  748. // In this case, check whether the scanned file itself contains a Q_OBJECT.
  749. // If this is the case, the moc_foo.cpp should probably be generated from
  750. // foo.cpp instead of foo.h, because otherwise it won't build.
  751. // But warn, since this is not how it is supposed to be used.
  752. std::string macroName;
  753. if (!dotMocIncluded && 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. std::string macroName;
  854. if (requiresMocing(contents, macroName)) {
  855. const std::string parentDir =
  856. this->TargetBuildSubDir + this->SourceRelativePath(headerName);
  857. const std::string basename =
  858. cmsys::SystemTools::GetFilenameWithoutLastExtension(headerName);
  859. const std::string currentMoc = parentDir + "moc_" + basename + ".cpp";
  860. notIncludedMocs[headerName] = currentMoc;
  861. }
  862. }
  863. this->ParseForUic(headerName, contents, includedUis);
  864. }
  865. }
  866. bool cmQtAutoGenerators::GenerateMocFiles(
  867. const std::map<std::string, std::string>& includedMocs,
  868. const std::map<std::string, std::string>& notIncludedMocs)
  869. {
  870. // look for name collisions
  871. {
  872. std::multimap<std::string, std::string> collisions;
  873. // Test merged map of included and notIncluded
  874. std::map<std::string, std::string> mergedMocs(includedMocs);
  875. mergedMocs.insert(notIncludedMocs.begin(), notIncludedMocs.end());
  876. if (this->NameCollisionTest(mergedMocs, collisions)) {
  877. std::stringstream err;
  878. err << "AUTOGEN: error: "
  879. "The same moc file will be generated "
  880. "from different sources."
  881. << std::endl
  882. << "To avoid this error either" << std::endl
  883. << "- rename the source files or" << std::endl
  884. << "- do not include the (moc_NAME.cpp|NAME.moc) file" << std::endl;
  885. this->NameCollisionLog(err.str(), collisions);
  886. ::exit(EXIT_FAILURE);
  887. }
  888. }
  889. // generate moc files that are included by source files.
  890. for (std::map<std::string, std::string>::const_iterator it =
  891. includedMocs.begin();
  892. it != includedMocs.end(); ++it) {
  893. if (!this->GenerateMoc(it->first, it->second)) {
  894. if (this->RunMocFailed) {
  895. return false;
  896. }
  897. }
  898. }
  899. // generate moc files that are _not_ included by source files.
  900. bool automocCppChanged = false;
  901. for (std::map<std::string, std::string>::const_iterator it =
  902. notIncludedMocs.begin();
  903. it != notIncludedMocs.end(); ++it) {
  904. if (this->GenerateMoc(it->first, it->second)) {
  905. automocCppChanged = true;
  906. } else {
  907. if (this->RunMocFailed) {
  908. return false;
  909. }
  910. }
  911. }
  912. // compose _automoc.cpp content
  913. std::string automocSource;
  914. {
  915. std::stringstream outStream;
  916. outStream << "/* This file is autogenerated, do not edit*/\n";
  917. if (notIncludedMocs.empty()) {
  918. outStream << "enum some_compilers { need_more_than_nothing };\n";
  919. } else {
  920. for (std::map<std::string, std::string>::const_iterator it =
  921. notIncludedMocs.begin();
  922. it != notIncludedMocs.end(); ++it) {
  923. outStream << "#include \"" << it->second << "\"\n";
  924. }
  925. }
  926. outStream.flush();
  927. automocSource = outStream.str();
  928. }
  929. // check if we even need to update _automoc.cpp
  930. if (!automocCppChanged) {
  931. // compare contents of the _automoc.cpp file
  932. const std::string oldContents = ReadAll(this->OutMocCppFilenameAbs);
  933. if (oldContents == automocSource) {
  934. // nothing changed: don't touch the _automoc.cpp file
  935. if (this->Verbose) {
  936. std::stringstream err;
  937. err << "AUTOGEN: " << this->OutMocCppFilenameRel << " still up to date"
  938. << std::endl;
  939. this->LogInfo(err.str());
  940. }
  941. return true;
  942. }
  943. }
  944. // actually write _automoc.cpp
  945. {
  946. std::string msg = "Generating moc compilation ";
  947. msg += this->OutMocCppFilenameRel;
  948. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  949. cmsysTerminal_Color_ForegroundBold,
  950. msg.c_str(), true, this->ColorOutput);
  951. }
  952. {
  953. cmsys::ofstream outfile;
  954. outfile.open(this->OutMocCppFilenameAbs.c_str(), std::ios::trunc);
  955. outfile << automocSource;
  956. outfile.close();
  957. }
  958. return true;
  959. }
  960. bool cmQtAutoGenerators::GenerateMoc(const std::string& sourceFile,
  961. const std::string& mocFileName)
  962. {
  963. const std::string mocFilePath = this->Builddir + mocFileName;
  964. int sourceNewerThanMoc = 0;
  965. bool success = cmsys::SystemTools::FileTimeCompare(sourceFile, mocFilePath,
  966. &sourceNewerThanMoc);
  967. if (this->GenerateAll || !success || sourceNewerThanMoc >= 0) {
  968. // make sure the directory for the resulting moc file exists
  969. std::string mocDir = mocFilePath.substr(0, mocFilePath.rfind('/'));
  970. if (!cmsys::SystemTools::FileExists(mocDir.c_str(), false)) {
  971. cmsys::SystemTools::MakeDirectory(mocDir.c_str());
  972. }
  973. std::string msg = "Generating moc source ";
  974. msg += mocFileName;
  975. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  976. cmsysTerminal_Color_ForegroundBold,
  977. msg.c_str(), true, this->ColorOutput);
  978. std::vector<std::string> command;
  979. command.push_back(this->MocExecutable);
  980. command.insert(command.end(), this->MocIncludes.begin(),
  981. this->MocIncludes.end());
  982. command.insert(command.end(), this->MocDefinitions.begin(),
  983. this->MocDefinitions.end());
  984. command.insert(command.end(), this->MocOptions.begin(),
  985. this->MocOptions.end());
  986. #ifdef _WIN32
  987. command.push_back("-DWIN32");
  988. #endif
  989. command.push_back("-o");
  990. command.push_back(mocFilePath);
  991. command.push_back(sourceFile);
  992. if (this->Verbose) {
  993. this->LogCommand(command);
  994. }
  995. std::string output;
  996. int retVal = 0;
  997. bool result =
  998. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  999. if (!result || retVal) {
  1000. std::stringstream err;
  1001. err << "AUTOGEN: error: process for " << mocFilePath << " failed:\n"
  1002. << output << std::endl;
  1003. this->LogError(err.str());
  1004. this->RunMocFailed = true;
  1005. cmSystemTools::RemoveFile(mocFilePath);
  1006. }
  1007. return true;
  1008. }
  1009. return false;
  1010. }
  1011. bool cmQtAutoGenerators::GenerateUiFiles(
  1012. const std::map<std::string, std::vector<std::string> >& includedUis)
  1013. {
  1014. // single map with input / output names
  1015. std::map<std::string, std::map<std::string, std::string> > uiGenMap;
  1016. std::map<std::string, std::string> testMap;
  1017. for (std::map<std::string, std::vector<std::string> >::const_iterator it =
  1018. includedUis.begin();
  1019. it != includedUis.end(); ++it) {
  1020. // source file path
  1021. std::string sourcePath = cmsys::SystemTools::GetFilenamePath(it->first);
  1022. sourcePath += '/';
  1023. // insert new map for source file an use new reference
  1024. uiGenMap[it->first] = std::map<std::string, std::string>();
  1025. std::map<std::string, std::string>& sourceMap = uiGenMap[it->first];
  1026. for (std::vector<std::string>::const_iterator sit = it->second.begin();
  1027. sit != it->second.end(); ++sit) {
  1028. const std::string& uiFileName = *sit;
  1029. const std::string uiInputFile = sourcePath + uiFileName + ".ui";
  1030. const std::string uiOutputFile = "ui_" + uiFileName + ".h";
  1031. sourceMap[uiInputFile] = uiOutputFile;
  1032. testMap[uiInputFile] = uiOutputFile;
  1033. }
  1034. }
  1035. // look for name collisions
  1036. {
  1037. std::multimap<std::string, std::string> collisions;
  1038. if (this->NameCollisionTest(testMap, collisions)) {
  1039. std::stringstream err;
  1040. err << "AUTOGEN: error: The same ui_NAME.h file will be generated "
  1041. "from different sources."
  1042. << std::endl
  1043. << "To avoid this error rename the source files." << std::endl;
  1044. this->NameCollisionLog(err.str(), collisions);
  1045. ::exit(EXIT_FAILURE);
  1046. }
  1047. }
  1048. testMap.clear();
  1049. // generate ui files
  1050. for (std::map<std::string,
  1051. std::map<std::string, std::string> >::const_iterator it =
  1052. uiGenMap.begin();
  1053. it != uiGenMap.end(); ++it) {
  1054. for (std::map<std::string, std::string>::const_iterator sit =
  1055. it->second.begin();
  1056. sit != it->second.end(); ++sit) {
  1057. if (!this->GenerateUi(it->first, sit->first, sit->second)) {
  1058. if (this->RunUicFailed) {
  1059. return false;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. return true;
  1065. }
  1066. bool cmQtAutoGenerators::GenerateUi(const std::string& realName,
  1067. const std::string& uiInputFile,
  1068. const std::string& uiOutputFile)
  1069. {
  1070. if (!cmsys::SystemTools::FileExists(this->Builddir.c_str(), false)) {
  1071. cmsys::SystemTools::MakeDirectory(this->Builddir.c_str());
  1072. }
  1073. const ::std::string uiBuildFile = this->Builddir + uiOutputFile;
  1074. int sourceNewerThanUi = 0;
  1075. bool success = cmsys::SystemTools::FileTimeCompare(uiInputFile, uiBuildFile,
  1076. &sourceNewerThanUi);
  1077. if (this->GenerateAll || !success || sourceNewerThanUi >= 0) {
  1078. std::string msg = "Generating ui header ";
  1079. msg += uiOutputFile;
  1080. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1081. cmsysTerminal_Color_ForegroundBold,
  1082. msg.c_str(), true, this->ColorOutput);
  1083. std::vector<std::string> command;
  1084. command.push_back(this->UicExecutable);
  1085. std::vector<std::string> opts = this->UicTargetOptions;
  1086. std::map<std::string, std::string>::const_iterator optionIt =
  1087. this->UicOptions.find(uiInputFile);
  1088. if (optionIt != this->UicOptions.end()) {
  1089. std::vector<std::string> fileOpts;
  1090. cmSystemTools::ExpandListArgument(optionIt->second, fileOpts);
  1091. cmQtAutoGenerators::MergeUicOptions(opts, fileOpts,
  1092. this->QtMajorVersion == "5");
  1093. }
  1094. command.insert(command.end(), opts.begin(), opts.end());
  1095. command.push_back("-o");
  1096. command.push_back(uiBuildFile);
  1097. command.push_back(uiInputFile);
  1098. if (this->Verbose) {
  1099. this->LogCommand(command);
  1100. }
  1101. std::string output;
  1102. int retVal = 0;
  1103. bool result =
  1104. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1105. if (!result || retVal) {
  1106. std::stringstream err;
  1107. err << "AUTOUIC: error: process for " << uiOutputFile
  1108. << " needed by\n \"" << realName << "\"\nfailed:\n"
  1109. << output << std::endl;
  1110. this->LogError(err.str());
  1111. this->RunUicFailed = true;
  1112. cmSystemTools::RemoveFile(uiOutputFile);
  1113. return false;
  1114. }
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. bool cmQtAutoGenerators::InputFilesNewerThanQrc(const std::string& qrcFile,
  1120. const std::string& rccOutput)
  1121. {
  1122. std::vector<std::string> const& files = this->RccInputs[qrcFile];
  1123. for (std::vector<std::string>::const_iterator it = files.begin();
  1124. it != files.end(); ++it) {
  1125. int inputNewerThanQrc = 0;
  1126. bool success =
  1127. cmsys::SystemTools::FileTimeCompare(*it, rccOutput, &inputNewerThanQrc);
  1128. if (!success || inputNewerThanQrc >= 0) {
  1129. return true;
  1130. }
  1131. }
  1132. return false;
  1133. }
  1134. bool cmQtAutoGenerators::GenerateQrcFiles()
  1135. {
  1136. // generate single map with input / output names
  1137. std::map<std::string, std::string> qrcGenMap;
  1138. for (std::vector<std::string>::const_iterator si = this->RccSources.begin();
  1139. si != this->RccSources.end(); ++si) {
  1140. const std::string ext = cmsys::SystemTools::GetFilenameLastExtension(*si);
  1141. if (ext == ".qrc") {
  1142. std::string basename =
  1143. cmsys::SystemTools::GetFilenameWithoutLastExtension(*si);
  1144. std::string qrcOutputFile = this->TargetBuildSubDir +
  1145. this->SourceRelativePath(*si) + "qrc_" + basename + ".cpp";
  1146. // std::string qrcOutputFile = "CMakeFiles/" + this->OriginTargetName
  1147. // + ".dir/qrc_" + basename + ".cpp";
  1148. qrcGenMap[*si] = qrcOutputFile;
  1149. }
  1150. }
  1151. // look for name collisions
  1152. {
  1153. std::multimap<std::string, std::string> collisions;
  1154. if (this->NameCollisionTest(qrcGenMap, collisions)) {
  1155. std::stringstream err;
  1156. err << "AUTOGEN: error: The same qrc_NAME.cpp file"
  1157. " will be generated from different sources."
  1158. << std::endl
  1159. << "To avoid this error rename the source .qrc files." << std::endl;
  1160. this->NameCollisionLog(err.str(), collisions);
  1161. ::exit(EXIT_FAILURE);
  1162. }
  1163. }
  1164. // generate qrc files
  1165. for (std::map<std::string, std::string>::const_iterator si =
  1166. qrcGenMap.begin();
  1167. si != qrcGenMap.end(); ++si) {
  1168. if (!this->GenerateQrc(si->first, si->second)) {
  1169. if (this->RunRccFailed) {
  1170. return false;
  1171. }
  1172. }
  1173. }
  1174. return true;
  1175. }
  1176. bool cmQtAutoGenerators::GenerateQrc(const std::string& qrcInputFile,
  1177. const std::string& qrcOutputFile)
  1178. {
  1179. std::string relName = this->SourceRelativePath(qrcInputFile);
  1180. std::replace(relName.begin(), relName.end(), '/', '_');
  1181. relName += cmsys::SystemTools::GetFilenameWithoutLastExtension(qrcInputFile);
  1182. const ::std::string qrcBuildFile = this->Builddir + qrcOutputFile;
  1183. int sourceNewerThanQrc = 0;
  1184. bool generateQrc = !cmsys::SystemTools::FileTimeCompare(
  1185. qrcInputFile, qrcBuildFile, &sourceNewerThanQrc);
  1186. generateQrc = generateQrc || (sourceNewerThanQrc >= 0);
  1187. generateQrc =
  1188. generateQrc || this->InputFilesNewerThanQrc(qrcInputFile, qrcBuildFile);
  1189. if (this->GenerateAll || generateQrc) {
  1190. std::string msg = "Generating qrc source ";
  1191. msg += qrcOutputFile;
  1192. cmSystemTools::MakefileColorEcho(cmsysTerminal_Color_ForegroundBlue |
  1193. cmsysTerminal_Color_ForegroundBold,
  1194. msg.c_str(), true, this->ColorOutput);
  1195. std::vector<std::string> command;
  1196. command.push_back(this->RccExecutable);
  1197. std::map<std::string, std::string>::const_iterator optionIt =
  1198. this->RccOptions.find(qrcInputFile);
  1199. if (optionIt != this->RccOptions.end()) {
  1200. cmSystemTools::ExpandListArgument(optionIt->second, command);
  1201. }
  1202. command.push_back("-name");
  1203. command.push_back(relName);
  1204. command.push_back("-o");
  1205. command.push_back(qrcBuildFile);
  1206. command.push_back(qrcInputFile);
  1207. if (this->Verbose) {
  1208. this->LogCommand(command);
  1209. }
  1210. std::string output;
  1211. int retVal = 0;
  1212. bool result =
  1213. cmSystemTools::RunSingleCommand(command, &output, &output, &retVal);
  1214. if (!result || retVal) {
  1215. std::stringstream err;
  1216. err << "AUTORCC: error: process for " << qrcOutputFile << " failed:\n"
  1217. << output << std::endl;
  1218. this->LogError(err.str());
  1219. this->RunRccFailed = true;
  1220. cmSystemTools::RemoveFile(qrcBuildFile);
  1221. return false;
  1222. }
  1223. }
  1224. return true;
  1225. }
  1226. std::string cmQtAutoGenerators::SourceRelativePath(const std::string& filename)
  1227. {
  1228. std::string pathRel;
  1229. // Test if the file is child to any of the known directories
  1230. {
  1231. std::string fileNameReal = cmsys::SystemTools::GetRealPath(filename);
  1232. std::string parentDirectory;
  1233. bool match(false);
  1234. {
  1235. const ::std::string* testDirs[4];
  1236. testDirs[0] = &(this->Srcdir);
  1237. testDirs[1] = &(this->Builddir);
  1238. testDirs[2] = &(this->ProjectSourceDir);
  1239. testDirs[3] = &(this->ProjectBinaryDir);
  1240. for (int ii = 0; ii != sizeof(testDirs) / sizeof(const ::std::string*);
  1241. ++ii) {
  1242. const ::std::string testDir =
  1243. cmsys::SystemTools::GetRealPath(*(testDirs[ii]));
  1244. if (cmsys::SystemTools::IsSubDirectory(fileNameReal, testDir)) {
  1245. parentDirectory = testDir;
  1246. match = true;
  1247. break;
  1248. }
  1249. }
  1250. }
  1251. // Use root as fallback parent directory
  1252. if (!match) {
  1253. cmsys::SystemTools::SplitPathRootComponent(fileNameReal,
  1254. &parentDirectory);
  1255. }
  1256. pathRel = cmsys::SystemTools::RelativePath(
  1257. parentDirectory, cmsys::SystemTools::GetParentDirectory(fileNameReal));
  1258. }
  1259. // Sanitize relative path
  1260. if (!pathRel.empty()) {
  1261. pathRel += '/';
  1262. cmSystemTools::ReplaceString(pathRel, "..", "__");
  1263. }
  1264. return pathRel;
  1265. }
  1266. /**
  1267. * @brief Collects name collisions as output/input pairs
  1268. * @return True if there were collisions
  1269. */
  1270. bool cmQtAutoGenerators::NameCollisionTest(
  1271. const std::map<std::string, std::string>& genFiles,
  1272. std::multimap<std::string, std::string>& collisions)
  1273. {
  1274. typedef std::map<std::string, std::string>::const_iterator Iter;
  1275. typedef std::map<std::string, std::string>::value_type VType;
  1276. for (Iter ait = genFiles.begin(); ait != genFiles.end(); ++ait) {
  1277. bool first_match(true);
  1278. for (Iter bit = (++Iter(ait)); bit != genFiles.end(); ++bit) {
  1279. if (ait->second == bit->second) {
  1280. if (first_match) {
  1281. if (collisions.find(ait->second) != collisions.end()) {
  1282. // We already know of this collision from before
  1283. break;
  1284. }
  1285. collisions.insert(VType(ait->second, ait->first));
  1286. first_match = false;
  1287. }
  1288. collisions.insert(VType(bit->second, bit->first));
  1289. }
  1290. }
  1291. }
  1292. return !collisions.empty();
  1293. }
  1294. void cmQtAutoGenerators::NameCollisionLog(
  1295. const std::string& message,
  1296. const std::multimap<std::string, std::string>& collisions)
  1297. {
  1298. typedef std::multimap<std::string, std::string>::const_iterator Iter;
  1299. std::stringstream err;
  1300. // Add message
  1301. err << message;
  1302. // Append collision list
  1303. for (Iter it = collisions.begin(); it != collisions.end(); ++it) {
  1304. err << it->first << " : " << it->second << std::endl;
  1305. }
  1306. this->LogError(err.str());
  1307. }
  1308. void cmQtAutoGenerators::LogInfo(const std::string& message)
  1309. {
  1310. std::cout << message;
  1311. }
  1312. void cmQtAutoGenerators::LogError(const std::string& message)
  1313. {
  1314. std::cerr << message;
  1315. }
  1316. void cmQtAutoGenerators::LogCommand(const std::vector<std::string>& command)
  1317. {
  1318. std::stringstream sbuf;
  1319. for (std::vector<std::string>::const_iterator cmdIt = command.begin();
  1320. cmdIt != command.end(); ++cmdIt) {
  1321. if (cmdIt != command.begin()) {
  1322. sbuf << " ";
  1323. }
  1324. sbuf << *cmdIt;
  1325. }
  1326. if (!sbuf.str().empty()) {
  1327. sbuf << std::endl;
  1328. this->LogInfo(sbuf.str());
  1329. }
  1330. }
  1331. std::string cmQtAutoGenerators::JoinExts(const std::vector<std::string>& lst)
  1332. {
  1333. if (lst.empty()) {
  1334. return "";
  1335. }
  1336. std::string result;
  1337. std::string separator = ",";
  1338. for (std::vector<std::string>::const_iterator it = lst.begin();
  1339. it != lst.end(); ++it) {
  1340. if (it != lst.begin()) {
  1341. result += separator;
  1342. }
  1343. result += '.' + (*it);
  1344. }
  1345. result.erase(result.end() - 1);
  1346. return result;
  1347. }