cmQtAutoGenInitializer.cxx 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  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. std::vector<std::string> dependencies(
  1258. this->AutogenTarget.DependFiles.begin(),
  1259. this->AutogenTarget.DependFiles.end());
  1260. if (useDepfile) {
  1261. // Create a custom command that generates a timestamp file and
  1262. // has a depfile assigned. The depfile is created by JobDepFilesMergeT.
  1263. //
  1264. // Also create an additional '_autogen_timestamp_deps' that the custom
  1265. // command will depend on. It will have no sources or commands to
  1266. // execute, but it will have dependencies that would originally be
  1267. // assigned to the pre-Qt 5.15 'autogen' target. These dependencies will
  1268. // serve as a list of order-only dependencies for the custom command,
  1269. // without forcing the custom command to re-execute.
  1270. //
  1271. // The dependency tree would then look like
  1272. // '_autogen_timestamp_deps (order-only)' <- '/timestamp' file <-
  1273. // '_autogen' target.
  1274. const auto timestampTargetName =
  1275. cmStrCat(this->GenTarget->GetName(), "_autogen_timestamp_deps");
  1276. // Add additional autogen target dependencies to
  1277. // '_autogen_timestamp_deps'.
  1278. for (const cmTarget* t : this->AutogenTarget.DependTargets) {
  1279. std::string depname = t->GetName();
  1280. if (t->IsImported()) {
  1281. auto const ttype = t->GetType();
  1282. if (ttype == cmStateEnums::TargetType::STATIC_LIBRARY ||
  1283. ttype == cmStateEnums::TargetType::SHARED_LIBRARY ||
  1284. ttype == cmStateEnums::TargetType::UNKNOWN_LIBRARY) {
  1285. depname = cmStrCat("$<TARGET_LINKER_FILE:", t->GetName(), ">");
  1286. }
  1287. }
  1288. dependencies.emplace_back(std::move(depname));
  1289. }
  1290. auto cc = cm::make_unique<cmCustomCommand>();
  1291. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1292. cc->SetDepends(dependencies);
  1293. cc->SetEscapeOldStyle(false);
  1294. cmTarget* timestampTarget = this->LocalGen->AddUtilityCommand(
  1295. timestampTargetName, true, std::move(cc));
  1296. this->LocalGen->AddGeneratorTarget(
  1297. cm::make_unique<cmGeneratorTarget>(timestampTarget, this->LocalGen));
  1298. // Set FOLDER property on the timestamp target, so it appears in the
  1299. // appropriate folder in an IDE or in the file api.
  1300. if (!this->TargetsFolder.empty()) {
  1301. timestampTarget->SetProperty("FOLDER", this->TargetsFolder);
  1302. }
  1303. // Make '/timestamp' file depend on '_autogen_timestamp_deps' and on the
  1304. // moc and uic executables (whichever are enabled).
  1305. dependencies.clear();
  1306. dependencies.push_back(timestampTargetName);
  1307. AddAutogenExecutableToDependencies(this->Moc, dependencies);
  1308. AddAutogenExecutableToDependencies(this->Uic, dependencies);
  1309. // Create the custom command that outputs the timestamp file.
  1310. const char timestampFileName[] = "timestamp";
  1311. const std::string outputFile =
  1312. cmStrCat(this->Dir.Build, "/", timestampFileName);
  1313. this->AutogenTarget.DepFile = cmStrCat(this->Dir.Build, "/deps");
  1314. this->AutogenTarget.DepFileRuleName =
  1315. cmStrCat(this->Dir.RelativeBuild, "/", timestampFileName);
  1316. commandLines.push_back(cmMakeCommandLine(
  1317. { cmSystemTools::GetCMakeCommand(), "-E", "touch", outputFile }));
  1318. this->AddGeneratedSource(outputFile, this->Moc);
  1319. cc = cm::make_unique<cmCustomCommand>();
  1320. cc->SetOutputs(outputFile);
  1321. cc->SetByproducts(timestampByproducts);
  1322. cc->SetDepends(dependencies);
  1323. cc->SetCommandLines(commandLines);
  1324. cc->SetComment(autogenComment.c_str());
  1325. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1326. cc->SetEscapeOldStyle(false);
  1327. cc->SetDepfile(this->AutogenTarget.DepFile);
  1328. cc->SetStdPipesUTF8(stdPipesUTF8);
  1329. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1330. // Alter variables for the autogen target which now merely wraps the
  1331. // custom command
  1332. dependencies.clear();
  1333. dependencies.emplace_back(outputFile);
  1334. commandLines.clear();
  1335. autogenComment.clear();
  1336. }
  1337. // Create autogen target
  1338. auto cc = cm::make_unique<cmCustomCommand>();
  1339. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1340. cc->SetByproducts(autogenByproducts);
  1341. cc->SetDepends(dependencies);
  1342. cc->SetCommandLines(commandLines);
  1343. cc->SetEscapeOldStyle(false);
  1344. cc->SetComment(autogenComment.c_str());
  1345. cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand(
  1346. this->AutogenTarget.Name, true, std::move(cc));
  1347. // Create autogen generator target
  1348. this->LocalGen->AddGeneratorTarget(
  1349. cm::make_unique<cmGeneratorTarget>(autogenTarget, this->LocalGen));
  1350. // Forward origin utilities to autogen target
  1351. if (this->AutogenTarget.DependOrigin) {
  1352. for (BT<std::pair<std::string, bool>> const& depName :
  1353. this->GenTarget->GetUtilities()) {
  1354. autogenTarget->AddUtility(depName.Value.first, false, this->Makefile);
  1355. }
  1356. }
  1357. if (!useDepfile) {
  1358. // Add additional autogen target dependencies to autogen target
  1359. for (cmTarget const* depTarget : this->AutogenTarget.DependTargets) {
  1360. autogenTarget->AddUtility(depTarget->GetName(), false, this->Makefile);
  1361. }
  1362. }
  1363. // Set FOLDER property in autogen target
  1364. if (!this->TargetsFolder.empty()) {
  1365. autogenTarget->SetProperty("FOLDER", this->TargetsFolder);
  1366. }
  1367. // Add autogen target to the origin target dependencies
  1368. this->GenTarget->Target->AddUtility(this->AutogenTarget.Name, false,
  1369. this->Makefile);
  1370. // Add autogen target to the global autogen target dependencies
  1371. if (this->AutogenTarget.GlobalTarget) {
  1372. this->GlobalInitializer->AddToGlobalAutoGen(this->LocalGen,
  1373. this->AutogenTarget.Name);
  1374. }
  1375. }
  1376. return true;
  1377. }
  1378. bool cmQtAutoGenInitializer::InitRccTargets()
  1379. {
  1380. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1381. // Register info file as generated by CMake
  1382. this->Makefile->AddCMakeOutputFile(qrc.InfoFile);
  1383. // Register file at target
  1384. {
  1385. cmSourceFile* sf = this->AddGeneratedSource(qrc.OutputFile, this->Rcc);
  1386. sf->SetProperty("SKIP_UNITY_BUILD_INCLUSION", "On");
  1387. }
  1388. std::vector<std::string> ccOutput;
  1389. ccOutput.push_back(qrc.OutputFile);
  1390. std::vector<std::string> ccDepends;
  1391. // Add the .qrc and info file to the custom command dependencies
  1392. ccDepends.push_back(qrc.QrcFile);
  1393. ccDepends.push_back(qrc.InfoFile);
  1394. cmCustomCommandLines commandLines;
  1395. if (this->MultiConfig) {
  1396. // Build for all configurations
  1397. for (std::string const& config : this->ConfigsList) {
  1398. commandLines.push_back(
  1399. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1400. "cmake_autorcc", qrc.InfoFile, config }));
  1401. }
  1402. } else {
  1403. commandLines.push_back(
  1404. cmMakeCommandLine({ cmSystemTools::GetCMakeCommand(), "-E",
  1405. "cmake_autorcc", qrc.InfoFile, "$<CONFIG>" }));
  1406. }
  1407. std::string const ccComment =
  1408. cmStrCat("Automatic RCC for ",
  1409. FileProjectRelativePath(this->Makefile, qrc.QrcFile));
  1410. auto cc = cm::make_unique<cmCustomCommand>();
  1411. cc->SetWorkingDirectory(this->Dir.Work.c_str());
  1412. cc->SetCommandLines(commandLines);
  1413. cc->SetComment(ccComment.c_str());
  1414. cc->SetStdPipesUTF8(true);
  1415. if (qrc.Generated || this->Rcc.GlobalTarget) {
  1416. // Create custom rcc target
  1417. std::string ccName;
  1418. {
  1419. ccName = cmStrCat(this->GenTarget->GetName(), "_arcc_", qrc.QrcName);
  1420. if (!qrc.Unique) {
  1421. ccName += cmStrCat('_', qrc.QrcPathChecksum);
  1422. }
  1423. cc->SetByproducts(ccOutput);
  1424. cc->SetDepends(ccDepends);
  1425. cc->SetEscapeOldStyle(false);
  1426. cmTarget* autoRccTarget =
  1427. this->LocalGen->AddUtilityCommand(ccName, true, std::move(cc));
  1428. // Create autogen generator target
  1429. this->LocalGen->AddGeneratorTarget(
  1430. cm::make_unique<cmGeneratorTarget>(autoRccTarget, this->LocalGen));
  1431. // Set FOLDER property in autogen target
  1432. if (!this->TargetsFolder.empty()) {
  1433. autoRccTarget->SetProperty("FOLDER", this->TargetsFolder);
  1434. }
  1435. if (!this->Rcc.ExecutableTargetName.empty()) {
  1436. autoRccTarget->AddUtility(this->Rcc.ExecutableTargetName, false,
  1437. this->Makefile);
  1438. }
  1439. }
  1440. // Add autogen target to the origin target dependencies
  1441. this->GenTarget->Target->AddUtility(ccName, false, this->Makefile);
  1442. // Add autogen target to the global autogen target dependencies
  1443. if (this->Rcc.GlobalTarget) {
  1444. this->GlobalInitializer->AddToGlobalAutoRcc(this->LocalGen, ccName);
  1445. }
  1446. } else {
  1447. // Create custom rcc command
  1448. {
  1449. // Add the resource files to the dependencies
  1450. for (std::string const& fileName : qrc.Resources) {
  1451. // Add resource file to the custom command dependencies
  1452. ccDepends.push_back(fileName);
  1453. }
  1454. if (!this->Rcc.ExecutableTargetName.empty()) {
  1455. ccDepends.push_back(this->Rcc.ExecutableTargetName);
  1456. }
  1457. cc->SetOutputs(ccOutput);
  1458. cc->SetDepends(ccDepends);
  1459. this->LocalGen->AddCustomCommandToOutput(std::move(cc));
  1460. }
  1461. // Reconfigure when .qrc file changes
  1462. this->Makefile->AddCMakeDependFile(qrc.QrcFile);
  1463. }
  1464. }
  1465. return true;
  1466. }
  1467. bool cmQtAutoGenInitializer::SetupCustomTargets()
  1468. {
  1469. // Create info directory on demand
  1470. if (!cmSystemTools::MakeDirectory(this->Dir.Info)) {
  1471. cmSystemTools::Error(cmStrCat("AutoGen: Could not create directory: ",
  1472. Quoted(this->Dir.Info)));
  1473. return false;
  1474. }
  1475. // Generate autogen target info file
  1476. if (this->MocOrUicEnabled()) {
  1477. // Write autogen target info files
  1478. if (!this->SetupWriteAutogenInfo()) {
  1479. return false;
  1480. }
  1481. }
  1482. // Write AUTORCC info files
  1483. return !this->Rcc.Enabled || this->SetupWriteRccInfo();
  1484. }
  1485. bool cmQtAutoGenInitializer::SetupWriteAutogenInfo()
  1486. {
  1487. // Utility lambdas
  1488. auto MfDef = [this](std::string const& key) {
  1489. return this->Makefile->GetSafeDefinition(key);
  1490. };
  1491. // Filtered headers and sources
  1492. std::set<std::string> moc_skip;
  1493. std::set<std::string> uic_skip;
  1494. std::vector<MUFile const*> headers;
  1495. std::vector<MUFile const*> sources;
  1496. // Filter headers
  1497. {
  1498. headers.reserve(this->AutogenTarget.Headers.size());
  1499. for (auto const& pair : this->AutogenTarget.Headers) {
  1500. MUFile const* const muf = pair.second.get();
  1501. if (muf->SkipMoc) {
  1502. moc_skip.insert(muf->FullPath);
  1503. }
  1504. if (muf->SkipUic) {
  1505. uic_skip.insert(muf->FullPath);
  1506. }
  1507. if (muf->Generated && !this->CMP0071Accept) {
  1508. continue;
  1509. }
  1510. if (muf->MocIt || muf->UicIt) {
  1511. headers.emplace_back(muf);
  1512. }
  1513. }
  1514. std::sort(headers.begin(), headers.end(),
  1515. [](MUFile const* a, MUFile const* b) {
  1516. return (a->FullPath < b->FullPath);
  1517. });
  1518. }
  1519. // Filter sources
  1520. {
  1521. sources.reserve(this->AutogenTarget.Sources.size());
  1522. for (auto const& pair : this->AutogenTarget.Sources) {
  1523. MUFile const* const muf = pair.second.get();
  1524. if (muf->Generated && !this->CMP0071Accept) {
  1525. continue;
  1526. }
  1527. if (muf->SkipMoc) {
  1528. moc_skip.insert(muf->FullPath);
  1529. }
  1530. if (muf->SkipUic) {
  1531. uic_skip.insert(muf->FullPath);
  1532. }
  1533. if (muf->MocIt || muf->UicIt) {
  1534. sources.emplace_back(muf);
  1535. }
  1536. }
  1537. std::sort(sources.begin(), sources.end(),
  1538. [](MUFile const* a, MUFile const* b) {
  1539. return (a->FullPath < b->FullPath);
  1540. });
  1541. }
  1542. // Info writer
  1543. InfoWriter info;
  1544. // General
  1545. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1546. info.SetUInt("PARALLEL", this->AutogenTarget.Parallel);
  1547. info.SetUInt("VERBOSITY", this->Verbosity);
  1548. // Directories
  1549. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1550. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1551. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1552. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1553. info.Set("BUILD_DIR", this->Dir.Build);
  1554. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1555. info.SetUInt("QT_VERSION_MAJOR", this->QtVersion.Major);
  1556. info.SetUInt("QT_VERSION_MINOR", this->QtVersion.Minor);
  1557. info.Set("QT_MOC_EXECUTABLE", this->Moc.Executable);
  1558. info.Set("QT_UIC_EXECUTABLE", this->Uic.Executable);
  1559. info.Set("CMAKE_EXECUTABLE", cmSystemTools::GetCMakeCommand());
  1560. info.SetConfig("SETTINGS_FILE", this->AutogenTarget.SettingsFile);
  1561. info.SetConfig("PARSE_CACHE_FILE", this->AutogenTarget.ParseCacheFile);
  1562. info.Set("DEP_FILE", this->AutogenTarget.DepFile);
  1563. info.Set("DEP_FILE_RULE_NAME", this->AutogenTarget.DepFileRuleName);
  1564. info.SetArray("CMAKE_LIST_FILES", this->Makefile->GetListFiles());
  1565. info.SetArray("HEADER_EXTENSIONS",
  1566. this->Makefile->GetCMakeInstance()->GetHeaderExtensions());
  1567. auto cfgArray = [this](std::vector<size_t> const& configs) -> Json::Value {
  1568. Json::Value value;
  1569. if (!configs.empty()) {
  1570. value = Json::arrayValue;
  1571. for (size_t ci : configs) {
  1572. value.append(this->ConfigsList[ci]);
  1573. }
  1574. }
  1575. return value;
  1576. };
  1577. info.SetArrayArray("HEADERS", headers,
  1578. [this, &cfgArray](Json::Value& jval, MUFile const* muf) {
  1579. jval.resize(4u);
  1580. jval[0u] = muf->FullPath;
  1581. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm',
  1582. muf->UicIt ? 'U' : 'u');
  1583. jval[2u] = this->GetMocBuildPath(*muf);
  1584. jval[3u] = cfgArray(muf->Configs);
  1585. });
  1586. info.SetArrayArray(
  1587. "SOURCES", sources, [&cfgArray](Json::Value& jval, MUFile const* muf) {
  1588. jval.resize(3u);
  1589. jval[0u] = muf->FullPath;
  1590. jval[1u] = cmStrCat(muf->MocIt ? 'M' : 'm', muf->UicIt ? 'U' : 'u');
  1591. jval[2u] = cfgArray(muf->Configs);
  1592. });
  1593. // Write moc settings
  1594. if (this->Moc.Enabled) {
  1595. info.SetArray("MOC_SKIP", moc_skip);
  1596. info.SetConfigArray("MOC_DEFINITIONS", this->Moc.Defines);
  1597. info.SetConfigArray("MOC_INCLUDES", this->Moc.Includes);
  1598. info.SetArray("MOC_OPTIONS", this->Moc.Options);
  1599. info.SetBool("MOC_RELAXED_MODE", this->Moc.RelaxedMode);
  1600. info.SetBool("MOC_PATH_PREFIX", this->Moc.PathPrefix);
  1601. cmGeneratorExpressionDAGChecker dagChecker(
  1602. this->GenTarget, "AUTOMOC_MACRO_NAMES", nullptr, nullptr);
  1603. EvaluatedTargetPropertyEntries InterfaceAutoMocMacroNamesEntries;
  1604. if (this->MultiConfig) {
  1605. for (auto const& cfg : this->ConfigsList) {
  1606. if (!cfg.empty()) {
  1607. AddInterfaceEntries(this->GenTarget, cfg,
  1608. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX",
  1609. &dagChecker, InterfaceAutoMocMacroNamesEntries,
  1610. IncludeRuntimeInterface::Yes);
  1611. }
  1612. }
  1613. } else {
  1614. AddInterfaceEntries(this->GenTarget, this->ConfigDefault,
  1615. "INTERFACE_AUTOMOC_MACRO_NAMES", "CXX", &dagChecker,
  1616. InterfaceAutoMocMacroNamesEntries,
  1617. IncludeRuntimeInterface::Yes);
  1618. }
  1619. for (auto const& entry : InterfaceAutoMocMacroNamesEntries.Entries) {
  1620. this->Moc.MacroNames.insert(this->Moc.MacroNames.end(),
  1621. entry.Values.begin(), entry.Values.end());
  1622. }
  1623. this->Moc.MacroNames.erase(cmRemoveDuplicates(this->Moc.MacroNames),
  1624. this->Moc.MacroNames.end());
  1625. info.SetArray("MOC_MACRO_NAMES", this->Moc.MacroNames);
  1626. info.SetArrayArray(
  1627. "MOC_DEPEND_FILTERS", this->Moc.DependFilters,
  1628. [](Json::Value& jval, std::pair<std::string, std::string> const& pair) {
  1629. jval.resize(2u);
  1630. jval[0u] = pair.first;
  1631. jval[1u] = pair.second;
  1632. });
  1633. info.SetConfig("MOC_COMPILATION_FILE", this->Moc.CompilationFile);
  1634. info.SetConfig("MOC_PREDEFS_FILE", this->Moc.PredefsFile);
  1635. cmStandardLevelResolver const resolver{ this->Makefile };
  1636. auto const CompileOptionFlag =
  1637. resolver.GetCompileOptionDef(this->GenTarget, "CXX", "");
  1638. auto const CompileOptionValue =
  1639. this->GenTarget->Makefile->GetSafeDefinition(CompileOptionFlag);
  1640. if (!CompileOptionValue.empty()) {
  1641. if (this->Moc.PredefsCmd.size() >= 3) {
  1642. this->Moc.PredefsCmd.insert(this->Moc.PredefsCmd.begin() + 1,
  1643. CompileOptionValue);
  1644. }
  1645. }
  1646. info.SetArray("MOC_PREDEFS_CMD", this->Moc.PredefsCmd);
  1647. }
  1648. // Write uic settings
  1649. if (this->Uic.Enabled) {
  1650. // Add skipped .ui files
  1651. uic_skip.insert(this->Uic.SkipUi.begin(), this->Uic.SkipUi.end());
  1652. info.SetArray("UIC_SKIP", uic_skip);
  1653. info.SetArrayArray("UIC_UI_FILES", this->Uic.UiFilesWithOptions,
  1654. [](Json::Value& jval, UicT::UiFileT const& uiFile) {
  1655. jval.resize(2u);
  1656. jval[0u] = uiFile.first;
  1657. InfoWriter::MakeStringArray(jval[1u], uiFile.second);
  1658. });
  1659. info.SetConfigArray("UIC_OPTIONS", this->Uic.Options);
  1660. info.SetArray("UIC_SEARCH_PATHS", this->Uic.SearchPaths);
  1661. }
  1662. info.Save(this->AutogenTarget.InfoFile);
  1663. return true;
  1664. }
  1665. bool cmQtAutoGenInitializer::SetupWriteRccInfo()
  1666. {
  1667. for (Qrc const& qrc : this->Rcc.Qrcs) {
  1668. // Utility lambdas
  1669. auto MfDef = [this](std::string const& key) {
  1670. return this->Makefile->GetSafeDefinition(key);
  1671. };
  1672. InfoWriter info;
  1673. // General
  1674. info.SetBool("MULTI_CONFIG", this->MultiConfig);
  1675. info.SetUInt("VERBOSITY", this->Verbosity);
  1676. info.Set("GENERATOR", this->GlobalGen->GetName());
  1677. // Files
  1678. info.Set("LOCK_FILE", qrc.LockFile);
  1679. info.SetConfig("SETTINGS_FILE", qrc.SettingsFile);
  1680. // Directories
  1681. info.Set("CMAKE_SOURCE_DIR", MfDef("CMAKE_SOURCE_DIR"));
  1682. info.Set("CMAKE_BINARY_DIR", MfDef("CMAKE_BINARY_DIR"));
  1683. info.Set("CMAKE_CURRENT_SOURCE_DIR", MfDef("CMAKE_CURRENT_SOURCE_DIR"));
  1684. info.Set("CMAKE_CURRENT_BINARY_DIR", MfDef("CMAKE_CURRENT_BINARY_DIR"));
  1685. info.Set("BUILD_DIR", this->Dir.Build);
  1686. info.SetConfig("INCLUDE_DIR", this->Dir.Include);
  1687. // rcc executable
  1688. info.Set("RCC_EXECUTABLE", this->Rcc.Executable);
  1689. info.SetArray("RCC_LIST_OPTIONS",
  1690. this->Rcc.ExecutableFeatures->ListOptions);
  1691. // qrc file
  1692. info.Set("SOURCE", qrc.QrcFile);
  1693. info.Set("OUTPUT_CHECKSUM", qrc.QrcPathChecksum);
  1694. info.Set("OUTPUT_NAME", cmSystemTools::GetFilenameName(qrc.OutputFile));
  1695. info.SetArray("OPTIONS", qrc.Options);
  1696. info.SetArray("INPUTS", qrc.Resources);
  1697. info.Save(qrc.InfoFile);
  1698. }
  1699. return true;
  1700. }
  1701. cmSourceFile* cmQtAutoGenInitializer::RegisterGeneratedSource(
  1702. std::string const& filename)
  1703. {
  1704. cmSourceFile* gFile = this->Makefile->GetOrCreateSource(filename, true);
  1705. gFile->MarkAsGenerated();
  1706. gFile->SetProperty("SKIP_AUTOGEN", "1");
  1707. gFile->SetProperty("SKIP_LINTING", "ON");
  1708. gFile->SetProperty("CXX_SCAN_FOR_MODULES", "0");
  1709. return gFile;
  1710. }
  1711. cmSourceFile* cmQtAutoGenInitializer::AddGeneratedSource(
  1712. std::string const& filename, GenVarsT const& genVars, bool prepend)
  1713. {
  1714. // Register source at makefile
  1715. cmSourceFile* gFile = this->RegisterGeneratedSource(filename);
  1716. // Add source file to target
  1717. this->GenTarget->AddSource(filename, prepend);
  1718. // Add source file to source group
  1719. this->AddToSourceGroup(filename, genVars.GenNameUpper);
  1720. return gFile;
  1721. }
  1722. void cmQtAutoGenInitializer::AddGeneratedSource(ConfigString const& filename,
  1723. GenVarsT const& genVars,
  1724. bool prepend)
  1725. {
  1726. // XXX(xcode-per-cfg-src): Drop the Xcode-specific part of the condition
  1727. // when the Xcode generator supports per-config sources.
  1728. if (!this->MultiConfig || this->GlobalGen->IsXcode()) {
  1729. cmSourceFile* sf =
  1730. this->AddGeneratedSource(filename.Default, genVars, prepend);
  1731. handleSkipPch(sf);
  1732. return;
  1733. }
  1734. for (auto const& cfg : this->ConfigsList) {
  1735. std::string const& filenameCfg = filename.Config.at(cfg);
  1736. // Register source at makefile
  1737. cmSourceFile* sf = this->RegisterGeneratedSource(filenameCfg);
  1738. handleSkipPch(sf);
  1739. // Add source file to target for this configuration.
  1740. this->GenTarget->AddSource(
  1741. cmStrCat("$<$<CONFIG:"_s, cfg, ">:"_s, filenameCfg, ">"_s), prepend);
  1742. // Add source file to source group
  1743. this->AddToSourceGroup(filenameCfg, genVars.GenNameUpper);
  1744. }
  1745. }
  1746. void cmQtAutoGenInitializer::AddToSourceGroup(std::string const& fileName,
  1747. cm::string_view genNameUpper)
  1748. {
  1749. cmSourceGroup* sourceGroup = nullptr;
  1750. // Acquire source group
  1751. {
  1752. std::string property;
  1753. std::string groupName;
  1754. {
  1755. // Prefer generator specific source group name
  1756. std::initializer_list<std::string> const props{
  1757. cmStrCat(genNameUpper, "_SOURCE_GROUP"), "AUTOGEN_SOURCE_GROUP"
  1758. };
  1759. for (std::string const& prop : props) {
  1760. cmValue propName = this->Makefile->GetState()->GetGlobalProperty(prop);
  1761. if (cmNonempty(propName)) {
  1762. groupName = *propName;
  1763. property = prop;
  1764. break;
  1765. }
  1766. }
  1767. }
  1768. // Generate a source group on demand
  1769. if (!groupName.empty()) {
  1770. sourceGroup = this->Makefile->GetOrCreateSourceGroup(groupName);
  1771. if (!sourceGroup) {
  1772. cmSystemTools::Error(
  1773. cmStrCat(genNameUpper, " error in ", property,
  1774. ": Could not find or create the source group ",
  1775. cmQtAutoGen::Quoted(groupName)));
  1776. }
  1777. }
  1778. }
  1779. if (sourceGroup) {
  1780. sourceGroup->AddGroupFile(fileName);
  1781. }
  1782. }
  1783. void cmQtAutoGenInitializer::AddCleanFile(std::string const& fileName)
  1784. {
  1785. this->GenTarget->Target->AppendProperty("ADDITIONAL_CLEAN_FILES", fileName);
  1786. }
  1787. void cmQtAutoGenInitializer::ConfigFileNames(ConfigString& configString,
  1788. cm::string_view prefix,
  1789. cm::string_view suffix)
  1790. {
  1791. configString.Default = cmStrCat(prefix, suffix);
  1792. if (this->MultiConfig) {
  1793. for (auto const& cfg : this->ConfigsList) {
  1794. configString.Config[cfg] = cmStrCat(prefix, '_', cfg, suffix);
  1795. }
  1796. }
  1797. }
  1798. void cmQtAutoGenInitializer::ConfigFileNamesAndGenex(
  1799. ConfigString& configString, std::string& genex, cm::string_view const prefix,
  1800. cm::string_view const suffix)
  1801. {
  1802. this->ConfigFileNames(configString, prefix, suffix);
  1803. if (this->MultiConfig) {
  1804. genex = cmStrCat(prefix, "_$<CONFIG>"_s, suffix);
  1805. } else {
  1806. genex = configString.Default;
  1807. }
  1808. }
  1809. void cmQtAutoGenInitializer::ConfigFileClean(ConfigString& configString)
  1810. {
  1811. this->AddCleanFile(configString.Default);
  1812. if (this->MultiConfig) {
  1813. for (auto const& pair : configString.Config) {
  1814. this->AddCleanFile(pair.second);
  1815. }
  1816. }
  1817. }
  1818. static cmQtAutoGen::IntegerVersion parseMocVersion(std::string str)
  1819. {
  1820. cmQtAutoGen::IntegerVersion result;
  1821. static const std::string prelude = "moc ";
  1822. size_t const pos = str.find(prelude);
  1823. if (pos == std::string::npos) {
  1824. return result;
  1825. }
  1826. str.erase(0, prelude.size() + pos);
  1827. std::istringstream iss(str);
  1828. std::string major;
  1829. std::string minor;
  1830. if (!std::getline(iss, major, '.') || !std::getline(iss, minor, '.')) {
  1831. return result;
  1832. }
  1833. result.Major = static_cast<unsigned int>(std::stoi(major));
  1834. result.Minor = static_cast<unsigned int>(std::stoi(minor));
  1835. return result;
  1836. }
  1837. static cmQtAutoGen::IntegerVersion GetMocVersion(
  1838. const std::string& mocExecutablePath)
  1839. {
  1840. std::string capturedStdOut;
  1841. int exitCode;
  1842. if (!cmSystemTools::RunSingleCommand({ mocExecutablePath, "--version" },
  1843. &capturedStdOut, nullptr, &exitCode,
  1844. nullptr, cmSystemTools::OUTPUT_NONE)) {
  1845. return {};
  1846. }
  1847. if (exitCode != 0) {
  1848. return {};
  1849. }
  1850. return parseMocVersion(capturedStdOut);
  1851. }
  1852. static std::string FindMocExecutableFromMocTarget(cmMakefile const* makefile,
  1853. unsigned int qtMajorVersion)
  1854. {
  1855. std::string result;
  1856. const std::string mocTargetName =
  1857. "Qt" + std::to_string(qtMajorVersion) + "::moc";
  1858. cmTarget const* mocTarget = makefile->FindTargetToUse(mocTargetName);
  1859. if (mocTarget) {
  1860. result = mocTarget->GetSafeProperty("IMPORTED_LOCATION");
  1861. }
  1862. return result;
  1863. }
  1864. std::pair<cmQtAutoGen::IntegerVersion, unsigned int>
  1865. cmQtAutoGenInitializer::GetQtVersion(cmGeneratorTarget const* target,
  1866. std::string mocExecutable)
  1867. {
  1868. // Converts a char ptr to an unsigned int value
  1869. auto toUInt = [](const char* const input) -> unsigned int {
  1870. unsigned long tmp = 0;
  1871. if (input && cmStrToULong(input, &tmp)) {
  1872. return static_cast<unsigned int>(tmp);
  1873. }
  1874. return 0u;
  1875. };
  1876. auto toUInt2 = [](cmValue input) -> unsigned int {
  1877. unsigned long tmp = 0;
  1878. if (input && cmStrToULong(*input, &tmp)) {
  1879. return static_cast<unsigned int>(tmp);
  1880. }
  1881. return 0u;
  1882. };
  1883. // Initialize return value to a default
  1884. std::pair<IntegerVersion, unsigned int> res(
  1885. IntegerVersion(),
  1886. toUInt(target->GetLinkInterfaceDependentStringProperty("QT_MAJOR_VERSION",
  1887. "")));
  1888. // Acquire known Qt versions
  1889. std::vector<cmQtAutoGen::IntegerVersion> knownQtVersions;
  1890. {
  1891. // Qt version variable prefixes
  1892. static std::initializer_list<
  1893. std::pair<cm::string_view, cm::string_view>> const keys{
  1894. { "Qt6Core_VERSION_MAJOR", "Qt6Core_VERSION_MINOR" },
  1895. { "Qt5Core_VERSION_MAJOR", "Qt5Core_VERSION_MINOR" },
  1896. { "QT_VERSION_MAJOR", "QT_VERSION_MINOR" },
  1897. };
  1898. knownQtVersions.reserve(keys.size() * 2);
  1899. // Adds a version to the result (nullptr safe)
  1900. auto addVersion = [&knownQtVersions, &toUInt2](cmValue major,
  1901. cmValue minor) {
  1902. cmQtAutoGen::IntegerVersion ver(toUInt2(major), toUInt2(minor));
  1903. if (ver.Major != 0) {
  1904. knownQtVersions.emplace_back(ver);
  1905. }
  1906. };
  1907. // Read versions from variables
  1908. for (auto const& keyPair : keys) {
  1909. addVersion(target->Makefile->GetDefinition(std::string(keyPair.first)),
  1910. target->Makefile->GetDefinition(std::string(keyPair.second)));
  1911. }
  1912. // Read versions from directory properties
  1913. for (auto const& keyPair : keys) {
  1914. addVersion(target->Makefile->GetProperty(std::string(keyPair.first)),
  1915. target->Makefile->GetProperty(std::string(keyPair.second)));
  1916. }
  1917. }
  1918. // Evaluate known Qt versions
  1919. if (!knownQtVersions.empty()) {
  1920. if (res.second == 0) {
  1921. // No specific version was requested by the target:
  1922. // Use highest known Qt version.
  1923. res.first = knownQtVersions.at(0);
  1924. } else {
  1925. // Pick a version from the known versions:
  1926. for (auto const& it : knownQtVersions) {
  1927. if (it.Major == res.second) {
  1928. res.first = it;
  1929. break;
  1930. }
  1931. }
  1932. }
  1933. }
  1934. if (res.first.Major == 0) {
  1935. // We could not get the version number from variables or directory
  1936. // properties. This might happen if the find_package call for Qt is wrapped
  1937. // in a function. Try to find the moc executable path from the available
  1938. // targets and call "moc --version" to get the Qt version.
  1939. if (mocExecutable.empty()) {
  1940. mocExecutable =
  1941. FindMocExecutableFromMocTarget(target->Makefile, res.second);
  1942. }
  1943. if (!mocExecutable.empty()) {
  1944. res.first = GetMocVersion(mocExecutable);
  1945. }
  1946. }
  1947. return res;
  1948. }
  1949. std::string cmQtAutoGenInitializer::GetMocBuildPath(MUFile const& muf)
  1950. {
  1951. std::string res;
  1952. if (!muf.MocIt) {
  1953. return res;
  1954. }
  1955. std::string basePath =
  1956. cmStrCat(this->PathCheckSum.getPart(muf.FullPath), "/moc_",
  1957. FileNameWithoutLastExtension(muf.FullPath));
  1958. res = cmStrCat(basePath, ".cpp");
  1959. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1960. return res;
  1961. }
  1962. // File name already emitted.
  1963. // Try appending the header suffix to the base path.
  1964. basePath = cmStrCat(basePath, '_', muf.SF->GetExtension());
  1965. res = cmStrCat(basePath, ".cpp");
  1966. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1967. return res;
  1968. }
  1969. // File name with header extension already emitted.
  1970. // Try adding a number to the base path.
  1971. constexpr std::size_t number_begin = 2;
  1972. constexpr std::size_t number_end = 256;
  1973. for (std::size_t ii = number_begin; ii != number_end; ++ii) {
  1974. res = cmStrCat(basePath, '_', ii, ".cpp");
  1975. if (this->Moc.EmittedBuildPaths.emplace(res).second) {
  1976. return res;
  1977. }
  1978. }
  1979. // Output file name conflict (unlikely, but still...)
  1980. cmSystemTools::Error(
  1981. cmStrCat("moc output file name conflict for ", muf.FullPath));
  1982. return res;
  1983. }
  1984. bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars,
  1985. const std::string& executable,
  1986. bool ignoreMissingTarget) const
  1987. {
  1988. auto print_err = [this, &genVars](std::string const& err) {
  1989. cmSystemTools::Error(cmStrCat(genVars.GenNameUpper, " for target ",
  1990. this->GenTarget->GetName(), ": ", err));
  1991. };
  1992. // Custom executable
  1993. {
  1994. std::string const prop = cmStrCat(genVars.GenNameUpper, "_EXECUTABLE");
  1995. std::string const& val = this->GenTarget->Target->GetSafeProperty(prop);
  1996. if (!val.empty()) {
  1997. // Evaluate generator expression
  1998. {
  1999. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  2000. cmGeneratorExpression ge(*this->Makefile->GetCMakeInstance(), lfbt);
  2001. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(val);
  2002. genVars.Executable = cge->Evaluate(this->LocalGen, "");
  2003. }
  2004. if (genVars.Executable.empty() && !ignoreMissingTarget) {
  2005. print_err(prop + " evaluates to an empty value");
  2006. return false;
  2007. }
  2008. // Create empty compiler features.
  2009. genVars.ExecutableFeatures =
  2010. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  2011. return true;
  2012. }
  2013. }
  2014. // Find executable target
  2015. {
  2016. // Find executable target name
  2017. cm::string_view prefix;
  2018. if (this->QtVersion.Major == 4) {
  2019. prefix = "Qt4::";
  2020. } else if (this->QtVersion.Major == 5) {
  2021. prefix = "Qt5::";
  2022. } else if (this->QtVersion.Major == 6) {
  2023. prefix = "Qt6::";
  2024. }
  2025. std::string const targetName = cmStrCat(prefix, executable);
  2026. // Find target
  2027. cmGeneratorTarget* genTarget =
  2028. this->LocalGen->FindGeneratorTargetToUse(targetName);
  2029. if (genTarget) {
  2030. genVars.ExecutableTargetName = targetName;
  2031. genVars.ExecutableTarget = genTarget;
  2032. if (genTarget->IsImported()) {
  2033. genVars.Executable = genTarget->ImportedGetLocation("");
  2034. } else {
  2035. genVars.Executable = genTarget->GetLocation("");
  2036. }
  2037. } else {
  2038. if (ignoreMissingTarget) {
  2039. // Create empty compiler features.
  2040. genVars.ExecutableFeatures =
  2041. std::make_shared<cmQtAutoGen::CompilerFeatures>();
  2042. return true;
  2043. }
  2044. print_err(cmStrCat("Could not find ", executable, " executable target ",
  2045. targetName));
  2046. return false;
  2047. }
  2048. }
  2049. // Get executable features
  2050. {
  2051. std::string err;
  2052. genVars.ExecutableFeatures = this->GlobalInitializer->GetCompilerFeatures(
  2053. executable, genVars.Executable, err);
  2054. if (!genVars.ExecutableFeatures) {
  2055. print_err(err);
  2056. return false;
  2057. }
  2058. }
  2059. return true;
  2060. }
  2061. void cmQtAutoGenInitializer::handleSkipPch(cmSourceFile* sf)
  2062. {
  2063. bool skipPch = true;
  2064. for (auto const& pair : this->AutogenTarget.Sources) {
  2065. if (!pair.first->GetIsGenerated() &&
  2066. !pair.first->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  2067. skipPch = false;
  2068. }
  2069. }
  2070. if (skipPch) {
  2071. sf->SetProperty("SKIP_PRECOMPILE_HEADERS", "ON");
  2072. }
  2073. }