cmExportFileGenerator.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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 "cmExportFileGenerator.h"
  4. #include <cassert>
  5. #include <cstring>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include "cmsys/FStream.hxx"
  10. #include "cmComputeLinkInformation.h"
  11. #include "cmFileSet.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLinkItem.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmOutputConverter.h"
  20. #include "cmPolicies.h"
  21. #include "cmPropertyMap.h"
  22. #include "cmStateTypes.h"
  23. #include "cmStringAlgorithms.h"
  24. #include "cmSystemTools.h"
  25. #include "cmTarget.h"
  26. #include "cmValue.h"
  27. static std::string cmExportFileGeneratorEscape(std::string const& str)
  28. {
  29. // Escape a property value for writing into a .cmake file.
  30. std::string result = cmOutputConverter::EscapeForCMake(str);
  31. // Un-escape variable references generated by our own export code.
  32. cmSystemTools::ReplaceString(result, "\\${_IMPORT_PREFIX}",
  33. "${_IMPORT_PREFIX}");
  34. cmSystemTools::ReplaceString(result, "\\${CMAKE_IMPORT_LIBRARY_SUFFIX}",
  35. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  36. return result;
  37. }
  38. cmExportFileGenerator::cmExportFileGenerator()
  39. {
  40. this->AppendMode = false;
  41. this->ExportOld = false;
  42. }
  43. void cmExportFileGenerator::AddConfiguration(const std::string& config)
  44. {
  45. this->Configurations.push_back(config);
  46. }
  47. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  48. {
  49. this->MainImportFile = mainFile;
  50. this->FileDir = cmSystemTools::GetFilenamePath(this->MainImportFile);
  51. this->FileBase =
  52. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  53. this->FileExt =
  54. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  55. }
  56. const std::string& cmExportFileGenerator::GetMainExportFileName() const
  57. {
  58. return this->MainImportFile;
  59. }
  60. bool cmExportFileGenerator::GenerateImportFile()
  61. {
  62. // Open the output file to generate it.
  63. std::unique_ptr<cmsys::ofstream> foutPtr;
  64. if (this->AppendMode) {
  65. // Open for append.
  66. auto openmodeApp = std::ios::app;
  67. foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
  68. openmodeApp);
  69. } else {
  70. // Generate atomically and with copy-if-different.
  71. std::unique_ptr<cmGeneratedFileStream> ap(
  72. new cmGeneratedFileStream(this->MainImportFile, true));
  73. ap->SetCopyIfDifferent(true);
  74. foutPtr = std::move(ap);
  75. }
  76. if (!foutPtr || !*foutPtr) {
  77. std::string se = cmSystemTools::GetLastSystemError();
  78. std::ostringstream e;
  79. e << "cannot write to file \"" << this->MainImportFile << "\": " << se;
  80. cmSystemTools::Error(e.str());
  81. return false;
  82. }
  83. std::ostream& os = *foutPtr;
  84. // Start with the import file header.
  85. this->GeneratePolicyHeaderCode(os);
  86. this->GenerateImportHeaderCode(os);
  87. // Create all the imported targets.
  88. bool result = this->GenerateMainFile(os);
  89. // End with the import file footer.
  90. this->GenerateImportFooterCode(os);
  91. this->GeneratePolicyFooterCode(os);
  92. return result;
  93. }
  94. void cmExportFileGenerator::GenerateImportConfig(
  95. std::ostream& os, const std::string& config,
  96. std::vector<std::string>& missingTargets)
  97. {
  98. // Construct the property configuration suffix.
  99. std::string suffix = "_";
  100. if (!config.empty()) {
  101. suffix += cmSystemTools::UpperCase(config);
  102. } else {
  103. suffix += "NOCONFIG";
  104. }
  105. // Generate the per-config target information.
  106. this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
  107. }
  108. void cmExportFileGenerator::PopulateInterfaceProperty(
  109. const std::string& propName, cmGeneratorTarget const* target,
  110. ImportPropertyMap& properties)
  111. {
  112. cmValue input = target->GetProperty(propName);
  113. if (input) {
  114. properties[propName] = *input;
  115. }
  116. }
  117. void cmExportFileGenerator::PopulateInterfaceProperty(
  118. const std::string& propName, const std::string& outputName,
  119. cmGeneratorTarget const* target,
  120. cmGeneratorExpression::PreprocessContext preprocessRule,
  121. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  122. {
  123. cmValue input = target->GetProperty(propName);
  124. if (input) {
  125. if (input->empty()) {
  126. // Set to empty
  127. properties[outputName].clear();
  128. return;
  129. }
  130. std::string prepro =
  131. cmGeneratorExpression::Preprocess(*input, preprocessRule);
  132. if (!prepro.empty()) {
  133. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  134. missingTargets);
  135. properties[outputName] = prepro;
  136. }
  137. }
  138. }
  139. void cmExportFileGenerator::GenerateRequiredCMakeVersion(
  140. std::ostream& os, const char* versionString)
  141. {
  142. /* clang-format off */
  143. os << "if(CMAKE_VERSION VERSION_LESS " << versionString << ")\n"
  144. " message(FATAL_ERROR \"This file relies on consumers using "
  145. "CMake " << versionString << " or greater.\")\n"
  146. "endif()\n\n";
  147. /* clang-format on */
  148. }
  149. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  150. cmGeneratorTarget const* target,
  151. cmGeneratorExpression::PreprocessContext preprocessRule,
  152. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  153. {
  154. if (!target->IsLinkable()) {
  155. return false;
  156. }
  157. cmValue input = target->GetProperty("INTERFACE_LINK_LIBRARIES");
  158. if (input) {
  159. std::string prepro =
  160. cmGeneratorExpression::Preprocess(*input, preprocessRule);
  161. if (!prepro.empty()) {
  162. this->ResolveTargetsInGeneratorExpressions(
  163. prepro, target, missingTargets, ReplaceFreeTargets);
  164. properties["INTERFACE_LINK_LIBRARIES"] = prepro;
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. static bool isSubDirectory(std::string const& a, std::string const& b)
  171. {
  172. return (cmSystemTools::ComparePath(a, b) ||
  173. cmSystemTools::IsSubDirectory(a, b));
  174. }
  175. static bool checkInterfaceDirs(const std::string& prepro,
  176. cmGeneratorTarget const* target,
  177. const std::string& prop)
  178. {
  179. std::string const& installDir =
  180. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  181. std::string const& topSourceDir =
  182. target->GetLocalGenerator()->GetSourceDirectory();
  183. std::string const& topBinaryDir =
  184. target->GetLocalGenerator()->GetBinaryDirectory();
  185. std::vector<std::string> parts;
  186. cmGeneratorExpression::Split(prepro, parts);
  187. const bool inSourceBuild = topSourceDir == topBinaryDir;
  188. bool hadFatalError = false;
  189. for (std::string const& li : parts) {
  190. size_t genexPos = cmGeneratorExpression::Find(li);
  191. if (genexPos == 0) {
  192. continue;
  193. }
  194. if (cmHasLiteralPrefix(li, "${_IMPORT_PREFIX}")) {
  195. continue;
  196. }
  197. MessageType messageType = MessageType::FATAL_ERROR;
  198. std::ostringstream e;
  199. if (genexPos != std::string::npos) {
  200. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  201. switch (target->GetPolicyStatusCMP0041()) {
  202. case cmPolicies::WARN:
  203. messageType = MessageType::WARNING;
  204. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0041) << "\n";
  205. break;
  206. case cmPolicies::OLD:
  207. continue;
  208. case cmPolicies::REQUIRED_IF_USED:
  209. case cmPolicies::REQUIRED_ALWAYS:
  210. case cmPolicies::NEW:
  211. hadFatalError = true;
  212. break; // Issue fatal message.
  213. }
  214. } else {
  215. hadFatalError = true;
  216. }
  217. }
  218. if (!cmSystemTools::FileIsFullPath(li)) {
  219. /* clang-format off */
  220. e << "Target \"" << target->GetName() << "\" " << prop <<
  221. " property contains relative path:\n"
  222. " \"" << li << "\"";
  223. /* clang-format on */
  224. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  225. }
  226. bool inBinary = isSubDirectory(li, topBinaryDir);
  227. bool inSource = isSubDirectory(li, topSourceDir);
  228. if (isSubDirectory(li, installDir)) {
  229. // The include directory is inside the install tree. If the
  230. // install tree is not inside the source tree or build tree then
  231. // fall through to the checks below that the include directory is not
  232. // also inside the source tree or build tree.
  233. bool shouldContinue =
  234. (!inBinary || isSubDirectory(installDir, topBinaryDir)) &&
  235. (!inSource || isSubDirectory(installDir, topSourceDir));
  236. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  237. if (!shouldContinue) {
  238. switch (target->GetPolicyStatusCMP0052()) {
  239. case cmPolicies::WARN: {
  240. std::ostringstream s;
  241. s << cmPolicies::GetPolicyWarning(cmPolicies::CMP0052) << "\n";
  242. s << "Directory:\n \"" << li
  243. << "\"\nin "
  244. "INTERFACE_INCLUDE_DIRECTORIES of target \""
  245. << target->GetName()
  246. << "\" is a subdirectory of the install "
  247. "directory:\n \""
  248. << installDir
  249. << "\"\nhowever it is also "
  250. "a subdirectory of the "
  251. << (inBinary ? "build" : "source") << " tree:\n \""
  252. << (inBinary ? topBinaryDir : topSourceDir) << "\"\n";
  253. target->GetLocalGenerator()->IssueMessage(
  254. MessageType::AUTHOR_WARNING, s.str());
  255. CM_FALLTHROUGH;
  256. }
  257. case cmPolicies::OLD:
  258. shouldContinue = true;
  259. break;
  260. case cmPolicies::REQUIRED_ALWAYS:
  261. case cmPolicies::REQUIRED_IF_USED:
  262. case cmPolicies::NEW:
  263. break;
  264. }
  265. }
  266. }
  267. if (shouldContinue) {
  268. continue;
  269. }
  270. }
  271. if (inBinary) {
  272. /* clang-format off */
  273. e << "Target \"" << target->GetName() << "\" " << prop <<
  274. " property contains path:\n"
  275. " \"" << li << "\"\nwhich is prefixed in the build directory.";
  276. /* clang-format on */
  277. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  278. }
  279. if (!inSourceBuild) {
  280. if (inSource) {
  281. e << "Target \"" << target->GetName() << "\" " << prop
  282. << " property contains path:\n"
  283. " \""
  284. << li << "\"\nwhich is prefixed in the source directory.";
  285. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  286. }
  287. }
  288. }
  289. return !hadFatalError;
  290. }
  291. static void prefixItems(std::string& exportDirs)
  292. {
  293. std::vector<std::string> entries;
  294. cmGeneratorExpression::Split(exportDirs, entries);
  295. exportDirs.clear();
  296. const char* sep = "";
  297. for (std::string const& e : entries) {
  298. exportDirs += sep;
  299. sep = ";";
  300. if (!cmSystemTools::FileIsFullPath(e) &&
  301. e.find("${_IMPORT_PREFIX}") == std::string::npos) {
  302. exportDirs += "${_IMPORT_PREFIX}/";
  303. }
  304. exportDirs += e;
  305. }
  306. }
  307. void cmExportFileGenerator::PopulateSourcesInterface(
  308. cmGeneratorTarget const* gt,
  309. cmGeneratorExpression::PreprocessContext preprocessRule,
  310. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  311. {
  312. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  313. const char* propName = "INTERFACE_SOURCES";
  314. cmValue input = gt->GetProperty(propName);
  315. if (!input) {
  316. return;
  317. }
  318. if (input->empty()) {
  319. properties[propName].clear();
  320. return;
  321. }
  322. std::string prepro =
  323. cmGeneratorExpression::Preprocess(*input, preprocessRule, true);
  324. if (!prepro.empty()) {
  325. this->ResolveTargetsInGeneratorExpressions(prepro, gt, missingTargets);
  326. if (!checkInterfaceDirs(prepro, gt, propName)) {
  327. return;
  328. }
  329. properties[propName] = prepro;
  330. }
  331. }
  332. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  333. cmGeneratorTarget const* target,
  334. cmGeneratorExpression::PreprocessContext preprocessRule,
  335. ImportPropertyMap& properties, std::vector<std::string>& missingTargets,
  336. cmTargetExport const& te)
  337. {
  338. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  339. const char* propName = "INTERFACE_INCLUDE_DIRECTORIES";
  340. cmValue input = target->GetProperty(propName);
  341. cmGeneratorExpression ge;
  342. std::string dirs = cmGeneratorExpression::Preprocess(
  343. cmJoin(target->Target->GetInstallIncludeDirectoriesEntries(te), ";"),
  344. preprocessRule, true);
  345. this->ReplaceInstallPrefix(dirs);
  346. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(dirs);
  347. std::string exportDirs =
  348. cge->Evaluate(target->GetLocalGenerator(), "", target);
  349. if (cge->GetHadContextSensitiveCondition()) {
  350. cmLocalGenerator* lg = target->GetLocalGenerator();
  351. std::ostringstream e;
  352. e << "Target \"" << target->GetName()
  353. << "\" is installed with "
  354. "INCLUDES DESTINATION set to a context sensitive path. Paths which "
  355. "depend on the configuration, policy values or the link interface "
  356. "are "
  357. "not supported. Consider using target_include_directories instead.";
  358. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  359. return;
  360. }
  361. if (!input && exportDirs.empty()) {
  362. return;
  363. }
  364. if ((input && input->empty()) && exportDirs.empty()) {
  365. // Set to empty
  366. properties[propName].clear();
  367. return;
  368. }
  369. prefixItems(exportDirs);
  370. std::string includes = (input ? *input : "");
  371. const char* sep = input ? ";" : "";
  372. includes += sep + exportDirs;
  373. std::string prepro =
  374. cmGeneratorExpression::Preprocess(includes, preprocessRule, true);
  375. if (!prepro.empty()) {
  376. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets);
  377. if (!checkInterfaceDirs(prepro, target, propName)) {
  378. return;
  379. }
  380. properties[propName] = prepro;
  381. }
  382. }
  383. void cmExportFileGenerator::PopulateLinkDependsInterface(
  384. cmGeneratorTarget const* gt,
  385. cmGeneratorExpression::PreprocessContext preprocessRule,
  386. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  387. {
  388. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  389. const char* propName = "INTERFACE_LINK_DEPENDS";
  390. cmValue input = gt->GetProperty(propName);
  391. if (!input) {
  392. return;
  393. }
  394. if (input->empty()) {
  395. properties[propName].clear();
  396. return;
  397. }
  398. std::string prepro =
  399. cmGeneratorExpression::Preprocess(*input, preprocessRule, true);
  400. if (!prepro.empty()) {
  401. this->ResolveTargetsInGeneratorExpressions(prepro, gt, missingTargets);
  402. if (!checkInterfaceDirs(prepro, gt, propName)) {
  403. return;
  404. }
  405. properties[propName] = prepro;
  406. }
  407. }
  408. void cmExportFileGenerator::PopulateLinkDirectoriesInterface(
  409. cmGeneratorTarget const* gt,
  410. cmGeneratorExpression::PreprocessContext preprocessRule,
  411. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  412. {
  413. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  414. const char* propName = "INTERFACE_LINK_DIRECTORIES";
  415. cmValue input = gt->GetProperty(propName);
  416. if (!input) {
  417. return;
  418. }
  419. if (input->empty()) {
  420. properties[propName].clear();
  421. return;
  422. }
  423. std::string prepro =
  424. cmGeneratorExpression::Preprocess(*input, preprocessRule, true);
  425. if (!prepro.empty()) {
  426. this->ResolveTargetsInGeneratorExpressions(prepro, gt, missingTargets);
  427. if (!checkInterfaceDirs(prepro, gt, propName)) {
  428. return;
  429. }
  430. properties[propName] = prepro;
  431. }
  432. }
  433. void cmExportFileGenerator::PopulateInterfaceProperty(
  434. const std::string& propName, cmGeneratorTarget const* target,
  435. cmGeneratorExpression::PreprocessContext preprocessRule,
  436. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  437. {
  438. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  439. properties, missingTargets);
  440. }
  441. static void getPropertyContents(cmGeneratorTarget const* tgt,
  442. const std::string& prop,
  443. std::set<std::string>& ifaceProperties)
  444. {
  445. cmValue p = tgt->GetProperty(prop);
  446. if (!p) {
  447. return;
  448. }
  449. std::vector<std::string> content = cmExpandedList(*p);
  450. ifaceProperties.insert(content.begin(), content.end());
  451. }
  452. static void getCompatibleInterfaceProperties(
  453. cmGeneratorTarget const* target, std::set<std::string>& ifaceProperties,
  454. const std::string& config)
  455. {
  456. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  457. // object libraries have no link information, so nothing to compute
  458. return;
  459. }
  460. cmComputeLinkInformation* info = target->GetLinkInformation(config);
  461. if (!info) {
  462. cmLocalGenerator* lg = target->GetLocalGenerator();
  463. std::ostringstream e;
  464. e << "Exporting the target \"" << target->GetName()
  465. << "\" is not "
  466. "allowed since its linker language cannot be determined";
  467. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  468. return;
  469. }
  470. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  471. for (auto const& dep : deps) {
  472. if (!dep.Target) {
  473. continue;
  474. }
  475. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_BOOL",
  476. ifaceProperties);
  477. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_STRING",
  478. ifaceProperties);
  479. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  480. ifaceProperties);
  481. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  482. ifaceProperties);
  483. }
  484. }
  485. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  486. cmGeneratorTarget const* gtarget, ImportPropertyMap& properties)
  487. {
  488. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL", gtarget,
  489. properties);
  490. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING", gtarget,
  491. properties);
  492. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN", gtarget,
  493. properties);
  494. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX", gtarget,
  495. properties);
  496. std::set<std::string> ifaceProperties;
  497. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  498. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  499. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  500. ifaceProperties);
  501. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  502. ifaceProperties);
  503. if (gtarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  504. std::vector<std::string> configNames =
  505. gtarget->Target->GetMakefile()->GetGeneratorConfigs(
  506. cmMakefile::IncludeEmptyConfig);
  507. for (std::string const& cn : configNames) {
  508. getCompatibleInterfaceProperties(gtarget, ifaceProperties, cn);
  509. }
  510. }
  511. for (std::string const& ip : ifaceProperties) {
  512. this->PopulateInterfaceProperty("INTERFACE_" + ip, gtarget, properties);
  513. }
  514. }
  515. void cmExportFileGenerator::GenerateInterfaceProperties(
  516. const cmGeneratorTarget* target, std::ostream& os,
  517. const ImportPropertyMap& properties)
  518. {
  519. if (!properties.empty()) {
  520. std::string targetName =
  521. cmStrCat(this->Namespace, target->GetExportName());
  522. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  523. for (auto const& property : properties) {
  524. os << " " << property.first << " "
  525. << cmExportFileGeneratorEscape(property.second) << "\n";
  526. }
  527. os << ")\n\n";
  528. }
  529. }
  530. bool cmExportFileGenerator::AddTargetNamespace(
  531. std::string& input, cmGeneratorTarget const* target,
  532. std::vector<std::string>& missingTargets)
  533. {
  534. cmGeneratorTarget::TargetOrString resolved =
  535. target->ResolveTargetReference(input);
  536. cmGeneratorTarget* tgt = resolved.Target;
  537. if (!tgt) {
  538. input = resolved.String;
  539. return false;
  540. }
  541. if (tgt->IsImported()) {
  542. input = tgt->GetName();
  543. return true;
  544. }
  545. if (this->ExportedTargets.find(tgt) != this->ExportedTargets.end()) {
  546. input = this->Namespace + tgt->GetExportName();
  547. } else {
  548. std::string namespacedTarget;
  549. this->HandleMissingTarget(namespacedTarget, missingTargets, target, tgt);
  550. if (!namespacedTarget.empty()) {
  551. input = namespacedTarget;
  552. } else {
  553. input = tgt->GetName();
  554. }
  555. }
  556. return true;
  557. }
  558. void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  559. std::string& input, cmGeneratorTarget const* target,
  560. std::vector<std::string>& missingTargets, FreeTargetsReplace replace)
  561. {
  562. if (replace == NoReplaceFreeTargets) {
  563. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  564. return;
  565. }
  566. std::vector<std::string> parts;
  567. cmGeneratorExpression::Split(input, parts);
  568. std::string sep;
  569. input.clear();
  570. for (std::string& li : parts) {
  571. if (cmHasLiteralPrefix(li, CMAKE_DIRECTORY_ID_SEP)) {
  572. continue;
  573. }
  574. if (cmGeneratorExpression::Find(li) == std::string::npos) {
  575. this->AddTargetNamespace(li, target, missingTargets);
  576. } else {
  577. this->ResolveTargetsInGeneratorExpression(li, target, missingTargets);
  578. }
  579. input += sep + li;
  580. sep = ";";
  581. }
  582. }
  583. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  584. std::string& input, cmGeneratorTarget const* target,
  585. std::vector<std::string>& missingTargets)
  586. {
  587. std::string::size_type pos = 0;
  588. std::string::size_type lastPos = pos;
  589. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  590. std::string::npos) {
  591. std::string::size_type nameStartPos =
  592. pos + sizeof("$<TARGET_PROPERTY:") - 1;
  593. std::string::size_type closePos = input.find('>', nameStartPos);
  594. std::string::size_type commaPos = input.find(',', nameStartPos);
  595. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  596. if (commaPos == std::string::npos // Implied 'this' target
  597. || closePos == std::string::npos // Incomplete expression.
  598. || closePos < commaPos // Implied 'this' target
  599. || nextOpenPos < commaPos) // Non-literal
  600. {
  601. lastPos = nameStartPos;
  602. continue;
  603. }
  604. std::string targetName =
  605. input.substr(nameStartPos, commaPos - nameStartPos);
  606. if (this->AddTargetNamespace(targetName, target, missingTargets)) {
  607. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  608. }
  609. lastPos = nameStartPos + targetName.size() + 1;
  610. }
  611. std::string errorString;
  612. pos = 0;
  613. lastPos = pos;
  614. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  615. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  616. std::string::size_type endPos = input.find('>', nameStartPos);
  617. if (endPos == std::string::npos) {
  618. errorString = "$<TARGET_NAME:...> expression incomplete";
  619. break;
  620. }
  621. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  622. if (targetName.find("$<") != std::string::npos) {
  623. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  624. "literal.";
  625. break;
  626. }
  627. if (!this->AddTargetNamespace(targetName, target, missingTargets)) {
  628. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  629. "reachable target.";
  630. break;
  631. }
  632. input.replace(pos, endPos - pos + 1, targetName);
  633. lastPos = pos + targetName.size();
  634. }
  635. pos = 0;
  636. lastPos = pos;
  637. while (errorString.empty() &&
  638. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  639. std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
  640. std::string::size_type endPos = input.find('>', nameStartPos);
  641. if (endPos == std::string::npos) {
  642. errorString = "$<LINK_ONLY:...> expression incomplete";
  643. break;
  644. }
  645. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  646. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  647. this->AddTargetNamespace(libName, target, missingTargets)) {
  648. input.replace(nameStartPos, endPos - nameStartPos, libName);
  649. }
  650. lastPos = nameStartPos + libName.size() + 1;
  651. }
  652. this->ReplaceInstallPrefix(input);
  653. if (!errorString.empty()) {
  654. target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  655. errorString);
  656. }
  657. }
  658. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/)
  659. {
  660. // Do nothing
  661. }
  662. void cmExportFileGenerator::SetImportLinkInterface(
  663. const std::string& config, std::string const& suffix,
  664. cmGeneratorExpression::PreprocessContext preprocessRule,
  665. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  666. std::vector<std::string>& missingTargets)
  667. {
  668. // Add the transitive link dependencies for this configuration.
  669. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  670. if (!iface) {
  671. return;
  672. }
  673. if (iface->ImplementationIsInterface) {
  674. // Policy CMP0022 must not be NEW.
  675. this->SetImportLinkProperty(
  676. suffix, target, "IMPORTED_LINK_INTERFACE_LIBRARIES", iface->Libraries,
  677. properties, missingTargets, ImportLinkPropertyTargetNames::Yes);
  678. return;
  679. }
  680. cmValue propContent;
  681. if (cmValue prop_suffixed =
  682. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  683. propContent = prop_suffixed;
  684. } else if (cmValue prop = target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  685. propContent = prop;
  686. } else {
  687. return;
  688. }
  689. const bool newCMP0022Behavior =
  690. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  691. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  692. if (newCMP0022Behavior && !this->ExportOld) {
  693. cmLocalGenerator* lg = target->GetLocalGenerator();
  694. std::ostringstream e;
  695. e << "Target \"" << target->GetName()
  696. << "\" has policy CMP0022 enabled, "
  697. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  698. "populated, but it was exported without the "
  699. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  700. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  701. return;
  702. }
  703. if (propContent->empty()) {
  704. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  705. return;
  706. }
  707. std::string prepro =
  708. cmGeneratorExpression::Preprocess(*propContent, preprocessRule);
  709. if (!prepro.empty()) {
  710. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets,
  711. ReplaceFreeTargets);
  712. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  713. }
  714. }
  715. void cmExportFileGenerator::SetImportDetailProperties(
  716. const std::string& config, std::string const& suffix,
  717. cmGeneratorTarget* target, ImportPropertyMap& properties,
  718. std::vector<std::string>& missingTargets)
  719. {
  720. // Get the makefile in which to lookup target information.
  721. cmMakefile* mf = target->Makefile;
  722. // Add the soname for unix shared libraries.
  723. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  724. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  725. if (!target->IsDLLPlatform()) {
  726. std::string prop;
  727. std::string value;
  728. if (target->HasSOName(config)) {
  729. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  730. value = this->InstallNameDir(target, config);
  731. }
  732. prop = "IMPORTED_SONAME";
  733. value += target->GetSOName(config);
  734. } else {
  735. prop = "IMPORTED_NO_SONAME";
  736. value = "TRUE";
  737. }
  738. prop += suffix;
  739. properties[prop] = value;
  740. }
  741. }
  742. // Add the transitive link dependencies for this configuration.
  743. if (cmLinkInterface const* iface =
  744. target->GetLinkInterface(config, target)) {
  745. this->SetImportLinkProperty(
  746. suffix, target, "IMPORTED_LINK_INTERFACE_LANGUAGES", iface->Languages,
  747. properties, missingTargets, ImportLinkPropertyTargetNames::No);
  748. std::vector<std::string> dummy;
  749. this->SetImportLinkProperty(
  750. suffix, target, "IMPORTED_LINK_DEPENDENT_LIBRARIES", iface->SharedDeps,
  751. properties, dummy, ImportLinkPropertyTargetNames::Yes);
  752. if (iface->Multiplicity > 0) {
  753. std::string prop =
  754. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  755. properties[prop] = std::to_string(iface->Multiplicity);
  756. }
  757. }
  758. // Add information if this target is a managed target
  759. if (target->GetManagedType(config) !=
  760. cmGeneratorTarget::ManagedType::Native) {
  761. std::string prop = cmStrCat("IMPORTED_COMMON_LANGUAGE_RUNTIME", suffix);
  762. std::string propval;
  763. if (cmValue p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  764. propval = *p;
  765. } else if (target->IsCSharpOnly()) {
  766. // C# projects do not have the /clr flag, so we set the property
  767. // here to mark the target as (only) managed (i.e. no .lib file
  768. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  769. // property would have to be set manually for C# targets to make
  770. // exporting/importing work.
  771. propval = "CSharp";
  772. }
  773. properties[prop] = propval;
  774. }
  775. }
  776. static std::string const& asString(std::string const& l)
  777. {
  778. return l;
  779. }
  780. static std::string const& asString(cmLinkItem const& l)
  781. {
  782. return l.AsStr();
  783. }
  784. template <typename T>
  785. void cmExportFileGenerator::SetImportLinkProperty(
  786. std::string const& suffix, cmGeneratorTarget const* target,
  787. const std::string& propName, std::vector<T> const& entries,
  788. ImportPropertyMap& properties, std::vector<std::string>& missingTargets,
  789. ImportLinkPropertyTargetNames targetNames)
  790. {
  791. // Skip the property if there are no entries.
  792. if (entries.empty()) {
  793. return;
  794. }
  795. // Construct the property value.
  796. std::string link_entries;
  797. const char* sep = "";
  798. for (T const& l : entries) {
  799. // Separate this from the previous entry.
  800. link_entries += sep;
  801. sep = ";";
  802. if (targetNames == ImportLinkPropertyTargetNames::Yes) {
  803. std::string temp = asString(l);
  804. this->AddTargetNamespace(temp, target, missingTargets);
  805. link_entries += temp;
  806. } else {
  807. link_entries += asString(l);
  808. }
  809. }
  810. // Store the property.
  811. std::string prop = cmStrCat(propName, suffix);
  812. properties[prop] = link_entries;
  813. }
  814. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  815. {
  816. // Protect that file against use with older CMake versions.
  817. /* clang-format off */
  818. os << "# Generated by CMake\n\n";
  819. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.6)\n"
  820. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  821. << "endif()\n";
  822. /* clang-format on */
  823. // Isolate the file policy level.
  824. // Support CMake versions as far back as 2.6 but also support using NEW
  825. // policy settings for up to CMake 3.21 (this upper limit may be reviewed
  826. // and increased from time to time). This reduces the opportunity for CMake
  827. // warnings when an older export file is later used with newer CMake
  828. // versions.
  829. /* clang-format off */
  830. os << "cmake_policy(PUSH)\n"
  831. << "cmake_policy(VERSION 2.6...3.21)\n";
  832. /* clang-format on */
  833. }
  834. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  835. {
  836. os << "cmake_policy(POP)\n";
  837. }
  838. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  839. const std::string& config)
  840. {
  841. os << "#----------------------------------------------------------------\n"
  842. << "# Generated CMake target import file";
  843. if (!config.empty()) {
  844. os << " for configuration \"" << config << "\".\n";
  845. } else {
  846. os << ".\n";
  847. }
  848. os << "#----------------------------------------------------------------\n"
  849. << "\n";
  850. this->GenerateImportVersionCode(os);
  851. }
  852. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  853. {
  854. os << "# Commands beyond this point should not need to know the version.\n"
  855. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  856. }
  857. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  858. {
  859. // Store an import file format version. This will let us change the
  860. // format later while still allowing old import files to work.
  861. /* clang-format off */
  862. os << "# Commands may need to know the format version.\n"
  863. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  864. << "\n";
  865. /* clang-format on */
  866. }
  867. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  868. std::ostream& os, const std::string& expectedTargets)
  869. {
  870. /* clang-format off */
  871. os << "# Protect against multiple inclusion, which would fail when already "
  872. "imported targets are added once more.\n"
  873. "set(_targetsDefined)\n"
  874. "set(_targetsNotDefined)\n"
  875. "set(_expectedTargets)\n"
  876. "foreach(_expectedTarget " << expectedTargets << ")\n"
  877. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  878. " if(NOT TARGET ${_expectedTarget})\n"
  879. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  880. " endif()\n"
  881. " if(TARGET ${_expectedTarget})\n"
  882. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  883. " endif()\n"
  884. "endforeach()\n"
  885. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  886. " unset(_targetsDefined)\n"
  887. " unset(_targetsNotDefined)\n"
  888. " unset(_expectedTargets)\n"
  889. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  890. " cmake_policy(POP)\n"
  891. " return()\n"
  892. "endif()\n"
  893. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  894. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  895. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  896. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  897. "endif()\n"
  898. "unset(_targetsDefined)\n"
  899. "unset(_targetsNotDefined)\n"
  900. "unset(_expectedTargets)\n"
  901. "\n\n";
  902. /* clang-format on */
  903. }
  904. void cmExportFileGenerator::GenerateImportTargetCode(
  905. std::ostream& os, cmGeneratorTarget const* target,
  906. cmStateEnums::TargetType targetType)
  907. {
  908. // Construct the imported target name.
  909. std::string targetName = this->Namespace;
  910. targetName += target->GetExportName();
  911. // Create the imported target.
  912. os << "# Create imported target " << targetName << "\n";
  913. switch (targetType) {
  914. case cmStateEnums::EXECUTABLE:
  915. os << "add_executable(" << targetName << " IMPORTED)\n";
  916. break;
  917. case cmStateEnums::STATIC_LIBRARY:
  918. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  919. break;
  920. case cmStateEnums::SHARED_LIBRARY:
  921. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  922. break;
  923. case cmStateEnums::MODULE_LIBRARY:
  924. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  925. break;
  926. case cmStateEnums::UNKNOWN_LIBRARY:
  927. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  928. break;
  929. case cmStateEnums::OBJECT_LIBRARY:
  930. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  931. break;
  932. case cmStateEnums::INTERFACE_LIBRARY:
  933. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  934. break;
  935. default: // should never happen
  936. break;
  937. }
  938. // Mark the imported executable if it has exports.
  939. if (target->IsExecutableWithExports()) {
  940. os << "set_property(TARGET " << targetName
  941. << " PROPERTY ENABLE_EXPORTS 1)\n";
  942. }
  943. // Mark the imported library if it is a framework.
  944. if (target->IsFrameworkOnApple()) {
  945. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  946. }
  947. // Mark the imported executable if it is an application bundle.
  948. if (target->IsAppBundleOnApple()) {
  949. os << "set_property(TARGET " << targetName
  950. << " PROPERTY MACOSX_BUNDLE 1)\n";
  951. }
  952. if (target->IsCFBundleOnApple()) {
  953. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  954. }
  955. // generate DEPRECATION
  956. if (target->IsDeprecated()) {
  957. os << "set_property(TARGET " << targetName << " PROPERTY DEPRECATION "
  958. << cmExportFileGeneratorEscape(target->GetDeprecation()) << ")\n";
  959. }
  960. if (target->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
  961. os << "set_property(TARGET " << targetName
  962. << " PROPERTY IMPORTED_NO_SYSTEM 1)\n";
  963. }
  964. os << "\n";
  965. }
  966. void cmExportFileGenerator::GenerateImportPropertyCode(
  967. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  968. ImportPropertyMap const& properties)
  969. {
  970. // Construct the imported target name.
  971. std::string targetName = this->Namespace;
  972. targetName += target->GetExportName();
  973. // Set the import properties.
  974. os << "# Import target \"" << targetName << "\" for configuration \""
  975. << config << "\"\n";
  976. os << "set_property(TARGET " << targetName
  977. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  978. if (!config.empty()) {
  979. os << cmSystemTools::UpperCase(config);
  980. } else {
  981. os << "NOCONFIG";
  982. }
  983. os << ")\n";
  984. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  985. for (auto const& property : properties) {
  986. os << " " << property.first << " "
  987. << cmExportFileGeneratorEscape(property.second) << "\n";
  988. }
  989. os << " )\n"
  990. << "\n";
  991. }
  992. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  993. std::ostream& os, const std::vector<std::string>& missingTargets)
  994. {
  995. if (missingTargets.empty()) {
  996. /* clang-format off */
  997. os << "# This file does not depend on other imported targets which have\n"
  998. "# been exported from the same project but in a separate "
  999. "export set.\n\n";
  1000. /* clang-format on */
  1001. return;
  1002. }
  1003. /* clang-format off */
  1004. os << "# Make sure the targets which have been exported in some other\n"
  1005. "# export set exist.\n"
  1006. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1007. "foreach(_target ";
  1008. /* clang-format on */
  1009. std::set<std::string> emitted;
  1010. for (std::string const& missingTarget : missingTargets) {
  1011. if (emitted.insert(missingTarget).second) {
  1012. os << "\"" << missingTarget << "\" ";
  1013. }
  1014. }
  1015. /* clang-format off */
  1016. os << ")\n"
  1017. " if(NOT TARGET \"${_target}\" )\n"
  1018. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  1019. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  1020. "\n"
  1021. " endif()\n"
  1022. "endforeach()\n"
  1023. "\n"
  1024. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1025. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  1026. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  1027. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  1028. "\"The following imported targets are "
  1029. "referenced, but are missing: "
  1030. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1031. " else()\n"
  1032. " message(FATAL_ERROR \"The following imported targets are "
  1033. "referenced, but are missing: "
  1034. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1035. " endif()\n"
  1036. "endif()\n"
  1037. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1038. "\n";
  1039. /* clang-format on */
  1040. }
  1041. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  1042. {
  1043. // Add code which verifies at cmake time that the file which is being
  1044. // imported actually exists on disk. This should in theory always be theory
  1045. // case, but still when packages are split into normal and development
  1046. // packages this might get broken (e.g. the Config.cmake could be part of
  1047. // the non-development package, something similar happened to me without
  1048. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1049. // but the development package was not installed.).
  1050. /* clang-format off */
  1051. os << "# Loop over all imported files and verify that they actually exist\n"
  1052. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1053. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1054. " if(NOT EXISTS \"${file}\" )\n"
  1055. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1056. " references the file\n"
  1057. " \\\"${file}\\\"\n"
  1058. "but this file does not exist. Possible reasons include:\n"
  1059. "* The file was deleted, renamed, or moved to another location.\n"
  1060. "* An install or uninstall procedure did not complete successfully.\n"
  1061. "* The installation package was faulty and contained\n"
  1062. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1063. "but not all the files it references.\n"
  1064. "\")\n"
  1065. " endif()\n"
  1066. " endforeach()\n"
  1067. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1068. "endforeach()\n"
  1069. "unset(_IMPORT_CHECK_TARGETS)\n"
  1070. "\n";
  1071. /* clang-format on */
  1072. }
  1073. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1074. std::ostream& os, cmGeneratorTarget* target,
  1075. ImportPropertyMap const& properties,
  1076. const std::set<std::string>& importedLocations)
  1077. {
  1078. // Construct the imported target name.
  1079. std::string targetName = cmStrCat(this->Namespace, target->GetExportName());
  1080. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1081. << " )\n"
  1082. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1083. << targetName << " ";
  1084. for (std::string const& li : importedLocations) {
  1085. auto pi = properties.find(li);
  1086. if (pi != properties.end()) {
  1087. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1088. }
  1089. }
  1090. os << ")\n\n";
  1091. }
  1092. bool cmExportFileGenerator::PopulateExportProperties(
  1093. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  1094. std::string& errorMessage)
  1095. {
  1096. const auto& targetProperties = gte->Target->GetProperties();
  1097. if (cmValue exportProperties =
  1098. targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
  1099. for (auto& prop : cmExpandedList(*exportProperties)) {
  1100. /* Black list reserved properties */
  1101. if (cmHasLiteralPrefix(prop, "IMPORTED_") ||
  1102. cmHasLiteralPrefix(prop, "INTERFACE_")) {
  1103. std::ostringstream e;
  1104. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1105. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1106. << "properties are reserved.";
  1107. errorMessage = e.str();
  1108. return false;
  1109. }
  1110. cmValue propertyValue = targetProperties.GetPropertyValue(prop);
  1111. if (!propertyValue) {
  1112. // Asked to export a property that isn't defined on the target. Do not
  1113. // consider this an error, there's just nothing to export.
  1114. continue;
  1115. }
  1116. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1117. *propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1118. if (evaluatedValue != *propertyValue) {
  1119. std::ostringstream e;
  1120. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1121. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1122. << "generator expression. This is not allowed.";
  1123. errorMessage = e.str();
  1124. return false;
  1125. }
  1126. properties[prop] = *propertyValue;
  1127. }
  1128. }
  1129. return true;
  1130. }
  1131. void cmExportFileGenerator::GenerateTargetFileSets(cmGeneratorTarget* gte,
  1132. std::ostream& os,
  1133. cmTargetExport* te)
  1134. {
  1135. auto interfaceFileSets = gte->Target->GetAllInterfaceFileSets();
  1136. if (!interfaceFileSets.empty()) {
  1137. std::string targetName = cmStrCat(this->Namespace, gte->GetExportName());
  1138. os << "if(NOT CMAKE_VERSION VERSION_LESS \"3.23.0\")\n"
  1139. " target_sources("
  1140. << targetName << "\n";
  1141. for (auto const& name : interfaceFileSets) {
  1142. auto* fileSet = gte->Target->GetFileSet(name);
  1143. if (!fileSet) {
  1144. gte->Makefile->IssueMessage(
  1145. MessageType::FATAL_ERROR,
  1146. cmStrCat("File set \"", name,
  1147. "\" is listed in interface file sets of ", gte->GetName(),
  1148. " but has not been created"));
  1149. return;
  1150. }
  1151. os << " INTERFACE"
  1152. << "\n FILE_SET " << cmOutputConverter::EscapeForCMake(name)
  1153. << "\n TYPE "
  1154. << cmOutputConverter::EscapeForCMake(fileSet->GetType())
  1155. << "\n BASE_DIRS "
  1156. << this->GetFileSetDirectories(gte, fileSet, te) << "\n FILES "
  1157. << this->GetFileSetFiles(gte, fileSet, te) << "\n";
  1158. }
  1159. os << " )\nendif()\n\n";
  1160. }
  1161. }