cmExportFileGenerator.cxx 43 KB

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