cmQtAutoGenerators.cxx 46 KB

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