cmQtAutoGenInitializer.cxx 76 KB

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