cmExportFileGenerator.cxx 42 KB

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