cmQtAutoGenInitializer.cxx 61 KB

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