cmQtAutoGeneratorInitializer.cxx 38 KB

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