cmQtAutoGeneratorInitializer.cxx 38 KB

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