cmQtAutoGenInitializer.cxx 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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 "cmQtAutoGenInitializer.h"
  4. #include <cstddef>
  5. #include <deque>
  6. #include <initializer_list>
  7. #include <map>
  8. #include <ostream>
  9. #include <set>
  10. #include <string>
  11. #include <unordered_set>
  12. #include <utility>
  13. #include <vector>
  14. #include <cm/algorithm>
  15. #include <cm/iterator>
  16. #include <cm/memory>
  17. #include "cmsys/SystemInformation.hxx"
  18. #include "cm_jsoncpp_value.h"
  19. #include "cm_jsoncpp_writer.h"
  20. #include "cmAlgorithms.h"
  21. #include "cmCustomCommand.h"
  22. #include "cmCustomCommandLines.h"
  23. #include "cmGeneratedFileStream.h"
  24. #include "cmGeneratorExpression.h"
  25. #include "cmGeneratorTarget.h"
  26. #include "cmGlobalGenerator.h"
  27. #include "cmLinkItem.h"
  28. #include "cmListFileCache.h"
  29. #include "cmLocalGenerator.h"
  30. #include "cmMakefile.h"
  31. #include "cmMessageType.h"
  32. #include "cmPolicies.h"
  33. #include "cmQtAutoGen.h"
  34. #include "cmQtAutoGenGlobalInitializer.h"
  35. #include "cmSourceFile.h"
  36. #include "cmSourceFileLocationKind.h"
  37. #include "cmSourceGroup.h"
  38. #include "cmState.h"
  39. #include "cmStateTypes.h"
  40. #include "cmString.hxx"
  41. #include "cmStringAlgorithms.h"
  42. #include "cmSystemTools.h"
  43. #include "cmTarget.h"
  44. #include "cmake.h"
  45. namespace {
  46. unsigned int GetParallelCPUCount()
  47. {
  48. static unsigned int count = 0;
  49. // Detect only on the first call
  50. if (count == 0) {
  51. cmsys::SystemInformation info;
  52. info.RunCPUCheck();
  53. count =
  54. cm::clamp(info.GetNumberOfPhysicalCPU(), 1u, cmQtAutoGen::ParallelMax);
  55. }
  56. return count;
  57. }
  58. std::string FileProjectRelativePath(cmMakefile* makefile,
  59. std::string const& fileName)
  60. {
  61. std::string res;
  62. {
  63. std::string pSource = cmSystemTools::RelativePath(
  64. makefile->GetCurrentSourceDirectory(), fileName);
  65. std::string pBinary = cmSystemTools::RelativePath(
  66. makefile->GetCurrentBinaryDirectory(), fileName);
  67. if (pSource.size() < pBinary.size()) {
  68. res = std::move(pSource);
  69. } else if (pBinary.size() < fileName.size()) {
  70. res = std::move(pBinary);
  71. } else {
  72. res = fileName;
  73. }
  74. }
  75. return res;
  76. }
  77. /**
  78. * Tests if targetDepend is a STATIC_LIBRARY and if any of its
  79. * recursive STATIC_LIBRARY dependencies depends on targetOrigin
  80. * (STATIC_LIBRARY cycle).
  81. */
  82. bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
  83. cmGeneratorTarget const* targetDepend,
  84. std::string const& config)
  85. {
  86. bool cycle = false;
  87. if ((targetOrigin->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  88. (targetDepend->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  89. std::set<cmGeneratorTarget const*> knownLibs;
  90. std::deque<cmGeneratorTarget const*> testLibs;
  91. // Insert initial static_library dependency
  92. knownLibs.insert(targetDepend);
  93. testLibs.push_back(targetDepend);
  94. while (!testLibs.empty()) {
  95. cmGeneratorTarget const* testTarget = testLibs.front();
  96. testLibs.pop_front();
  97. // Check if the test target is the origin target (cycle)
  98. if (testTarget == targetOrigin) {
  99. cycle = true;
  100. break;
  101. }
  102. // Collect all static_library dependencies from the test target
  103. cmLinkImplementationLibraries const* libs =
  104. testTarget->GetLinkImplementationLibraries(config);
  105. if (libs != nullptr) {
  106. for (cmLinkItem const& item : libs->Libraries) {
  107. cmGeneratorTarget const* depTarget = item.Target;
  108. if ((depTarget != nullptr) &&
  109. (depTarget->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  110. knownLibs.insert(depTarget).second) {
  111. testLibs.push_back(depTarget);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return cycle;
  118. }
  119. /** Sanitizes file search paths. */
  120. class SearchPathSanitizer
  121. {
  122. public:
  123. SearchPathSanitizer(cmMakefile* makefile)
  124. : SourcePath_(makefile->GetCurrentSourceDirectory())
  125. {
  126. }
  127. std::vector<std::string> operator()(
  128. std::vector<std::string> const& paths) const;
  129. private:
  130. std::string SourcePath_;
  131. };
  132. std::vector<std::string> SearchPathSanitizer::operator()(
  133. std::vector<std::string> const& paths) const
  134. {
  135. std::vector<std::string> res;
  136. res.reserve(paths.size());
  137. for (std::string const& srcPath : paths) {
  138. // Collapse relative paths
  139. std::string path = cmSystemTools::CollapseFullPath(srcPath, SourcePath_);
  140. // Remove suffix slashes
  141. while (cmHasSuffix(path, '/')) {
  142. path.pop_back();
  143. }
  144. // Accept only non empty paths
  145. if (!path.empty()) {
  146. res.emplace_back(std::move(path));
  147. }
  148. }
  149. return res;
  150. }
  151. /** @brief Writes a CMake info file. */
  152. class InfoWriter
  153. {
  154. public:
  155. // -- Single value
  156. void Set(std::string const& key, std::string const& value)
  157. {
  158. Value_[key] = value;
  159. }
  160. void SetConfig(std::string const& key,
  161. cmQtAutoGenInitializer::ConfigString const& cfgStr);
  162. void SetBool(std::string const& key, bool value) { Value_[key] = value; }
  163. void SetUInt(std::string const& key, unsigned int value)
  164. {
  165. Value_[key] = value;
  166. }
  167. // -- Array utility
  168. template <typename CONT>
  169. static bool MakeArray(Json::Value& jval, CONT const& container);
  170. template <typename CONT>
  171. static void MakeStringArray(Json::Value& jval, CONT const& container);
  172. // -- Array value
  173. template <typename CONT>
  174. void SetArray(std::string const& key, CONT const& container);
  175. template <typename CONT>
  176. void SetConfigArray(
  177. std::string const& key,
  178. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr);
  179. // -- Array of arrays
  180. template <typename CONT, typename FUNC>
  181. void SetArrayArray(std::string const& key, CONT const& container, FUNC func);
  182. // -- Save to json file
  183. bool Save(std::string const& filename);
  184. private:
  185. Json::Value Value_;
  186. };
  187. void InfoWriter::SetConfig(std::string const& key,
  188. cmQtAutoGenInitializer::ConfigString const& cfgStr)
  189. {
  190. Set(key, cfgStr.Default);
  191. for (auto const& item : cfgStr.Config) {
  192. Set(cmStrCat(key, '_', item.first), item.second);
  193. }
  194. }
  195. template <typename CONT>
  196. bool InfoWriter::MakeArray(Json::Value& jval, CONT const& container)
  197. {
  198. jval = Json::arrayValue;
  199. std::size_t const listSize = cm::size(container);
  200. if (listSize == 0) {
  201. return false;
  202. }
  203. jval.resize(static_cast<unsigned int>(listSize));
  204. return true;
  205. }
  206. template <typename CONT>
  207. void InfoWriter::MakeStringArray(Json::Value& jval, CONT const& container)
  208. {
  209. if (MakeArray(jval, container)) {
  210. Json::ArrayIndex ii = 0;
  211. for (std::string const& item : container) {
  212. jval[ii++] = item;
  213. }
  214. }
  215. }
  216. template <typename CONT>
  217. void InfoWriter::SetArray(std::string const& key, CONT const& container)
  218. {
  219. MakeStringArray(Value_[key], container);
  220. }
  221. template <typename CONT, typename FUNC>
  222. void InfoWriter::SetArrayArray(std::string const& key, CONT const& container,
  223. FUNC func)
  224. {
  225. Json::Value& jval = Value_[key];
  226. if (MakeArray(jval, container)) {
  227. Json::ArrayIndex ii = 0;
  228. for (auto const& citem : container) {
  229. Json::Value& aval = jval[ii++];
  230. aval = Json::arrayValue;
  231. func(aval, citem);
  232. }
  233. }
  234. }
  235. template <typename CONT>
  236. void InfoWriter::SetConfigArray(
  237. std::string const& key,
  238. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr)
  239. {
  240. SetArray(key, cfgStr.Default);
  241. for (auto const& item : cfgStr.Config) {
  242. SetArray(cmStrCat(key, '_', item.first), item.second);
  243. }
  244. }
  245. bool InfoWriter::Save(std::string const& filename)
  246. {
  247. cmGeneratedFileStream fileStream;
  248. fileStream.SetCopyIfDifferent(true);
  249. fileStream.Open(filename, false, true);
  250. if (!fileStream) {
  251. return false;
  252. }
  253. Json::StyledStreamWriter jsonWriter;
  254. try {
  255. jsonWriter.write(fileStream, Value_);
  256. } catch (...) {
  257. return false;
  258. }
  259. return fileStream.Close();
  260. }
  261. } // End of unnamed namespace
  262. cmQtAutoGenInitializer::cmQtAutoGenInitializer(
  263. cmQtAutoGenGlobalInitializer* globalInitializer,
  264. cmGeneratorTarget* genTarget, IntegerVersion const& qtVersion,
  265. bool mocEnabled, bool uicEnabled, bool rccEnabled, bool globalAutogenTarget,
  266. bool globalAutoRccTarget)
  267. : GlobalInitializer(globalInitializer)
  268. , GenTarget(genTarget)
  269. , GlobalGen(genTarget->GetGlobalGenerator())
  270. , LocalGen(genTarget->GetLocalGenerator())
  271. , Makefile(genTarget->Makefile)
  272. , PathCheckSum(genTarget->Makefile)
  273. , QtVersion(qtVersion)
  274. {
  275. AutogenTarget.GlobalTarget = globalAutogenTarget;
  276. Moc.Enabled = mocEnabled;
  277. Uic.Enabled = uicEnabled;
  278. Rcc.Enabled = rccEnabled;
  279. Rcc.GlobalTarget = globalAutoRccTarget;
  280. }
  281. bool cmQtAutoGenInitializer::InitCustomTargets()
  282. {
  283. // Configurations
  284. this->MultiConfig = this->GlobalGen->IsMultiConfig();
  285. this->ConfigDefault = this->Makefile->GetConfigurations(this->ConfigsList);
  286. if (this->ConfigsList.empty()) {
  287. this->ConfigsList.push_back(this->ConfigDefault);
  288. }
  289. // Verbosity
  290. {
  291. std::string def =
  292. this->Makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
  293. if (!def.empty()) {
  294. unsigned long iVerb = 0;
  295. if (cmStrToULong(def, &iVerb)) {
  296. // Numeric verbosity
  297. this->Verbosity = static_cast<unsigned int>(iVerb);
  298. } else {
  299. // Non numeric verbosity
  300. if (cmIsOn(def)) {
  301. this->Verbosity = 1;
  302. }
  303. }
  304. }
  305. }
  306. // Targets FOLDER
  307. {
  308. const char* folder =
  309. this->Makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  310. if (folder == nullptr) {
  311. folder = this->Makefile->GetState()->GetGlobalProperty(
  312. "AUTOGEN_TARGETS_FOLDER");
  313. }
  314. // Inherit FOLDER property from target (#13688)
  315. if (folder == nullptr) {
  316. folder = this->GenTarget->GetProperty("FOLDER");
  317. }
  318. if (folder != nullptr) {
  319. this->TargetsFolder = folder;
  320. }
  321. }
  322. // Check status of policy CMP0071 regarding handling of GENERATED files
  323. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0071)) {
  324. case cmPolicies::WARN:
  325. // Ignore GENERATED files but warn
  326. this->CMP0071Warn = true;
  327. CM_FALLTHROUGH;
  328. case cmPolicies::OLD:
  329. // Ignore GENERATED files
  330. break;
  331. case cmPolicies::REQUIRED_IF_USED:
  332. case cmPolicies::REQUIRED_ALWAYS:
  333. case cmPolicies::NEW:
  334. // Process GENERATED files
  335. this->CMP0071Accept = true;
  336. break;
  337. }
  338. // Check status of policy CMP0100 regarding handling of .hh headers
  339. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0100)) {
  340. case cmPolicies::WARN:
  341. // Ignore but .hh files but warn
  342. this->CMP0100Warn = true;
  343. CM_FALLTHROUGH;
  344. case cmPolicies::OLD:
  345. // Ignore .hh files
  346. break;
  347. case cmPolicies::REQUIRED_IF_USED:
  348. case cmPolicies::REQUIRED_ALWAYS:
  349. case cmPolicies::NEW:
  350. // Process .hh file
  351. this->CMP0100Accept = true;
  352. break;
  353. }
  354. // Common directories
  355. {
  356. // Collapsed current binary directory
  357. std::string const cbd = cmSystemTools::CollapseFullPath(
  358. std::string(), this->Makefile->GetCurrentBinaryDirectory());
  359. // Info directory
  360. this->Dir.Info = cmStrCat(cbd, "/CMakeFiles/", this->GenTarget->GetName(),
  361. "_autogen.dir");
  362. cmSystemTools::ConvertToUnixSlashes(this->Dir.Info);
  363. // Build directory
  364. this->Dir.Build = this->GenTarget->GetSafeProperty("AUTOGEN_BUILD_DIR");
  365. if (this->Dir.Build.empty()) {
  366. this->Dir.Build =
  367. cmStrCat(cbd, '/', this->GenTarget->GetName(), "_autogen");
  368. }
  369. cmSystemTools::ConvertToUnixSlashes(this->Dir.Build);
  370. // Cleanup build directory
  371. this->AddCleanFile(this->Dir.Build);
  372. // Working directory
  373. this->Dir.Work = cbd;
  374. cmSystemTools::ConvertToUnixSlashes(this->Dir.Work);
  375. // Include directory
  376. ConfigFileNames(this->Dir.Include, cmStrCat(this->Dir.Build, "/include"),
  377. "");
  378. this->Dir.IncludeGenExp = this->Dir.Include.Default;
  379. if (this->MultiConfig) {
  380. this->Dir.IncludeGenExp += "_$<CONFIG>";
  381. }
  382. }
  383. // Moc, Uic and _autogen target settings
  384. if (this->MocOrUicEnabled()) {
  385. // Init moc specific settings
  386. if (this->Moc.Enabled && !InitMoc()) {
  387. return false;
  388. }
  389. // Init uic specific settings
  390. if (this->Uic.Enabled && !InitUic()) {
  391. return false;
  392. }
  393. // Autogen target name
  394. this->AutogenTarget.Name =
  395. cmStrCat(this->GenTarget->GetName(), "_autogen");
  396. // Autogen target parallel processing
  397. {
  398. std::string prop = this->GenTarget->GetSafeProperty("AUTOGEN_PARALLEL");
  399. if (prop.empty() || (prop == "AUTO")) {
  400. // Autodetect number of CPUs
  401. this->AutogenTarget.Parallel = GetParallelCPUCount();
  402. } else {
  403. this->AutogenTarget.Parallel = 1;
  404. }
  405. }
  406. // Autogen target info and settings files
  407. {
  408. // Info file
  409. this->AutogenTarget.InfoFile =
  410. cmStrCat(this->Dir.Info, "/AutogenInfo.json");
  411. // Used settings file
  412. ConfigFileNames(this->AutogenTarget.SettingsFile,
  413. cmStrCat(this->Dir.Info, "/AutogenUsed"), ".txt");
  414. ConfigFileClean(this->AutogenTarget.SettingsFile);
  415. // Parse cache file
  416. ConfigFileNames(this->AutogenTarget.ParseCacheFile,
  417. cmStrCat(this->Dir.Info, "/ParseCache"), ".txt");
  418. ConfigFileClean(this->AutogenTarget.ParseCacheFile);
  419. }
  420. // Autogen target: Compute user defined dependencies
  421. {
  422. this->AutogenTarget.DependOrigin =
  423. this->GenTarget->GetPropertyAsBool("AUTOGEN_ORIGIN_DEPENDS");
  424. std::string const deps =
  425. this->GenTarget->GetSafeProperty("AUTOGEN_TARGET_DEPENDS");
  426. if (!deps.empty()) {
  427. for (std::string const& depName : cmExpandedList(deps)) {
  428. // Allow target and file dependencies
  429. auto* depTarget = this->Makefile->FindTargetToUse(depName);
  430. if (depTarget != nullptr) {
  431. this->AutogenTarget.DependTargets.insert(depTarget);
  432. } else {
  433. this->AutogenTarget.DependFiles.insert(depName);
  434. }
  435. }
  436. }
  437. }
  438. if (this->Moc.Enabled) {
  439. // Path prefix
  440. if (cmIsOn(this->GenTarget->GetSafeProperty("AUTOMOC_PATH_PREFIX"))) {
  441. this->Moc.PathPrefix = true;
  442. }
  443. // CMAKE_AUTOMOC_RELAXED_MODE
  444. if (this->Makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE")) {
  445. this->Moc.RelaxedMode = true;
  446. this->Makefile->IssueMessage(
  447. MessageType::AUTHOR_WARNING,
  448. cmStrCat("AUTOMOC: CMAKE_AUTOMOC_RELAXED_MODE is "
  449. "deprecated an will be removed in the future. Consider "
  450. "disabling it and converting the target ",
  451. this->GenTarget->GetName(), " to regular mode."));
  452. }
  453. // Options
  454. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MOC_OPTIONS"),
  455. this->Moc.Options);
  456. // Filters
  457. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MACRO_NAMES"),
  458. this->Moc.MacroNames);
  459. {
  460. auto filterList = cmExpandedList(
  461. this->GenTarget->GetSafeProperty("AUTOMOC_DEPEND_FILTERS"));
  462. if ((filterList.size() % 2) != 0) {
  463. cmSystemTools::Error(
  464. cmStrCat("AutoMoc: AUTOMOC_DEPEND_FILTERS predefs size ",
  465. filterList.size(), " is not a multiple of 2."));
  466. return false;
  467. }
  468. this->Moc.DependFilters.reserve(1 + (filterList.size() / 2));
  469. this->Moc.DependFilters.emplace_back(
  470. "Q_PLUGIN_METADATA",
  471. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  472. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  473. for (std::size_t ii = 0; ii != filterList.size(); ii += 2) {
  474. this->Moc.DependFilters.emplace_back(filterList[ii],
  475. filterList[ii + 1]);
  476. }
  477. }
  478. }
  479. }
  480. // Init rcc specific settings
  481. if (this->Rcc.Enabled && !InitRcc()) {
  482. return false;
  483. }
  484. // Add autogen include directory to the origin target INCLUDE_DIRECTORIES
  485. if (this->MocOrUicEnabled() || (this->Rcc.Enabled && this->MultiConfig)) {
  486. this->GenTarget->AddIncludeDirectory(this->Dir.IncludeGenExp, true);
  487. }
  488. // Scan files
  489. if (!this->InitScanFiles()) {
  490. return false;
  491. }
  492. // Create autogen target
  493. if (this->MocOrUicEnabled() && !this->InitAutogenTarget()) {
  494. return false;
  495. }
  496. // Create rcc targets
  497. if (this->Rcc.Enabled && !this->InitRccTargets()) {
  498. return false;
  499. }
  500. return true;
  501. }
  502. bool cmQtAutoGenInitializer::InitMoc()
  503. {
  504. // Mocs compilation file
  505. this->Moc.CompilationFile =
  506. cmStrCat(this->Dir.Build, "/mocs_compilation.cpp");
  507. // Moc predefs
  508. if (this->GenTarget->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  509. (this->QtVersion >= IntegerVersion(5, 8))) {
  510. // Command
  511. cmExpandList(this->Makefile->GetSafeDefinition(
  512. "CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"),
  513. this->Moc.PredefsCmd);
  514. // Header
  515. if (!this->Moc.PredefsCmd.empty()) {
  516. ConfigFileNames(this->Moc.PredefsFile,
  517. cmStrCat(this->Dir.Build, "/moc_predefs"), ".h");
  518. }
  519. }
  520. // Moc includes
  521. {
  522. SearchPathSanitizer sanitizer(this->Makefile);
  523. auto getDirs =
  524. [this, &sanitizer](std::string const& cfg) -> std::vector<std::string> {
  525. // Get the include dirs for this target, without stripping the implicit
  526. // include dirs off, see issue #13667.
  527. std::vector<std::string> dirs;
  528. bool const appendImplicit = (this->QtVersion.Major >= 5);
  529. this->LocalGen->GetIncludeDirectoriesImplicit(
  530. dirs, this->GenTarget, "CXX", cfg, false, appendImplicit);
  531. return sanitizer(dirs);
  532. };
  533. // Default configuration include directories
  534. this->Moc.Includes.Default = getDirs(this->ConfigDefault);
  535. // Other configuration settings
  536. if (this->MultiConfig) {
  537. for (std::string const& cfg : this->ConfigsList) {
  538. std::vector<std::string> dirs = getDirs(cfg);
  539. if (dirs == this->Moc.Includes.Default) {
  540. continue;
  541. }
  542. this->Moc.Includes.Config[cfg] = std::move(dirs);
  543. }
  544. }
  545. }
  546. // Moc compile definitions
  547. {
  548. auto getDefs = [this](std::string const& cfg) -> std::set<std::string> {
  549. std::set<std::string> defines;
  550. this->LocalGen->GetTargetDefines(this->GenTarget, cfg, "CXX", defines);
  551. #ifdef _WIN32
  552. if (this->Moc.PredefsCmd.empty()) {
  553. // Add WIN32 definition if we don't have a moc_predefs.h
  554. defines.insert("WIN32");
  555. }
  556. #endif
  557. return defines;
  558. };
  559. // Default configuration defines
  560. this->Moc.Defines.Default = getDefs(this->ConfigDefault);
  561. // Other configuration defines
  562. if (this->MultiConfig) {
  563. for (std::string const& cfg : this->ConfigsList) {
  564. std::set<std::string> defines = getDefs(cfg);
  565. if (defines == this->Moc.Defines.Default) {
  566. continue;
  567. }
  568. this->Moc.Defines.Config[cfg] = std::move(defines);
  569. }
  570. }
  571. }
  572. // Moc executable
  573. {
  574. if (!this->GetQtExecutable(this->Moc, "moc", false)) {
  575. return false;
  576. }
  577. // Let the _autogen target depend on the moc executable
  578. if (this->Moc.ExecutableTarget != nullptr) {
  579. this->AutogenTarget.DependTargets.insert(
  580. this->Moc.ExecutableTarget->Target);
  581. }
  582. }
  583. return true;
  584. }
  585. bool cmQtAutoGenInitializer::InitUic()
  586. {
  587. // Uic search paths
  588. {
  589. std::string const usp =
  590. this->GenTarget->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  591. if (!usp.empty()) {
  592. this->Uic.SearchPaths =
  593. SearchPathSanitizer(this->Makefile)(cmExpandedList(usp));
  594. }
  595. }
  596. // Uic target options
  597. {
  598. auto getOpts = [this](std::string const& cfg) -> std::vector<std::string> {
  599. std::vector<std::string> opts;
  600. this->GenTarget->GetAutoUicOptions(opts, cfg);
  601. return opts;
  602. };
  603. // Default options
  604. this->Uic.Options.Default = getOpts(this->ConfigDefault);
  605. // Configuration specific options
  606. if (this->MultiConfig) {
  607. for (std::string const& cfg : this->ConfigsList) {
  608. std::vector<std::string> options = getOpts(cfg);
  609. if (options == this->Uic.Options.Default) {
  610. continue;
  611. }
  612. this->Uic.Options.Config[cfg] = std::move(options);
  613. }
  614. }
  615. }
  616. // Uic executable
  617. {
  618. if (!this->GetQtExecutable(this->Uic, "uic", true)) {
  619. return false;
  620. }
  621. // Let the _autogen target depend on the uic executable
  622. if (this->Uic.ExecutableTarget != nullptr) {
  623. this->AutogenTarget.DependTargets.insert(
  624. this->Uic.ExecutableTarget->Target);
  625. }
  626. }
  627. return true;
  628. }
  629. bool cmQtAutoGenInitializer::InitRcc()
  630. {
  631. // Rcc executable
  632. {
  633. if (!this->GetQtExecutable(this->Rcc, "rcc", false)) {
  634. return false;
  635. }
  636. // Evaluate test output on demand
  637. CompilerFeatures& features = *this->Rcc.ExecutableFeatures;
  638. if (!features.Evaluated) {
  639. // Look for list options
  640. if (this->QtVersion.Major == 5 || this->QtVersion.Major == 6) {
  641. if (features.HelpOutput.find("--list") != std::string::npos) {
  642. features.ListOptions.emplace_back("--list");
  643. } else if (features.HelpOutput.find("-list") != std::string::npos) {
  644. features.ListOptions.emplace_back("-list");
  645. }
  646. }
  647. // Evaluation finished
  648. features.Evaluated = true;
  649. }
  650. }
  651. return true;
  652. }
  653. bool cmQtAutoGenInitializer::InitScanFiles()
  654. {
  655. cmake const* cm = this->Makefile->GetCMakeInstance();
  656. auto const& kw = this->GlobalInitializer->kw();
  657. auto makeMUFile = [this, &kw](cmSourceFile* sf, std::string const& fullPath,
  658. bool muIt) -> MUFileHandle {
  659. MUFileHandle muf = cm::make_unique<MUFile>();
  660. muf->FullPath = fullPath;
  661. muf->SF = sf;
  662. muf->Generated = sf->GetIsGenerated();
  663. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  664. muf->SkipMoc = this->Moc.Enabled &&
  665. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOMOC));
  666. muf->SkipUic = this->Uic.Enabled &&
  667. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  668. if (muIt) {
  669. muf->MocIt = this->Moc.Enabled && !muf->SkipMoc;
  670. muf->UicIt = this->Uic.Enabled && !muf->SkipUic;
  671. }
  672. return muf;
  673. };
  674. auto addMUHeader = [this](MUFileHandle&& muf, cm::string_view extension) {
  675. cmSourceFile* sf = muf->SF;
  676. const bool muIt = (muf->MocIt || muf->UicIt);
  677. if (this->CMP0100Accept || (extension != "hh")) {
  678. // Accept
  679. if (muIt && muf->Generated) {
  680. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  681. }
  682. this->AutogenTarget.Headers.emplace(sf, std::move(muf));
  683. } else if (muIt && this->CMP0100Warn) {
  684. // Store file for warning message
  685. this->AutogenTarget.CMP0100HeadersWarn.push_back(sf);
  686. }
  687. };
  688. auto addMUSource = [this](MUFileHandle&& muf) {
  689. if ((muf->MocIt || muf->UicIt) && muf->Generated) {
  690. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  691. }
  692. this->AutogenTarget.Sources.emplace(muf->SF, std::move(muf));
  693. };
  694. // Scan through target files
  695. {
  696. // Scan through target files
  697. std::vector<cmSourceFile*> srcFiles;
  698. this->GenTarget->GetConfigCommonSourceFiles(srcFiles);
  699. for (cmSourceFile* sf : srcFiles) {
  700. // sf->GetExtension() is only valid after sf->ResolveFullPath() ...
  701. // Since we're iterating over source files that might be not in the
  702. // target we need to check for path errors (not existing files).
  703. std::string pathError;
  704. std::string const& fullPath = sf->ResolveFullPath(&pathError);
  705. if (!pathError.empty() || fullPath.empty()) {
  706. continue;
  707. }
  708. std::string const& extLower =
  709. cmSystemTools::LowerCase(sf->GetExtension());
  710. // Register files that will be scanned by moc or uic
  711. if (this->MocOrUicEnabled()) {
  712. if (cm->IsHeaderExtension(extLower)) {
  713. addMUHeader(makeMUFile(sf, fullPath, true), extLower);
  714. } else if (cm->IsSourceExtension(extLower)) {
  715. addMUSource(makeMUFile(sf, fullPath, true));
  716. }
  717. }
  718. // Register rcc enabled files
  719. if (this->Rcc.Enabled) {
  720. if ((extLower == kw.qrc) && !sf->GetPropertyAsBool(kw.SKIP_AUTOGEN) &&
  721. !sf->GetPropertyAsBool(kw.SKIP_AUTORCC)) {
  722. // Register qrc file
  723. Qrc qrc;
  724. qrc.QrcFile = fullPath;
  725. qrc.QrcName =
  726. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  727. qrc.Generated = sf->GetIsGenerated();
  728. // RCC options
  729. {
  730. std::string const opts = sf->GetSafeProperty(kw.AUTORCC_OPTIONS);
  731. if (!opts.empty()) {
  732. cmExpandList(opts, qrc.Options);
  733. }
  734. }
  735. this->Rcc.Qrcs.push_back(std::move(qrc));
  736. }
  737. }
  738. }
  739. }
  740. // cmGeneratorTarget::GetConfigCommonSourceFiles computes the target's
  741. // sources meta data cache. Clear it so that OBJECT library targets that
  742. // are AUTOGEN initialized after this target get their added
  743. // mocs_compilation.cpp source acknowledged by this target.
  744. this->GenTarget->ClearSourcesCache();
  745. // For source files find additional headers and private headers
  746. if (this->MocOrUicEnabled()) {
  747. // Header search suffixes and extensions
  748. static std::initializer_list<cm::string_view> const suffixes{ "", "_p" };
  749. auto const& exts = cm->GetHeaderExtensions();
  750. // Scan through sources
  751. for (auto const& pair : this->AutogenTarget.Sources) {
  752. MUFile const& muf = *pair.second;
  753. if (muf.MocIt || muf.UicIt) {
  754. // Search for the default header file and a private header
  755. std::string const& srcFullPath = muf.SF->ResolveFullPath();
  756. std::string basePath = cmStrCat(
  757. cmQtAutoGen::SubDirPrefix(srcFullPath),
  758. cmSystemTools::GetFilenameWithoutLastExtension(srcFullPath));
  759. for (auto const& suffix : suffixes) {
  760. std::string const suffixedPath = cmStrCat(basePath, suffix);
  761. for (auto const& ext : exts) {
  762. std::string fullPath = cmStrCat(suffixedPath, '.', ext);
  763. auto constexpr locationKind = cmSourceFileLocationKind::Known;
  764. cmSourceFile* sf =
  765. this->Makefile->GetSource(fullPath, locationKind);
  766. if (sf != nullptr) {
  767. // Check if we know about this header already
  768. if (cmContains(this->AutogenTarget.Headers, sf)) {
  769. continue;
  770. }
  771. // We only accept not-GENERATED files that do exist.
  772. if (!sf->GetIsGenerated() &&
  773. !cmSystemTools::FileExists(fullPath)) {
  774. continue;
  775. }
  776. } else if (cmSystemTools::FileExists(fullPath)) {
  777. // Create a new source file for the existing file
  778. sf = this->Makefile->CreateSource(fullPath, false, locationKind);
  779. }
  780. if (sf != nullptr) {
  781. auto eMuf = makeMUFile(sf, fullPath, true);
  782. // Ony process moc/uic when the parent is processed as well
  783. if (!muf.MocIt) {
  784. eMuf->MocIt = false;
  785. }
  786. if (!muf.UicIt) {
  787. eMuf->UicIt = false;
  788. }
  789. addMUHeader(std::move(eMuf), ext);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. // Scan through all source files in the makefile to extract moc and uic
  797. // parameters. Historically we support non target source file parameters.
  798. // The reason is that their file names might be discovered from source files
  799. // at generation time.
  800. if (this->MocOrUicEnabled()) {
  801. for (const auto& sf : this->Makefile->GetSourceFiles()) {
  802. // sf->GetExtension() is only valid after sf->ResolveFullPath() ...
  803. // Since we're iterating over source files that might be not in the
  804. // target we need to check for path errors (not existing files).
  805. std::string pathError;
  806. std::string const& fullPath = sf->ResolveFullPath(&pathError);
  807. if (!pathError.empty() || fullPath.empty()) {
  808. continue;
  809. }
  810. std::string const& extLower =
  811. cmSystemTools::LowerCase(sf->GetExtension());
  812. if (cm->IsHeaderExtension(extLower)) {
  813. if (!cmContains(this->AutogenTarget.Headers, sf.get())) {
  814. auto muf = makeMUFile(sf.get(), fullPath, false);
  815. if (muf->SkipMoc || muf->SkipUic) {
  816. addMUHeader(std::move(muf), extLower);
  817. }
  818. }
  819. } else if (cm->IsSourceExtension(extLower)) {
  820. if (!cmContains(this->AutogenTarget.Sources, sf.get())) {
  821. auto muf = makeMUFile(sf.get(), fullPath, false);
  822. if (muf->SkipMoc || muf->SkipUic) {
  823. addMUSource(std::move(muf));
  824. }
  825. }
  826. } else if (this->Uic.Enabled && (extLower == kw.ui)) {
  827. // .ui file
  828. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  829. bool const skipUic =
  830. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  831. if (!skipUic) {
  832. // Check if the .ui file has uic options
  833. std::string const uicOpts = sf->GetSafeProperty(kw.AUTOUIC_OPTIONS);
  834. if (!uicOpts.empty()) {
  835. this->Uic.UiFiles.emplace_back(fullPath, cmExpandedList(uicOpts));
  836. }
  837. } else {
  838. // Register skipped .ui file
  839. this->Uic.SkipUi.insert(fullPath);
  840. }
  841. }
  842. }
  843. }
  844. // Process GENERATED sources and headers
  845. if (this->MocOrUicEnabled() && !this->AutogenTarget.FilesGenerated.empty()) {
  846. if (this->CMP0071Accept) {
  847. // Let the autogen target depend on the GENERATED files
  848. for (MUFile* muf : this->AutogenTarget.FilesGenerated) {
  849. this->AutogenTarget.DependFiles.insert(muf->FullPath);
  850. }
  851. } else if (this->CMP0071Warn) {
  852. cm::string_view property;
  853. if (this->Moc.Enabled && this->Uic.Enabled) {
  854. property = "SKIP_AUTOGEN";
  855. } else if (this->Moc.Enabled) {
  856. property = "SKIP_AUTOMOC";
  857. } else if (this->Uic.Enabled) {
  858. property = "SKIP_AUTOUIC";
  859. }
  860. std::string files;
  861. for (MUFile* muf : this->AutogenTarget.FilesGenerated) {
  862. files += cmStrCat(" ", Quoted(muf->FullPath), '\n');
  863. }
  864. this->Makefile->IssueMessage(
  865. MessageType::AUTHOR_WARNING,
  866. cmStrCat(
  867. cmPolicies::GetPolicyWarning(cmPolicies::CMP0071), '\n',
  868. "For compatibility, CMake is excluding the GENERATED source "
  869. "file(s):\n",
  870. files, "from processing by ",
  871. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  872. ". If any of the files should be processed, set CMP0071 to NEW. "
  873. "If any of the files should not be processed, "
  874. "explicitly exclude them by setting the source file property ",
  875. property, ":\n set_property(SOURCE file.h PROPERTY ", property,
  876. " ON)\n"));
  877. }
  878. }
  879. // Generate CMP0100 warning
  880. if (this->MocOrUicEnabled() &&
  881. !this->AutogenTarget.CMP0100HeadersWarn.empty()) {
  882. cm::string_view property;
  883. if (this->Moc.Enabled && this->Uic.Enabled) {
  884. property = "SKIP_AUTOGEN";
  885. } else if (this->Moc.Enabled) {
  886. property = "SKIP_AUTOMOC";
  887. } else if (this->Uic.Enabled) {
  888. property = "SKIP_AUTOUIC";
  889. }
  890. std::string files;
  891. for (cmSourceFile* sf : this->AutogenTarget.CMP0100HeadersWarn) {
  892. files += cmStrCat(" ", Quoted(sf->GetFullPath()), '\n');
  893. }
  894. this->Makefile->IssueMessage(
  895. MessageType::AUTHOR_WARNING,
  896. cmStrCat(
  897. cmPolicies::GetPolicyWarning(cmPolicies::CMP0100), '\n',
  898. "For compatibility, CMake is excluding the header file(s):\n", files,
  899. "from processing by ",
  900. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  901. ". If any of the files should be processed, set CMP0100 to NEW. "
  902. "If any of the files should not be processed, "
  903. "explicitly exclude them by setting the source file property ",
  904. property, ":\n set_property(SOURCE file.hh PROPERTY ", property,
  905. " ON)\n"));
  906. }
  907. // Process qrc files
  908. if (!this->Rcc.Qrcs.empty()) {
  909. const bool modernQt = (this->QtVersion.Major >= 5);
  910. // Target rcc options
  911. std::vector<std::string> optionsTarget =
  912. cmExpandedList(this->GenTarget->GetSafeProperty(kw.AUTORCC_OPTIONS));
  913. // Check if file name is unique
  914. for (Qrc& qrc : this->Rcc.Qrcs) {
  915. qrc.Unique = true;
  916. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  917. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  918. qrc.Unique = false;
  919. break;
  920. }
  921. }
  922. }
  923. // Path checksum and file names
  924. for (Qrc& qrc : this->Rcc.Qrcs) {
  925. // Path checksum
  926. qrc.QrcPathChecksum = this->PathCheckSum.getPart(qrc.QrcFile);
  927. // Output file name
  928. qrc.OutputFile = cmStrCat(this->Dir.Build, '/', qrc.QrcPathChecksum,
  929. "/qrc_", qrc.QrcName, ".cpp");
  930. std::string const base = cmStrCat(this->Dir.Info, "/AutoRcc_",
  931. qrc.QrcName, '_', qrc.QrcPathChecksum);
  932. qrc.LockFile = cmStrCat(base, "_Lock.lock");
  933. qrc.InfoFile = cmStrCat(base, "_Info.json");
  934. ConfigFileNames(qrc.SettingsFile, cmStrCat(base, "_Used"), ".txt");
  935. }
  936. // rcc options
  937. for (Qrc& qrc : this->Rcc.Qrcs) {
  938. // Target options
  939. std::vector<std::string> opts = optionsTarget;
  940. // Merge computed "-name XYZ" option
  941. {
  942. std::string name = qrc.QrcName;
  943. // Replace '-' with '_'. The former is not valid for symbol names.
  944. std::replace(name.begin(), name.end(), '-', '_');
  945. if (!qrc.Unique) {
  946. name += cmStrCat('_', qrc.QrcPathChecksum);
  947. }
  948. std::vector<std::string> nameOpts;
  949. nameOpts.emplace_back("-name");
  950. nameOpts.emplace_back(std::move(name));
  951. RccMergeOptions(opts, nameOpts, modernQt);
  952. }
  953. // Merge file option
  954. RccMergeOptions(opts, qrc.Options, modernQt);
  955. qrc.Options = std::move(opts);
  956. }
  957. // rcc resources
  958. for (Qrc& qrc : this->Rcc.Qrcs) {
  959. if (!qrc.Generated) {
  960. std::string error;
  961. RccLister const lister(this->Rcc.Executable,
  962. this->Rcc.ExecutableFeatures->ListOptions);
  963. if (!lister.list(qrc.QrcFile, qrc.Resources, error)) {
  964. cmSystemTools::Error(error);
  965. return false;
  966. }
  967. }
  968. }
  969. }
  970. return true;
  971. }
  972. bool cmQtAutoGenInitializer::InitAutogenTarget()
  973. {
  974. // Register info file as generated by CMake
  975. this->Makefile->AddCMakeOutputFile(this->AutogenTarget.InfoFile);
  976. // Files provided by the autogen target
  977. std::vector<std::string> autogenProvides;
  978. if (this->Moc.Enabled) {
  979. this->AddGeneratedSource(this->Moc.CompilationFile, this->Moc, true);
  980. autogenProvides.push_back(this->Moc.CompilationFile);
  981. }
  982. // Compose target comment
  983. std::string autogenComment;
  984. {
  985. std::string tools;
  986. if (this->Moc.Enabled) {
  987. tools += "MOC";
  988. }
  989. if (this->Uic.Enabled) {
  990. if (!tools.empty()) {
  991. tools += " and ";
  992. }
  993. tools += "UIC";
  994. }
  995. autogenComment = cmStrCat("Automatic ", tools, " for target ",
  996. this->GenTarget->GetName());
  997. }
  998. // Compose command lines
  999. // TODO: Refactor autogen to output a per-config mocs_compilation.cpp instead
  1000. // of fiddling with the include directories
  1001. std::vector<std::string> configs;
  1002. this->GlobalGen->GetQtAutoGenConfigs(configs);
  1003. cmCustomCommandLines commandLines;
  1004. for (auto const& config : configs) {
  1005. commandLines.push_back(cmMakeCommandLine(
  1006. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_autogen",
  1007. this->AutogenTarget.InfoFile, config }));
  1008. }
  1009. // Use PRE_BUILD on demand
  1010. bool usePRE_BUILD = false;
  1011. if (this->GlobalGen->GetName().find("Visual Studio") != std::string::npos) {
  1012. // Under VS use a PRE_BUILD event instead of a separate target to
  1013. // reduce the number of targets loaded into the IDE.
  1014. // This also works around a VS 11 bug that may skip updating the target:
  1015. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  1016. usePRE_BUILD = true;
  1017. }
  1018. // Disable PRE_BUILD in some cases
  1019. if (usePRE_BUILD) {
  1020. // Cannot use PRE_BUILD with file depends
  1021. if (!this->AutogenTarget.DependFiles.empty()) {
  1022. usePRE_BUILD = false;
  1023. }
  1024. // Cannot use PRE_BUILD when a global autogen target is in place
  1025. if (AutogenTarget.GlobalTarget) {
  1026. usePRE_BUILD = false;
  1027. }
  1028. }
  1029. // Create the autogen target/command
  1030. if (usePRE_BUILD) {
  1031. // Add additional autogen target dependencies to origin target
  1032. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  1033. this->GenTarget->Target->AddUtility(depTarget->GetName(),
  1034. this->Makefile);
  1035. }
  1036. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  1037. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  1038. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  1039. //
  1040. // PRE_BUILD does not support file dependencies!
  1041. const std::vector<std::string> no_output;
  1042. const std::vector<std::string> no_deps;
  1043. cmCustomCommand cc(no_output, autogenProvides, no_deps, commandLines,
  1044. this->Makefile->GetBacktrace(), autogenComment.c_str(),
  1045. this->Dir.Work.c_str());
  1046. cc.SetEscapeOldStyle(false);
  1047. cc.SetEscapeAllowMakeVars(true);
  1048. this->GenTarget->Target->AddPreBuildCommand(std::move(cc));
  1049. } else {
  1050. // Add link library target dependencies to the autogen target
  1051. // dependencies
  1052. if (this->AutogenTarget.DependOrigin) {
  1053. // add_dependencies/addUtility do not support generator expressions.
  1054. // We depend only on the libraries found in all configs therefore.
  1055. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  1056. for (std::string const& config : this->ConfigsList) {
  1057. cmLinkImplementationLibraries const* libs =
  1058. this->GenTarget->GetLinkImplementationLibraries(config);
  1059. if (libs != nullptr) {
  1060. for (cmLinkItem const& item : libs->Libraries) {
  1061. cmGeneratorTarget const* libTarget = item.Target;
  1062. if ((libTarget != nullptr) &&
  1063. !StaticLibraryCycle(this->GenTarget, libTarget, config)) {
  1064. // Increment target config count
  1065. commonTargets[libTarget]++;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. for (auto const& item : commonTargets) {
  1071. if (item.second == this->ConfigsList.size()) {
  1072. this->AutogenTarget.DependTargets.insert(item.first->Target);
  1073. }
  1074. }
  1075. }
  1076. // Create autogen target
  1077. cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
  1078. this->AutogenTarget.Name, true, this->Dir.Work.c_str(),
  1079. /*byproducts=*/autogenProvides,
  1080. std::vector<std::string>(this->AutogenTarget.DependFiles.begin(),
  1081. this->AutogenTarget.DependFiles.end()),
  1082. commandLines, false, autogenComment.c_str());
  1083. // Create autogen generator target
  1084. this->LocalGen->AddGeneratorTarget(
  1085. cm::make_unique<cmGeneratorTarget>(autogenTarget, this->LocalGen));
  1086. // Forward origin utilities to autogen target
  1087. if (this->AutogenTarget.DependOrigin) {
  1088. for (BT<std::string> const& depName : this->GenTarget->GetUtilities()) {
  1089. autogenTarget->AddUtility(depName.Value, this->Makefile);
  1090. }
  1091. }
  1092. // Add additional autogen target dependencies to autogen target
  1093. for (cmTarget* depTarget : this->AutogenTarget.DependTargets) {
  1094. autogenTarget->AddUtility(depTarget->GetName(), this->Makefile);
  1095. }
  1096. // Set FOLDER property in autogen target
  1097. if (!this->TargetsFolder.empty()) {
  1098. autogenTarget->SetProperty("FOLDER", this->TargetsFolder);
  1099. }
  1100. // Add autogen target to the origin target dependencies
  1101. this->GenTarget->Target->AddUtility(this->AutogenTarget.Name,
  1102. this->Makefile);
  1103. // Add autogen target to the global autogen target dependencies
  1104. if (this->AutogenTarget.GlobalTarget) {
  1105. this->GlobalInitializer->AddToGlobalAutoGen(this->LocalGen,
  1106. this->AutogenTarget.Name);
  1107. }
  1108. }
  1109. return true;
  1110. }
  1111. bool cmQtAutoGenInitializer::InitRccTargets()
  1112. {
  1113. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1114. // Register info file as generated by CMake
  1115. this->Makefile->AddCMakeOutputFile(qrc.InfoFile);
  1116. // Register file at target
  1117. {
  1118. cmSourceFile* sf = this->AddGeneratedSource(qrc.OutputFile, this->Rcc);
  1119. sf->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "On");
  1120. }
  1121. std::vector<std::string> ccOutput;
  1122. ccOutput.push_back(qrc.OutputFile);
  1123. std::vector<std::string> ccDepends;
  1124. // Add the .qrc and info file to the custom command dependencies
  1125. ccDepends.push_back(qrc.QrcFile);
  1126. ccDepends.push_back(qrc.InfoFile);
  1127. cmCustomCommandLines commandLines;
  1128. if (this->MultiConfig) {
  1129. // Build for all configurations
  1130. for (std::string const& config : this->ConfigsList) {
  1131. commandLines.push_back(
  1132. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1133. "cmake_autorcc", qrc.InfoFile, config }));
  1134. }
  1135. } else {
  1136. commandLines.push_back(
  1137. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1138. "cmake_autorcc", qrc.InfoFile, "$<CONFIG>" }));
  1139. }
  1140. std::string ccComment =
  1141. cmStrCat("Automatic RCC for ",
  1142. FileProjectRelativePath(this->Makefile, qrc.QrcFile));
  1143. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1144. // Create custom rcc target
  1145. std::string ccName;
  1146. {
  1147. ccName = cmStrCat(this->GenTarget->GetName(), "_arcc_", qrc.QrcName);
  1148. if (!qrc.Unique) {
  1149. ccName += cmStrCat('_', qrc.QrcPathChecksum);
  1150. }
  1151. cmTarget* autoRccTarget = this->LocalGen->AddUtilityCommand(
  1152. ccName, true, this->Dir.Work.c_str(), ccOutput, ccDepends,
  1153. commandLines, false, ccComment.c_str());
  1154. // Create autogen generator target
  1155. this->LocalGen->AddGeneratorTarget(
  1156. cm::make_unique<cmGeneratorTarget>(autoRccTarget, this->LocalGen));
  1157. // Set FOLDER property in autogen target
  1158. if (!this->TargetsFolder.empty()) {
  1159. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder);
  1160. }
  1161. if (!this->Rcc.ExecutableTargetName.empty()) {
  1162. autoRccTarget->AddUtility(this->Rcc.ExecutableTargetName,
  1163. this->Makefile);
  1164. }
  1165. }
  1166. // Add autogen target to the origin target dependencies
  1167. this->GenTarget->Target->AddUtility(ccName, this->Makefile);
  1168. // Add autogen target to the global autogen target dependencies
  1169. if (this->Rcc.GlobalTarget) {
  1170. this->GlobalInitializer->AddToGlobalAutoRcc(this->LocalGen, ccName);
  1171. }
  1172. } else {
  1173. // Create custom rcc command
  1174. {
  1175. std::vector<std::string> ccByproducts;
  1176. // Add the resource files to the dependencies
  1177. for (std::string const& fileName : qrc.Resources) {
  1178. // Add resource file to the custom command dependencies
  1179. ccDepends.push_back(fileName);
  1180. }
  1181. if (!this->Rcc.ExecutableTargetName.empty()) {
  1182. ccDepends.push_back(this->Rcc.ExecutableTargetName);
  1183. }
  1184. std::string no_main_dependency;
  1185. cmImplicitDependsList no_implicit_depends;
  1186. this->LocalGen->AddCustomCommandToOutput(
  1187. ccOutput, ccByproducts, ccDepends, no_main_dependency,
  1188. no_implicit_depends, commandLines, ccComment.c_str(),
  1189. this->Dir.Work.c_str());
  1190. }
  1191. // Reconfigure when .qrc file changes
  1192. this->Makefile->AddCMakeDependFile(qrc.QrcFile);
  1193. }
  1194. }
  1195. return true;
  1196. }
  1197. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1198. {
  1199. // Create info directory on demand
  1200. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1201. cmSystemTools::Error(cmStrCat("AutoGen: Could not create directory: ",
  1202. Quoted(this->Dir.Info)));
  1203. return false;
  1204. }
  1205. // Generate autogen target info file
  1206. if (this->MocOrUicEnabled()) {
  1207. // Write autogen target info files
  1208. if (!this->SetupWriteAutogenInfo()) {
  1209. return false;
  1210. }
  1211. }
  1212. // Write AUTORCC info files
  1213. return !this->Rcc.Enabled || this->SetupWriteRccInfo();
  1214. }
  1215. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1216. {
  1217. // Utility lambdas
  1218. auto MfDef = [this](std::string const& key) {
  1219. return this->Makefile->GetSafeDefinition(key);
  1220. };
  1221. // Filtered headers and sources
  1222. std::set<std::string> moc_skip;
  1223. std::set<std::string> uic_skip;
  1224. std::vector<MUFile const*> headers;
  1225. std::vector<MUFile const*> sources;
  1226. // Filter headers
  1227. {
  1228. headers.reserve(this->AutogenTarget.Headers.size());
  1229. for (auto const& pair : this->AutogenTarget.Headers) {
  1230. MUFile const* const muf = pair.second.get();
  1231. if (muf->Generated && !this->CMP0071Accept) {
  1232. continue;
  1233. }
  1234. if (muf->SkipMoc) {
  1235. moc_skip.insert(muf->FullPath);
  1236. }
  1237. if (muf->SkipUic) {
  1238. uic_skip.insert(muf->FullPath);
  1239. }
  1240. if (muf->MocIt || muf->UicIt) {
  1241. headers.emplace_back(muf);
  1242. }
  1243. }
  1244. std::sort(headers.begin(), headers.end(),
  1245. [](MUFile const* a, MUFile const* b) {
  1246. return (a->FullPath < b->FullPath);
  1247. });
  1248. }
  1249. // Filter sources
  1250. {
  1251. sources.reserve(this->AutogenTarget.Sources.size());
  1252. for (auto const& pair : this->AutogenTarget.Sources) {
  1253. MUFile const* const muf = pair.second.get();
  1254. if (muf->Generated && !this->CMP0071Accept) {
  1255. continue;
  1256. }
  1257. if (muf->SkipMoc) {
  1258. moc_skip.insert(muf->FullPath);
  1259. }
  1260. if (muf->SkipUic) {
  1261. uic_skip.insert(muf->FullPath);
  1262. }
  1263. if (muf->MocIt || muf->UicIt) {
  1264. sources.emplace_back(muf);
  1265. }
  1266. }
  1267. std::sort(sources.begin(), sources.end(),
  1268. [](MUFile const* a, MUFile const* b) {
  1269. return (a->FullPath < b->FullPath);
  1270. });
  1271. }
  1272. // Info writer
  1273. InfoWriter info;
  1274. // General
  1275. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1276. info.SetUInt("PARALLEL", this->AutogenTarget.Parallel);
  1277. info.SetUInt("VERBOSITY", this->Verbosity);
  1278. // Directories
  1279. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1280. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1281. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1282. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1283. info.Set("BUILD_DIR", this->Dir.Build);
  1284. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1285. info.SetUInt("QT_VERSION_MAJOR", this->QtVersion.Major);
  1286. info.Set("QT_MOC_EXECUTABLE", this->Moc.Executable);
  1287. info.Set("QT_UIC_EXECUTABLE", this->Uic.Executable);
  1288. info.Set("CMAKE_EXECUTABLE", cmSystemTools::GetCMakeCommand());
  1289. info.SetConfig("SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1290. info.SetConfig("PARSE_CACHE_FILE", this->AutogenTarget.ParseCacheFile);
  1291. info.SetArray("HEADER_EXTENSIONS",
  1292. this->Makefile->GetCMakeInstance()->GetHeaderExtensions());
  1293. info.SetArrayArray(
  1294. "HEADERS", headers, [this](Json::Value& jval, MUFile const* muf) {
  1295. jval.resize(3u);
  1296. jval[0u] = muf->FullPath;
  1297. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm', muf->UicIt ? 'U' : 'u');
  1298. jval[2u] = this->GetMocBuildPath(*muf);
  1299. });
  1300. info.SetArrayArray(
  1301. "SOURCES", sources, [](Json::Value& jval, MUFile const* muf) {
  1302. jval.resize(2u);
  1303. jval[0u] = muf->FullPath;
  1304. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm', muf->UicIt ? 'U' : 'u');
  1305. });
  1306. // Write moc settings
  1307. if (this->Moc.Enabled) {
  1308. info.SetArray("MOC_SKIP", moc_skip);
  1309. info.SetConfigArray("MOC_DEFINITIONS", this->Moc.Defines);
  1310. info.SetConfigArray("MOC_INCLUDES", this->Moc.Includes);
  1311. info.SetArray("MOC_OPTIONS", this->Moc.Options);
  1312. info.SetBool("MOC_RELAXED_MODE", this->Moc.RelaxedMode);
  1313. info.SetBool("MOC_PATH_PREFIX", this->Moc.PathPrefix);
  1314. info.SetArray("MOC_MACRO_NAMES", this->Moc.MacroNames);
  1315. info.SetArrayArray(
  1316. "MOC_DEPEND_FILTERS", this->Moc.DependFilters,
  1317. [](Json::Value& jval, std::pair<std::string, std::string> const& pair) {
  1318. jval.resize(2u);
  1319. jval[0u] = pair.first;
  1320. jval[1u] = pair.second;
  1321. });
  1322. info.Set("MOC_COMPILATION_FILE", this->Moc.CompilationFile);
  1323. info.SetArray("MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1324. info.SetConfig("MOC_PREDEFS_FILE", this->Moc.PredefsFile);
  1325. }
  1326. // Write uic settings
  1327. if (this->Uic.Enabled) {
  1328. // Add skipped .ui files
  1329. uic_skip.insert(this->Uic.SkipUi.begin(), this->Uic.SkipUi.end());
  1330. info.SetArray("UIC_SKIP", uic_skip);
  1331. info.SetArrayArray("UIC_UI_FILES", this->Uic.UiFiles,
  1332. [](Json::Value& jval, UicT::UiFileT const& uiFile) {
  1333. jval.resize(2u);
  1334. jval[0u] = uiFile.first;
  1335. InfoWriter::MakeStringArray(jval[1u], uiFile.second);
  1336. });
  1337. info.SetConfigArray("UIC_OPTIONS", this->Uic.Options);
  1338. info.SetArray("UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1339. }
  1340. info.Save(this->AutogenTarget.InfoFile);
  1341. return true;
  1342. }
  1343. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1344. {
  1345. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1346. // Utility lambdas
  1347. auto MfDef = [this](std::string const& key) {
  1348. return this->Makefile->GetSafeDefinition(key);
  1349. };
  1350. InfoWriter info;
  1351. // General
  1352. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1353. info.SetUInt("VERBOSITY", this->Verbosity);
  1354. // Files
  1355. info.Set("LOCK_FILE", qrc.LockFile);
  1356. info.SetConfig("SETTINGS_FILE", qrc.SettingsFile);
  1357. // Directories
  1358. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1359. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1360. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1361. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1362. info.Set("BUILD_DIR", this->Dir.Build);
  1363. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1364. // rcc executable
  1365. info.Set("RCC_EXECUTABLE", this->Rcc.Executable);
  1366. info.SetArray("RCC_LIST_OPTIONS",
  1367. this->Rcc.ExecutableFeatures->ListOptions);
  1368. // qrc file
  1369. info.Set("SOURCE", qrc.QrcFile);
  1370. info.Set("OUTPUT_CHECKSUM", qrc.QrcPathChecksum);
  1371. info.Set("OUTPUT_NAME", cmSystemTools::GetFilenameName(qrc.OutputFile));
  1372. info.SetArray("OPTIONS", qrc.Options);
  1373. info.SetArray("INPUTS", qrc.Resources);
  1374. info.Save(qrc.InfoFile);
  1375. }
  1376. return true;
  1377. }
  1378. cmSourceFile* cmQtAutoGenInitializer::RegisterGeneratedSource(
  1379. std::string const& filename)
  1380. {
  1381. cmSourceFile* gFile = this->Makefile->GetOrCreateSource(filename, true);
  1382. gFile->SetProperty("GENERATED", "1");
  1383. gFile->SetProperty("SKIP_AUTOGEN", "1");
  1384. return gFile;
  1385. }
  1386. cmSourceFile* cmQtAutoGenInitializer::AddGeneratedSource(
  1387. std::string const& filename, GenVarsT const& genVars, bool prepend)
  1388. {
  1389. // Register source at makefile
  1390. cmSourceFile* gFile = this->RegisterGeneratedSource(filename);
  1391. // Add source file to target
  1392. this->GenTarget->AddSource(filename, prepend);
  1393. // Add source file to source group
  1394. this->AddToSourceGroup(filename, genVars.GenNameUpper);
  1395. return gFile;
  1396. }
  1397. void cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
  1398. cm::string_view genNameUpper)
  1399. {
  1400. cmSourceGroup* sourceGroup = nullptr;
  1401. // Acquire source group
  1402. {
  1403. std::string property;
  1404. std::string groupName;
  1405. {
  1406. // Prefer generator specific source group name
  1407. std::initializer_list<std::string> const props{
  1408. cmStrCat(genNameUpper, "_SOURCE_GROUP"), "AUTOGEN_SOURCE_GROUP"
  1409. };
  1410. for (std::string const& prop : props) {
  1411. const char* propName =
  1412. this->Makefile->GetState()->GetGlobalProperty(prop);
  1413. if ((propName != nullptr) && (*propName != '\0')) {
  1414. groupName = propName;
  1415. property = prop;
  1416. break;
  1417. }
  1418. }
  1419. }
  1420. // Generate a source group on demand
  1421. if (!groupName.empty()) {
  1422. sourceGroup = this->Makefile->GetOrCreateSourceGroup(groupName);
  1423. if (sourceGroup == nullptr) {
  1424. cmSystemTools::Error(
  1425. cmStrCat(genNameUpper, " error in ", property,
  1426. ": Could not find or create the source group ",
  1427. cmQtAutoGen::Quoted(groupName)));
  1428. }
  1429. }
  1430. }
  1431. if (sourceGroup != nullptr) {
  1432. sourceGroup->AddGroupFile(fileName);
  1433. }
  1434. }
  1435. void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
  1436. {
  1437. this->GenTarget->Target->AppendProperty("ADDITIONAL_CLEAN_FILES", fileName,
  1438. false);
  1439. }
  1440. void cmQtAutoGenInitializer::ConfigFileNames(ConfigString& configString,
  1441. cm::string_view prefix,
  1442. cm::string_view suffix)
  1443. {
  1444. configString.Default = cmStrCat(prefix, suffix);
  1445. if (this->MultiConfig) {
  1446. for (auto const& cfg : this->ConfigsList) {
  1447. configString.Config[cfg] = cmStrCat(prefix, '_', cfg, suffix);
  1448. }
  1449. }
  1450. }
  1451. void cmQtAutoGenInitializer::ConfigFileClean(ConfigString& configString)
  1452. {
  1453. this->AddCleanFile(configString.Default);
  1454. if (this->MultiConfig) {
  1455. for (auto const& pair : configString.Config) {
  1456. this->AddCleanFile(pair.second);
  1457. }
  1458. }
  1459. }
  1460. std::pair<cmQtAutoGen::IntegerVersion, unsigned int>
  1461. cmQtAutoGenInitializer::GetQtVersion(cmGeneratorTarget const* target)
  1462. {
  1463. // Converts a char ptr to an unsigned int value
  1464. auto toUInt = [](const char* const input) -> unsigned int {
  1465. unsigned long tmp = 0;
  1466. if (input != nullptr && cmStrToULong(input, &tmp)) {
  1467. return static_cast<unsigned int>(tmp);
  1468. }
  1469. return 0u;
  1470. };
  1471. // Initialize return value to a default
  1472. std::pair<IntegerVersion, unsigned int> res(
  1473. IntegerVersion(),
  1474. toUInt(target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1475. "")));
  1476. // Acquire known Qt versions
  1477. std::vector<cmQtAutoGen::IntegerVersion> knownQtVersions;
  1478. {
  1479. // Qt version variable prefixes
  1480. static std::initializer_list<
  1481. std::pair<cm::string_view, cm::string_view>> const keys{
  1482. { "Qt6Core_VERSION_MAJOR", "Qt6Core_VERSION_MINOR" },
  1483. { "Qt5Core_VERSION_MAJOR", "Qt5Core_VERSION_MINOR" },
  1484. { "QT_VERSION_MAJOR", "QT_VERSION_MINOR" },
  1485. };
  1486. knownQtVersions.reserve(keys.size() * 2);
  1487. // Adds a version to the result (nullptr safe)
  1488. auto addVersion = [&knownQtVersions, &toUInt](const char* major,
  1489. const char* minor) {
  1490. cmQtAutoGen::IntegerVersion ver(toUInt(major), toUInt(minor));
  1491. if (ver.Major != 0) {
  1492. knownQtVersions.emplace_back(ver);
  1493. }
  1494. };
  1495. // Read versions from variables
  1496. for (auto const& keyPair : keys) {
  1497. addVersion(target->Makefile->GetDefinition(std::string(keyPair.first)),
  1498. target->Makefile->GetDefinition(std::string(keyPair.second)));
  1499. }
  1500. // Read versions from directory properties
  1501. for (auto const& keyPair : keys) {
  1502. addVersion(target->Makefile->GetProperty(std::string(keyPair.first)),
  1503. target->Makefile->GetProperty(std::string(keyPair.second)));
  1504. }
  1505. }
  1506. // Evaluate known Qt versions
  1507. if (!knownQtVersions.empty()) {
  1508. if (res.second == 0) {
  1509. // No specific version was requested by the target:
  1510. // Use highest known Qt version.
  1511. res.first = knownQtVersions.at(0);
  1512. } else {
  1513. // Pick a version from the known versions:
  1514. for (auto it : knownQtVersions) {
  1515. if (it.Major == res.second) {
  1516. res.first = it;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. }
  1522. return res;
  1523. }
  1524. std::string cmQtAutoGenInitializer::GetMocBuildPath(MUFile const& muf)
  1525. {
  1526. std::string res;
  1527. if (!muf.MocIt) {
  1528. return res;
  1529. }
  1530. std::string basePath =
  1531. cmStrCat(this->PathCheckSum.getPart(muf.FullPath), "/moc_",
  1532. FileNameWithoutLastExtension(muf.FullPath));
  1533. res = cmStrCat(basePath, ".cpp");
  1534. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1535. return res;
  1536. }
  1537. // File name already emitted.
  1538. // Try appending the header suffix to the base path.
  1539. basePath = cmStrCat(basePath, '_', muf.SF->GetExtension());
  1540. res = cmStrCat(basePath, ".cpp");
  1541. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1542. return res;
  1543. }
  1544. // File name with header extension already emitted.
  1545. // Try adding a number to the base path.
  1546. constexpr std::size_t number_begin = 2;
  1547. constexpr std::size_t number_end = 256;
  1548. for (std::size_t ii = number_begin; ii != number_end; ++ii) {
  1549. res = cmStrCat(basePath, '_', ii, ".cpp");
  1550. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1551. return res;
  1552. }
  1553. }
  1554. // Output file name conflict (unlikely, but still...)
  1555. cmSystemTools::Error(
  1556. cmStrCat("moc output file name conflict for ", muf.FullPath));
  1557. return res;
  1558. }
  1559. bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
  1560. const std::string& executable,
  1561. bool ignoreMissingTarget) const
  1562. {
  1563. auto print_err = [this, &genVars](std::string const& err) {
  1564. cmSystemTools::Error(cmStrCat(genVars.GenNameUpper, " for target ",
  1565. this->GenTarget->GetName(), ": ", err));
  1566. };
  1567. // Custom executable
  1568. {
  1569. std::string const prop = cmStrCat(genVars.GenNameUpper, "_EXECUTABLE");
  1570. std::string const val = this->GenTarget->Target->GetSafeProperty(prop);
  1571. if (!val.empty()) {
  1572. // Evaluate generator expression
  1573. {
  1574. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  1575. cmGeneratorExpression ge(lfbt);
  1576. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(val);
  1577. genVars.Executable = cge->Evaluate(this->LocalGen, "");
  1578. }
  1579. if (genVars.Executable.empty() && !ignoreMissingTarget) {
  1580. print_err(prop + " evaluates to an empty value");
  1581. return false;
  1582. }
  1583. // Create empty compiler features.
  1584. genVars.ExecutableFeatures =
  1585. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  1586. return true;
  1587. }
  1588. }
  1589. // Find executable target
  1590. {
  1591. // Find executable target name
  1592. cm::string_view prefix;
  1593. if (this->QtVersion.Major == 4) {
  1594. prefix = "Qt4::";
  1595. } else if (this->QtVersion.Major == 5) {
  1596. prefix = "Qt5::";
  1597. } else if (this->QtVersion.Major == 6) {
  1598. prefix = "Qt6::";
  1599. }
  1600. std::string const targetName = cmStrCat(prefix, executable);
  1601. // Find target
  1602. cmGeneratorTarget* genTarget =
  1603. this->LocalGen->FindGeneratorTargetToUse(targetName);
  1604. if (genTarget != nullptr) {
  1605. genVars.ExecutableTargetName = targetName;
  1606. genVars.ExecutableTarget = genTarget;
  1607. if (genTarget->IsImported()) {
  1608. genVars.Executable = genTarget->ImportedGetLocation("");
  1609. } else {
  1610. genVars.Executable = genTarget->GetLocation("");
  1611. }
  1612. } else {
  1613. if (ignoreMissingTarget) {
  1614. // Create empty compiler features.
  1615. genVars.ExecutableFeatures =
  1616. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  1617. return true;
  1618. }
  1619. print_err(cmStrCat("Could not find ", executable, " executable target ",
  1620. targetName));
  1621. return false;
  1622. }
  1623. }
  1624. // Get executable features
  1625. {
  1626. std::string err;
  1627. genVars.ExecutableFeatures = this->GlobalInitializer->GetCompilerFeatures(
  1628. executable, genVars.Executable, err);
  1629. if (!genVars.ExecutableFeatures) {
  1630. print_err(err);
  1631. return false;
  1632. }
  1633. }
  1634. return true;
  1635. }