cmQtAutoGeneratorInitializer.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmQtAutoGeneratorInitializer.h"
  4. #include "cmQtAutoGeneratorCommon.h"
  5. #include "cmAlgorithms.h"
  6. #include "cmCustomCommandLines.h"
  7. #include "cmFilePathChecksum.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmSourceFile.h"
  13. #include "cmSourceGroup.h"
  14. #include "cmState.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cm_sys_stat.h"
  18. #include "cmake.h"
  19. #if defined(_WIN32) && !defined(__CYGWIN__)
  20. #include "cmGlobalVisualStudioGenerator.h"
  21. #endif
  22. #include <algorithm>
  23. #include <cmConfigure.h>
  24. #include <cmsys/FStream.hxx>
  25. #include <map>
  26. #include <set>
  27. #include <string>
  28. #include <utility>
  29. #include <vector>
  30. static void utilCopyTargetProperty(cmTarget* destinationTarget,
  31. cmTarget* sourceTarget,
  32. const std::string& propertyName)
  33. {
  34. const char* propertyValue = sourceTarget->GetProperty(propertyName);
  35. if (propertyValue) {
  36. destinationTarget->SetProperty(propertyName, propertyValue);
  37. }
  38. }
  39. inline static bool PropertyEnabled(cmSourceFile* sourceFile, const char* key)
  40. {
  41. return cmSystemTools::IsOn(sourceFile->GetPropertyForUser(key));
  42. }
  43. static std::string GetSafeProperty(cmGeneratorTarget const* target,
  44. const char* key)
  45. {
  46. const char* tmp = target->GetProperty(key);
  47. return std::string((tmp != CM_NULLPTR) ? tmp : "");
  48. }
  49. static std::string GetAutogenTargetName(cmGeneratorTarget const* target)
  50. {
  51. std::string autogenTargetName = target->GetName();
  52. autogenTargetName += "_autogen";
  53. return autogenTargetName;
  54. }
  55. static std::string GetAutogenTargetFilesDir(cmGeneratorTarget const* target)
  56. {
  57. cmMakefile* makefile = target->Target->GetMakefile();
  58. std::string targetDir = makefile->GetCurrentBinaryDirectory();
  59. targetDir += makefile->GetCMakeInstance()->GetCMakeFilesDirectory();
  60. targetDir += "/";
  61. targetDir += GetAutogenTargetName(target);
  62. targetDir += ".dir/";
  63. return targetDir;
  64. }
  65. static std::string GetAutogenTargetBuildDir(cmGeneratorTarget const* target)
  66. {
  67. cmMakefile* makefile = target->Target->GetMakefile();
  68. std::string targetDir = makefile->GetCurrentBinaryDirectory();
  69. targetDir += "/";
  70. targetDir += GetAutogenTargetName(target);
  71. targetDir += "/";
  72. return targetDir;
  73. }
  74. static std::string GetQtMajorVersion(cmGeneratorTarget const* target)
  75. {
  76. cmMakefile* makefile = target->Target->GetMakefile();
  77. std::string qtMajorVersion = makefile->GetSafeDefinition("QT_VERSION_MAJOR");
  78. if (qtMajorVersion.empty()) {
  79. qtMajorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MAJOR");
  80. }
  81. const char* targetQtVersion =
  82. target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION", "");
  83. if (targetQtVersion != CM_NULLPTR) {
  84. qtMajorVersion = targetQtVersion;
  85. }
  86. return qtMajorVersion;
  87. }
  88. static std::string GetQtMinorVersion(cmGeneratorTarget const* target,
  89. const std::string& qtMajorVersion)
  90. {
  91. cmMakefile* makefile = target->Target->GetMakefile();
  92. std::string qtMinorVersion;
  93. if (qtMajorVersion == "5") {
  94. qtMinorVersion = makefile->GetSafeDefinition("Qt5Core_VERSION_MINOR");
  95. }
  96. if (qtMinorVersion.empty()) {
  97. qtMinorVersion = makefile->GetSafeDefinition("QT_VERSION_MINOR");
  98. }
  99. const char* targetQtVersion =
  100. target->GetLinkInterfaceDependentStringProperty("QT_MINOR_VERSION", "");
  101. if (targetQtVersion != CM_NULLPTR) {
  102. qtMinorVersion = targetQtVersion;
  103. }
  104. return qtMinorVersion;
  105. }
  106. static bool QtVersionGreaterOrEqual(const std::string& major,
  107. const std::string& minor,
  108. unsigned long requestMajor,
  109. unsigned long requestMinor)
  110. {
  111. unsigned long majorUL(0);
  112. unsigned long minorUL(0);
  113. if (cmSystemTools::StringToULong(major.c_str(), &majorUL) &&
  114. cmSystemTools::StringToULong(minor.c_str(), &minorUL)) {
  115. return (majorUL > requestMajor) ||
  116. (majorUL == requestMajor && minorUL >= requestMinor);
  117. }
  118. return false;
  119. }
  120. static void GetCompileDefinitionsAndDirectories(
  121. cmGeneratorTarget const* target, const std::string& config,
  122. std::string& incs, std::string& defs)
  123. {
  124. cmLocalGenerator* localGen = target->GetLocalGenerator();
  125. {
  126. std::vector<std::string> includeDirs;
  127. // Get the include dirs for this target, without stripping the implicit
  128. // include dirs off, see
  129. // https://gitlab.kitware.com/cmake/cmake/issues/13667
  130. localGen->GetIncludeDirectories(includeDirs, target, "CXX", config, false);
  131. incs = cmJoin(includeDirs, ";");
  132. }
  133. {
  134. std::set<std::string> defines;
  135. localGen->AddCompileDefinitions(defines, target, config, "CXX");
  136. defs += cmJoin(defines, ";");
  137. }
  138. }
  139. static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
  140. const std::string& value)
  141. {
  142. makefile->AddDefinition(key,
  143. cmOutputConverter::EscapeForCMake(value).c_str());
  144. }
  145. static void AddDefinitionEscaped(cmMakefile* makefile, const char* key,
  146. const std::vector<std::string>& values)
  147. {
  148. makefile->AddDefinition(
  149. key, cmOutputConverter::EscapeForCMake(cmJoin(values, ";")).c_str());
  150. }
  151. static bool AddToSourceGroup(cmMakefile* makefile, const std::string& fileName,
  152. cmQtAutoGeneratorCommon::GeneratorType genType)
  153. {
  154. cmSourceGroup* sourceGroup = CM_NULLPTR;
  155. // Acquire source group
  156. {
  157. const char* groupName = CM_NULLPTR;
  158. // Use generator specific group name
  159. switch (genType) {
  160. case cmQtAutoGeneratorCommon::MOC:
  161. groupName =
  162. makefile->GetState()->GetGlobalProperty("AUTOMOC_SOURCE_GROUP");
  163. break;
  164. case cmQtAutoGeneratorCommon::RCC:
  165. groupName =
  166. makefile->GetState()->GetGlobalProperty("AUTORCC_SOURCE_GROUP");
  167. break;
  168. default:
  169. break;
  170. }
  171. // Use default group name on demand
  172. if ((groupName == CM_NULLPTR) || (*groupName == 0)) {
  173. groupName =
  174. makefile->GetState()->GetGlobalProperty("AUTOGEN_SOURCE_GROUP");
  175. }
  176. // Generate a source group on demand
  177. if ((groupName != CM_NULLPTR) && (*groupName != 0)) {
  178. {
  179. const char* delimiter =
  180. makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
  181. if (delimiter == CM_NULLPTR) {
  182. delimiter = "\\";
  183. }
  184. std::vector<std::string> folders =
  185. cmSystemTools::tokenize(groupName, delimiter);
  186. sourceGroup = makefile->GetSourceGroup(folders);
  187. if (sourceGroup == CM_NULLPTR) {
  188. makefile->AddSourceGroup(folders);
  189. sourceGroup = makefile->GetSourceGroup(folders);
  190. }
  191. }
  192. if (sourceGroup == CM_NULLPTR) {
  193. cmSystemTools::Error(
  194. "Autogen: Could not create or find source group: ",
  195. cmQtAutoGeneratorCommon::Quoted(groupName).c_str());
  196. return false;
  197. }
  198. }
  199. }
  200. if (sourceGroup != CM_NULLPTR) {
  201. sourceGroup->AddGroupFile(fileName);
  202. }
  203. return true;
  204. }
  205. static void AcquireScanFiles(cmGeneratorTarget const* target,
  206. std::vector<std::string>& mocUicSources,
  207. std::vector<std::string>& mocUicHeaders,
  208. std::vector<std::string>& mocSkipList,
  209. std::vector<std::string>& uicSkipList)
  210. {
  211. const bool mocTarget = target->GetPropertyAsBool("AUTOMOC");
  212. const bool uicTarget = target->GetPropertyAsBool("AUTOUIC");
  213. std::vector<cmSourceFile*> srcFiles;
  214. target->GetConfigCommonSourceFiles(srcFiles);
  215. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  216. fileIt != srcFiles.end(); ++fileIt) {
  217. cmSourceFile* sf = *fileIt;
  218. const cmSystemTools::FileFormat fileType =
  219. cmSystemTools::GetFileFormat(sf->GetExtension().c_str());
  220. if (!(fileType == cmSystemTools::CXX_FILE_FORMAT) &&
  221. !(fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  222. continue;
  223. }
  224. const std::string absFile =
  225. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  226. // Skip flags
  227. const bool skipAll = PropertyEnabled(sf, "SKIP_AUTOGEN");
  228. const bool mocSkip = skipAll || PropertyEnabled(sf, "SKIP_AUTOMOC");
  229. const bool uicSkip = skipAll || PropertyEnabled(sf, "SKIP_AUTOUIC");
  230. // Add file name to skip lists.
  231. // Do this even when the file is not added to the sources/headers lists
  232. // because the file name may be extracted from an other file when
  233. // processing
  234. if (mocSkip) {
  235. mocSkipList.push_back(absFile);
  236. }
  237. if (uicSkip) {
  238. uicSkipList.push_back(absFile);
  239. }
  240. if ((mocTarget && !mocSkip) || (uicTarget && !uicSkip)) {
  241. // Add file name to sources or headers list
  242. switch (fileType) {
  243. case cmSystemTools::CXX_FILE_FORMAT:
  244. mocUicSources.push_back(absFile);
  245. break;
  246. case cmSystemTools::HEADER_FILE_FORMAT:
  247. mocUicHeaders.push_back(absFile);
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. static void MocSetupAutoTarget(
  256. cmGeneratorTarget const* target, const std::string& autogenTargetName,
  257. const std::string& qtMajorVersion,
  258. std::vector<std::string> const& mocSkipList,
  259. std::map<std::string, std::string>& configIncludes,
  260. std::map<std::string, std::string>& configDefines)
  261. {
  262. cmLocalGenerator* lg = target->GetLocalGenerator();
  263. cmMakefile* makefile = target->Target->GetMakefile();
  264. AddDefinitionEscaped(makefile, "_moc_options",
  265. GetSafeProperty(target, "AUTOMOC_MOC_OPTIONS"));
  266. AddDefinitionEscaped(makefile, "_moc_skip", mocSkipList);
  267. AddDefinitionEscaped(makefile, "_moc_relaxed_mode",
  268. makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE") ? "TRUE"
  269. : "FALSE");
  270. AddDefinitionEscaped(makefile, "_moc_depend_filters",
  271. GetSafeProperty(target, "AUTOMOC_DEPEND_FILTERS"));
  272. if (QtVersionGreaterOrEqual(
  273. qtMajorVersion, GetQtMinorVersion(target, qtMajorVersion), 5, 8)) {
  274. AddDefinitionEscaped(
  275. makefile, "_moc_predefs_cmd",
  276. makefile->GetSafeDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
  277. }
  278. // Moc includes and compile definitions
  279. {
  280. std::string _moc_incs;
  281. std::string _moc_compile_defs;
  282. std::vector<std::string> configs;
  283. {
  284. const std::string& config = makefile->GetConfigurations(configs);
  285. GetCompileDefinitionsAndDirectories(target, config, _moc_incs,
  286. _moc_compile_defs);
  287. AddDefinitionEscaped(makefile, "_moc_incs", _moc_incs);
  288. AddDefinitionEscaped(makefile, "_moc_compile_defs", _moc_compile_defs);
  289. }
  290. for (std::vector<std::string>::const_iterator li = configs.begin();
  291. li != configs.end(); ++li) {
  292. std::string config_moc_incs;
  293. std::string config_moc_compile_defs;
  294. GetCompileDefinitionsAndDirectories(target, *li, config_moc_incs,
  295. config_moc_compile_defs);
  296. if (config_moc_incs != _moc_incs) {
  297. configIncludes[*li] =
  298. cmOutputConverter::EscapeForCMake(config_moc_incs);
  299. if (_moc_incs.empty()) {
  300. _moc_incs = config_moc_incs;
  301. }
  302. }
  303. if (config_moc_compile_defs != _moc_compile_defs) {
  304. configDefines[*li] =
  305. cmOutputConverter::EscapeForCMake(config_moc_compile_defs);
  306. if (_moc_compile_defs.empty()) {
  307. _moc_compile_defs = config_moc_compile_defs;
  308. }
  309. }
  310. }
  311. }
  312. // Moc executable
  313. {
  314. std::string err;
  315. const char* mocExec = CM_NULLPTR;
  316. if (qtMajorVersion == "5") {
  317. cmGeneratorTarget* qt5Moc = lg->FindGeneratorTargetToUse("Qt5::moc");
  318. if (qt5Moc != CM_NULLPTR) {
  319. mocExec = qt5Moc->ImportedGetLocation("");
  320. } else {
  321. err = "Qt5::moc target not found " + autogenTargetName;
  322. }
  323. } else if (qtMajorVersion == "4") {
  324. cmGeneratorTarget* qt4Moc = lg->FindGeneratorTargetToUse("Qt4::moc");
  325. if (qt4Moc != CM_NULLPTR) {
  326. mocExec = qt4Moc->ImportedGetLocation("");
  327. } else {
  328. err = "Qt4::moc target not found " + autogenTargetName;
  329. }
  330. } else {
  331. err = "The CMAKE_AUTOMOC feature supports only Qt 4 and Qt 5 ";
  332. err += autogenTargetName;
  333. }
  334. // Add definition or error
  335. if (err.empty()) {
  336. AddDefinitionEscaped(makefile, "_qt_moc_executable",
  337. mocExec ? mocExec : "");
  338. } else {
  339. cmSystemTools::Error(err.c_str());
  340. }
  341. }
  342. }
  343. static void UicGetOpts(cmGeneratorTarget const* target,
  344. const std::string& config, std::string& optString)
  345. {
  346. std::vector<std::string> opts;
  347. target->GetAutoUicOptions(opts, config);
  348. optString = cmJoin(opts, ";");
  349. }
  350. static void UicSetupAutoTarget(
  351. cmGeneratorTarget const* target, const std::string& qtMajorVersion,
  352. std::vector<std::string> const& uicSkipList,
  353. std::map<std::string, std::string>& configUicOptions)
  354. {
  355. cmLocalGenerator* lg = target->GetLocalGenerator();
  356. cmMakefile* makefile = target->Target->GetMakefile();
  357. AddDefinitionEscaped(makefile, "_uic_skip", uicSkipList);
  358. // Uic search paths
  359. {
  360. std::vector<std::string> uicSearchPaths;
  361. cmSystemTools::ExpandListArgument(
  362. GetSafeProperty(target, "AUTOUIC_SEARCH_PATHS"), uicSearchPaths);
  363. const std::string srcDir = makefile->GetCurrentSourceDirectory();
  364. for (std::vector<std::string>::iterator it = uicSearchPaths.begin();
  365. it != uicSearchPaths.end(); ++it) {
  366. *it = cmSystemTools::CollapseFullPath(*it, srcDir);
  367. }
  368. AddDefinitionEscaped(makefile, "_uic_search_paths", uicSearchPaths);
  369. }
  370. // Uic target options
  371. {
  372. std::string _uic_opts;
  373. std::vector<std::string> configs;
  374. UicGetOpts(target, makefile->GetConfigurations(configs), _uic_opts);
  375. AddDefinitionEscaped(makefile, "_uic_target_options", _uic_opts);
  376. for (std::vector<std::string>::const_iterator li = configs.begin();
  377. li != configs.end(); ++li) {
  378. std::string config_uic_opts;
  379. UicGetOpts(target, *li, config_uic_opts);
  380. if (config_uic_opts != _uic_opts) {
  381. configUicOptions[*li] =
  382. cmOutputConverter::EscapeForCMake(config_uic_opts);
  383. if (_uic_opts.empty()) {
  384. _uic_opts = config_uic_opts;
  385. }
  386. }
  387. }
  388. }
  389. // Uic files options
  390. {
  391. std::vector<std::string> uiFileFiles;
  392. std::vector<std::string> uiFileOptions;
  393. {
  394. std::set<std::string> skipped;
  395. skipped.insert(uicSkipList.begin(), uicSkipList.end());
  396. const std::vector<cmSourceFile*> uiFilesWithOptions =
  397. makefile->GetQtUiFilesWithOptions();
  398. for (std::vector<cmSourceFile*>::const_iterator fileIt =
  399. uiFilesWithOptions.begin();
  400. fileIt != uiFilesWithOptions.end(); ++fileIt) {
  401. cmSourceFile* sf = *fileIt;
  402. const std::string absFile =
  403. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  404. if (skipped.insert(absFile).second) {
  405. // The file wasn't skipped
  406. uiFileFiles.push_back(absFile);
  407. {
  408. std::string opts = sf->GetProperty("AUTOUIC_OPTIONS");
  409. cmSystemTools::ReplaceString(opts, ";",
  410. cmQtAutoGeneratorCommon::listSep);
  411. uiFileOptions.push_back(opts);
  412. }
  413. }
  414. }
  415. }
  416. AddDefinitionEscaped(makefile, "_qt_uic_options_files", uiFileFiles);
  417. AddDefinitionEscaped(makefile, "_qt_uic_options_options", uiFileOptions);
  418. }
  419. // Uic executable
  420. {
  421. std::string err;
  422. const char* uicExec = CM_NULLPTR;
  423. if (qtMajorVersion == "5") {
  424. cmGeneratorTarget* qt5Uic = lg->FindGeneratorTargetToUse("Qt5::uic");
  425. if (qt5Uic != CM_NULLPTR) {
  426. uicExec = qt5Uic->ImportedGetLocation("");
  427. } else {
  428. // Project does not use Qt5Widgets, but has AUTOUIC ON anyway
  429. }
  430. } else if (qtMajorVersion == "4") {
  431. cmGeneratorTarget* qt4Uic = lg->FindGeneratorTargetToUse("Qt4::uic");
  432. if (qt4Uic != CM_NULLPTR) {
  433. uicExec = qt4Uic->ImportedGetLocation("");
  434. } else {
  435. err = "Qt4::uic target not found " + target->GetName();
  436. }
  437. } else {
  438. err = "The CMAKE_AUTOUIC feature supports only Qt 4 and Qt 5 ";
  439. err += target->GetName();
  440. }
  441. // Add definition or error
  442. if (err.empty()) {
  443. AddDefinitionEscaped(makefile, "_qt_uic_executable",
  444. uicExec ? uicExec : "");
  445. } else {
  446. cmSystemTools::Error(err.c_str());
  447. }
  448. }
  449. }
  450. static std::string RccGetExecutable(cmGeneratorTarget const* target,
  451. const std::string& qtMajorVersion)
  452. {
  453. std::string rccExec;
  454. cmLocalGenerator* lg = target->GetLocalGenerator();
  455. if (qtMajorVersion == "5") {
  456. cmGeneratorTarget* qt5Rcc = lg->FindGeneratorTargetToUse("Qt5::rcc");
  457. if (qt5Rcc != CM_NULLPTR) {
  458. rccExec = qt5Rcc->ImportedGetLocation("");
  459. } else {
  460. cmSystemTools::Error("Qt5::rcc target not found ",
  461. target->GetName().c_str());
  462. }
  463. } else if (qtMajorVersion == "4") {
  464. cmGeneratorTarget* qt4Rcc = lg->FindGeneratorTargetToUse("Qt4::rcc");
  465. if (qt4Rcc != CM_NULLPTR) {
  466. rccExec = qt4Rcc->ImportedGetLocation("");
  467. } else {
  468. cmSystemTools::Error("Qt4::rcc target not found ",
  469. target->GetName().c_str());
  470. }
  471. } else {
  472. cmSystemTools::Error(
  473. "The CMAKE_AUTORCC feature supports only Qt 4 and Qt 5 ",
  474. target->GetName().c_str());
  475. }
  476. return rccExec;
  477. }
  478. static void RccMergeOptions(std::vector<std::string>& opts,
  479. const std::vector<std::string>& fileOpts,
  480. bool isQt5)
  481. {
  482. static const char* valueOptions[] = { "name", "root", "compress",
  483. "threshold" };
  484. std::vector<std::string> extraOpts;
  485. for (std::vector<std::string>::const_iterator fit = fileOpts.begin();
  486. fit != fileOpts.end(); ++fit) {
  487. std::vector<std::string>::iterator existingIt =
  488. std::find(opts.begin(), opts.end(), *fit);
  489. if (existingIt != opts.end()) {
  490. const char* optName = fit->c_str();
  491. if (*optName == '-') {
  492. ++optName;
  493. if (isQt5 && *optName == '-') {
  494. ++optName;
  495. }
  496. }
  497. // Test if this is a value option and change the existing value
  498. if ((optName != fit->c_str()) &&
  499. std::find_if(cmArrayBegin(valueOptions), cmArrayEnd(valueOptions),
  500. cmStrCmp(optName)) != cmArrayEnd(valueOptions)) {
  501. const std::vector<std::string>::iterator existValueIt(existingIt + 1);
  502. const std::vector<std::string>::const_iterator fileValueIt(fit + 1);
  503. if ((existValueIt != opts.end()) && (fileValueIt != fileOpts.end())) {
  504. *existValueIt = *fileValueIt;
  505. ++fit;
  506. }
  507. }
  508. } else {
  509. extraOpts.push_back(*fit);
  510. }
  511. }
  512. opts.insert(opts.end(), extraOpts.begin(), extraOpts.end());
  513. }
  514. static void RccSetupAutoTarget(cmGeneratorTarget const* target,
  515. const std::string& qtMajorVersion)
  516. {
  517. cmMakefile* makefile = target->Target->GetMakefile();
  518. const bool qtMajorVersion5 = (qtMajorVersion == "5");
  519. const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
  520. std::vector<std::string> _rcc_files;
  521. std::vector<std::string> _rcc_inputs;
  522. std::vector<std::string> rccFileFiles;
  523. std::vector<std::string> rccFileOptions;
  524. std::vector<std::string> rccOptionsTarget;
  525. if (const char* opts = target->GetProperty("AUTORCC_OPTIONS")) {
  526. cmSystemTools::ExpandListArgument(opts, rccOptionsTarget);
  527. }
  528. std::vector<cmSourceFile*> srcFiles;
  529. target->GetConfigCommonSourceFiles(srcFiles);
  530. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  531. fileIt != srcFiles.end(); ++fileIt) {
  532. cmSourceFile* sf = *fileIt;
  533. if ((sf->GetExtension() == "qrc") &&
  534. !PropertyEnabled(sf, "SKIP_AUTOGEN") &&
  535. !PropertyEnabled(sf, "SKIP_AUTORCC")) {
  536. const std::string absFile =
  537. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  538. // qrc file
  539. _rcc_files.push_back(absFile);
  540. // qrc file entries
  541. {
  542. std::string entriesList = "{";
  543. // Read input file list only for non generated .qrc files.
  544. if (!PropertyEnabled(sf, "GENERATED")) {
  545. std::string error;
  546. std::vector<std::string> files;
  547. if (cmQtAutoGeneratorCommon::RccListInputs(
  548. qtMajorVersion, rccCommand, absFile, files, &error)) {
  549. entriesList += cmJoin(files, cmQtAutoGeneratorCommon::listSep);
  550. } else {
  551. cmSystemTools::Error(error.c_str());
  552. }
  553. }
  554. entriesList += "}";
  555. _rcc_inputs.push_back(entriesList);
  556. }
  557. // rcc options for this qrc file
  558. {
  559. // Merged target and file options
  560. std::vector<std::string> rccOptions(rccOptionsTarget);
  561. if (const char* prop = sf->GetProperty("AUTORCC_OPTIONS")) {
  562. std::vector<std::string> optsVec;
  563. cmSystemTools::ExpandListArgument(prop, optsVec);
  564. RccMergeOptions(rccOptions, optsVec, qtMajorVersion5);
  565. }
  566. // Only store non empty options lists
  567. if (!rccOptions.empty()) {
  568. rccFileFiles.push_back(absFile);
  569. rccFileOptions.push_back(
  570. cmJoin(rccOptions, cmQtAutoGeneratorCommon::listSep));
  571. }
  572. }
  573. }
  574. }
  575. AddDefinitionEscaped(makefile, "_qt_rcc_executable", rccCommand);
  576. AddDefinitionEscaped(makefile, "_rcc_files", _rcc_files);
  577. AddDefinitionEscaped(makefile, "_rcc_inputs", _rcc_inputs);
  578. AddDefinitionEscaped(makefile, "_rcc_options_files", rccFileFiles);
  579. AddDefinitionEscaped(makefile, "_rcc_options_options", rccFileOptions);
  580. }
  581. void cmQtAutoGeneratorInitializer::InitializeAutogenSources(
  582. cmGeneratorTarget* target)
  583. {
  584. if (target->GetPropertyAsBool("AUTOMOC")) {
  585. cmMakefile* makefile = target->Target->GetMakefile();
  586. std::string mocCppFile = GetAutogenTargetBuildDir(target);
  587. mocCppFile += "moc_compilation.cpp";
  588. {
  589. cmSourceFile* gFile = makefile->GetOrCreateSource(mocCppFile, true);
  590. gFile->SetProperty("SKIP_AUTOGEN", "On");
  591. }
  592. target->AddSource(mocCppFile);
  593. AddToSourceGroup(makefile, mocCppFile, cmQtAutoGeneratorCommon::MOC);
  594. }
  595. }
  596. void cmQtAutoGeneratorInitializer::InitializeAutogenTarget(
  597. cmLocalGenerator* lg, cmGeneratorTarget* target)
  598. {
  599. cmMakefile* makefile = target->Target->GetMakefile();
  600. // Create a custom target for running generators at buildtime
  601. const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
  602. const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
  603. const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
  604. const std::string autogenTargetName = GetAutogenTargetName(target);
  605. const std::string autogenBuildDir = GetAutogenTargetBuildDir(target);
  606. const std::string workingDirectory =
  607. cmSystemTools::CollapseFullPath("", makefile->GetCurrentBinaryDirectory());
  608. const std::string qtMajorVersion = GetQtMajorVersion(target);
  609. const std::string rccCommand = RccGetExecutable(target, qtMajorVersion);
  610. std::vector<std::string> autogenDepends;
  611. std::vector<std::string> autogenProvides;
  612. // Remove old settings on cleanup
  613. {
  614. std::string fname = GetAutogenTargetFilesDir(target);
  615. fname += "/AutogenOldSettings.cmake";
  616. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES", fname.c_str(),
  617. false);
  618. }
  619. // Compose command lines
  620. cmCustomCommandLines commandLines;
  621. {
  622. cmCustomCommandLine currentLine;
  623. currentLine.push_back(cmSystemTools::GetCMakeCommand());
  624. currentLine.push_back("-E");
  625. currentLine.push_back("cmake_autogen");
  626. currentLine.push_back(GetAutogenTargetFilesDir(target));
  627. currentLine.push_back("$<CONFIGURATION>");
  628. commandLines.push_back(currentLine);
  629. }
  630. // Compose target comment
  631. std::string autogenComment;
  632. {
  633. std::vector<std::string> toolNames;
  634. if (mocEnabled) {
  635. toolNames.push_back("MOC");
  636. }
  637. if (uicEnabled) {
  638. toolNames.push_back("UIC");
  639. }
  640. if (rccEnabled) {
  641. toolNames.push_back("RCC");
  642. }
  643. std::string tools = toolNames[0];
  644. toolNames.erase(toolNames.begin());
  645. while (toolNames.size() > 1) {
  646. tools += ", " + toolNames[0];
  647. toolNames.erase(toolNames.begin());
  648. }
  649. if (toolNames.size() == 1) {
  650. tools += " and " + toolNames[0];
  651. }
  652. autogenComment = "Automatic " + tools + " for target " + target->GetName();
  653. }
  654. // Create autogen target build directory and add it to the clean files
  655. cmSystemTools::MakeDirectory(autogenBuildDir);
  656. makefile->AppendProperty("ADDITIONAL_MAKE_CLEAN_FILES",
  657. autogenBuildDir.c_str(), false);
  658. // Create autogen target includes directory and
  659. // add it to the origin target INCLUDE_DIRECTORIES
  660. if (mocEnabled || uicEnabled) {
  661. const std::string incsDir = autogenBuildDir + "include";
  662. cmSystemTools::MakeDirectory(incsDir);
  663. target->AddIncludeDirectory(incsDir, true);
  664. }
  665. // Register moc compilation file as generated
  666. if (mocEnabled) {
  667. autogenProvides.push_back(autogenBuildDir + "moc_compilation.cpp");
  668. }
  669. #if defined(_WIN32) && !defined(__CYGWIN__)
  670. bool usePRE_BUILD = false;
  671. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  672. if (gg->GetName().find("Visual Studio") != std::string::npos) {
  673. cmGlobalVisualStudioGenerator* vsgg =
  674. static_cast<cmGlobalVisualStudioGenerator*>(gg);
  675. // Under VS >= 7 use a PRE_BUILD event instead of a separate target to
  676. // reduce the number of targets loaded into the IDE.
  677. // This also works around a VS 11 bug that may skip updating the target:
  678. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  679. usePRE_BUILD = vsgg->GetVersion() >= cmGlobalVisualStudioGenerator::VS7;
  680. }
  681. #endif
  682. // Initialize autogen target dependencies
  683. if (const char* deps = target->GetProperty("AUTOGEN_TARGET_DEPENDS")) {
  684. cmSystemTools::ExpandListArgument(deps, autogenDepends);
  685. }
  686. // Add link library targets to the autogen dependencies
  687. {
  688. const cmTarget::LinkLibraryVectorType& libVec =
  689. target->Target->GetOriginalLinkLibraries();
  690. for (cmTarget::LinkLibraryVectorType::const_iterator it = libVec.begin();
  691. it != libVec.end(); ++it) {
  692. const std::string& libName = it->first;
  693. if (makefile->FindTargetToUse(libName) != CM_NULLPTR) {
  694. autogenDepends.push_back(libName);
  695. }
  696. }
  697. }
  698. {
  699. cmFilePathChecksum fpathCheckSum(makefile);
  700. // Iterate over all source files
  701. std::vector<cmSourceFile*> srcFiles;
  702. target->GetConfigCommonSourceFiles(srcFiles);
  703. for (std::vector<cmSourceFile*>::const_iterator fileIt = srcFiles.begin();
  704. fileIt != srcFiles.end(); ++fileIt) {
  705. cmSourceFile* sf = *fileIt;
  706. if (!PropertyEnabled(sf, "SKIP_AUTOGEN")) {
  707. const std::string ext = sf->GetExtension();
  708. // Add generated file that will be scanned by moc or uic to
  709. // the dependencies
  710. if (mocEnabled || uicEnabled) {
  711. const cmSystemTools::FileFormat fileType =
  712. cmSystemTools::GetFileFormat(ext.c_str());
  713. if ((fileType == cmSystemTools::CXX_FILE_FORMAT) ||
  714. (fileType == cmSystemTools::HEADER_FILE_FORMAT)) {
  715. if (PropertyEnabled(sf, "GENERATED")) {
  716. if ((mocEnabled && !PropertyEnabled(sf, "SKIP_AUTOMOC")) ||
  717. (uicEnabled && !PropertyEnabled(sf, "SKIP_AUTOUIC"))) {
  718. autogenDepends.push_back(
  719. cmsys::SystemTools::GetRealPath(sf->GetFullPath()));
  720. #if defined(_WIN32) && !defined(__CYGWIN__)
  721. // Cannot use PRE_BUILD with generated files
  722. usePRE_BUILD = false;
  723. #endif
  724. }
  725. }
  726. }
  727. }
  728. // Process rcc enabled files
  729. if (rccEnabled && (ext == "qrc") &&
  730. !PropertyEnabled(sf, "SKIP_AUTORCC")) {
  731. const std::string absFile =
  732. cmsys::SystemTools::GetRealPath(sf->GetFullPath());
  733. // Compose rcc output file name
  734. {
  735. std::string rccOut = autogenBuildDir;
  736. rccOut += fpathCheckSum.getPart(absFile);
  737. rccOut += "/qrc_";
  738. rccOut +=
  739. cmsys::SystemTools::GetFilenameWithoutLastExtension(absFile);
  740. rccOut += ".cpp";
  741. // Register rcc output file as generated
  742. autogenProvides.push_back(rccOut);
  743. // Add rcc output file to origin target sources
  744. {
  745. cmSourceFile* gFile = makefile->GetOrCreateSource(rccOut, true);
  746. gFile->SetProperty("SKIP_AUTOGEN", "On");
  747. }
  748. target->AddSource(rccOut);
  749. AddToSourceGroup(makefile, rccOut, cmQtAutoGeneratorCommon::RCC);
  750. }
  751. if (PropertyEnabled(sf, "GENERATED")) {
  752. // Add generated qrc file to the dependencies
  753. autogenDepends.push_back(absFile);
  754. } else {
  755. // Run cmake again when .qrc file changes
  756. makefile->AddCMakeDependFile(absFile);
  757. // Add the qrc input files to the dependencies
  758. std::string error;
  759. if (!cmQtAutoGeneratorCommon::RccListInputs(
  760. qtMajorVersion, rccCommand, absFile, autogenDepends,
  761. &error)) {
  762. cmSystemTools::Error(error.c_str());
  763. }
  764. }
  765. #if defined(_WIN32) && !defined(__CYGWIN__)
  766. // Cannot use PRE_BUILD because the resource files themselves
  767. // may not be sources within the target so VS may not know the
  768. // target needs to re-build at all.
  769. usePRE_BUILD = false;
  770. #endif
  771. }
  772. }
  773. }
  774. }
  775. #if defined(_WIN32) && !defined(__CYGWIN__)
  776. if (usePRE_BUILD) {
  777. // If the autogen target depends on an other target don't use PRE_BUILD
  778. for (std::vector<std::string>::iterator it = autogenDepends.begin();
  779. it != autogenDepends.end(); ++it) {
  780. if (makefile->FindTargetToUse(*it) != CM_NULLPTR) {
  781. usePRE_BUILD = false;
  782. break;
  783. }
  784. }
  785. }
  786. if (usePRE_BUILD) {
  787. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  788. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  789. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  790. std::vector<std::string> no_output;
  791. cmCustomCommand cc(makefile, no_output, autogenProvides, autogenDepends,
  792. commandLines, autogenComment.c_str(),
  793. workingDirectory.c_str());
  794. cc.SetEscapeOldStyle(false);
  795. cc.SetEscapeAllowMakeVars(true);
  796. target->Target->AddPreBuildCommand(cc);
  797. } else
  798. #endif
  799. {
  800. cmTarget* autogenTarget = makefile->AddUtilityCommand(
  801. autogenTargetName, true, workingDirectory.c_str(),
  802. /*byproducts=*/autogenProvides, autogenDepends, commandLines, false,
  803. autogenComment.c_str());
  804. cmGeneratorTarget* gt = new cmGeneratorTarget(autogenTarget, lg);
  805. lg->AddGeneratorTarget(gt);
  806. // Set target folder
  807. const char* autogenFolder =
  808. makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  809. if (!autogenFolder) {
  810. autogenFolder =
  811. makefile->GetState()->GetGlobalProperty("AUTOGEN_TARGETS_FOLDER");
  812. }
  813. if (autogenFolder && *autogenFolder) {
  814. autogenTarget->SetProperty("FOLDER", autogenFolder);
  815. } else {
  816. // inherit FOLDER property from target (#13688)
  817. utilCopyTargetProperty(gt->Target, target->Target, "FOLDER");
  818. }
  819. target->Target->AddUtility(autogenTargetName);
  820. }
  821. }
  822. void cmQtAutoGeneratorInitializer::SetupAutoGenerateTarget(
  823. cmGeneratorTarget const* target)
  824. {
  825. cmMakefile* makefile = target->Target->GetMakefile();
  826. // forget the variables added here afterwards again:
  827. cmMakefile::ScopePushPop varScope(makefile);
  828. static_cast<void>(varScope);
  829. std::map<std::string, std::string> configMocIncludes;
  830. std::map<std::string, std::string> configMocDefines;
  831. std::map<std::string, std::string> configUicOptions;
  832. {
  833. const bool mocEnabled = target->GetPropertyAsBool("AUTOMOC");
  834. const bool uicEnabled = target->GetPropertyAsBool("AUTOUIC");
  835. const bool rccEnabled = target->GetPropertyAsBool("AUTORCC");
  836. const std::string autogenTargetName = GetAutogenTargetName(target);
  837. const std::string qtMajorVersion = GetQtMajorVersion(target);
  838. std::vector<std::string> _sources;
  839. std::vector<std::string> _headers;
  840. if (mocEnabled || uicEnabled || rccEnabled) {
  841. std::vector<std::string> mocSkipList;
  842. std::vector<std::string> uicSkipList;
  843. AcquireScanFiles(target, _sources, _headers, mocSkipList, uicSkipList);
  844. if (mocEnabled) {
  845. MocSetupAutoTarget(target, autogenTargetName, qtMajorVersion,
  846. mocSkipList, configMocIncludes, configMocDefines);
  847. }
  848. if (uicEnabled) {
  849. UicSetupAutoTarget(target, qtMajorVersion, uicSkipList,
  850. configUicOptions);
  851. }
  852. if (rccEnabled) {
  853. RccSetupAutoTarget(target, qtMajorVersion);
  854. }
  855. }
  856. AddDefinitionEscaped(makefile, "_autogen_target_name", autogenTargetName);
  857. AddDefinitionEscaped(makefile, "_origin_target_name", target->GetName());
  858. AddDefinitionEscaped(makefile, "_qt_version_major", qtMajorVersion);
  859. AddDefinitionEscaped(makefile, "_sources", _sources);
  860. AddDefinitionEscaped(makefile, "_headers", _headers);
  861. }
  862. // Generate config file
  863. std::string inputFile = cmSystemTools::GetCMakeRoot();
  864. inputFile += "/Modules/AutogenInfo.cmake.in";
  865. std::string outputFile = GetAutogenTargetFilesDir(target);
  866. outputFile += "/AutogenInfo.cmake";
  867. makefile->ConfigureFile(inputFile.c_str(), outputFile.c_str(), false, true,
  868. false);
  869. // Append custom config definitions to info file
  870. if (!configMocDefines.empty() || !configMocIncludes.empty() ||
  871. !configUicOptions.empty()) {
  872. // Ensure we have write permission in case .in was read-only.
  873. mode_t perm = 0;
  874. #if defined(_WIN32) && !defined(__CYGWIN__)
  875. mode_t mode_write = S_IWRITE;
  876. #else
  877. mode_t mode_write = S_IWUSR;
  878. #endif
  879. cmSystemTools::GetPermissions(outputFile, perm);
  880. if (!(perm & mode_write)) {
  881. cmSystemTools::SetPermissions(outputFile, perm | mode_write);
  882. }
  883. cmsys::ofstream infoFile(outputFile.c_str(), std::ios::app);
  884. if (!infoFile) {
  885. std::string error = "Internal CMake error when trying to open file: ";
  886. error += outputFile;
  887. error += " for writing.";
  888. cmSystemTools::Error(error.c_str());
  889. } else {
  890. infoFile << "# Configuration specific options\n";
  891. if (!configMocDefines.empty()) {
  892. for (std::map<std::string, std::string>::iterator
  893. it = configMocDefines.begin(),
  894. end = configMocDefines.end();
  895. it != end; ++it) {
  896. infoFile << "set(AM_MOC_DEFINITIONS_" << it->first << " "
  897. << it->second << ")\n";
  898. }
  899. }
  900. if (!configMocIncludes.empty()) {
  901. for (std::map<std::string, std::string>::iterator
  902. it = configMocIncludes.begin(),
  903. end = configMocIncludes.end();
  904. it != end; ++it) {
  905. infoFile << "set(AM_MOC_INCLUDES_" << it->first << " " << it->second
  906. << ")\n";
  907. }
  908. }
  909. if (!configUicOptions.empty()) {
  910. for (std::map<std::string, std::string>::iterator
  911. it = configUicOptions.begin(),
  912. end = configUicOptions.end();
  913. it != end; ++it) {
  914. infoFile << "set(AM_UIC_TARGET_OPTIONS_" << it->first << " "
  915. << it->second << ")\n";
  916. }
  917. }
  918. }
  919. }
  920. }