cmQtAutoGeneratorInitializer.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 "cmQtAutoGeneratorInitializer.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSourceFile.h"
  15. #include <sys/stat.h>
  16. #include <cmsys/FStream.hxx>
  17. #if defined(_WIN32) && !defined(__CYGWIN__)
  18. # include "cmGlobalVisualStudioGenerator.h"
  19. #endif
  20. void cmQtAutoGeneratorInitializer::SetupSourceFiles(cmTarget const* target,
  21. std::vector<std::string>& skipMoc,
  22. std::vector<std::string>& mocSources,
  23. std::vector<std::string>& mocHeaders,
  24. std::vector<std::string>& skipUic)
  25. {
  26. cmMakefile* makefile = target->GetMakefile();
  27. std::vector<cmSourceFile*> srcFiles;
  28. cmGeneratorTarget *gtgt = target->GetMakefile()
  29. ->GetGlobalGenerator()
  30. ->GetGeneratorTarget(target);
  31. gtgt->GetConfigCommonSourceFiles(srcFiles);
  32. std::vector<std::string> newRccFiles;
  33. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  34. fileIt != srcFiles.end();
  35. ++fileIt)
  36. {
  37. cmSourceFile* sf = *fileIt;
  38. std::string absFile = cmsys::SystemTools::GetRealPath(
  39. sf->GetFullPath());
  40. bool skipFileForMoc =
  41. cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOMOC"));
  42. bool generated = cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED"));
  43. if(cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTOUIC")))
  44. {
  45. skipUic.push_back(absFile);
  46. }
  47. std::string ext = sf->GetExtension();
  48. if (target->GetPropertyAsBool("AUTORCC"))
  49. {
  50. if (ext == "qrc"
  51. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  52. {
  53. std::string basename = cmsys::SystemTools::
  54. GetFilenameWithoutLastExtension(absFile);
  55. std::string rcc_output_dir = target->GetSupportDirectory();
  56. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  57. std::string rcc_output_file = rcc_output_dir;
  58. rcc_output_file += "/qrc_" + basename + ".cpp";
  59. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  60. rcc_output_file.c_str(), false);
  61. makefile->GetOrCreateSource(rcc_output_file, true);
  62. newRccFiles.push_back(rcc_output_file);
  63. }
  64. }
  65. if (!generated)
  66. {
  67. if (skipFileForMoc)
  68. {
  69. skipMoc.push_back(absFile);
  70. }
  71. else
  72. {
  73. cmSystemTools::FileFormat fileType = cmSystemTools::GetFileFormat(
  74. ext.c_str());
  75. if (fileType == cmSystemTools::CXX_FILE_FORMAT)
  76. {
  77. mocSources.push_back(absFile);
  78. }
  79. else if (fileType == cmSystemTools::HEADER_FILE_FORMAT)
  80. {
  81. mocHeaders.push_back(absFile);
  82. }
  83. }
  84. }
  85. }
  86. for(std::vector<std::string>::const_iterator fileIt = newRccFiles.begin();
  87. fileIt != newRccFiles.end();
  88. ++fileIt)
  89. {
  90. const_cast<cmTarget*>(target)->AddSource(*fileIt);
  91. }
  92. }
  93. static void GetCompileDefinitionsAndDirectories(cmTarget const* target,
  94. const std::string& config,
  95. std::string &incs,
  96. std::string &defs)
  97. {
  98. cmMakefile* makefile = target->GetMakefile();
  99. cmGlobalGenerator* globalGen = makefile->GetGlobalGenerator();
  100. std::vector<std::string> includeDirs;
  101. cmGeneratorTarget *gtgt = globalGen->GetGeneratorTarget(target);
  102. cmLocalGenerator *localGen = gtgt->GetLocalGenerator();
  103. // Get the include dirs for this target, without stripping the implicit
  104. // include dirs off, see http://public.kitware.com/Bug/view.php?id=13667
  105. localGen->GetIncludeDirectories(includeDirs, gtgt, "CXX", config, false);
  106. incs = cmJoin(includeDirs, ";");
  107. std::set<std::string> defines;
  108. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  109. defs += cmJoin(defines, ";");
  110. }
  111. void cmQtAutoGeneratorInitializer::SetupAutoMocTarget(cmTarget const* target,
  112. const std::string &autogenTargetName,
  113. std::vector<std::string> const& skipMoc,
  114. std::vector<std::string> const& mocHeaders,
  115. std::map<std::string, std::string> &configIncludes,
  116. std::map<std::string, std::string> &configDefines)
  117. {
  118. cmMakefile* makefile = target->GetMakefile();
  119. const char* tmp = target->GetProperty("AUTOMOC_MOC_OPTIONS");
  120. std::string _moc_options = (tmp!=0 ? tmp : "");
  121. makefile->AddDefinition("_moc_options",
  122. cmOutputConverter::EscapeForCMake(_moc_options).c_str());
  123. makefile->AddDefinition("_skip_moc",
  124. cmOutputConverter::EscapeForCMake(cmJoin(skipMoc, ";")).c_str());
  125. makefile->AddDefinition("_moc_headers",
  126. cmOutputConverter::EscapeForCMake(cmJoin(mocHeaders, ";")).c_str());
  127. bool relaxedMode = makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE");
  128. makefile->AddDefinition("_moc_relaxed_mode", relaxedMode ? "TRUE" : "FALSE");
  129. std::string _moc_incs;
  130. std::string _moc_compile_defs;
  131. std::vector<std::string> configs;
  132. const std::string& config = makefile->GetConfigurations(configs);
  133. GetCompileDefinitionsAndDirectories(target, config,
  134. _moc_incs, _moc_compile_defs);
  135. makefile->AddDefinition("_moc_incs",
  136. cmOutputConverter::EscapeForCMake(_moc_incs).c_str());
  137. makefile->AddDefinition("_moc_compile_defs",
  138. cmOutputConverter::EscapeForCMake(_moc_compile_defs).c_str());
  139. for (std::vector<std::string>::const_iterator li = configs.begin();
  140. li != configs.end(); ++li)
  141. {
  142. std::string config_moc_incs;
  143. std::string config_moc_compile_defs;
  144. GetCompileDefinitionsAndDirectories(target, *li,
  145. config_moc_incs,
  146. config_moc_compile_defs);
  147. if (config_moc_incs != _moc_incs)
  148. {
  149. configIncludes[*li] =
  150. cmOutputConverter::EscapeForCMake(config_moc_incs);
  151. if(_moc_incs.empty())
  152. {
  153. _moc_incs = config_moc_incs;
  154. }
  155. }
  156. if (config_moc_compile_defs != _moc_compile_defs)
  157. {
  158. configDefines[*li] =
  159. cmOutputConverter::EscapeForCMake(config_moc_compile_defs);
  160. if(_moc_compile_defs.empty())
  161. {
  162. _moc_compile_defs = config_moc_compile_defs;
  163. }
  164. }
  165. }
  166. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  167. if (strcmp(qtVersion, "5") == 0)
  168. {
  169. cmTarget *qt5Moc = makefile->FindTargetToUse("Qt5::moc");
  170. if (!qt5Moc)
  171. {
  172. cmSystemTools::Error("Qt5::moc target not found ",
  173. autogenTargetName.c_str());
  174. return;
  175. }
  176. makefile->AddDefinition("_qt_moc_executable",
  177. qt5Moc->ImportedGetLocation(""));
  178. }
  179. else if (strcmp(qtVersion, "4") == 0)
  180. {
  181. cmTarget *qt4Moc = makefile->FindTargetToUse("Qt4::moc");
  182. if (!qt4Moc)
  183. {
  184. cmSystemTools::Error("Qt4::moc target not found ",
  185. autogenTargetName.c_str());
  186. return;
  187. }
  188. makefile->AddDefinition("_qt_moc_executable",
  189. qt4Moc->ImportedGetLocation(""));
  190. }
  191. else
  192. {
  193. cmSystemTools::Error("The CMAKE_AUTOMOC feature supports only Qt 4 and "
  194. "Qt 5 ", autogenTargetName.c_str());
  195. }
  196. }
  197. static void GetUicOpts(cmTarget const* target, const std::string& config,
  198. std::string &optString)
  199. {
  200. cmGeneratorTarget *gtgt = target->GetMakefile()
  201. ->GetGlobalGenerator()
  202. ->GetGeneratorTarget(target);
  203. std::vector<std::string> opts;
  204. gtgt->GetAutoUicOptions(opts, config);
  205. optString = cmJoin(opts, ";");
  206. }
  207. std::string cmQtAutoGeneratorInitializer::GetAutogenTargetName(
  208. cmTarget const* target)
  209. {
  210. std::string autogenTargetName = target->GetName();
  211. autogenTargetName += "_automoc";
  212. return autogenTargetName;
  213. }
  214. std::string cmQtAutoGeneratorInitializer::GetAutogenTargetDir(
  215. cmTarget const* target)
  216. {
  217. cmMakefile* makefile = target->GetMakefile();
  218. std::string targetDir = makefile->GetCurrentBinaryDirectory();
  219. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  220. targetDir += "/";
  221. targetDir += cmQtAutoGeneratorInitializer::GetAutogenTargetName(target);
  222. targetDir += ".dir/";
  223. return targetDir;
  224. }
  225. static void copyTargetProperty(cmTarget* destinationTarget,
  226. cmTarget* sourceTarget,
  227. const std::string& propertyName)
  228. {
  229. const char* propertyValue = sourceTarget->GetProperty(propertyName);
  230. if (propertyValue)
  231. {
  232. destinationTarget->SetProperty(propertyName, propertyValue);
  233. }
  234. }
  235. static std::string cmQtAutoGeneratorsStripCR(std::string const& line)
  236. {
  237. // Strip CR characters rcc may have printed (possibly more than one!).
  238. std::string::size_type cr = line.find('\r');
  239. if (cr != line.npos)
  240. {
  241. return line.substr(0, cr);
  242. }
  243. return line;
  244. }
  245. static std::string ReadAll(const std::string& filename)
  246. {
  247. cmsys::ifstream file(filename.c_str());
  248. std::stringstream stream;
  249. stream << file.rdbuf();
  250. file.close();
  251. return stream.str();
  252. }
  253. std::string cmQtAutoGeneratorInitializer::ListQt5RccInputs(cmSourceFile* sf,
  254. cmTarget const* target,
  255. std::vector<std::string>& depends)
  256. {
  257. std::string rccCommand
  258. = cmQtAutoGeneratorInitializer::GetRccExecutable(target);
  259. std::vector<std::string> qrcEntries;
  260. std::vector<std::string> command;
  261. command.push_back(rccCommand);
  262. command.push_back("-list");
  263. std::string absFile = cmsys::SystemTools::GetRealPath(
  264. sf->GetFullPath());
  265. command.push_back(absFile);
  266. std::string rccStdOut;
  267. std::string rccStdErr;
  268. int retVal = 0;
  269. bool result = cmSystemTools::RunSingleCommand(
  270. command, &rccStdOut, &rccStdErr,
  271. &retVal, 0, cmSystemTools::OUTPUT_NONE);
  272. if (!result || retVal)
  273. {
  274. std::cerr << "AUTOGEN: error: Rcc list process for " << sf->GetFullPath()
  275. << " failed:\n" << rccStdOut << "\n" << rccStdErr << std::endl;
  276. return std::string();
  277. }
  278. {
  279. std::istringstream ostr(rccStdOut);
  280. std::string oline;
  281. while(std::getline(ostr, oline))
  282. {
  283. oline = cmQtAutoGeneratorsStripCR(oline);
  284. if(!oline.empty())
  285. {
  286. qrcEntries.push_back(oline);
  287. }
  288. }
  289. }
  290. {
  291. std::istringstream estr(rccStdErr);
  292. std::string eline;
  293. while(std::getline(estr, eline))
  294. {
  295. eline = cmQtAutoGeneratorsStripCR(eline);
  296. if (cmHasLiteralPrefix(eline, "RCC: Error in"))
  297. {
  298. static std::string searchString = "Cannot find file '";
  299. std::string::size_type pos = eline.find(searchString);
  300. if (pos == std::string::npos)
  301. {
  302. std::cerr << "AUTOGEN: error: Rcc lists unparsable output "
  303. << eline << std::endl;
  304. return std::string();
  305. }
  306. pos += searchString.length();
  307. std::string::size_type sz = eline.size() - pos - 1;
  308. qrcEntries.push_back(eline.substr(pos, sz));
  309. }
  310. }
  311. }
  312. depends.insert(depends.end(), qrcEntries.begin(), qrcEntries.end());
  313. return cmJoin(qrcEntries, "@list_sep@");
  314. }
  315. std::string cmQtAutoGeneratorInitializer::ListQt4RccInputs(cmSourceFile* sf,
  316. std::vector<std::string>& depends)
  317. {
  318. const std::string qrcContents = ReadAll(sf->GetFullPath());
  319. cmsys::RegularExpression fileMatchRegex("(<file[^<]+)");
  320. std::string entriesList;
  321. const char* sep = "";
  322. size_t offset = 0;
  323. while (fileMatchRegex.find(qrcContents.c_str() + offset))
  324. {
  325. std::string qrcEntry = fileMatchRegex.match(1);
  326. offset += qrcEntry.size();
  327. cmsys::RegularExpression fileReplaceRegex("(^<file[^>]*>)");
  328. fileReplaceRegex.find(qrcEntry);
  329. std::string tag = fileReplaceRegex.match(1);
  330. qrcEntry = qrcEntry.substr(tag.size());
  331. if (!cmSystemTools::FileIsFullPath(qrcEntry.c_str()))
  332. {
  333. qrcEntry = sf->GetLocation().GetDirectory() + "/" + qrcEntry;
  334. }
  335. entriesList += sep;
  336. entriesList += qrcEntry;
  337. sep = "@list_sep@";
  338. depends.push_back(qrcEntry);
  339. }
  340. return entriesList;
  341. }
  342. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(cmTarget* target)
  343. {
  344. cmMakefile* makefile = target->GetMakefile();
  345. if (target->GetPropertyAsBool("AUTOMOC"))
  346. {
  347. std::string automocTargetName =
  348. cmQtAutoGeneratorInitializer::GetAutogenTargetName(target);
  349. std::string mocCppFile = makefile->GetCurrentBinaryDirectory();
  350. mocCppFile += "/";
  351. mocCppFile += automocTargetName;
  352. mocCppFile += ".cpp";
  353. makefile->GetOrCreateSource(mocCppFile, true);
  354. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  355. mocCppFile.c_str(), false);
  356. target->AddSource(mocCppFile);
  357. }
  358. }
  359. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  360. cmLocalGenerator* lg,
  361. cmTarget* target)
  362. {
  363. cmMakefile* makefile = target->GetMakefile();
  364. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  365. if (qtMajorVersion == "")
  366. {
  367. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  368. }
  369. // create a custom target for running generators at buildtime:
  370. std::string autogenTargetName =
  371. cmQtAutoGeneratorInitializer::GetAutogenTargetName(target);
  372. std::string targetDir =
  373. cmQtAutoGeneratorInitializer::GetAutogenTargetDir(target);
  374. cmCustomCommandLine currentLine;
  375. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  376. currentLine.push_back("-E");
  377. currentLine.push_back("cmake_autogen");
  378. currentLine.push_back(targetDir);
  379. currentLine.push_back("$<CONFIGURATION>");
  380. cmCustomCommandLines commandLines;
  381. commandLines.push_back(currentLine);
  382. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  383. "", makefile->GetCurrentBinaryDirectory());
  384. std::vector<std::string> depends;
  385. if (const char *autogenDepends =
  386. target->GetProperty("AUTOGEN_TARGET_DEPENDS"))
  387. {
  388. cmSystemTools::ExpandListArgument(autogenDepends, depends);
  389. }
  390. std::vector<std::string> toolNames;
  391. if (target->GetPropertyAsBool("AUTOMOC"))
  392. {
  393. toolNames.push_back("moc");
  394. }
  395. if (target->GetPropertyAsBool("AUTOUIC"))
  396. {
  397. toolNames.push_back("uic");
  398. }
  399. if (target->GetPropertyAsBool("AUTORCC"))
  400. {
  401. toolNames.push_back("rcc");
  402. }
  403. std::string tools = toolNames[0];
  404. toolNames.erase(toolNames.begin());
  405. while (toolNames.size() > 1)
  406. {
  407. tools += ", " + toolNames[0];
  408. toolNames.erase(toolNames.begin());
  409. }
  410. if (toolNames.size() == 1)
  411. {
  412. tools += " and " + toolNames[0];
  413. }
  414. std::string autogenComment = "Automatic " + tools + " for target ";
  415. autogenComment += target->GetName();
  416. #if defined(_WIN32) && !defined(__CYGWIN__)
  417. bool usePRE_BUILD = false;
  418. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  419. if(gg->GetName().find("Visual Studio") != std::string::npos)
  420. {
  421. cmGlobalVisualStudioGenerator* vsgg =
  422. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  423. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  424. // reduce the number of targets loaded into the IDE.
  425. // This also works around a VS 11 bug that may skip updating the target:
  426. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  427. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  428. if(usePRE_BUILD)
  429. {
  430. for (std::vector<std::string>::iterator it = depends.begin();
  431. it != depends.end(); ++it)
  432. {
  433. if(!makefile->FindTargetToUse(it->c_str()))
  434. {
  435. usePRE_BUILD = false;
  436. break;
  437. }
  438. }
  439. }
  440. }
  441. #endif
  442. std::vector<std::string> rcc_output;
  443. bool const isNinja =
  444. lg->GetGlobalGenerator()->GetName() == "Ninja";
  445. if(isNinja
  446. #if defined(_WIN32) && !defined(__CYGWIN__)
  447. || usePRE_BUILD
  448. #endif
  449. )
  450. {
  451. std::vector<cmSourceFile*> srcFiles;
  452. cmGeneratorTarget* gtgt =
  453. lg->GetGlobalGenerator()->GetGeneratorTarget(target);
  454. gtgt->GetConfigCommonSourceFiles(srcFiles);
  455. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  456. fileIt != srcFiles.end();
  457. ++fileIt)
  458. {
  459. cmSourceFile* sf = *fileIt;
  460. std::string absFile = cmsys::SystemTools::GetRealPath(
  461. sf->GetFullPath());
  462. std::string ext = sf->GetExtension();
  463. if (target->GetPropertyAsBool("AUTORCC"))
  464. {
  465. if (ext == "qrc"
  466. && !cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC")))
  467. {
  468. std::string basename = cmsys::SystemTools::
  469. GetFilenameWithoutLastExtension(absFile);
  470. std::string rcc_output_dir = target->GetSupportDirectory();
  471. cmSystemTools::MakeDirectory(rcc_output_dir.c_str());
  472. std::string rcc_output_file = rcc_output_dir;
  473. rcc_output_file += "/qrc_" + basename + ".cpp";
  474. rcc_output.push_back(rcc_output_file);
  475. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  476. {
  477. if (qtMajorVersion == "5")
  478. {
  479. cmQtAutoGeneratorInitializer::ListQt5RccInputs(sf, target,
  480. depends);
  481. }
  482. else
  483. {
  484. cmQtAutoGeneratorInitializer::ListQt4RccInputs(sf, depends);
  485. }
  486. #if defined(_WIN32) && !defined(__CYGWIN__)
  487. // Cannot use PRE_BUILD because the resource files themselves
  488. // may not be sources within the target so VS may not know the
  489. // target needs to re-build at all.
  490. usePRE_BUILD = false;
  491. #endif
  492. }
  493. }
  494. }
  495. }
  496. }
  497. #if defined(_WIN32) && !defined(__CYGWIN__)
  498. if(usePRE_BUILD)
  499. {
  500. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  501. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  502. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  503. std::vector<std::string> no_output;
  504. std::vector<std::string> no_byproducts;
  505. cmCustomCommand cc(makefile, no_output, no_byproducts, depends,
  506. commandLines, autogenComment.c_str(),
  507. workingDirectory.c_str());
  508. cc.SetEscapeOldStyle(false);
  509. cc.SetEscapeAllowMakeVars(true);
  510. target->AddPreBuildCommand(cc);
  511. }
  512. else
  513. #endif
  514. {
  515. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  516. autogenTargetName, true,
  517. workingDirectory.c_str(),
  518. /*byproducts=*/rcc_output, depends,
  519. commandLines, false, autogenComment.c_str());
  520. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  521. makefile->AddGeneratorTarget(autogenTarget, gt);
  522. // Set target folder
  523. const char* autogenFolder = makefile->GetState()
  524. ->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  525. if (!autogenFolder)
  526. {
  527. autogenFolder = makefile->GetState()
  528. ->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  529. }
  530. if (autogenFolder && *autogenFolder)
  531. {
  532. autogenTarget->SetProperty("FOLDER", autogenFolder);
  533. }
  534. else
  535. {
  536. // inherit FOLDER property from target (#13688)
  537. copyTargetProperty(autogenTarget, target, "FOLDER");
  538. }
  539. target->AddUtility(autogenTargetName);
  540. }
  541. }
  542. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  543. cmTarget const* target)
  544. {
  545. cmMakefile* makefile = target->GetMakefile();
  546. // forget the variables added here afterwards again:
  547. cmMakefile::ScopePushPop varScope(makefile);
  548. static_cast<void>(varScope);
  549. // create a custom target for running generators at buildtime:
  550. std::string autogenTargetName =
  551. cmQtAutoGeneratorInitializer::GetAutogenTargetName(target);
  552. makefile->AddDefinition("_moc_target_name",
  553. cmOutputConverter::EscapeForCMake(autogenTargetName).c_str());
  554. makefile->AddDefinition("_origin_target_name",
  555. cmOutputConverter::EscapeForCMake(target->GetName()).c_str());
  556. std::string targetDir =
  557. cmQtAutoGeneratorInitializer::GetAutogenTargetDir(target);
  558. const char *qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  559. if (!qtVersion)
  560. {
  561. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  562. }
  563. cmGeneratorTarget *gtgt = target->GetMakefile()
  564. ->GetGlobalGenerator()
  565. ->GetGeneratorTarget(target);
  566. if (const char *targetQtVersion =
  567. gtgt->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  568. {
  569. qtVersion = targetQtVersion;
  570. }
  571. if (qtVersion)
  572. {
  573. makefile->AddDefinition("_target_qt_version", qtVersion);
  574. }
  575. std::vector<std::string> skipUic;
  576. std::vector<std::string> skipMoc;
  577. std::vector<std::string> mocSources;
  578. std::vector<std::string> mocHeaders;
  579. std::map<std::string, std::string> configIncludes;
  580. std::map<std::string, std::string> configDefines;
  581. std::map<std::string, std::string> configUicOptions;
  582. if (target->GetPropertyAsBool("AUTOMOC")
  583. || target->GetPropertyAsBool("AUTOUIC")
  584. || target->GetPropertyAsBool("AUTORCC"))
  585. {
  586. cmQtAutoGeneratorInitializer::SetupSourceFiles(target, skipMoc,
  587. mocSources, mocHeaders, skipUic);
  588. }
  589. makefile->AddDefinition("_cpp_files",
  590. cmOutputConverter::EscapeForCMake(cmJoin(mocSources, ";")).c_str());
  591. if (target->GetPropertyAsBool("AUTOMOC"))
  592. {
  593. cmQtAutoGeneratorInitializer::SetupAutoMocTarget(target, autogenTargetName,
  594. skipMoc, mocHeaders,
  595. configIncludes, configDefines);
  596. }
  597. if (target->GetPropertyAsBool("AUTOUIC"))
  598. {
  599. cmQtAutoGeneratorInitializer::SetupAutoUicTarget(target, skipUic,
  600. configUicOptions);
  601. }
  602. if (target->GetPropertyAsBool("AUTORCC"))
  603. {
  604. cmQtAutoGeneratorInitializer::SetupAutoRccTarget(target);
  605. }
  606. const char* cmakeRoot = makefile->GetSafeDefinition("CMAKE_ROOT");
  607. std::string inputFile = cmakeRoot;
  608. inputFile += "/Modules/AutogenInfo.cmake.in";
  609. std::string outputFile = targetDir;
  610. outputFile += "/AutogenInfo.cmake";
  611. makefile->AddDefinition("_qt_rcc_inputs",
  612. makefile->GetDefinition("_qt_rcc_inputs_" + target->GetName()));
  613. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(),
  614. false, true, false);
  615. // Ensure we have write permission in case .in was read-only.
  616. mode_t perm = 0;
  617. #if defined(_WIN32) && !defined(__CYGWIN__)
  618. mode_t mode_write = S_IWRITE;
  619. #else
  620. mode_t mode_write = S_IWUSR;
  621. #endif
  622. cmSystemTools::GetPermissions(outputFile, perm);
  623. if (!(perm & mode_write))
  624. {
  625. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  626. }
  627. if (!configDefines.empty()
  628. || !configIncludes.empty()
  629. || !configUicOptions.empty())
  630. {
  631. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  632. if ( !infoFile )
  633. {
  634. std::string error = "Internal CMake error when trying to open file: ";
  635. error += outputFile.c_str();
  636. error += " for writing.";
  637. cmSystemTools::Error(error.c_str());
  638. return;
  639. }
  640. if (!configDefines.empty())
  641. {
  642. for (std::map<std::string, std::string>::iterator
  643. it = configDefines.begin(), end = configDefines.end();
  644. it != end; ++it)
  645. {
  646. infoFile << "set(AM_MOC_COMPILE_DEFINITIONS_" << it->first <<
  647. " " << it->second << ")\n";
  648. }
  649. }
  650. if (!configIncludes.empty())
  651. {
  652. for (std::map<std::string, std::string>::iterator
  653. it = configIncludes.begin(), end = configIncludes.end();
  654. it != end; ++it)
  655. {
  656. infoFile << "set(AM_MOC_INCLUDES_" << it->first <<
  657. " " << it->second << ")\n";
  658. }
  659. }
  660. if (!configUicOptions.empty())
  661. {
  662. for (std::map<std::string, std::string>::iterator
  663. it = configUicOptions.begin(), end = configUicOptions.end();
  664. it != end; ++it)
  665. {
  666. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first <<
  667. " " << it->second << ")\n";
  668. }
  669. }
  670. }
  671. }
  672. void cmQtAutoGeneratorInitializer::SetupAutoUicTarget(cmTarget const* target,
  673. std::vector<std::string> const& skipUic,
  674. std::map<std::string, std::string> &configUicOptions)
  675. {
  676. cmMakefile *makefile = target->GetMakefile();
  677. std::set<std::string> skipped;
  678. skipped.insert(skipUic.begin(), skipUic.end());
  679. makefile->AddDefinition("_skip_uic",
  680. cmOutputConverter::EscapeForCMake(cmJoin(skipUic, ";")).c_str());
  681. std::vector<cmSourceFile*> uiFilesWithOptions
  682. = makefile->GetQtUiFilesWithOptions();
  683. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  684. std::string _uic_opts;
  685. std::vector<std::string> configs;
  686. const std::string& config = makefile->GetConfigurations(configs);
  687. GetUicOpts(target, config, _uic_opts);
  688. if (!_uic_opts.empty())
  689. {
  690. _uic_opts = cmOutputConverter::EscapeForCMake(_uic_opts);
  691. makefile->AddDefinition("_uic_target_options", _uic_opts.c_str());
  692. }
  693. for (std::vector<std::string>::const_iterator li = configs.begin();
  694. li != configs.end(); ++li)
  695. {
  696. std::string config_uic_opts;
  697. GetUicOpts(target, *li, config_uic_opts);
  698. if (config_uic_opts != _uic_opts)
  699. {
  700. configUicOptions[*li] =
  701. cmOutputConverter::EscapeForCMake(config_uic_opts);
  702. if(_uic_opts.empty())
  703. {
  704. _uic_opts = config_uic_opts;
  705. }
  706. }
  707. }
  708. std::string uiFileFiles;
  709. std::string uiFileOptions;
  710. const char* sep = "";
  711. for(std::vector<cmSourceFile*>::const_iterator fileIt =
  712. uiFilesWithOptions.begin();
  713. fileIt != uiFilesWithOptions.end();
  714. ++fileIt)
  715. {
  716. cmSourceFile* sf = *fileIt;
  717. std::string absFile = cmsys::SystemTools::GetRealPath(
  718. sf->GetFullPath());
  719. if (!skipped.insert(absFile).second)
  720. {
  721. continue;
  722. }
  723. uiFileFiles += sep;
  724. uiFileFiles += absFile;
  725. uiFileOptions += sep;
  726. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  727. cmSystemTools::ReplaceString(opts, ";", "@list_sep@");
  728. uiFileOptions += opts;
  729. sep = ";";
  730. }
  731. makefile->AddDefinition("_qt_uic_options_files",
  732. cmOutputConverter::EscapeForCMake(uiFileFiles).c_str());
  733. makefile->AddDefinition("_qt_uic_options_options",
  734. cmOutputConverter::EscapeForCMake(uiFileOptions).c_str());
  735. std::string targetName = target->GetName();
  736. if (strcmp(qtVersion, "5") == 0)
  737. {
  738. cmTarget *qt5Uic = makefile->FindTargetToUse("Qt5::uic");
  739. if (!qt5Uic)
  740. {
  741. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  742. }
  743. else
  744. {
  745. makefile->AddDefinition("_qt_uic_executable",
  746. qt5Uic->ImportedGetLocation(""));
  747. }
  748. }
  749. else if (strcmp(qtVersion, "4") == 0)
  750. {
  751. cmTarget *qt4Uic = makefile->FindTargetToUse("Qt4::uic");
  752. if (!qt4Uic)
  753. {
  754. cmSystemTools::Error("Qt4::uic target not found ",
  755. targetName.c_str());
  756. return;
  757. }
  758. makefile->AddDefinition("_qt_uic_executable",
  759. qt4Uic->ImportedGetLocation(""));
  760. }
  761. else
  762. {
  763. cmSystemTools::Error("The CMAKE_AUTOUIC feature supports only Qt 4 and "
  764. "Qt 5 ", targetName.c_str());
  765. }
  766. }
  767. void cmQtAutoGeneratorInitializer::MergeRccOptions(
  768. std::vector<std::string> &opts,
  769. const std::vector<std::string> &fileOpts,
  770. bool isQt5)
  771. {
  772. static const char* valueOptions[] = {
  773. "name",
  774. "root",
  775. "compress",
  776. "threshold"
  777. };
  778. std::vector<std::string> extraOpts;
  779. for(std::vector<std::string>::const_iterator it = fileOpts.begin();
  780. it != fileOpts.end(); ++it)
  781. {
  782. std::vector<std::string>::iterator existingIt
  783. = std::find(opts.begin(), opts.end(), *it);
  784. if (existingIt != opts.end())
  785. {
  786. const char *o = it->c_str();
  787. if (*o == '-')
  788. {
  789. ++o;
  790. }
  791. if (isQt5 && *o == '-')
  792. {
  793. ++o;
  794. }
  795. if (std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  796. cmStrCmp(*it)) != cmArrayEnd(valueOptions))
  797. {
  798. assert(existingIt + 1 != opts.end());
  799. *(existingIt + 1) = *(it + 1);
  800. ++it;
  801. }
  802. }
  803. else
  804. {
  805. extraOpts.push_back(*it);
  806. }
  807. }
  808. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  809. }
  810. void cmQtAutoGeneratorInitializer::SetupAutoRccTarget(cmTarget const* target)
  811. {
  812. std::string _rcc_files;
  813. const char* sepRccFiles = "";
  814. cmMakefile *makefile = target->GetMakefile();
  815. std::vector<cmSourceFile*> srcFiles;
  816. cmGeneratorTarget *gtgt = target->GetMakefile()
  817. ->GetGlobalGenerator()
  818. ->GetGeneratorTarget(target);
  819. gtgt->GetConfigCommonSourceFiles(srcFiles);
  820. std::string qrcInputs;
  821. const char* qrcInputsSep = "";
  822. std::string rccFileFiles;
  823. std::string rccFileOptions;
  824. const char *optionSep = "";
  825. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  826. std::vector<std::string> rccOptions;
  827. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS"))
  828. {
  829. cmSystemTools::ExpandListArgument(opts, rccOptions);
  830. }
  831. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  832. if (qtMajorVersion == "")
  833. {
  834. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  835. }
  836. for(std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  837. fileIt != srcFiles.end();
  838. ++fileIt)
  839. {
  840. cmSourceFile* sf = *fileIt;
  841. std::string ext = sf->GetExtension();
  842. if (ext == "qrc")
  843. {
  844. std::string absFile = cmsys::SystemTools::GetRealPath(
  845. sf->GetFullPath());
  846. bool skip = cmSystemTools::IsOn(sf->GetPropertyForUser("SKIP_AUTORCC"));
  847. if (!skip)
  848. {
  849. _rcc_files += sepRccFiles;
  850. _rcc_files += absFile;
  851. sepRccFiles = ";";
  852. if (const char *prop = sf->GetProperty("AUTORCC_OPTIONS"))
  853. {
  854. std::vector<std::string> optsVec;
  855. cmSystemTools::ExpandListArgument(prop, optsVec);
  856. cmQtAutoGeneratorInitializer::MergeRccOptions(rccOptions, optsVec,
  857. strcmp(qtVersion, "5") == 0);
  858. }
  859. if (!rccOptions.empty())
  860. {
  861. rccFileFiles += optionSep;
  862. rccFileFiles += absFile;
  863. rccFileOptions += optionSep;
  864. }
  865. const char *listSep = "";
  866. for(std::vector<std::string>::const_iterator it = rccOptions.begin();
  867. it != rccOptions.end();
  868. ++it)
  869. {
  870. rccFileOptions += listSep;
  871. rccFileOptions += *it;
  872. listSep = "@list_sep@";
  873. }
  874. optionSep = ";";
  875. std::vector<std::string> depends;
  876. std::string entriesList;
  877. if (!cmSystemTools::IsOn(sf->GetPropertyForUser("GENERATED")))
  878. {
  879. if (qtMajorVersion == "5")
  880. {
  881. entriesList = cmQtAutoGeneratorInitializer::ListQt5RccInputs(sf,
  882. target,
  883. depends);
  884. }
  885. else
  886. {
  887. entriesList =
  888. cmQtAutoGeneratorInitializer::ListQt4RccInputs(sf, depends);
  889. }
  890. if (entriesList.empty())
  891. {
  892. return;
  893. }
  894. }
  895. qrcInputs += qrcInputsSep;
  896. qrcInputs += entriesList;
  897. qrcInputsSep = ";";
  898. }
  899. }
  900. }
  901. makefile->AddDefinition("_qt_rcc_inputs_" + target->GetName(),
  902. cmOutputConverter::EscapeForCMake(qrcInputs).c_str());
  903. makefile->AddDefinition("_rcc_files",
  904. cmOutputConverter::EscapeForCMake(_rcc_files).c_str());
  905. makefile->AddDefinition("_qt_rcc_options_files",
  906. cmOutputConverter::EscapeForCMake(rccFileFiles).c_str());
  907. makefile->AddDefinition("_qt_rcc_options_options",
  908. cmOutputConverter::EscapeForCMake(rccFileOptions).c_str());
  909. makefile->AddDefinition("_qt_rcc_executable",
  910. cmQtAutoGeneratorInitializer::GetRccExecutable(target).c_str());
  911. }
  912. std::string cmQtAutoGeneratorInitializer::GetRccExecutable(
  913. cmTarget const* target)
  914. {
  915. cmGeneratorTarget *gtgt = target->GetMakefile()
  916. ->GetGlobalGenerator()
  917. ->GetGeneratorTarget(target);
  918. cmMakefile *makefile = target->GetMakefile();
  919. const char *qtVersion = makefile->GetDefinition("_target_qt_version");
  920. if (!qtVersion)
  921. {
  922. qtVersion = makefile->GetDefinition("Qt5Core_VERSION_MAJOR");
  923. if (!qtVersion)
  924. {
  925. qtVersion = makefile->GetDefinition("QT_VERSION_MAJOR");
  926. }
  927. if (const char *targetQtVersion =
  928. gtgt->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", ""))
  929. {
  930. qtVersion = targetQtVersion;
  931. }
  932. }
  933. std::string targetName = target->GetName();
  934. if (strcmp(qtVersion, "5") == 0)
  935. {
  936. cmTarget *qt5Rcc = makefile->FindTargetToUse("Qt5::rcc");
  937. if (!qt5Rcc)
  938. {
  939. cmSystemTools::Error("Qt5::rcc target not found ",
  940. targetName.c_str());
  941. return std::string();
  942. }
  943. return qt5Rcc->ImportedGetLocation("");
  944. }
  945. else if (strcmp(qtVersion, "4") == 0)
  946. {
  947. cmTarget *qt4Rcc = makefile->FindTargetToUse("Qt4::rcc");
  948. if (!qt4Rcc)
  949. {
  950. cmSystemTools::Error("Qt4::rcc target not found ",
  951. targetName.c_str());
  952. return std::string();
  953. }
  954. return qt4Rcc->ImportedGetLocation("");
  955. }
  956. cmSystemTools::Error("The CMAKE_AUTORCC feature supports only Qt 4 and "
  957. "Qt 5 ", targetName.c_str());
  958. return std::string();
  959. }