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