cmExportFileGenerator.cxx 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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 "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. static const std::array<std::string, 3> linkIfaceProps = {
  158. { "INTERFACE_LINK_LIBRARIES", "INTERFACE_LINK_LIBRARIES_DIRECT",
  159. "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE" }
  160. };
  161. bool hadINTERFACE_LINK_LIBRARIES = false;
  162. for (std::string const& linkIfaceProp : linkIfaceProps) {
  163. if (cmValue input = target->GetProperty(linkIfaceProp)) {
  164. std::string prepro =
  165. cmGeneratorExpression::Preprocess(*input, preprocessRule);
  166. if (!prepro.empty()) {
  167. this->ResolveTargetsInGeneratorExpressions(
  168. prepro, target, missingTargets, ReplaceFreeTargets);
  169. properties[linkIfaceProp] = prepro;
  170. hadINTERFACE_LINK_LIBRARIES = true;
  171. }
  172. }
  173. }
  174. return hadINTERFACE_LINK_LIBRARIES;
  175. }
  176. static bool isSubDirectory(std::string const& a, std::string const& b)
  177. {
  178. return (cmSystemTools::ComparePath(a, b) ||
  179. cmSystemTools::IsSubDirectory(a, b));
  180. }
  181. static bool checkInterfaceDirs(const std::string& prepro,
  182. cmGeneratorTarget const* target,
  183. const std::string& prop)
  184. {
  185. std::string const& installDir =
  186. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  187. std::string const& topSourceDir =
  188. target->GetLocalGenerator()->GetSourceDirectory();
  189. std::string const& topBinaryDir =
  190. target->GetLocalGenerator()->GetBinaryDirectory();
  191. std::vector<std::string> parts;
  192. cmGeneratorExpression::Split(prepro, parts);
  193. const bool inSourceBuild = topSourceDir == topBinaryDir;
  194. bool hadFatalError = false;
  195. for (std::string const& li : parts) {
  196. size_t genexPos = cmGeneratorExpression::Find(li);
  197. if (genexPos == 0) {
  198. continue;
  199. }
  200. if (cmHasLiteralPrefix(li, "${_IMPORT_PREFIX}")) {
  201. continue;
  202. }
  203. MessageType messageType = MessageType::FATAL_ERROR;
  204. std::ostringstream e;
  205. if (genexPos != std::string::npos) {
  206. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  207. switch (target->GetPolicyStatusCMP0041()) {
  208. case cmPolicies::WARN:
  209. messageType = MessageType::WARNING;
  210. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0041) << "\n";
  211. break;
  212. case cmPolicies::OLD:
  213. continue;
  214. case cmPolicies::REQUIRED_IF_USED:
  215. case cmPolicies::REQUIRED_ALWAYS:
  216. case cmPolicies::NEW:
  217. hadFatalError = true;
  218. break; // Issue fatal message.
  219. }
  220. } else {
  221. hadFatalError = true;
  222. }
  223. }
  224. if (!cmSystemTools::FileIsFullPath(li)) {
  225. /* clang-format off */
  226. e << "Target \"" << target->GetName() << "\" " << prop <<
  227. " property contains relative path:\n"
  228. " \"" << li << "\"";
  229. /* clang-format on */
  230. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  231. }
  232. bool inBinary = isSubDirectory(li, topBinaryDir);
  233. bool inSource = isSubDirectory(li, topSourceDir);
  234. if (isSubDirectory(li, installDir)) {
  235. // The include directory is inside the install tree. If the
  236. // install tree is not inside the source tree or build tree then
  237. // fall through to the checks below that the include directory is not
  238. // also inside the source tree or build tree.
  239. bool shouldContinue =
  240. (!inBinary || isSubDirectory(installDir, topBinaryDir)) &&
  241. (!inSource || isSubDirectory(installDir, topSourceDir));
  242. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  243. if (!shouldContinue) {
  244. switch (target->GetPolicyStatusCMP0052()) {
  245. case cmPolicies::WARN: {
  246. std::ostringstream s;
  247. s << cmPolicies::GetPolicyWarning(cmPolicies::CMP0052) << "\n";
  248. s << "Directory:\n \"" << li
  249. << "\"\nin "
  250. "INTERFACE_INCLUDE_DIRECTORIES of target \""
  251. << target->GetName()
  252. << "\" is a subdirectory of the install "
  253. "directory:\n \""
  254. << installDir
  255. << "\"\nhowever it is also "
  256. "a subdirectory of the "
  257. << (inBinary ? "build" : "source") << " tree:\n \""
  258. << (inBinary ? topBinaryDir : topSourceDir) << "\"\n";
  259. target->GetLocalGenerator()->IssueMessage(
  260. MessageType::AUTHOR_WARNING, s.str());
  261. CM_FALLTHROUGH;
  262. }
  263. case cmPolicies::OLD:
  264. shouldContinue = true;
  265. break;
  266. case cmPolicies::REQUIRED_ALWAYS:
  267. case cmPolicies::REQUIRED_IF_USED:
  268. case cmPolicies::NEW:
  269. break;
  270. }
  271. }
  272. }
  273. if (shouldContinue) {
  274. continue;
  275. }
  276. }
  277. if (inBinary) {
  278. /* clang-format off */
  279. e << "Target \"" << target->GetName() << "\" " << prop <<
  280. " property contains path:\n"
  281. " \"" << li << "\"\nwhich is prefixed in the build directory.";
  282. /* clang-format on */
  283. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  284. }
  285. if (!inSourceBuild) {
  286. if (inSource) {
  287. e << "Target \"" << target->GetName() << "\" " << prop
  288. << " property contains path:\n"
  289. " \""
  290. << li << "\"\nwhich is prefixed in the source directory.";
  291. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  292. }
  293. }
  294. }
  295. return !hadFatalError;
  296. }
  297. static void prefixItems(std::string& exportDirs)
  298. {
  299. std::vector<std::string> entries;
  300. cmGeneratorExpression::Split(exportDirs, entries);
  301. exportDirs.clear();
  302. const char* sep = "";
  303. for (std::string const& e : entries) {
  304. exportDirs += sep;
  305. sep = ";";
  306. if (!cmSystemTools::FileIsFullPath(e) &&
  307. e.find("${_IMPORT_PREFIX}") == std::string::npos) {
  308. exportDirs += "${_IMPORT_PREFIX}/";
  309. }
  310. exportDirs += e;
  311. }
  312. }
  313. void cmExportFileGenerator::PopulateSourcesInterface(
  314. cmGeneratorTarget const* gt,
  315. cmGeneratorExpression::PreprocessContext preprocessRule,
  316. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  317. {
  318. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  319. const char* propName = "INTERFACE_SOURCES";
  320. cmValue input = gt->GetProperty(propName);
  321. if (!input) {
  322. return;
  323. }
  324. if (input->empty()) {
  325. properties[propName].clear();
  326. return;
  327. }
  328. std::string prepro =
  329. cmGeneratorExpression::Preprocess(*input, preprocessRule, true);
  330. if (!prepro.empty()) {
  331. this->ResolveTargetsInGeneratorExpressions(prepro, gt, missingTargets);
  332. if (!checkInterfaceDirs(prepro, gt, propName)) {
  333. return;
  334. }
  335. properties[propName] = prepro;
  336. }
  337. }
  338. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  339. cmGeneratorTarget const* target,
  340. cmGeneratorExpression::PreprocessContext preprocessRule,
  341. ImportPropertyMap& properties, std::vector<std::string>& missingTargets,
  342. cmTargetExport const& te)
  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(te), ";"),
  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. cmLocalGenerator const* lg, std::vector<std::string>& missingTargets)
  539. {
  540. cmGeneratorTarget::TargetOrString resolved =
  541. target->ResolveTargetReference(input, lg);
  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. cmLocalGenerator const* lg = target->GetLocalGenerator();
  569. if (replace == NoReplaceFreeTargets) {
  570. this->ResolveTargetsInGeneratorExpression(input, target, lg,
  571. missingTargets);
  572. return;
  573. }
  574. std::vector<std::string> parts;
  575. cmGeneratorExpression::Split(input, parts);
  576. std::string sep;
  577. input.clear();
  578. for (std::string& li : parts) {
  579. if (target->IsLinkLookupScope(li, lg)) {
  580. continue;
  581. }
  582. if (cmGeneratorExpression::Find(li) == std::string::npos) {
  583. this->AddTargetNamespace(li, target, lg, missingTargets);
  584. } else {
  585. this->ResolveTargetsInGeneratorExpression(li, target, lg,
  586. missingTargets);
  587. }
  588. input += sep + li;
  589. sep = ";";
  590. }
  591. }
  592. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  593. std::string& input, cmGeneratorTarget const* target,
  594. cmLocalGenerator const* lg, std::vector<std::string>& missingTargets)
  595. {
  596. std::string::size_type pos = 0;
  597. std::string::size_type lastPos = pos;
  598. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  599. std::string::npos) {
  600. std::string::size_type nameStartPos =
  601. pos + sizeof("$<TARGET_PROPERTY:") - 1;
  602. std::string::size_type closePos = input.find('>', nameStartPos);
  603. std::string::size_type commaPos = input.find(',', nameStartPos);
  604. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  605. if (commaPos == std::string::npos // Implied 'this' target
  606. || closePos == std::string::npos // Incomplete expression.
  607. || closePos < commaPos // Implied 'this' target
  608. || nextOpenPos < commaPos) // Non-literal
  609. {
  610. lastPos = nameStartPos;
  611. continue;
  612. }
  613. std::string targetName =
  614. input.substr(nameStartPos, commaPos - nameStartPos);
  615. if (this->AddTargetNamespace(targetName, target, lg, missingTargets)) {
  616. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  617. }
  618. lastPos = nameStartPos + targetName.size() + 1;
  619. }
  620. std::string errorString;
  621. pos = 0;
  622. lastPos = pos;
  623. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  624. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  625. std::string::size_type endPos = input.find('>', nameStartPos);
  626. if (endPos == std::string::npos) {
  627. errorString = "$<TARGET_NAME:...> expression incomplete";
  628. break;
  629. }
  630. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  631. if (targetName.find("$<") != std::string::npos) {
  632. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  633. "literal.";
  634. break;
  635. }
  636. if (!this->AddTargetNamespace(targetName, target, lg, missingTargets)) {
  637. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  638. "reachable target.";
  639. break;
  640. }
  641. input.replace(pos, endPos - pos + 1, targetName);
  642. lastPos = pos + targetName.size();
  643. }
  644. pos = 0;
  645. lastPos = pos;
  646. while (errorString.empty() &&
  647. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  648. std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
  649. std::string::size_type endPos = input.find('>', nameStartPos);
  650. if (endPos == std::string::npos) {
  651. errorString = "$<LINK_ONLY:...> expression incomplete";
  652. break;
  653. }
  654. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  655. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  656. this->AddTargetNamespace(libName, target, lg, missingTargets)) {
  657. input.replace(nameStartPos, endPos - nameStartPos, libName);
  658. }
  659. lastPos = nameStartPos + libName.size() + 1;
  660. }
  661. this->ReplaceInstallPrefix(input);
  662. if (!errorString.empty()) {
  663. target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  664. errorString);
  665. }
  666. }
  667. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/)
  668. {
  669. // Do nothing
  670. }
  671. void cmExportFileGenerator::SetImportLinkInterface(
  672. const std::string& config, std::string const& suffix,
  673. cmGeneratorExpression::PreprocessContext preprocessRule,
  674. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  675. std::vector<std::string>& missingTargets)
  676. {
  677. // Add the transitive link dependencies for this configuration.
  678. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  679. if (!iface) {
  680. return;
  681. }
  682. if (iface->ImplementationIsInterface) {
  683. // Policy CMP0022 must not be NEW.
  684. this->SetImportLinkProperty(
  685. suffix, target, "IMPORTED_LINK_INTERFACE_LIBRARIES", iface->Libraries,
  686. properties, missingTargets, ImportLinkPropertyTargetNames::Yes);
  687. return;
  688. }
  689. cmValue propContent;
  690. if (cmValue prop_suffixed =
  691. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  692. propContent = prop_suffixed;
  693. } else if (cmValue prop = target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  694. propContent = prop;
  695. } else {
  696. return;
  697. }
  698. const bool newCMP0022Behavior =
  699. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  700. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  701. if (newCMP0022Behavior && !this->ExportOld) {
  702. cmLocalGenerator* lg = target->GetLocalGenerator();
  703. std::ostringstream e;
  704. e << "Target \"" << target->GetName()
  705. << "\" has policy CMP0022 enabled, "
  706. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  707. "populated, but it was exported without the "
  708. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  709. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  710. return;
  711. }
  712. if (propContent->empty()) {
  713. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  714. return;
  715. }
  716. std::string prepro =
  717. cmGeneratorExpression::Preprocess(*propContent, preprocessRule);
  718. if (!prepro.empty()) {
  719. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets,
  720. ReplaceFreeTargets);
  721. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  722. }
  723. }
  724. void cmExportFileGenerator::SetImportDetailProperties(
  725. const std::string& config, std::string const& suffix,
  726. cmGeneratorTarget* target, ImportPropertyMap& properties,
  727. std::vector<std::string>& missingTargets)
  728. {
  729. // Get the makefile in which to lookup target information.
  730. cmMakefile* mf = target->Makefile;
  731. // Add the soname for unix shared libraries.
  732. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  733. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  734. if (!target->IsDLLPlatform()) {
  735. std::string prop;
  736. std::string value;
  737. if (target->HasSOName(config)) {
  738. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  739. value = this->InstallNameDir(target, config);
  740. }
  741. prop = "IMPORTED_SONAME";
  742. value += target->GetSOName(config);
  743. } else {
  744. prop = "IMPORTED_NO_SONAME";
  745. value = "TRUE";
  746. }
  747. prop += suffix;
  748. properties[prop] = value;
  749. }
  750. }
  751. // Add the transitive link dependencies for this configuration.
  752. if (cmLinkInterface const* iface =
  753. target->GetLinkInterface(config, target)) {
  754. this->SetImportLinkProperty(
  755. suffix, target, "IMPORTED_LINK_INTERFACE_LANGUAGES", iface->Languages,
  756. properties, missingTargets, ImportLinkPropertyTargetNames::No);
  757. std::vector<std::string> dummy;
  758. this->SetImportLinkProperty(
  759. suffix, target, "IMPORTED_LINK_DEPENDENT_LIBRARIES", iface->SharedDeps,
  760. properties, dummy, ImportLinkPropertyTargetNames::Yes);
  761. if (iface->Multiplicity > 0) {
  762. std::string prop =
  763. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  764. properties[prop] = std::to_string(iface->Multiplicity);
  765. }
  766. }
  767. // Add information if this target is a managed target
  768. if (target->GetManagedType(config) !=
  769. cmGeneratorTarget::ManagedType::Native) {
  770. std::string prop = cmStrCat("IMPORTED_COMMON_LANGUAGE_RUNTIME", suffix);
  771. std::string propval;
  772. if (cmValue p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  773. propval = *p;
  774. } else if (target->IsCSharpOnly()) {
  775. // C# projects do not have the /clr flag, so we set the property
  776. // here to mark the target as (only) managed (i.e. no .lib file
  777. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  778. // property would have to be set manually for C# targets to make
  779. // exporting/importing work.
  780. propval = "CSharp";
  781. }
  782. properties[prop] = propval;
  783. }
  784. }
  785. static std::string const& asString(std::string const& l)
  786. {
  787. return l;
  788. }
  789. static std::string const& asString(cmLinkItem const& l)
  790. {
  791. return l.AsStr();
  792. }
  793. template <typename T>
  794. void cmExportFileGenerator::SetImportLinkProperty(
  795. std::string const& suffix, cmGeneratorTarget const* target,
  796. const std::string& propName, std::vector<T> const& entries,
  797. ImportPropertyMap& properties, std::vector<std::string>& missingTargets,
  798. ImportLinkPropertyTargetNames targetNames)
  799. {
  800. // Skip the property if there are no entries.
  801. if (entries.empty()) {
  802. return;
  803. }
  804. cmLocalGenerator const* lg = target->GetLocalGenerator();
  805. // Construct the property value.
  806. std::string link_entries;
  807. const char* sep = "";
  808. for (T const& l : entries) {
  809. // Separate this from the previous entry.
  810. link_entries += sep;
  811. sep = ";";
  812. if (targetNames == ImportLinkPropertyTargetNames::Yes) {
  813. std::string temp = asString(l);
  814. this->AddTargetNamespace(temp, target, lg, missingTargets);
  815. link_entries += temp;
  816. } else {
  817. link_entries += asString(l);
  818. }
  819. }
  820. // Store the property.
  821. std::string prop = cmStrCat(propName, suffix);
  822. properties[prop] = link_entries;
  823. }
  824. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  825. {
  826. // Protect that file against use with older CMake versions.
  827. /* clang-format off */
  828. os << "# Generated by CMake\n\n";
  829. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.6)\n"
  830. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  831. << "endif()\n";
  832. /* clang-format on */
  833. // Isolate the file policy level.
  834. // Support CMake versions as far back as 2.6 but also support using NEW
  835. // policy settings for up to CMake 3.22 (this upper limit may be reviewed
  836. // and increased from time to time). This reduces the opportunity for CMake
  837. // warnings when an older export file is later used with newer CMake
  838. // versions.
  839. /* clang-format off */
  840. os << "cmake_policy(PUSH)\n"
  841. << "cmake_policy(VERSION 2.6...3.22)\n";
  842. /* clang-format on */
  843. }
  844. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  845. {
  846. os << "cmake_policy(POP)\n";
  847. }
  848. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  849. const std::string& config)
  850. {
  851. os << "#----------------------------------------------------------------\n"
  852. << "# Generated CMake target import file";
  853. if (!config.empty()) {
  854. os << " for configuration \"" << config << "\".\n";
  855. } else {
  856. os << ".\n";
  857. }
  858. os << "#----------------------------------------------------------------\n"
  859. << "\n";
  860. this->GenerateImportVersionCode(os);
  861. }
  862. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  863. {
  864. os << "# Commands beyond this point should not need to know the version.\n"
  865. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  866. }
  867. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  868. {
  869. // Store an import file format version. This will let us change the
  870. // format later while still allowing old import files to work.
  871. /* clang-format off */
  872. os << "# Commands may need to know the format version.\n"
  873. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  874. << "\n";
  875. /* clang-format on */
  876. }
  877. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  878. std::ostream& os, const std::string& expectedTargets)
  879. {
  880. /* clang-format off */
  881. os << "# Protect against multiple inclusion, which would fail when already "
  882. "imported targets are added once more.\n"
  883. "set(_targetsDefined)\n"
  884. "set(_targetsNotDefined)\n"
  885. "set(_expectedTargets)\n"
  886. "foreach(_expectedTarget " << expectedTargets << ")\n"
  887. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  888. " if(NOT TARGET ${_expectedTarget})\n"
  889. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  890. " endif()\n"
  891. " if(TARGET ${_expectedTarget})\n"
  892. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  893. " endif()\n"
  894. "endforeach()\n"
  895. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  896. " unset(_targetsDefined)\n"
  897. " unset(_targetsNotDefined)\n"
  898. " unset(_expectedTargets)\n"
  899. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  900. " cmake_policy(POP)\n"
  901. " return()\n"
  902. "endif()\n"
  903. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  904. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  905. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  906. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  907. "endif()\n"
  908. "unset(_targetsDefined)\n"
  909. "unset(_targetsNotDefined)\n"
  910. "unset(_expectedTargets)\n"
  911. "\n\n";
  912. /* clang-format on */
  913. }
  914. void cmExportFileGenerator::GenerateImportTargetCode(
  915. std::ostream& os, cmGeneratorTarget const* target,
  916. cmStateEnums::TargetType targetType)
  917. {
  918. // Construct the imported target name.
  919. std::string targetName = this->Namespace;
  920. targetName += target->GetExportName();
  921. // Create the imported target.
  922. os << "# Create imported target " << targetName << "\n";
  923. switch (targetType) {
  924. case cmStateEnums::EXECUTABLE:
  925. os << "add_executable(" << targetName << " IMPORTED)\n";
  926. break;
  927. case cmStateEnums::STATIC_LIBRARY:
  928. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  929. break;
  930. case cmStateEnums::SHARED_LIBRARY:
  931. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  932. break;
  933. case cmStateEnums::MODULE_LIBRARY:
  934. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  935. break;
  936. case cmStateEnums::UNKNOWN_LIBRARY:
  937. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  938. break;
  939. case cmStateEnums::OBJECT_LIBRARY:
  940. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  941. break;
  942. case cmStateEnums::INTERFACE_LIBRARY:
  943. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  944. break;
  945. default: // should never happen
  946. break;
  947. }
  948. // Mark the imported executable if it has exports.
  949. if (target->IsExecutableWithExports()) {
  950. os << "set_property(TARGET " << targetName
  951. << " PROPERTY ENABLE_EXPORTS 1)\n";
  952. }
  953. // Mark the imported library if it is a framework.
  954. if (target->IsFrameworkOnApple()) {
  955. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  956. }
  957. // Mark the imported executable if it is an application bundle.
  958. if (target->IsAppBundleOnApple()) {
  959. os << "set_property(TARGET " << targetName
  960. << " PROPERTY MACOSX_BUNDLE 1)\n";
  961. }
  962. if (target->IsCFBundleOnApple()) {
  963. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  964. }
  965. // generate DEPRECATION
  966. if (target->IsDeprecated()) {
  967. os << "set_property(TARGET " << targetName << " PROPERTY DEPRECATION "
  968. << cmExportFileGeneratorEscape(target->GetDeprecation()) << ")\n";
  969. }
  970. if (target->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
  971. os << "set_property(TARGET " << targetName
  972. << " PROPERTY IMPORTED_NO_SYSTEM 1)\n";
  973. }
  974. os << "\n";
  975. }
  976. void cmExportFileGenerator::GenerateImportPropertyCode(
  977. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  978. ImportPropertyMap const& properties)
  979. {
  980. // Construct the imported target name.
  981. std::string targetName = this->Namespace;
  982. targetName += target->GetExportName();
  983. // Set the import properties.
  984. os << "# Import target \"" << targetName << "\" for configuration \""
  985. << config << "\"\n";
  986. os << "set_property(TARGET " << targetName
  987. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  988. if (!config.empty()) {
  989. os << cmSystemTools::UpperCase(config);
  990. } else {
  991. os << "NOCONFIG";
  992. }
  993. os << ")\n";
  994. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  995. for (auto const& property : properties) {
  996. os << " " << property.first << " "
  997. << cmExportFileGeneratorEscape(property.second) << "\n";
  998. }
  999. os << " )\n"
  1000. << "\n";
  1001. }
  1002. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  1003. std::ostream& os, const std::vector<std::string>& missingTargets)
  1004. {
  1005. if (missingTargets.empty()) {
  1006. /* clang-format off */
  1007. os << "# This file does not depend on other imported targets which have\n"
  1008. "# been exported from the same project but in a separate "
  1009. "export set.\n\n";
  1010. /* clang-format on */
  1011. return;
  1012. }
  1013. /* clang-format off */
  1014. os << "# Make sure the targets which have been exported in some other\n"
  1015. "# export set exist.\n"
  1016. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1017. "foreach(_target ";
  1018. /* clang-format on */
  1019. std::set<std::string> emitted;
  1020. for (std::string const& missingTarget : missingTargets) {
  1021. if (emitted.insert(missingTarget).second) {
  1022. os << "\"" << missingTarget << "\" ";
  1023. }
  1024. }
  1025. /* clang-format off */
  1026. os << ")\n"
  1027. " if(NOT TARGET \"${_target}\" )\n"
  1028. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  1029. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  1030. "\n"
  1031. " endif()\n"
  1032. "endforeach()\n"
  1033. "\n"
  1034. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1035. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  1036. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  1037. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  1038. "\"The following imported targets are "
  1039. "referenced, but are missing: "
  1040. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1041. " else()\n"
  1042. " message(FATAL_ERROR \"The following imported targets are "
  1043. "referenced, but are missing: "
  1044. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1045. " endif()\n"
  1046. "endif()\n"
  1047. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1048. "\n";
  1049. /* clang-format on */
  1050. }
  1051. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  1052. {
  1053. // Add code which verifies at cmake time that the file which is being
  1054. // imported actually exists on disk. This should in theory always be theory
  1055. // case, but still when packages are split into normal and development
  1056. // packages this might get broken (e.g. the Config.cmake could be part of
  1057. // the non-development package, something similar happened to me without
  1058. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1059. // but the development package was not installed.).
  1060. /* clang-format off */
  1061. os << "# Loop over all imported files and verify that they actually exist\n"
  1062. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1063. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1064. " if(NOT EXISTS \"${file}\" )\n"
  1065. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1066. " references the file\n"
  1067. " \\\"${file}\\\"\n"
  1068. "but this file does not exist. Possible reasons include:\n"
  1069. "* The file was deleted, renamed, or moved to another location.\n"
  1070. "* An install or uninstall procedure did not complete successfully.\n"
  1071. "* The installation package was faulty and contained\n"
  1072. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1073. "but not all the files it references.\n"
  1074. "\")\n"
  1075. " endif()\n"
  1076. " endforeach()\n"
  1077. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1078. "endforeach()\n"
  1079. "unset(_IMPORT_CHECK_TARGETS)\n"
  1080. "\n";
  1081. /* clang-format on */
  1082. }
  1083. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1084. std::ostream& os, cmGeneratorTarget* target,
  1085. ImportPropertyMap const& properties,
  1086. const std::set<std::string>& importedLocations)
  1087. {
  1088. // Construct the imported target name.
  1089. std::string targetName = cmStrCat(this->Namespace, target->GetExportName());
  1090. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1091. << " )\n"
  1092. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1093. << targetName << " ";
  1094. for (std::string const& li : importedLocations) {
  1095. auto pi = properties.find(li);
  1096. if (pi != properties.end()) {
  1097. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1098. }
  1099. }
  1100. os << ")\n\n";
  1101. }
  1102. bool cmExportFileGenerator::PopulateExportProperties(
  1103. cmGeneratorTarget const* gte, ImportPropertyMap& properties,
  1104. std::string& errorMessage)
  1105. {
  1106. const auto& targetProperties = gte->Target->GetProperties();
  1107. if (cmValue exportProperties =
  1108. targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
  1109. for (auto& prop : cmExpandedList(*exportProperties)) {
  1110. /* Black list reserved properties */
  1111. if (cmHasLiteralPrefix(prop, "IMPORTED_") ||
  1112. cmHasLiteralPrefix(prop, "INTERFACE_")) {
  1113. std::ostringstream e;
  1114. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1115. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1116. << "properties are reserved.";
  1117. errorMessage = e.str();
  1118. return false;
  1119. }
  1120. cmValue propertyValue = targetProperties.GetPropertyValue(prop);
  1121. if (!propertyValue) {
  1122. // Asked to export a property that isn't defined on the target. Do not
  1123. // consider this an error, there's just nothing to export.
  1124. continue;
  1125. }
  1126. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1127. *propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1128. if (evaluatedValue != *propertyValue) {
  1129. std::ostringstream e;
  1130. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1131. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1132. << "generator expression. This is not allowed.";
  1133. errorMessage = e.str();
  1134. return false;
  1135. }
  1136. properties[prop] = *propertyValue;
  1137. }
  1138. }
  1139. return true;
  1140. }
  1141. void cmExportFileGenerator::GenerateTargetFileSets(cmGeneratorTarget* gte,
  1142. std::ostream& os,
  1143. cmTargetExport* te)
  1144. {
  1145. auto interfaceFileSets = gte->Target->GetAllInterfaceFileSets();
  1146. if (!interfaceFileSets.empty()) {
  1147. std::string targetName = cmStrCat(this->Namespace, gte->GetExportName());
  1148. os << "if(NOT CMAKE_VERSION VERSION_LESS \"3.23.0\")\n"
  1149. " target_sources("
  1150. << targetName << "\n";
  1151. for (auto const& name : interfaceFileSets) {
  1152. auto* fileSet = gte->Target->GetFileSet(name);
  1153. if (!fileSet) {
  1154. gte->Makefile->IssueMessage(
  1155. MessageType::FATAL_ERROR,
  1156. cmStrCat("File set \"", name,
  1157. "\" is listed in interface file sets of ", gte->GetName(),
  1158. " but has not been created"));
  1159. return;
  1160. }
  1161. os << " INTERFACE"
  1162. << "\n FILE_SET " << cmOutputConverter::EscapeForCMake(name)
  1163. << "\n TYPE "
  1164. << cmOutputConverter::EscapeForCMake(fileSet->GetType())
  1165. << "\n BASE_DIRS "
  1166. << this->GetFileSetDirectories(gte, fileSet, te) << "\n FILES "
  1167. << this->GetFileSetFiles(gte, fileSet, te) << "\n";
  1168. }
  1169. os << " )\nendif()\n\n";
  1170. }
  1171. }