cmQtAutoGeneratorInitializer.cxx 35 KB

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