cmExportFileGenerator.cxx 45 KB

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