cmQtAutoGenInitializer.cxx 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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 <limits>
  8. #include <map>
  9. #include <set>
  10. #include <sstream> // for basic_ios, istringstream
  11. #include <string>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/algorithm>
  16. #include <cm/iterator>
  17. #include <cm/memory>
  18. #include <cmext/algorithm>
  19. #include <cmext/string_view>
  20. #include <cm3p/json/value.h>
  21. #include <cm3p/json/writer.h>
  22. #include "cmsys/SystemInformation.hxx"
  23. #include "cmAlgorithms.h"
  24. #include "cmCustomCommand.h"
  25. #include "cmCustomCommandLines.h"
  26. #include "cmEvaluatedTargetProperty.h"
  27. #include "cmGeneratedFileStream.h"
  28. #include "cmGeneratorExpression.h"
  29. #include "cmGeneratorExpressionDAGChecker.h"
  30. #include "cmGeneratorTarget.h"
  31. #include "cmGlobalGenerator.h"
  32. #include "cmLinkItem.h"
  33. #include "cmList.h"
  34. #include "cmListFileCache.h"
  35. #include "cmLocalGenerator.h"
  36. #include "cmMakefile.h"
  37. #include "cmMessageType.h"
  38. #include "cmPolicies.h"
  39. #include "cmQtAutoGen.h"
  40. #include "cmQtAutoGenGlobalInitializer.h"
  41. #include "cmSourceFile.h"
  42. #include "cmSourceFileLocationKind.h"
  43. #include "cmSourceGroup.h"
  44. #include "cmStandardLevelResolver.h"
  45. #include "cmState.h"
  46. #include "cmStateTypes.h"
  47. #include "cmStringAlgorithms.h"
  48. #include "cmSystemTools.h"
  49. #include "cmTarget.h"
  50. #include "cmValue.h"
  51. #include "cmake.h"
  52. namespace {
  53. unsigned int GetParallelCPUCount()
  54. {
  55. static unsigned int count = 0;
  56. // Detect only on the first call
  57. if (count == 0) {
  58. cmsys::SystemInformation info;
  59. info.RunCPUCheck();
  60. count =
  61. cm::clamp(info.GetNumberOfPhysicalCPU(), 1u, cmQtAutoGen::ParallelMax);
  62. }
  63. return count;
  64. }
  65. std::string FileProjectRelativePath(cmMakefile const* makefile,
  66. std::string const& fileName)
  67. {
  68. std::string res;
  69. {
  70. std::string pSource = cmSystemTools::RelativePath(
  71. makefile->GetCurrentSourceDirectory(), fileName);
  72. std::string pBinary = cmSystemTools::RelativePath(
  73. makefile->GetCurrentBinaryDirectory(), fileName);
  74. if (pSource.size() < pBinary.size()) {
  75. res = std::move(pSource);
  76. } else if (pBinary.size() < fileName.size()) {
  77. res = std::move(pBinary);
  78. } else {
  79. res = fileName;
  80. }
  81. }
  82. return res;
  83. }
  84. /**
  85. * Tests if targetDepend is a STATIC_LIBRARY and if any of its
  86. * recursive STATIC_LIBRARY dependencies depends on targetOrigin
  87. * (STATIC_LIBRARY cycle).
  88. */
  89. bool StaticLibraryCycle(cmGeneratorTarget const* targetOrigin,
  90. cmGeneratorTarget const* targetDepend,
  91. std::string const& config)
  92. {
  93. bool cycle = false;
  94. if ((targetOrigin->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  95. (targetDepend->GetType() == cmStateEnums::STATIC_LIBRARY)) {
  96. std::set<cmGeneratorTarget const*> knownLibs;
  97. std::deque<cmGeneratorTarget const*> testLibs;
  98. // Insert initial static_library dependency
  99. knownLibs.insert(targetDepend);
  100. testLibs.push_back(targetDepend);
  101. while (!testLibs.empty()) {
  102. cmGeneratorTarget const* testTarget = testLibs.front();
  103. testLibs.pop_front();
  104. // Check if the test target is the origin target (cycle)
  105. if (testTarget == targetOrigin) {
  106. cycle = true;
  107. break;
  108. }
  109. // Collect all static_library dependencies from the test target
  110. cmLinkImplementationLibraries const* libs =
  111. testTarget->GetLinkImplementationLibraries(
  112. config, cmGeneratorTarget::LinkInterfaceFor::Link);
  113. if (libs) {
  114. for (cmLinkItem const& item : libs->Libraries) {
  115. cmGeneratorTarget const* depTarget = item.Target;
  116. if (depTarget &&
  117. (depTarget->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  118. knownLibs.insert(depTarget).second) {
  119. testLibs.push_back(depTarget);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return cycle;
  126. }
  127. /** Sanitizes file search paths. */
  128. class SearchPathSanitizer
  129. {
  130. public:
  131. SearchPathSanitizer(cmMakefile* makefile)
  132. : SourcePath_(makefile->GetCurrentSourceDirectory())
  133. {
  134. }
  135. std::vector<std::string> operator()(
  136. std::vector<std::string> const& paths) const;
  137. private:
  138. std::string SourcePath_;
  139. };
  140. std::vector<std::string> SearchPathSanitizer::operator()(
  141. std::vector<std::string> const& paths) const
  142. {
  143. std::vector<std::string> res;
  144. res.reserve(paths.size());
  145. for (std::string const& srcPath : paths) {
  146. // Collapse relative paths
  147. std::string path =
  148. cmSystemTools::CollapseFullPath(srcPath, this->SourcePath_);
  149. // Remove suffix slashes
  150. while (cmHasSuffix(path, '/')) {
  151. path.pop_back();
  152. }
  153. // Accept only non empty paths
  154. if (!path.empty()) {
  155. res.emplace_back(std::move(path));
  156. }
  157. }
  158. return res;
  159. }
  160. /** @brief Writes a CMake info file. */
  161. class InfoWriter
  162. {
  163. public:
  164. // -- Single value
  165. void Set(std::string const& key, std::string const& value)
  166. {
  167. this->Value_[key] = value;
  168. }
  169. void SetConfig(std::string const& key,
  170. cmQtAutoGenInitializer::ConfigString const& cfgStr);
  171. void SetBool(std::string const& key, bool value)
  172. {
  173. this->Value_[key] = value;
  174. }
  175. void SetUInt(std::string const& key, unsigned int value)
  176. {
  177. this->Value_[key] = value;
  178. }
  179. // -- Array utility
  180. template <typename CONT>
  181. static bool MakeArray(Json::Value& jval, CONT const& container);
  182. template <typename CONT>
  183. static void MakeStringArray(Json::Value& jval, CONT const& container);
  184. // -- Array value
  185. template <typename CONT>
  186. void SetArray(std::string const& key, CONT const& container);
  187. template <typename CONT>
  188. void SetConfigArray(
  189. std::string const& key,
  190. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr);
  191. // -- Array of arrays
  192. template <typename CONT, typename FUNC>
  193. void SetArrayArray(std::string const& key, CONT const& container, FUNC func);
  194. // -- Save to json file
  195. bool Save(std::string const& filename);
  196. private:
  197. Json::Value Value_;
  198. };
  199. void InfoWriter::SetConfig(std::string const& key,
  200. cmQtAutoGenInitializer::ConfigString const& cfgStr)
  201. {
  202. this->Set(key, cfgStr.Default);
  203. for (auto const& item : cfgStr.Config) {
  204. this->Set(cmStrCat(key, '_', item.first), item.second);
  205. }
  206. }
  207. template <typename CONT>
  208. bool InfoWriter::MakeArray(Json::Value& jval, CONT const& container)
  209. {
  210. jval = Json::arrayValue;
  211. std::size_t const listSize = cm::size(container);
  212. if (listSize == 0) {
  213. return false;
  214. }
  215. jval.resize(static_cast<unsigned int>(listSize));
  216. return true;
  217. }
  218. template <typename CONT>
  219. void InfoWriter::MakeStringArray(Json::Value& jval, CONT const& container)
  220. {
  221. if (MakeArray(jval, container)) {
  222. Json::ArrayIndex ii = 0;
  223. for (std::string const& item : container) {
  224. jval[ii++] = item;
  225. }
  226. }
  227. }
  228. template <typename CONT>
  229. void InfoWriter::SetArray(std::string const& key, CONT const& container)
  230. {
  231. MakeStringArray(this->Value_[key], container);
  232. }
  233. template <typename CONT, typename FUNC>
  234. void InfoWriter::SetArrayArray(std::string const& key, CONT const& container,
  235. FUNC func)
  236. {
  237. Json::Value& jval = this->Value_[key];
  238. if (MakeArray(jval, container)) {
  239. Json::ArrayIndex ii = 0;
  240. for (auto const& citem : container) {
  241. Json::Value& aval = jval[ii++];
  242. aval = Json::arrayValue;
  243. func(aval, citem);
  244. }
  245. }
  246. }
  247. template <typename CONT>
  248. void InfoWriter::SetConfigArray(
  249. std::string const& key,
  250. cmQtAutoGenInitializer::ConfigStrings<CONT> const& cfgStr)
  251. {
  252. this->SetArray(key, cfgStr.Default);
  253. for (auto const& item : cfgStr.Config) {
  254. this->SetArray(cmStrCat(key, '_', item.first), item.second);
  255. }
  256. }
  257. bool InfoWriter::Save(std::string const& filename)
  258. {
  259. cmGeneratedFileStream fileStream;
  260. fileStream.SetCopyIfDifferent(true);
  261. fileStream.Open(filename, false, true);
  262. if (!fileStream) {
  263. return false;
  264. }
  265. Json::StyledStreamWriter jsonWriter;
  266. try {
  267. jsonWriter.write(fileStream, this->Value_);
  268. } catch (...) {
  269. return false;
  270. }
  271. return fileStream.Close();
  272. }
  273. void AddAutogenExecutableToDependencies(
  274. cmQtAutoGenInitializer::GenVarsT const& genVars,
  275. std::vector<std::string>& dependencies)
  276. {
  277. if (genVars.ExecutableTarget != nullptr) {
  278. dependencies.push_back(genVars.ExecutableTarget->Target->GetName());
  279. } else if (!genVars.Executable.empty()) {
  280. dependencies.push_back(genVars.Executable);
  281. }
  282. }
  283. } // End of unnamed namespace
  284. cmQtAutoGenInitializer::cmQtAutoGenInitializer(
  285. cmQtAutoGenGlobalInitializer* globalInitializer,
  286. cmGeneratorTarget* genTarget, IntegerVersion const& qtVersion,
  287. bool mocEnabled, bool uicEnabled, bool rccEnabled, bool globalAutogenTarget,
  288. bool globalAutoRccTarget)
  289. : GlobalInitializer(globalInitializer)
  290. , GenTarget(genTarget)
  291. , GlobalGen(genTarget->GetGlobalGenerator())
  292. , LocalGen(genTarget->GetLocalGenerator())
  293. , Makefile(genTarget->Makefile)
  294. , PathCheckSum(genTarget->Makefile)
  295. , QtVersion(qtVersion)
  296. {
  297. this->AutogenTarget.GlobalTarget = globalAutogenTarget;
  298. this->Moc.Enabled = mocEnabled;
  299. this->Uic.Enabled = uicEnabled;
  300. this->Rcc.Enabled = rccEnabled;
  301. this->Rcc.GlobalTarget = globalAutoRccTarget;
  302. this->CrossConfig =
  303. !this->Makefile->GetSafeDefinition("CMAKE_CROSS_CONFIGS").empty();
  304. }
  305. bool cmQtAutoGenInitializer::InitCustomTargets()
  306. {
  307. // Configurations
  308. this->MultiConfig = this->GlobalGen->IsMultiConfig();
  309. this->ConfigDefault = this->Makefile->GetDefaultConfiguration();
  310. this->ConfigsList =
  311. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  312. // Verbosity
  313. {
  314. std::string const def =
  315. this->Makefile->GetSafeDefinition("CMAKE_AUTOGEN_VERBOSE");
  316. if (!def.empty()) {
  317. unsigned long iVerb = 0;
  318. if (cmStrToULong(def, &iVerb)) {
  319. // Numeric verbosity
  320. this->Verbosity = static_cast<unsigned int>(iVerb);
  321. } else {
  322. // Non numeric verbosity
  323. if (cmIsOn(def)) {
  324. this->Verbosity = 1;
  325. }
  326. }
  327. }
  328. }
  329. // Targets FOLDER
  330. {
  331. cmValue folder =
  332. this->Makefile->GetState()->GetGlobalProperty("AUTOMOC_TARGETS_FOLDER");
  333. if (!folder) {
  334. folder = this->Makefile->GetState()->GetGlobalProperty(
  335. "AUTOGEN_TARGETS_FOLDER");
  336. }
  337. // Inherit FOLDER property from target (#13688)
  338. if (!folder) {
  339. folder = this->GenTarget->GetProperty("FOLDER");
  340. }
  341. if (folder) {
  342. this->TargetsFolder = *folder;
  343. }
  344. }
  345. // Check status of policy CMP0071 regarding handling of GENERATED files
  346. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0071)) {
  347. case cmPolicies::WARN:
  348. // Ignore GENERATED files but warn
  349. this->CMP0071Warn = true;
  350. CM_FALLTHROUGH;
  351. case cmPolicies::OLD:
  352. // Ignore GENERATED files
  353. break;
  354. case cmPolicies::REQUIRED_IF_USED:
  355. case cmPolicies::REQUIRED_ALWAYS:
  356. case cmPolicies::NEW:
  357. // Process GENERATED files
  358. this->CMP0071Accept = true;
  359. break;
  360. }
  361. // Check status of policy CMP0100 regarding handling of .hh headers
  362. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0100)) {
  363. case cmPolicies::WARN:
  364. // Ignore but .hh files but warn
  365. this->CMP0100Warn = true;
  366. CM_FALLTHROUGH;
  367. case cmPolicies::OLD:
  368. // Ignore .hh files
  369. break;
  370. case cmPolicies::REQUIRED_IF_USED:
  371. case cmPolicies::REQUIRED_ALWAYS:
  372. case cmPolicies::NEW:
  373. // Process .hh file
  374. this->CMP0100Accept = true;
  375. break;
  376. }
  377. // Common directories
  378. std::string relativeBuildDir;
  379. {
  380. // Collapsed current binary directory
  381. std::string const cbd = cmSystemTools::CollapseFullPath(
  382. std::string(), this->Makefile->GetCurrentBinaryDirectory());
  383. // Info directory
  384. this->Dir.Info = cmStrCat(cbd, "/CMakeFiles/", this->GenTarget->GetName(),
  385. "_autogen.dir");
  386. cmSystemTools::ConvertToUnixSlashes(this->Dir.Info);
  387. // Build directory
  388. this->Dir.Build = this->GenTarget->GetSafeProperty("AUTOGEN_BUILD_DIR");
  389. if (this->Dir.Build.empty()) {
  390. this->Dir.Build =
  391. cmStrCat(cbd, '/', this->GenTarget->GetName(), "_autogen");
  392. }
  393. cmSystemTools::ConvertToUnixSlashes(this->Dir.Build);
  394. this->Dir.RelativeBuild =
  395. cmSystemTools::RelativePath(cbd, this->Dir.Build);
  396. // Cleanup build directory
  397. this->AddCleanFile(this->Dir.Build);
  398. // Working directory
  399. this->Dir.Work = cbd;
  400. cmSystemTools::ConvertToUnixSlashes(this->Dir.Work);
  401. // Include directory
  402. this->ConfigFileNamesAndGenex(this->Dir.Include, this->Dir.IncludeGenExp,
  403. cmStrCat(this->Dir.Build, "/include"), "");
  404. }
  405. // Moc, Uic and _autogen target settings
  406. if (this->MocOrUicEnabled()) {
  407. // Init moc specific settings
  408. if (this->Moc.Enabled && !this->InitMoc()) {
  409. return false;
  410. }
  411. // Init uic specific settings
  412. if (this->Uic.Enabled && !this->InitUic()) {
  413. return false;
  414. }
  415. // Autogen target name
  416. this->AutogenTarget.Name =
  417. cmStrCat(this->GenTarget->GetName(), "_autogen");
  418. // Autogen target parallel processing
  419. {
  420. using ParallelType = decltype(this->AutogenTarget.Parallel);
  421. unsigned long propInt = 0;
  422. std::string const& prop =
  423. this->GenTarget->GetSafeProperty("AUTOGEN_PARALLEL");
  424. if (prop.empty() || (prop == "AUTO")) {
  425. // Autodetect number of CPUs
  426. this->AutogenTarget.Parallel = GetParallelCPUCount();
  427. } else if (cmStrToULong(prop, &propInt) && propInt > 0 &&
  428. propInt <= std::numeric_limits<ParallelType>::max()) {
  429. this->AutogenTarget.Parallel = static_cast<ParallelType>(propInt);
  430. } else {
  431. // Warn the project author that AUTOGEN_PARALLEL is not valid.
  432. this->Makefile->IssueMessage(
  433. MessageType::AUTHOR_WARNING,
  434. cmStrCat("AUTOGEN_PARALLEL=\"", prop, "\" for target \"",
  435. this->GenTarget->GetName(),
  436. "\" is not valid. Using AUTOGEN_PARALLEL=1"));
  437. this->AutogenTarget.Parallel = 1;
  438. }
  439. }
  440. // Autogen target info and settings files
  441. {
  442. // Info file
  443. this->AutogenTarget.InfoFile =
  444. cmStrCat(this->Dir.Info, "/AutogenInfo.json");
  445. // Used settings file
  446. this->ConfigFileNames(this->AutogenTarget.SettingsFile,
  447. cmStrCat(this->Dir.Info, "/AutogenUsed"), ".txt");
  448. this->ConfigFileClean(this->AutogenTarget.SettingsFile);
  449. // Parse cache file
  450. this->ConfigFileNames(this->AutogenTarget.ParseCacheFile,
  451. cmStrCat(this->Dir.Info, "/ParseCache"), ".txt");
  452. this->ConfigFileClean(this->AutogenTarget.ParseCacheFile);
  453. }
  454. // Autogen target: Compute user defined dependencies
  455. {
  456. this->AutogenTarget.DependOrigin =
  457. this->GenTarget->GetPropertyAsBool("AUTOGEN_ORIGIN_DEPENDS");
  458. std::string const& deps =
  459. this->GenTarget->GetSafeProperty("AUTOGEN_TARGET_DEPENDS");
  460. if (!deps.empty()) {
  461. for (auto const& depName : cmList{ deps }) {
  462. // Allow target and file dependencies
  463. auto* depTarget = this->Makefile->FindTargetToUse(depName);
  464. if (depTarget) {
  465. this->AutogenTarget.DependTargets.insert(depTarget);
  466. } else {
  467. this->AutogenTarget.DependFiles.insert(depName);
  468. }
  469. }
  470. }
  471. }
  472. if (this->Moc.Enabled) {
  473. // Path prefix
  474. if (cmIsOn(this->GenTarget->GetProperty("AUTOMOC_PATH_PREFIX"))) {
  475. this->Moc.PathPrefix = true;
  476. }
  477. // CMAKE_AUTOMOC_RELAXED_MODE
  478. if (this->Makefile->IsOn("CMAKE_AUTOMOC_RELAXED_MODE")) {
  479. this->Moc.RelaxedMode = true;
  480. this->Makefile->IssueMessage(
  481. MessageType::AUTHOR_WARNING,
  482. cmStrCat("AUTOMOC: CMAKE_AUTOMOC_RELAXED_MODE is "
  483. "deprecated an will be removed in the future. Consider "
  484. "disabling it and converting the target ",
  485. this->GenTarget->GetName(), " to regular mode."));
  486. }
  487. // Options
  488. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MOC_OPTIONS"),
  489. this->Moc.Options);
  490. // Filters
  491. cmExpandList(this->GenTarget->GetSafeProperty("AUTOMOC_MACRO_NAMES"),
  492. this->Moc.MacroNames);
  493. this->Moc.MacroNames.erase(cmRemoveDuplicates(this->Moc.MacroNames),
  494. this->Moc.MacroNames.end());
  495. {
  496. cmList const filterList = { this->GenTarget->GetSafeProperty(
  497. "AUTOMOC_DEPEND_FILTERS") };
  498. if ((filterList.size() % 2) != 0) {
  499. cmSystemTools::Error(
  500. cmStrCat("AutoMoc: AUTOMOC_DEPEND_FILTERS predefs size ",
  501. filterList.size(), " is not a multiple of 2."));
  502. return false;
  503. }
  504. this->Moc.DependFilters.reserve(1 + (filterList.size() / 2));
  505. this->Moc.DependFilters.emplace_back(
  506. "Q_PLUGIN_METADATA",
  507. "[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
  508. "[^\\)]*FILE[ \t]*\"([^\"]+)\"");
  509. for (cmList::size_type ii = 0; ii != filterList.size(); ii += 2) {
  510. this->Moc.DependFilters.emplace_back(filterList[ii],
  511. filterList[ii + 1]);
  512. }
  513. }
  514. }
  515. }
  516. // Init rcc specific settings
  517. if (this->Rcc.Enabled && !this->InitRcc()) {
  518. return false;
  519. }
  520. // Add autogen include directory to the origin target INCLUDE_DIRECTORIES
  521. if (this->MocOrUicEnabled() || (this->Rcc.Enabled && this->MultiConfig)) {
  522. auto addBefore = false;
  523. auto const& value =
  524. this->GenTarget->GetProperty("AUTOGEN_USE_SYSTEM_INCLUDE");
  525. if (value.IsSet()) {
  526. if (cmIsOn(value)) {
  527. this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp,
  528. "CXX");
  529. } else {
  530. addBefore = true;
  531. }
  532. } else {
  533. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0151)) {
  534. case cmPolicies::WARN:
  535. case cmPolicies::OLD:
  536. addBefore = true;
  537. break;
  538. case cmPolicies::REQUIRED_IF_USED:
  539. case cmPolicies::REQUIRED_ALWAYS:
  540. case cmPolicies::NEW:
  541. this->GenTarget->AddSystemIncludeDirectory(this->Dir.IncludeGenExp,
  542. "CXX");
  543. break;
  544. }
  545. }
  546. this->GenTarget->AddIncludeDirectory(this->Dir.IncludeGenExp, addBefore);
  547. }
  548. // Scan files
  549. if (!this->InitScanFiles()) {
  550. return false;
  551. }
  552. // Create autogen target
  553. if (this->MocOrUicEnabled() && !this->InitAutogenTarget()) {
  554. return false;
  555. }
  556. // Create rcc targets
  557. if (this->Rcc.Enabled && !this->InitRccTargets()) {
  558. return false;
  559. }
  560. return true;
  561. }
  562. bool cmQtAutoGenInitializer::InitMoc()
  563. {
  564. // Mocs compilation file
  565. if (this->GlobalGen->IsXcode()) {
  566. // XXX(xcode-per-cfg-src): Drop this Xcode-specific code path
  567. // when the Xcode generator supports per-config sources.
  568. this->Moc.CompilationFile.Default =
  569. cmStrCat(this->Dir.Build, "/mocs_compilation.cpp");
  570. this->Moc.CompilationFileGenex = this->Moc.CompilationFile.Default;
  571. } else {
  572. this->ConfigFileNamesAndGenex(
  573. this->Moc.CompilationFile, this->Moc.CompilationFileGenex,
  574. cmStrCat(this->Dir.Build, "/mocs_compilation"_s), ".cpp"_s);
  575. }
  576. // Moc predefs
  577. if (this->GenTarget->GetPropertyAsBool("AUTOMOC_COMPILER_PREDEFINES") &&
  578. (this->QtVersion >= IntegerVersion(5, 8))) {
  579. // Command
  580. cmList::assign(
  581. this->Moc.PredefsCmd,
  582. this->Makefile->GetDefinition("CMAKE_CXX_COMPILER_PREDEFINES_COMMAND"));
  583. // Header
  584. if (!this->Moc.PredefsCmd.empty()) {
  585. this->ConfigFileNames(this->Moc.PredefsFile,
  586. cmStrCat(this->Dir.Build, "/moc_predefs"), ".h");
  587. }
  588. }
  589. // Moc includes
  590. {
  591. SearchPathSanitizer const sanitizer(this->Makefile);
  592. auto getDirs =
  593. [this, &sanitizer](std::string const& cfg) -> std::vector<std::string> {
  594. // Get the include dirs for this target, without stripping the implicit
  595. // include dirs off, see issue #13667.
  596. std::vector<std::string> dirs;
  597. bool const appendImplicit = (this->QtVersion.Major >= 5);
  598. this->LocalGen->GetIncludeDirectoriesImplicit(
  599. dirs, this->GenTarget, "CXX", cfg, false, appendImplicit);
  600. return sanitizer(dirs);
  601. };
  602. // Other configuration settings
  603. if (this->MultiConfig) {
  604. for (std::string const& cfg : this->ConfigsList) {
  605. std::vector<std::string> dirs = getDirs(cfg);
  606. if (dirs == this->Moc.Includes.Default) {
  607. continue;
  608. }
  609. this->Moc.Includes.Config[cfg] = std::move(dirs);
  610. }
  611. } else {
  612. // Default configuration include directories
  613. this->Moc.Includes.Default = getDirs(this->ConfigDefault);
  614. }
  615. }
  616. // Moc compile definitions
  617. {
  618. auto getDefs = [this](std::string const& cfg) -> std::set<std::string> {
  619. std::set<std::string> defines;
  620. this->LocalGen->GetTargetDefines(this->GenTarget, cfg, "CXX", defines);
  621. if (this->Moc.PredefsCmd.empty() &&
  622. this->Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") ==
  623. "Windows") {
  624. // Add WIN32 definition if we don't have a moc_predefs.h
  625. defines.insert("WIN32");
  626. }
  627. return defines;
  628. };
  629. // Other configuration defines
  630. if (this->MultiConfig) {
  631. for (std::string const& cfg : this->ConfigsList) {
  632. std::set<std::string> defines = getDefs(cfg);
  633. if (defines == this->Moc.Defines.Default) {
  634. continue;
  635. }
  636. this->Moc.Defines.Config[cfg] = std::move(defines);
  637. }
  638. } else {
  639. // Default configuration defines
  640. this->Moc.Defines.Default = getDefs(this->ConfigDefault);
  641. }
  642. }
  643. // Moc executable
  644. {
  645. if (!this->GetQtExecutable(this->Moc, "moc", false)) {
  646. return false;
  647. }
  648. // Let the _autogen target depend on the moc executable
  649. if (this->Moc.ExecutableTarget) {
  650. this->AutogenTarget.DependTargets.insert(
  651. this->Moc.ExecutableTarget->Target);
  652. }
  653. }
  654. return true;
  655. }
  656. bool cmQtAutoGenInitializer::InitUic()
  657. {
  658. // Uic search paths
  659. {
  660. std::string const& usp =
  661. this->GenTarget->GetSafeProperty("AUTOUIC_SEARCH_PATHS");
  662. if (!usp.empty()) {
  663. this->Uic.SearchPaths =
  664. SearchPathSanitizer(this->Makefile)(cmList{ usp });
  665. }
  666. }
  667. // Uic target options
  668. {
  669. auto getOpts = [this](std::string const& cfg) -> std::vector<std::string> {
  670. std::vector<std::string> opts;
  671. this->GenTarget->GetAutoUicOptions(opts, cfg);
  672. return opts;
  673. };
  674. // Default options
  675. this->Uic.Options.Default = getOpts(this->ConfigDefault);
  676. // Configuration specific options
  677. if (this->MultiConfig) {
  678. for (std::string const& cfg : this->ConfigsList) {
  679. std::vector<std::string> options = getOpts(cfg);
  680. if (options == this->Uic.Options.Default) {
  681. continue;
  682. }
  683. this->Uic.Options.Config[cfg] = std::move(options);
  684. }
  685. }
  686. }
  687. // Uic executable
  688. {
  689. if (!this->GetQtExecutable(this->Uic, "uic", true)) {
  690. return false;
  691. }
  692. // Let the _autogen target depend on the uic executable
  693. if (this->Uic.ExecutableTarget) {
  694. this->AutogenTarget.DependTargets.insert(
  695. this->Uic.ExecutableTarget->Target);
  696. }
  697. }
  698. return true;
  699. }
  700. bool cmQtAutoGenInitializer::InitRcc()
  701. {
  702. // Rcc executable
  703. {
  704. if (!this->GetQtExecutable(this->Rcc, "rcc", false)) {
  705. return false;
  706. }
  707. // Evaluate test output on demand
  708. CompilerFeatures& features = *this->Rcc.ExecutableFeatures;
  709. if (!features.Evaluated) {
  710. // Look for list options
  711. if (this->QtVersion.Major == 5 || this->QtVersion.Major == 6) {
  712. if (features.HelpOutput.find("--list") != std::string::npos) {
  713. features.ListOptions.emplace_back("--list");
  714. } else if (features.HelpOutput.find("-list") != std::string::npos) {
  715. features.ListOptions.emplace_back("-list");
  716. }
  717. }
  718. // Evaluation finished
  719. features.Evaluated = true;
  720. }
  721. }
  722. return true;
  723. }
  724. bool cmQtAutoGenInitializer::InitScanFiles()
  725. {
  726. cmake const* cm = this->Makefile->GetCMakeInstance();
  727. auto const& kw = this->GlobalInitializer->kw();
  728. auto makeMUFile = [this, &kw](cmSourceFile* sf, std::string const& fullPath,
  729. std::vector<size_t> const& configs,
  730. bool muIt) -> MUFileHandle {
  731. MUFileHandle muf = cm::make_unique<MUFile>();
  732. muf->FullPath = fullPath;
  733. muf->SF = sf;
  734. if (!configs.empty() && configs.size() != this->ConfigsList.size()) {
  735. muf->Configs = configs;
  736. }
  737. muf->Generated = sf->GetIsGenerated();
  738. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  739. muf->SkipMoc = this->Moc.Enabled &&
  740. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOMOC));
  741. muf->SkipUic = this->Uic.Enabled &&
  742. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  743. if (muIt) {
  744. muf->MocIt = this->Moc.Enabled && !muf->SkipMoc;
  745. muf->UicIt = this->Uic.Enabled && !muf->SkipUic;
  746. }
  747. return muf;
  748. };
  749. auto addMUHeader = [this](MUFileHandle&& muf, cm::string_view extension) {
  750. cmSourceFile* sf = muf->SF;
  751. const bool muIt = (muf->MocIt || muf->UicIt);
  752. if (this->CMP0100Accept || (extension != "hh")) {
  753. // Accept
  754. if (muIt && muf->Generated) {
  755. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  756. }
  757. this->AutogenTarget.Headers.emplace(sf, std::move(muf));
  758. } else if (muIt && this->CMP0100Warn) {
  759. // Store file for warning message
  760. this->AutogenTarget.CMP0100HeadersWarn.push_back(sf);
  761. }
  762. };
  763. auto addMUSource = [this](MUFileHandle&& muf) {
  764. if ((muf->MocIt || muf->UicIt) && muf->Generated) {
  765. this->AutogenTarget.FilesGenerated.emplace_back(muf.get());
  766. }
  767. this->AutogenTarget.Sources.emplace(muf->SF, std::move(muf));
  768. };
  769. // Scan through target files
  770. {
  771. // Scan through target files
  772. for (cmGeneratorTarget::AllConfigSource const& acs :
  773. this->GenTarget->GetAllConfigSources()) {
  774. std::string const& fullPath = acs.Source->GetFullPath();
  775. std::string const& extLower =
  776. cmSystemTools::LowerCase(acs.Source->GetExtension());
  777. // Register files that will be scanned by moc or uic
  778. if (this->MocOrUicEnabled()) {
  779. if (cm->IsAHeaderExtension(extLower)) {
  780. addMUHeader(makeMUFile(acs.Source, fullPath, acs.Configs, true),
  781. extLower);
  782. } else if (cm->IsACLikeSourceExtension(extLower)) {
  783. addMUSource(makeMUFile(acs.Source, fullPath, acs.Configs, true));
  784. }
  785. }
  786. // Register rcc enabled files
  787. if (this->Rcc.Enabled) {
  788. if ((extLower == kw.qrc) &&
  789. !acs.Source->GetPropertyAsBool(kw.SKIP_AUTOGEN) &&
  790. !acs.Source->GetPropertyAsBool(kw.SKIP_AUTORCC)) {
  791. // Register qrc file
  792. Qrc qrc;
  793. qrc.QrcFile = fullPath;
  794. qrc.QrcName =
  795. cmSystemTools::GetFilenameWithoutLastExtension(qrc.QrcFile);
  796. qrc.Generated = acs.Source->GetIsGenerated();
  797. // RCC options
  798. {
  799. std::string const& opts =
  800. acs.Source->GetSafeProperty(kw.AUTORCC_OPTIONS);
  801. if (!opts.empty()) {
  802. cmExpandList(opts, qrc.Options);
  803. }
  804. }
  805. this->Rcc.Qrcs.push_back(std::move(qrc));
  806. }
  807. }
  808. }
  809. }
  810. // cmGeneratorTarget::GetAllConfigSources computes the target's
  811. // sources meta data cache. Clear it so that OBJECT library targets that
  812. // are AUTOGEN initialized after this target get their added
  813. // mocs_compilation.cpp source acknowledged by this target.
  814. this->GenTarget->ClearSourcesCache();
  815. // For source files find additional headers and private headers
  816. if (this->MocOrUicEnabled()) {
  817. // Header search suffixes and extensions
  818. static std::initializer_list<cm::string_view> const suffixes{ "", "_p" };
  819. auto const& exts = cm->GetHeaderExtensions();
  820. // Scan through sources
  821. for (auto const& pair : this->AutogenTarget.Sources) {
  822. MUFile const& muf = *pair.second;
  823. if (muf.MocIt || muf.UicIt) {
  824. // Search for the default header file and a private header
  825. std::string const& srcFullPath = muf.SF->ResolveFullPath();
  826. std::string const basePath = cmStrCat(
  827. cmQtAutoGen::SubDirPrefix(srcFullPath),
  828. cmSystemTools::GetFilenameWithoutLastExtension(srcFullPath));
  829. for (auto const& suffix : suffixes) {
  830. std::string const suffixedPath = cmStrCat(basePath, suffix);
  831. for (auto const& ext : exts) {
  832. std::string const fullPath = cmStrCat(suffixedPath, '.', ext);
  833. auto constexpr locationKind = cmSourceFileLocationKind::Known;
  834. cmSourceFile* sf =
  835. this->Makefile->GetSource(fullPath, locationKind);
  836. if (sf) {
  837. // Check if we know about this header already
  838. if (cm::contains(this->AutogenTarget.Headers, sf)) {
  839. continue;
  840. }
  841. // We only accept not-GENERATED files that do exist.
  842. if (!sf->GetIsGenerated() &&
  843. !cmSystemTools::FileExists(fullPath)) {
  844. continue;
  845. }
  846. } else if (cmSystemTools::FileExists(fullPath)) {
  847. // Create a new source file for the existing file
  848. sf = this->Makefile->CreateSource(fullPath, false, locationKind);
  849. }
  850. if (sf) {
  851. auto eMuf = makeMUFile(sf, fullPath, muf.Configs, true);
  852. // Only process moc/uic when the parent is processed as well
  853. if (!muf.MocIt) {
  854. eMuf->MocIt = false;
  855. }
  856. if (!muf.UicIt) {
  857. eMuf->UicIt = false;
  858. }
  859. addMUHeader(std::move(eMuf), ext);
  860. }
  861. }
  862. }
  863. }
  864. }
  865. }
  866. // Scan through all source files in the makefile to extract moc and uic
  867. // parameters. Historically we support non target source file parameters.
  868. // The reason is that their file names might be discovered from source files
  869. // at generation time.
  870. if (this->MocOrUicEnabled()) {
  871. for (const auto& sf : this->Makefile->GetSourceFiles()) {
  872. // sf->GetExtension() is only valid after sf->ResolveFullPath() ...
  873. // Since we're iterating over source files that might be not in the
  874. // target we need to check for path errors (not existing files).
  875. std::string pathError;
  876. std::string const& fullPath = sf->ResolveFullPath(&pathError);
  877. if (!pathError.empty() || fullPath.empty()) {
  878. continue;
  879. }
  880. std::string const& extLower =
  881. cmSystemTools::LowerCase(sf->GetExtension());
  882. if (cm->IsAHeaderExtension(extLower)) {
  883. if (!cm::contains(this->AutogenTarget.Headers, sf.get())) {
  884. auto muf = makeMUFile(sf.get(), fullPath, {}, false);
  885. if (muf->SkipMoc || muf->SkipUic) {
  886. addMUHeader(std::move(muf), extLower);
  887. }
  888. }
  889. } else if (cm->IsACLikeSourceExtension(extLower)) {
  890. if (!cm::contains(this->AutogenTarget.Sources, sf.get())) {
  891. auto muf = makeMUFile(sf.get(), fullPath, {}, false);
  892. if (muf->SkipMoc || muf->SkipUic) {
  893. addMUSource(std::move(muf));
  894. }
  895. }
  896. } else if (this->Uic.Enabled && (extLower == kw.ui)) {
  897. // .ui file
  898. bool const skipAutogen = sf->GetPropertyAsBool(kw.SKIP_AUTOGEN);
  899. bool const skipUic =
  900. (skipAutogen || sf->GetPropertyAsBool(kw.SKIP_AUTOUIC));
  901. if (!skipUic) {
  902. // Check if the .ui file has uic options
  903. std::string const uicOpts = sf->GetSafeProperty(kw.AUTOUIC_OPTIONS);
  904. if (uicOpts.empty()) {
  905. this->Uic.UiFilesNoOptions.emplace_back(fullPath);
  906. } else {
  907. this->Uic.UiFilesWithOptions.emplace_back(
  908. fullPath, std::move(cmList{ uicOpts }.data()));
  909. }
  910. auto uiHeaderRelativePath = cmSystemTools::RelativePath(
  911. this->LocalGen->GetCurrentSourceDirectory(),
  912. cmSystemTools::GetFilenamePath(fullPath));
  913. // Avoid creating a path containing adjacent slashes
  914. if (!uiHeaderRelativePath.empty() &&
  915. uiHeaderRelativePath.back() != '/') {
  916. uiHeaderRelativePath += '/';
  917. }
  918. auto uiHeaderFilePath = cmStrCat(
  919. '/', uiHeaderRelativePath, "ui_"_s,
  920. cmSystemTools::GetFilenameWithoutLastExtension(fullPath), ".h"_s);
  921. ConfigString uiHeader;
  922. std::string uiHeaderGenex;
  923. this->ConfigFileNamesAndGenex(
  924. uiHeader, uiHeaderGenex, cmStrCat(this->Dir.Build, "/include"_s),
  925. uiHeaderFilePath);
  926. this->Uic.UiHeaders.emplace_back(uiHeader, uiHeaderGenex);
  927. } else {
  928. // Register skipped .ui file
  929. this->Uic.SkipUi.insert(fullPath);
  930. }
  931. }
  932. }
  933. }
  934. // Process GENERATED sources and headers
  935. if (this->MocOrUicEnabled() && !this->AutogenTarget.FilesGenerated.empty()) {
  936. if (this->CMP0071Accept) {
  937. // Let the autogen target depend on the GENERATED files
  938. if (this->MultiConfig && !this->CrossConfig) {
  939. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  940. if (muf->Configs.empty()) {
  941. this->AutogenTarget.DependFiles.insert(muf->FullPath);
  942. } else {
  943. for (size_t ci : muf->Configs) {
  944. std::string const& config = this->ConfigsList[ci];
  945. std::string const& pathWithConfig =
  946. cmStrCat("$<$<CONFIG:", config, ">:", muf->FullPath, '>');
  947. this->AutogenTarget.DependFiles.insert(pathWithConfig);
  948. }
  949. }
  950. }
  951. } else {
  952. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  953. this->AutogenTarget.DependFiles.insert(muf->FullPath);
  954. }
  955. }
  956. } else if (this->CMP0071Warn) {
  957. cm::string_view property;
  958. if (this->Moc.Enabled && this->Uic.Enabled) {
  959. property = "SKIP_AUTOGEN";
  960. } else if (this->Moc.Enabled) {
  961. property = "SKIP_AUTOMOC";
  962. } else if (this->Uic.Enabled) {
  963. property = "SKIP_AUTOUIC";
  964. }
  965. std::string files;
  966. for (MUFile const* muf : this->AutogenTarget.FilesGenerated) {
  967. files += cmStrCat(" ", Quoted(muf->FullPath), '\n');
  968. }
  969. this->Makefile->IssueMessage(
  970. MessageType::AUTHOR_WARNING,
  971. cmStrCat(
  972. cmPolicies::GetPolicyWarning(cmPolicies::CMP0071), '\n',
  973. "For compatibility, CMake is excluding the GENERATED source "
  974. "file(s):\n",
  975. files, "from processing by ",
  976. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  977. ". If any of the files should be processed, set CMP0071 to NEW. "
  978. "If any of the files should not be processed, "
  979. "explicitly exclude them by setting the source file property ",
  980. property, ":\n set_property(SOURCE file.h PROPERTY ", property,
  981. " ON)\n"));
  982. }
  983. }
  984. // Generate CMP0100 warning
  985. if (this->MocOrUicEnabled() &&
  986. !this->AutogenTarget.CMP0100HeadersWarn.empty()) {
  987. cm::string_view property;
  988. if (this->Moc.Enabled && this->Uic.Enabled) {
  989. property = "SKIP_AUTOGEN";
  990. } else if (this->Moc.Enabled) {
  991. property = "SKIP_AUTOMOC";
  992. } else if (this->Uic.Enabled) {
  993. property = "SKIP_AUTOUIC";
  994. }
  995. std::string files;
  996. for (cmSourceFile const* sf : this->AutogenTarget.CMP0100HeadersWarn) {
  997. files += cmStrCat(" ", Quoted(sf->GetFullPath()), '\n');
  998. }
  999. this->Makefile->IssueMessage(
  1000. MessageType::AUTHOR_WARNING,
  1001. cmStrCat(
  1002. cmPolicies::GetPolicyWarning(cmPolicies::CMP0100), '\n',
  1003. "For compatibility, CMake is excluding the header file(s):\n", files,
  1004. "from processing by ",
  1005. cmQtAutoGen::Tools(this->Moc.Enabled, this->Uic.Enabled, false),
  1006. ". If any of the files should be processed, set CMP0100 to NEW. "
  1007. "If any of the files should not be processed, "
  1008. "explicitly exclude them by setting the source file property ",
  1009. property, ":\n set_property(SOURCE file.hh PROPERTY ", property,
  1010. " ON)\n"));
  1011. }
  1012. // Process qrc files
  1013. if (!this->Rcc.Qrcs.empty()) {
  1014. const bool modernQt = (this->QtVersion.Major >= 5);
  1015. // Target rcc options
  1016. cmList const optionsTarget{ this->GenTarget->GetSafeProperty(
  1017. kw.AUTORCC_OPTIONS) };
  1018. // Check if file name is unique
  1019. for (Qrc& qrc : this->Rcc.Qrcs) {
  1020. qrc.Unique = true;
  1021. for (Qrc const& qrc2 : this->Rcc.Qrcs) {
  1022. if ((&qrc != &qrc2) && (qrc.QrcName == qrc2.QrcName)) {
  1023. qrc.Unique = false;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. // Path checksum and file names
  1029. for (Qrc& qrc : this->Rcc.Qrcs) {
  1030. // Path checksum
  1031. qrc.QrcPathChecksum = this->PathCheckSum.getPart(qrc.QrcFile);
  1032. // Output file name
  1033. qrc.OutputFile = cmStrCat(this->Dir.Build, '/', qrc.QrcPathChecksum,
  1034. "/qrc_", qrc.QrcName, ".cpp");
  1035. std::string const base = cmStrCat(this->Dir.Info, "/AutoRcc_",
  1036. qrc.QrcName, '_', qrc.QrcPathChecksum);
  1037. qrc.LockFile = cmStrCat(base, "_Lock.lock");
  1038. qrc.InfoFile = cmStrCat(base, "_Info.json");
  1039. this->ConfigFileNames(qrc.SettingsFile, cmStrCat(base, "_Used"), ".txt");
  1040. }
  1041. // rcc options
  1042. for (Qrc& qrc : this->Rcc.Qrcs) {
  1043. // Target options
  1044. std::vector<std::string> opts = optionsTarget;
  1045. // Merge computed "-name XYZ" option
  1046. {
  1047. std::string name = qrc.QrcName;
  1048. // Replace '-' with '_'. The former is not valid for symbol names.
  1049. std::replace(name.begin(), name.end(), '-', '_');
  1050. if (!qrc.Unique) {
  1051. name += cmStrCat('_', qrc.QrcPathChecksum);
  1052. }
  1053. std::vector<std::string> nameOpts;
  1054. nameOpts.emplace_back("-name");
  1055. nameOpts.emplace_back(std::move(name));
  1056. RccMergeOptions(opts, nameOpts, modernQt);
  1057. }
  1058. // Merge file option
  1059. RccMergeOptions(opts, qrc.Options, modernQt);
  1060. qrc.Options = std::move(opts);
  1061. }
  1062. // rcc resources
  1063. for (Qrc& qrc : this->Rcc.Qrcs) {
  1064. if (!qrc.Generated) {
  1065. std::string error;
  1066. RccLister const lister(this->Rcc.Executable,
  1067. this->Rcc.ExecutableFeatures->ListOptions);
  1068. if (!lister.list(qrc.QrcFile, qrc.Resources, error)) {
  1069. cmSystemTools::Error(error);
  1070. return false;
  1071. }
  1072. }
  1073. }
  1074. }
  1075. return true;
  1076. }
  1077. bool cmQtAutoGenInitializer::InitAutogenTarget()
  1078. {
  1079. // Register info file as generated by CMake
  1080. this->Makefile->AddCMakeOutputFile(this->AutogenTarget.InfoFile);
  1081. // Determine whether to use a depfile for the AUTOGEN target.
  1082. bool const useDepfile = [this]() -> bool {
  1083. auto const& gen = this->GlobalGen->GetName();
  1084. return this->QtVersion >= IntegerVersion(5, 15) &&
  1085. (gen.find("Ninja") != std::string::npos ||
  1086. gen.find("Make") != std::string::npos);
  1087. }();
  1088. // Files provided by the autogen target
  1089. std::vector<std::string> autogenByproducts;
  1090. std::vector<std::string> timestampByproducts;
  1091. if (this->Moc.Enabled) {
  1092. this->AddGeneratedSource(this->Moc.CompilationFile, this->Moc, true);
  1093. if (useDepfile) {
  1094. if (this->MultiConfig && this->CrossConfig &&
  1095. this->GlobalGen->GetName().find("Ninja") != std::string::npos) {
  1096. // Make all mocs_compilation_<CONFIG>.cpp files byproducts of the
  1097. // ${target}_autogen/timestamp custom command.
  1098. // We cannot just use Moc.CompilationFileGenex here, because that
  1099. // custom command runs cmake_autogen for each configuration.
  1100. for (const auto& p : this->Moc.CompilationFile.Config) {
  1101. timestampByproducts.push_back(p.second);
  1102. }
  1103. } else {
  1104. timestampByproducts.push_back(this->Moc.CompilationFileGenex);
  1105. }
  1106. } else {
  1107. autogenByproducts.push_back(this->Moc.CompilationFileGenex);
  1108. }
  1109. }
  1110. if (this->Uic.Enabled) {
  1111. for (const auto& file : this->Uic.UiHeaders) {
  1112. this->AddGeneratedSource(file.first, this->Uic);
  1113. autogenByproducts.push_back(file.second);
  1114. }
  1115. }
  1116. // Compose target comment
  1117. std::string autogenComment;
  1118. {
  1119. std::string tools;
  1120. if (this->Moc.Enabled) {
  1121. tools += "MOC";
  1122. }
  1123. if (this->Uic.Enabled) {
  1124. if (!tools.empty()) {
  1125. tools += " and ";
  1126. }
  1127. tools += "UIC";
  1128. }
  1129. autogenComment = cmStrCat("Automatic ", tools, " for target ",
  1130. this->GenTarget->GetName());
  1131. }
  1132. // Compose command lines
  1133. // FIXME: Take advantage of our per-config mocs_compilation_$<CONFIG>.cpp
  1134. // instead of fiddling with the include directories
  1135. std::vector<std::string> configs;
  1136. this->GlobalGen->GetQtAutoGenConfigs(configs);
  1137. bool constexpr stdPipesUTF8 = true;
  1138. cmCustomCommandLines commandLines;
  1139. if (!this->CrossConfig) {
  1140. std::string autogenInfoFileConfig;
  1141. if (this->MultiConfig) {
  1142. autogenInfoFileConfig = "$<CONFIG>";
  1143. } else {
  1144. autogenInfoFileConfig = configs[0];
  1145. }
  1146. commandLines.push_back(cmMakeCommandLine(
  1147. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_autogen",
  1148. this->AutogenTarget.InfoFile, autogenInfoFileConfig }));
  1149. } else {
  1150. for (auto const& config : configs) {
  1151. commandLines.push_back(cmMakeCommandLine(
  1152. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_autogen",
  1153. this->AutogenTarget.InfoFile, config }));
  1154. }
  1155. }
  1156. // Use PRE_BUILD on demand
  1157. bool usePRE_BUILD = false;
  1158. if (this->GlobalGen->GetName().find("Visual Studio") != std::string::npos) {
  1159. // Under VS use a PRE_BUILD event instead of a separate target to
  1160. // reduce the number of targets loaded into the IDE.
  1161. // This also works around a VS 11 bug that may skip updating the target:
  1162. // https://connect.microsoft.com/VisualStudio/feedback/details/769495
  1163. usePRE_BUILD = true;
  1164. }
  1165. // Disable PRE_BUILD in some cases
  1166. if (usePRE_BUILD) {
  1167. // Cannot use PRE_BUILD with file depends
  1168. if (!this->AutogenTarget.DependFiles.empty()) {
  1169. usePRE_BUILD = false;
  1170. }
  1171. // Cannot use PRE_BUILD when a global autogen target is in place
  1172. if (this->AutogenTarget.GlobalTarget) {
  1173. usePRE_BUILD = false;
  1174. }
  1175. }
  1176. // Create the autogen target/command
  1177. if (usePRE_BUILD) {
  1178. // Add additional autogen target dependencies to origin target
  1179. for (cmTarget const* depTarget : this->AutogenTarget.DependTargets) {
  1180. this->GenTarget->Target->AddUtility(depTarget->GetName(), false,
  1181. this->Makefile);
  1182. }
  1183. if (!this->Uic.UiFilesNoOptions.empty() ||
  1184. !this->Uic.UiFilesWithOptions.empty()) {
  1185. // Add a generated timestamp file
  1186. ConfigString timestampFile;
  1187. std::string timestampFileGenex;
  1188. ConfigFileNamesAndGenex(timestampFile, timestampFileGenex,
  1189. cmStrCat(this->Dir.Build, "/autouic"_s),
  1190. ".stamp"_s);
  1191. this->AddGeneratedSource(timestampFile, this->Uic);
  1192. // Add a step in the pre-build command to touch the timestamp file
  1193. commandLines.push_back(
  1194. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E", "touch",
  1195. timestampFileGenex }));
  1196. // UIC needs to be re-run if any of the known UI files change or the
  1197. // executable itself has been updated
  1198. auto uicDependencies = this->Uic.UiFilesNoOptions;
  1199. for (auto const& uiFile : this->Uic.UiFilesWithOptions) {
  1200. uicDependencies.push_back(uiFile.first);
  1201. }
  1202. AddAutogenExecutableToDependencies(this->Uic, uicDependencies);
  1203. // Add a rule file to cause the target to build if a dependency has
  1204. // changed, which will trigger the pre-build command to run autogen
  1205. auto cc = cm::make_unique<cmCustomCommand>();
  1206. cc->SetOutputs(timestampFileGenex);
  1207. cc->SetDepends(uicDependencies);
  1208. cc->SetComment("");
  1209. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1210. cc->SetEscapeOldStyle(false);
  1211. cc->SetStdPipesUTF8(stdPipesUTF8);
  1212. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1213. }
  1214. // Add the pre-build command directly to bypass the OBJECT_LIBRARY
  1215. // rejection in cmMakefile::AddCustomCommandToTarget because we know
  1216. // PRE_BUILD will work for an OBJECT_LIBRARY in this specific case.
  1217. //
  1218. // PRE_BUILD does not support file dependencies!
  1219. cmCustomCommand cc;
  1220. cc.SetByproducts(autogenByproducts);
  1221. cc.SetCommandLines(commandLines);
  1222. cc.SetComment(autogenComment.c_str());
  1223. cc.SetBacktrace(this->Makefile->GetBacktrace());
  1224. cc.SetWorkingDirectory(this->Dir.Work.c_str());
  1225. cc.SetStdPipesUTF8(stdPipesUTF8);
  1226. cc.SetEscapeOldStyle(false);
  1227. cc.SetEscapeAllowMakeVars(true);
  1228. this->GenTarget->Target->AddPreBuildCommand(std::move(cc));
  1229. } else {
  1230. // Add link library target dependencies to the autogen target
  1231. // dependencies
  1232. if (this->AutogenTarget.DependOrigin) {
  1233. // add_dependencies/addUtility do not support generator expressions.
  1234. // We depend only on the libraries found in all configs therefore.
  1235. std::map<cmGeneratorTarget const*, std::size_t> commonTargets;
  1236. for (std::string const& config : this->ConfigsList) {
  1237. cmLinkImplementationLibraries const* libs =
  1238. this->GenTarget->GetLinkImplementationLibraries(
  1239. config, cmGeneratorTarget::LinkInterfaceFor::Link);
  1240. if (libs) {
  1241. for (cmLinkItem const& item : libs->Libraries) {
  1242. cmGeneratorTarget const* libTarget = item.Target;
  1243. if (libTarget &&
  1244. !StaticLibraryCycle(this->GenTarget, libTarget, config)) {
  1245. // Increment target config count
  1246. commonTargets[libTarget]++;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. for (auto const& item : commonTargets) {
  1252. if (item.second == this->ConfigsList.size()) {
  1253. this->AutogenTarget.DependTargets.insert(item.first->Target);
  1254. }
  1255. }
  1256. }
  1257. cmTarget* timestampTarget = nullptr;
  1258. std::vector<std::string> dependencies(
  1259. this->AutogenTarget.DependFiles.begin(),
  1260. this->AutogenTarget.DependFiles.end());
  1261. if (useDepfile) {
  1262. // Create a custom command that generates a timestamp file and
  1263. // has a depfile assigned. The depfile is created by JobDepFilesMergeT.
  1264. //
  1265. // Also create an additional '_autogen_timestamp_deps' that the custom
  1266. // command will depend on. It will have no sources or commands to
  1267. // execute, but it will have dependencies that would originally be
  1268. // assigned to the pre-Qt 5.15 'autogen' target. These dependencies will
  1269. // serve as a list of order-only dependencies for the custom command,
  1270. // without forcing the custom command to re-execute.
  1271. //
  1272. // The dependency tree would then look like
  1273. // '_autogen_timestamp_deps (order-only)' <- '/timestamp' file <-
  1274. // '_autogen' target.
  1275. const auto timestampTargetName =
  1276. cmStrCat(this->GenTarget->GetName(), "_autogen_timestamp_deps");
  1277. auto cc = cm::make_unique<cmCustomCommand>();
  1278. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1279. cc->SetDepends(dependencies);
  1280. cc->SetEscapeOldStyle(false);
  1281. timestampTarget = this->LocalGen->AddUtilityCommand(timestampTargetName,
  1282. true, std::move(cc));
  1283. this->LocalGen->AddGeneratorTarget(
  1284. cm::make_unique<cmGeneratorTarget>(timestampTarget, this->LocalGen));
  1285. // Set FOLDER property on the timestamp target, so it appears in the
  1286. // appropriate folder in an IDE or in the file api.
  1287. if (!this->TargetsFolder.empty()) {
  1288. timestampTarget->SetProperty("FOLDER", this->TargetsFolder);
  1289. }
  1290. // Make '/timestamp' file depend on '_autogen_timestamp_deps' and on the
  1291. // moc and uic executables (whichever are enabled).
  1292. dependencies.clear();
  1293. dependencies.push_back(timestampTargetName);
  1294. AddAutogenExecutableToDependencies(this->Moc, dependencies);
  1295. AddAutogenExecutableToDependencies(this->Uic, dependencies);
  1296. // Create the custom command that outputs the timestamp file.
  1297. const char timestampFileName[] = "timestamp";
  1298. const std::string outputFile =
  1299. cmStrCat(this->Dir.Build, "/", timestampFileName);
  1300. this->AutogenTarget.DepFile = cmStrCat(this->Dir.Build, "/deps");
  1301. this->AutogenTarget.DepFileRuleName =
  1302. cmStrCat(this->Dir.RelativeBuild, "/", timestampFileName);
  1303. commandLines.push_back(cmMakeCommandLine(
  1304. { cmSystemTools::GetCMakeCommand(), "-E", "touch", outputFile }));
  1305. this->AddGeneratedSource(outputFile, this->Moc);
  1306. cc = cm::make_unique<cmCustomCommand>();
  1307. cc->SetOutputs(outputFile);
  1308. cc->SetByproducts(timestampByproducts);
  1309. cc->SetDepends(dependencies);
  1310. cc->SetCommandLines(commandLines);
  1311. cc->SetComment(autogenComment.c_str());
  1312. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1313. cc->SetEscapeOldStyle(false);
  1314. cc->SetDepfile(this->AutogenTarget.DepFile);
  1315. cc->SetStdPipesUTF8(stdPipesUTF8);
  1316. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1317. // Alter variables for the autogen target which now merely wraps the
  1318. // custom command
  1319. dependencies.clear();
  1320. dependencies.emplace_back(outputFile);
  1321. commandLines.clear();
  1322. autogenComment.clear();
  1323. }
  1324. // Create autogen target
  1325. auto cc = cm::make_unique<cmCustomCommand>();
  1326. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1327. cc->SetByproducts(autogenByproducts);
  1328. cc->SetDepends(dependencies);
  1329. cc->SetCommandLines(commandLines);
  1330. cc->SetEscapeOldStyle(false);
  1331. cc->SetComment(autogenComment.c_str());
  1332. cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
  1333. this->AutogenTarget.Name, true, std::move(cc));
  1334. // Create autogen generator target
  1335. this->LocalGen->AddGeneratorTarget(
  1336. cm::make_unique<cmGeneratorTarget>(autogenTarget, this->LocalGen));
  1337. // Order the autogen target(s) just before the original target.
  1338. cmTarget* orderTarget = timestampTarget ? timestampTarget : autogenTarget;
  1339. // Forward origin utilities to autogen target
  1340. if (this->AutogenTarget.DependOrigin) {
  1341. for (BT<std::pair<std::string, bool>> const& depName :
  1342. this->GenTarget->GetUtilities()) {
  1343. orderTarget->AddUtility(depName.Value.first, false, this->Makefile);
  1344. }
  1345. }
  1346. // Add additional autogen target dependencies to autogen target
  1347. for (cmTarget const* depTarget : this->AutogenTarget.DependTargets) {
  1348. orderTarget->AddUtility(depTarget->GetName(), false, this->Makefile);
  1349. }
  1350. // Set FOLDER property in autogen target
  1351. if (!this->TargetsFolder.empty()) {
  1352. autogenTarget->SetProperty("FOLDER", this->TargetsFolder);
  1353. }
  1354. // Add autogen target to the origin target dependencies
  1355. this->GenTarget->Target->AddUtility(this->AutogenTarget.Name, false,
  1356. this->Makefile);
  1357. // Add autogen target to the global autogen target dependencies
  1358. if (this->AutogenTarget.GlobalTarget) {
  1359. this->GlobalInitializer->AddToGlobalAutoGen(this->LocalGen,
  1360. this->AutogenTarget.Name);
  1361. }
  1362. }
  1363. return true;
  1364. }
  1365. bool cmQtAutoGenInitializer::InitRccTargets()
  1366. {
  1367. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1368. // Register info file as generated by CMake
  1369. this->Makefile->AddCMakeOutputFile(qrc.InfoFile);
  1370. // Register file at target
  1371. {
  1372. cmSourceFile* sf = this->AddGeneratedSource(qrc.OutputFile, this->Rcc);
  1373. sf->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "On");
  1374. }
  1375. std::vector<std::string> ccOutput;
  1376. ccOutput.push_back(qrc.OutputFile);
  1377. std::vector<std::string> ccDepends;
  1378. // Add the .qrc and info file to the custom command dependencies
  1379. ccDepends.push_back(qrc.QrcFile);
  1380. ccDepends.push_back(qrc.InfoFile);
  1381. cmCustomCommandLines commandLines;
  1382. if (this->MultiConfig) {
  1383. // Build for all configurations
  1384. for (std::string const& config : this->ConfigsList) {
  1385. commandLines.push_back(
  1386. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1387. "cmake_autorcc", qrc.InfoFile, config }));
  1388. }
  1389. } else {
  1390. commandLines.push_back(
  1391. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1392. "cmake_autorcc", qrc.InfoFile, "$<CONFIG>" }));
  1393. }
  1394. std::string const ccComment =
  1395. cmStrCat("Automatic RCC for ",
  1396. FileProjectRelativePath(this->Makefile, qrc.QrcFile));
  1397. auto cc = cm::make_unique<cmCustomCommand>();
  1398. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1399. cc->SetCommandLines(commandLines);
  1400. cc->SetComment(ccComment.c_str());
  1401. cc->SetStdPipesUTF8(true);
  1402. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1403. // Create custom rcc target
  1404. std::string ccName;
  1405. {
  1406. ccName = cmStrCat(this->GenTarget->GetName(), "_arcc_", qrc.QrcName);
  1407. if (!qrc.Unique) {
  1408. ccName += cmStrCat('_', qrc.QrcPathChecksum);
  1409. }
  1410. cc->SetByproducts(ccOutput);
  1411. cc->SetDepends(ccDepends);
  1412. cc->SetEscapeOldStyle(false);
  1413. cmTarget* autoRccTarget =
  1414. this->LocalGen->AddUtilityCommand(ccName, true, std::move(cc));
  1415. // Create autogen generator target
  1416. this->LocalGen->AddGeneratorTarget(
  1417. cm::make_unique<cmGeneratorTarget>(autoRccTarget, this->LocalGen));
  1418. // Set FOLDER property in autogen target
  1419. if (!this->TargetsFolder.empty()) {
  1420. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder);
  1421. }
  1422. if (!this->Rcc.ExecutableTargetName.empty()) {
  1423. autoRccTarget->AddUtility(this->Rcc.ExecutableTargetName, false,
  1424. this->Makefile);
  1425. }
  1426. }
  1427. // Add autogen target to the origin target dependencies
  1428. this->GenTarget->Target->AddUtility(ccName, false, this->Makefile);
  1429. // Add autogen target to the global autogen target dependencies
  1430. if (this->Rcc.GlobalTarget) {
  1431. this->GlobalInitializer->AddToGlobalAutoRcc(this->LocalGen, ccName);
  1432. }
  1433. } else {
  1434. // Create custom rcc command
  1435. {
  1436. // Add the resource files to the dependencies
  1437. for (std::string const& fileName : qrc.Resources) {
  1438. // Add resource file to the custom command dependencies
  1439. ccDepends.push_back(fileName);
  1440. }
  1441. if (!this->Rcc.ExecutableTargetName.empty()) {
  1442. ccDepends.push_back(this->Rcc.ExecutableTargetName);
  1443. }
  1444. cc->SetOutputs(ccOutput);
  1445. cc->SetDepends(ccDepends);
  1446. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1447. }
  1448. // Reconfigure when .qrc file changes
  1449. this->Makefile->AddCMakeDependFile(qrc.QrcFile);
  1450. }
  1451. }
  1452. return true;
  1453. }
  1454. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1455. {
  1456. // Create info directory on demand
  1457. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1458. cmSystemTools::Error(cmStrCat("AutoGen: Could not create directory: ",
  1459. Quoted(this->Dir.Info)));
  1460. return false;
  1461. }
  1462. // Generate autogen target info file
  1463. if (this->MocOrUicEnabled()) {
  1464. // Write autogen target info files
  1465. if (!this->SetupWriteAutogenInfo()) {
  1466. return false;
  1467. }
  1468. }
  1469. // Write AUTORCC info files
  1470. return !this->Rcc.Enabled || this->SetupWriteRccInfo();
  1471. }
  1472. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1473. {
  1474. // Utility lambdas
  1475. auto MfDef = [this](std::string const& key) {
  1476. return this->Makefile->GetSafeDefinition(key);
  1477. };
  1478. // Filtered headers and sources
  1479. std::set<std::string> moc_skip;
  1480. std::set<std::string> uic_skip;
  1481. std::vector<MUFile const*> headers;
  1482. std::vector<MUFile const*> sources;
  1483. // Filter headers
  1484. {
  1485. headers.reserve(this->AutogenTarget.Headers.size());
  1486. for (auto const& pair : this->AutogenTarget.Headers) {
  1487. MUFile const* const muf = pair.second.get();
  1488. if (muf->SkipMoc) {
  1489. moc_skip.insert(muf->FullPath);
  1490. }
  1491. if (muf->SkipUic) {
  1492. uic_skip.insert(muf->FullPath);
  1493. }
  1494. if (muf->Generated && !this->CMP0071Accept) {
  1495. continue;
  1496. }
  1497. if (muf->MocIt || muf->UicIt) {
  1498. headers.emplace_back(muf);
  1499. }
  1500. }
  1501. std::sort(headers.begin(), headers.end(),
  1502. [](MUFile const* a, MUFile const* b) {
  1503. return (a->FullPath < b->FullPath);
  1504. });
  1505. }
  1506. // Filter sources
  1507. {
  1508. sources.reserve(this->AutogenTarget.Sources.size());
  1509. for (auto const& pair : this->AutogenTarget.Sources) {
  1510. MUFile const* const muf = pair.second.get();
  1511. if (muf->Generated && !this->CMP0071Accept) {
  1512. continue;
  1513. }
  1514. if (muf->SkipMoc) {
  1515. moc_skip.insert(muf->FullPath);
  1516. }
  1517. if (muf->SkipUic) {
  1518. uic_skip.insert(muf->FullPath);
  1519. }
  1520. if (muf->MocIt || muf->UicIt) {
  1521. sources.emplace_back(muf);
  1522. }
  1523. }
  1524. std::sort(sources.begin(), sources.end(),
  1525. [](MUFile const* a, MUFile const* b) {
  1526. return (a->FullPath < b->FullPath);
  1527. });
  1528. }
  1529. // Info writer
  1530. InfoWriter info;
  1531. // General
  1532. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1533. info.SetUInt("PARALLEL", this->AutogenTarget.Parallel);
  1534. info.SetUInt("VERBOSITY", this->Verbosity);
  1535. // Directories
  1536. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1537. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1538. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1539. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1540. info.Set("BUILD_DIR", this->Dir.Build);
  1541. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1542. info.SetUInt("QT_VERSION_MAJOR", this->QtVersion.Major);
  1543. info.SetUInt("QT_VERSION_MINOR", this->QtVersion.Minor);
  1544. info.Set("QT_MOC_EXECUTABLE", this->Moc.Executable);
  1545. info.Set("QT_UIC_EXECUTABLE", this->Uic.Executable);
  1546. info.Set("CMAKE_EXECUTABLE", cmSystemTools::GetCMakeCommand());
  1547. info.SetConfig("SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1548. info.SetConfig("PARSE_CACHE_FILE", this->AutogenTarget.ParseCacheFile);
  1549. info.Set("DEP_FILE", this->AutogenTarget.DepFile);
  1550. info.Set("DEP_FILE_RULE_NAME", this->AutogenTarget.DepFileRuleName);
  1551. info.SetArray("CMAKE_LIST_FILES", this->Makefile->GetListFiles());
  1552. info.SetArray("HEADER_EXTENSIONS",
  1553. this->Makefile->GetCMakeInstance()->GetHeaderExtensions());
  1554. auto cfgArray = [this](std::vector<size_t> const& configs) -> Json::Value {
  1555. Json::Value value;
  1556. if (!configs.empty()) {
  1557. value = Json::arrayValue;
  1558. for (size_t ci : configs) {
  1559. value.append(this->ConfigsList[ci]);
  1560. }
  1561. }
  1562. return value;
  1563. };
  1564. info.SetArrayArray("HEADERS", headers,
  1565. [this, &cfgArray](Json::Value& jval, MUFile const* muf) {
  1566. jval.resize(4u);
  1567. jval[0u] = muf->FullPath;
  1568. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm',
  1569. muf->UicIt ? 'U' : 'u');
  1570. jval[2u] = this->GetMocBuildPath(*muf);
  1571. jval[3u] = cfgArray(muf->Configs);
  1572. });
  1573. info.SetArrayArray(
  1574. "SOURCES", sources, [&cfgArray](Json::Value& jval, MUFile const* muf) {
  1575. jval.resize(3u);
  1576. jval[0u] = muf->FullPath;
  1577. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm', muf->UicIt ? 'U' : 'u');
  1578. jval[2u] = cfgArray(muf->Configs);
  1579. });
  1580. // Write moc settings
  1581. if (this->Moc.Enabled) {
  1582. info.SetArray("MOC_SKIP", moc_skip);
  1583. info.SetConfigArray("MOC_DEFINITIONS", this->Moc.Defines);
  1584. info.SetConfigArray("MOC_INCLUDES", this->Moc.Includes);
  1585. info.SetArray("MOC_OPTIONS", this->Moc.Options);
  1586. info.SetBool("MOC_RELAXED_MODE", this->Moc.RelaxedMode);
  1587. info.SetBool("MOC_PATH_PREFIX", this->Moc.PathPrefix);
  1588. cmGeneratorExpressionDAGChecker dagChecker(
  1589. this->GenTarget, "AUTOMOC_MACRO_NAMES", nullptr, nullptr);
  1590. EvaluatedTargetPropertyEntries InterfaceAutoMocMacroNamesEntries;
  1591. if (this->MultiConfig) {
  1592. for (auto const& cfg : this->ConfigsList) {
  1593. if (!cfg.empty()) {
  1594. AddInterfaceEntries(this->GenTarget, cfg,
  1595. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX",
  1596. &dagChecker, InterfaceAutoMocMacroNamesEntries,
  1597. IncludeRuntimeInterface::Yes);
  1598. }
  1599. }
  1600. } else {
  1601. AddInterfaceEntries(this->GenTarget, this->ConfigDefault,
  1602. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX", &dagChecker,
  1603. InterfaceAutoMocMacroNamesEntries,
  1604. IncludeRuntimeInterface::Yes);
  1605. }
  1606. for (auto const& entry : InterfaceAutoMocMacroNamesEntries.Entries) {
  1607. this->Moc.MacroNames.insert(this->Moc.MacroNames.end(),
  1608. entry.Values.begin(), entry.Values.end());
  1609. }
  1610. this->Moc.MacroNames.erase(cmRemoveDuplicates(this->Moc.MacroNames),
  1611. this->Moc.MacroNames.end());
  1612. info.SetArray("MOC_MACRO_NAMES", this->Moc.MacroNames);
  1613. info.SetArrayArray(
  1614. "MOC_DEPEND_FILTERS", this->Moc.DependFilters,
  1615. [](Json::Value& jval, std::pair<std::string, std::string> const& pair) {
  1616. jval.resize(2u);
  1617. jval[0u] = pair.first;
  1618. jval[1u] = pair.second;
  1619. });
  1620. info.SetConfig("MOC_COMPILATION_FILE", this->Moc.CompilationFile);
  1621. info.SetConfig("MOC_PREDEFS_FILE", this->Moc.PredefsFile);
  1622. cmStandardLevelResolver const resolver{ this->Makefile };
  1623. auto const CompileOptionFlag =
  1624. resolver.GetCompileOptionDef(this->GenTarget, "CXX", "");
  1625. auto const CompileOptionValue =
  1626. this->GenTarget->Makefile->GetSafeDefinition(CompileOptionFlag);
  1627. if (!CompileOptionValue.empty()) {
  1628. if (this->Moc.PredefsCmd.size() >= 3) {
  1629. this->Moc.PredefsCmd.insert(this->Moc.PredefsCmd.begin() + 1,
  1630. CompileOptionValue);
  1631. }
  1632. }
  1633. info.SetArray("MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1634. }
  1635. // Write uic settings
  1636. if (this->Uic.Enabled) {
  1637. // Add skipped .ui files
  1638. uic_skip.insert(this->Uic.SkipUi.begin(), this->Uic.SkipUi.end());
  1639. info.SetArray("UIC_SKIP", uic_skip);
  1640. info.SetArrayArray("UIC_UI_FILES", this->Uic.UiFilesWithOptions,
  1641. [](Json::Value& jval, UicT::UiFileT const& uiFile) {
  1642. jval.resize(2u);
  1643. jval[0u] = uiFile.first;
  1644. InfoWriter::MakeStringArray(jval[1u], uiFile.second);
  1645. });
  1646. info.SetConfigArray("UIC_OPTIONS", this->Uic.Options);
  1647. info.SetArray("UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1648. }
  1649. info.Save(this->AutogenTarget.InfoFile);
  1650. return true;
  1651. }
  1652. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1653. {
  1654. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1655. // Utility lambdas
  1656. auto MfDef = [this](std::string const& key) {
  1657. return this->Makefile->GetSafeDefinition(key);
  1658. };
  1659. InfoWriter info;
  1660. // General
  1661. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1662. info.SetUInt("VERBOSITY", this->Verbosity);
  1663. info.Set("GENERATOR", this->GlobalGen->GetName());
  1664. // Files
  1665. info.Set("LOCK_FILE", qrc.LockFile);
  1666. info.SetConfig("SETTINGS_FILE", qrc.SettingsFile);
  1667. // Directories
  1668. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1669. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1670. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1671. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1672. info.Set("BUILD_DIR", this->Dir.Build);
  1673. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1674. // rcc executable
  1675. info.Set("RCC_EXECUTABLE", this->Rcc.Executable);
  1676. info.SetArray("RCC_LIST_OPTIONS",
  1677. this->Rcc.ExecutableFeatures->ListOptions);
  1678. // qrc file
  1679. info.Set("SOURCE", qrc.QrcFile);
  1680. info.Set("OUTPUT_CHECKSUM", qrc.QrcPathChecksum);
  1681. info.Set("OUTPUT_NAME", cmSystemTools::GetFilenameName(qrc.OutputFile));
  1682. info.SetArray("OPTIONS", qrc.Options);
  1683. info.SetArray("INPUTS", qrc.Resources);
  1684. info.Save(qrc.InfoFile);
  1685. }
  1686. return true;
  1687. }
  1688. cmSourceFile* cmQtAutoGenInitializer::RegisterGeneratedSource(
  1689. std::string const& filename)
  1690. {
  1691. cmSourceFile* gFile = this->Makefile->GetOrCreateSource(filename, true);
  1692. gFile->MarkAsGenerated();
  1693. gFile->SetProperty("SKIP_AUTOGEN", "1");
  1694. gFile->SetProperty("SKIP_LINTING", "ON");
  1695. gFile->SetProperty("CXX_SCAN_FOR_MODULES", "0");
  1696. return gFile;
  1697. }
  1698. cmSourceFile* cmQtAutoGenInitializer::AddGeneratedSource(
  1699. std::string const& filename, GenVarsT const& genVars, bool prepend)
  1700. {
  1701. // Register source at makefile
  1702. cmSourceFile* gFile = this->RegisterGeneratedSource(filename);
  1703. // Add source file to target
  1704. this->GenTarget->AddSource(filename, prepend);
  1705. // Add source file to source group
  1706. this->AddToSourceGroup(filename, genVars.GenNameUpper);
  1707. return gFile;
  1708. }
  1709. void cmQtAutoGenInitializer::AddGeneratedSource(ConfigString const& filename,
  1710. GenVarsT const& genVars,
  1711. bool prepend)
  1712. {
  1713. // XXX(xcode-per-cfg-src): Drop the Xcode-specific part of the condition
  1714. // when the Xcode generator supports per-config sources.
  1715. if (!this->MultiConfig || this->GlobalGen->IsXcode()) {
  1716. cmSourceFile* sf =
  1717. this->AddGeneratedSource(filename.Default, genVars, prepend);
  1718. handleSkipPch(sf);
  1719. return;
  1720. }
  1721. for (auto const& cfg : this->ConfigsList) {
  1722. std::string const& filenameCfg = filename.Config.at(cfg);
  1723. // Register source at makefile
  1724. cmSourceFile* sf = this->RegisterGeneratedSource(filenameCfg);
  1725. handleSkipPch(sf);
  1726. // Add source file to target for this configuration.
  1727. this->GenTarget->AddSource(
  1728. cmStrCat("$<$<CONFIG:"_s, cfg, ">:"_s, filenameCfg, ">"_s), prepend);
  1729. // Add source file to source group
  1730. this->AddToSourceGroup(filenameCfg, genVars.GenNameUpper);
  1731. }
  1732. }
  1733. void cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
  1734. cm::string_view genNameUpper)
  1735. {
  1736. cmSourceGroup* sourceGroup = nullptr;
  1737. // Acquire source group
  1738. {
  1739. std::string property;
  1740. std::string groupName;
  1741. {
  1742. // Prefer generator specific source group name
  1743. std::initializer_list<std::string> const props{
  1744. cmStrCat(genNameUpper, "_SOURCE_GROUP"), "AUTOGEN_SOURCE_GROUP"
  1745. };
  1746. for (std::string const& prop : props) {
  1747. cmValue propName = this->Makefile->GetState()->GetGlobalProperty(prop);
  1748. if (cmNonempty(propName)) {
  1749. groupName = *propName;
  1750. property = prop;
  1751. break;
  1752. }
  1753. }
  1754. }
  1755. // Generate a source group on demand
  1756. if (!groupName.empty()) {
  1757. sourceGroup = this->Makefile->GetOrCreateSourceGroup(groupName);
  1758. if (!sourceGroup) {
  1759. cmSystemTools::Error(
  1760. cmStrCat(genNameUpper, " error in ", property,
  1761. ": Could not find or create the source group ",
  1762. cmQtAutoGen::Quoted(groupName)));
  1763. }
  1764. }
  1765. }
  1766. if (sourceGroup) {
  1767. sourceGroup->AddGroupFile(fileName);
  1768. }
  1769. }
  1770. void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
  1771. {
  1772. this->GenTarget->Target->AppendProperty("ADDITIONAL_CLEAN_FILES", fileName);
  1773. }
  1774. void cmQtAutoGenInitializer::ConfigFileNames(ConfigString& configString,
  1775. cm::string_view prefix,
  1776. cm::string_view suffix)
  1777. {
  1778. configString.Default = cmStrCat(prefix, suffix);
  1779. if (this->MultiConfig) {
  1780. for (auto const& cfg : this->ConfigsList) {
  1781. configString.Config[cfg] = cmStrCat(prefix, '_', cfg, suffix);
  1782. }
  1783. }
  1784. }
  1785. void cmQtAutoGenInitializer::ConfigFileNamesAndGenex(
  1786. ConfigString& configString, std::string& genex, cm::string_view const prefix,
  1787. cm::string_view const suffix)
  1788. {
  1789. this->ConfigFileNames(configString, prefix, suffix);
  1790. if (this->MultiConfig) {
  1791. genex = cmStrCat(prefix, "_$<CONFIG>"_s, suffix);
  1792. } else {
  1793. genex = configString.Default;
  1794. }
  1795. }
  1796. void cmQtAutoGenInitializer::ConfigFileClean(ConfigString& configString)
  1797. {
  1798. this->AddCleanFile(configString.Default);
  1799. if (this->MultiConfig) {
  1800. for (auto const& pair : configString.Config) {
  1801. this->AddCleanFile(pair.second);
  1802. }
  1803. }
  1804. }
  1805. static cmQtAutoGen::IntegerVersion parseMocVersion(std::string str)
  1806. {
  1807. cmQtAutoGen::IntegerVersion result;
  1808. static const std::string prelude = "moc ";
  1809. size_t const pos = str.find(prelude);
  1810. if (pos == std::string::npos) {
  1811. return result;
  1812. }
  1813. str.erase(0, prelude.size() + pos);
  1814. std::istringstream iss(str);
  1815. std::string major;
  1816. std::string minor;
  1817. if (!std::getline(iss, major, '.') || !std::getline(iss, minor, '.')) {
  1818. return result;
  1819. }
  1820. result.Major = static_cast<unsigned int>(std::stoi(major));
  1821. result.Minor = static_cast<unsigned int>(std::stoi(minor));
  1822. return result;
  1823. }
  1824. static cmQtAutoGen::IntegerVersion GetMocVersion(
  1825. const std::string& mocExecutablePath)
  1826. {
  1827. std::string capturedStdOut;
  1828. int exitCode;
  1829. if (!cmSystemTools::RunSingleCommand({ mocExecutablePath, "--version" },
  1830. &capturedStdOut, nullptr, &exitCode,
  1831. nullptr, cmSystemTools::OUTPUT_NONE)) {
  1832. return {};
  1833. }
  1834. if (exitCode != 0) {
  1835. return {};
  1836. }
  1837. return parseMocVersion(capturedStdOut);
  1838. }
  1839. static std::string FindMocExecutableFromMocTarget(cmMakefile const* makefile,
  1840. unsigned int qtMajorVersion)
  1841. {
  1842. std::string result;
  1843. const std::string mocTargetName =
  1844. "Qt" + std::to_string(qtMajorVersion) + "::moc";
  1845. cmTarget const* mocTarget = makefile->FindTargetToUse(mocTargetName);
  1846. if (mocTarget) {
  1847. result = mocTarget->GetSafeProperty("IMPORTED_LOCATION");
  1848. }
  1849. return result;
  1850. }
  1851. std::pair<cmQtAutoGen::IntegerVersion, unsigned int>
  1852. cmQtAutoGenInitializer::GetQtVersion(cmGeneratorTarget const* target,
  1853. std::string mocExecutable)
  1854. {
  1855. // Converts a char ptr to an unsigned int value
  1856. auto toUInt = [](const char* const input) -> unsigned int {
  1857. unsigned long tmp = 0;
  1858. if (input && cmStrToULong(input, &tmp)) {
  1859. return static_cast<unsigned int>(tmp);
  1860. }
  1861. return 0u;
  1862. };
  1863. auto toUInt2 = [](cmValue input) -> unsigned int {
  1864. unsigned long tmp = 0;
  1865. if (input && cmStrToULong(*input, &tmp)) {
  1866. return static_cast<unsigned int>(tmp);
  1867. }
  1868. return 0u;
  1869. };
  1870. // Initialize return value to a default
  1871. std::pair<IntegerVersion, unsigned int> res(
  1872. IntegerVersion(),
  1873. toUInt(target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1874. "")));
  1875. // Acquire known Qt versions
  1876. std::vector<cmQtAutoGen::IntegerVersion> knownQtVersions;
  1877. {
  1878. // Qt version variable prefixes
  1879. static std::initializer_list<
  1880. std::pair<cm::string_view, cm::string_view>> const keys{
  1881. { "Qt6Core_VERSION_MAJOR", "Qt6Core_VERSION_MINOR" },
  1882. { "Qt5Core_VERSION_MAJOR", "Qt5Core_VERSION_MINOR" },
  1883. { "QT_VERSION_MAJOR", "QT_VERSION_MINOR" },
  1884. };
  1885. knownQtVersions.reserve(keys.size() * 2);
  1886. // Adds a version to the result (nullptr safe)
  1887. auto addVersion = [&knownQtVersions, &toUInt2](cmValue major,
  1888. cmValue minor) {
  1889. cmQtAutoGen::IntegerVersion ver(toUInt2(major), toUInt2(minor));
  1890. if (ver.Major != 0) {
  1891. knownQtVersions.emplace_back(ver);
  1892. }
  1893. };
  1894. // Read versions from variables
  1895. for (auto const& keyPair : keys) {
  1896. addVersion(target->Makefile->GetDefinition(std::string(keyPair.first)),
  1897. target->Makefile->GetDefinition(std::string(keyPair.second)));
  1898. }
  1899. // Read versions from directory properties
  1900. for (auto const& keyPair : keys) {
  1901. addVersion(target->Makefile->GetProperty(std::string(keyPair.first)),
  1902. target->Makefile->GetProperty(std::string(keyPair.second)));
  1903. }
  1904. }
  1905. // Evaluate known Qt versions
  1906. if (!knownQtVersions.empty()) {
  1907. if (res.second == 0) {
  1908. // No specific version was requested by the target:
  1909. // Use highest known Qt version.
  1910. res.first = knownQtVersions.at(0);
  1911. } else {
  1912. // Pick a version from the known versions:
  1913. for (auto const& it : knownQtVersions) {
  1914. if (it.Major == res.second) {
  1915. res.first = it;
  1916. break;
  1917. }
  1918. }
  1919. }
  1920. }
  1921. if (res.first.Major == 0) {
  1922. // We could not get the version number from variables or directory
  1923. // properties. This might happen if the find_package call for Qt is wrapped
  1924. // in a function. Try to find the moc executable path from the available
  1925. // targets and call "moc --version" to get the Qt version.
  1926. if (mocExecutable.empty()) {
  1927. mocExecutable =
  1928. FindMocExecutableFromMocTarget(target->Makefile, res.second);
  1929. }
  1930. if (!mocExecutable.empty()) {
  1931. res.first = GetMocVersion(mocExecutable);
  1932. }
  1933. }
  1934. return res;
  1935. }
  1936. std::string cmQtAutoGenInitializer::GetMocBuildPath(MUFile const& muf)
  1937. {
  1938. std::string res;
  1939. if (!muf.MocIt) {
  1940. return res;
  1941. }
  1942. std::string basePath =
  1943. cmStrCat(this->PathCheckSum.getPart(muf.FullPath), "/moc_",
  1944. FileNameWithoutLastExtension(muf.FullPath));
  1945. res = cmStrCat(basePath, ".cpp");
  1946. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1947. return res;
  1948. }
  1949. // File name already emitted.
  1950. // Try appending the header suffix to the base path.
  1951. basePath = cmStrCat(basePath, '_', muf.SF->GetExtension());
  1952. res = cmStrCat(basePath, ".cpp");
  1953. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1954. return res;
  1955. }
  1956. // File name with header extension already emitted.
  1957. // Try adding a number to the base path.
  1958. constexpr std::size_t number_begin = 2;
  1959. constexpr std::size_t number_end = 256;
  1960. for (std::size_t ii = number_begin; ii != number_end; ++ii) {
  1961. res = cmStrCat(basePath, '_', ii, ".cpp");
  1962. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1963. return res;
  1964. }
  1965. }
  1966. // Output file name conflict (unlikely, but still...)
  1967. cmSystemTools::Error(
  1968. cmStrCat("moc output file name conflict for ", muf.FullPath));
  1969. return res;
  1970. }
  1971. bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
  1972. const std::string& executable,
  1973. bool ignoreMissingTarget) const
  1974. {
  1975. auto print_err = [this, &genVars](std::string const& err) {
  1976. cmSystemTools::Error(cmStrCat(genVars.GenNameUpper, " for target ",
  1977. this->GenTarget->GetName(), ": ", err));
  1978. };
  1979. // Custom executable
  1980. {
  1981. std::string const prop = cmStrCat(genVars.GenNameUpper, "_EXECUTABLE");
  1982. std::string const& val = this->GenTarget->Target->GetSafeProperty(prop);
  1983. if (!val.empty()) {
  1984. // Evaluate generator expression
  1985. {
  1986. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  1987. cmGeneratorExpression ge(*this->Makefile->GetCMakeInstance(), lfbt);
  1988. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(val);
  1989. genVars.Executable = cge->Evaluate(this->LocalGen, "");
  1990. }
  1991. if (genVars.Executable.empty() && !ignoreMissingTarget) {
  1992. print_err(prop + " evaluates to an empty value");
  1993. return false;
  1994. }
  1995. // Create empty compiler features.
  1996. genVars.ExecutableFeatures =
  1997. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  1998. return true;
  1999. }
  2000. }
  2001. // Find executable target
  2002. {
  2003. // Find executable target name
  2004. cm::string_view prefix;
  2005. if (this->QtVersion.Major == 4) {
  2006. prefix = "Qt4::";
  2007. } else if (this->QtVersion.Major == 5) {
  2008. prefix = "Qt5::";
  2009. } else if (this->QtVersion.Major == 6) {
  2010. prefix = "Qt6::";
  2011. }
  2012. std::string const targetName = cmStrCat(prefix, executable);
  2013. // Find target
  2014. cmGeneratorTarget* genTarget =
  2015. this->LocalGen->FindGeneratorTargetToUse(targetName);
  2016. if (genTarget) {
  2017. genVars.ExecutableTargetName = targetName;
  2018. genVars.ExecutableTarget = genTarget;
  2019. if (genTarget->IsImported()) {
  2020. genVars.Executable = genTarget->ImportedGetLocation("");
  2021. } else {
  2022. genVars.Executable = genTarget->GetLocation("");
  2023. }
  2024. } else {
  2025. if (ignoreMissingTarget) {
  2026. // Create empty compiler features.
  2027. genVars.ExecutableFeatures =
  2028. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  2029. return true;
  2030. }
  2031. print_err(cmStrCat("Could not find ", executable, " executable target ",
  2032. targetName));
  2033. return false;
  2034. }
  2035. }
  2036. // Get executable features
  2037. {
  2038. std::string err;
  2039. genVars.ExecutableFeatures = this->GlobalInitializer->GetCompilerFeatures(
  2040. executable, genVars.Executable, err);
  2041. if (!genVars.ExecutableFeatures) {
  2042. print_err(err);
  2043. return false;
  2044. }
  2045. }
  2046. return true;
  2047. }
  2048. void cmQtAutoGenInitializer::handleSkipPch(cmSourceFile* sf)
  2049. {
  2050. bool skipPch = true;
  2051. for (auto const& pair : this->AutogenTarget.Sources) {
  2052. if (!pair.first->GetIsGenerated() &&
  2053. !pair.first->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  2054. skipPch = false;
  2055. }
  2056. }
  2057. if (skipPch) {
  2058. sf->SetProperty("SKIP_PRECOMPILE_HEADERS", "ON");
  2059. }
  2060. }