cmExportFileGenerator.cxx 41 KB

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