cmQtAutoGenInitializer.cxx 61 KB

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