cmExportFileGenerator.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLinkItem.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmOutputConverter.h"
  13. #include "cmPolicies.h"
  14. #include "cmPropertyMap.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmTargetExport.h"
  19. #include "cmsys/FStream.hxx"
  20. #include <assert.h>
  21. #include <memory>
  22. #include <sstream>
  23. #include <string.h>
  24. #include <utility>
  25. static std::string cmExportFileGeneratorEscape(std::string const& str)
  26. {
  27. // Escape a property value for writing into a .cmake file.
  28. std::string result = cmOutputConverter::EscapeForCMake(str);
  29. // Un-escape variable references generated by our own export code.
  30. cmSystemTools::ReplaceString(result, "\\${_IMPORT_PREFIX}",
  31. "${_IMPORT_PREFIX}");
  32. cmSystemTools::ReplaceString(result, "\\${CMAKE_IMPORT_LIBRARY_SUFFIX}",
  33. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  34. return result;
  35. }
  36. cmExportFileGenerator::cmExportFileGenerator()
  37. {
  38. this->AppendMode = false;
  39. this->ExportOld = false;
  40. }
  41. void cmExportFileGenerator::AddConfiguration(const std::string& config)
  42. {
  43. this->Configurations.push_back(config);
  44. }
  45. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  46. {
  47. this->MainImportFile = mainFile;
  48. this->FileDir = cmSystemTools::GetFilenamePath(this->MainImportFile);
  49. this->FileBase =
  50. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  51. this->FileExt =
  52. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  53. }
  54. const char* cmExportFileGenerator::GetMainExportFileName() const
  55. {
  56. return this->MainImportFile.c_str();
  57. }
  58. bool cmExportFileGenerator::GenerateImportFile()
  59. {
  60. // Open the output file to generate it.
  61. std::unique_ptr<cmsys::ofstream> foutPtr;
  62. if (this->AppendMode) {
  63. // Open for append.
  64. auto openmodeApp = std::ios::app;
  65. foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
  66. openmodeApp);
  67. } else {
  68. // Generate atomically and with copy-if-different.
  69. std::unique_ptr<cmGeneratedFileStream> ap(
  70. new cmGeneratedFileStream(this->MainImportFile, true));
  71. ap->SetCopyIfDifferent(true);
  72. foutPtr = std::move(ap);
  73. }
  74. if (!foutPtr || !*foutPtr) {
  75. std::string se = cmSystemTools::GetLastSystemError();
  76. std::ostringstream e;
  77. e << "cannot write to file \"" << this->MainImportFile << "\": " << se;
  78. cmSystemTools::Error(e.str());
  79. return false;
  80. }
  81. std::ostream& os = *foutPtr;
  82. // Start with the import file header.
  83. this->GeneratePolicyHeaderCode(os);
  84. this->GenerateImportHeaderCode(os);
  85. // Create all the imported targets.
  86. bool result = this->GenerateMainFile(os);
  87. // End with the import file footer.
  88. this->GenerateImportFooterCode(os);
  89. this->GeneratePolicyFooterCode(os);
  90. return result;
  91. }
  92. void cmExportFileGenerator::GenerateImportConfig(
  93. std::ostream& os, const std::string& config,
  94. std::vector<std::string>& missingTargets)
  95. {
  96. // Construct the property configuration suffix.
  97. std::string suffix = "_";
  98. if (!config.empty()) {
  99. suffix += cmSystemTools::UpperCase(config);
  100. } else {
  101. suffix += "NOCONFIG";
  102. }
  103. // Generate the per-config target information.
  104. this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
  105. }
  106. void cmExportFileGenerator::PopulateInterfaceProperty(
  107. const std::string& propName, cmGeneratorTarget* target,
  108. ImportPropertyMap& properties)
  109. {
  110. const char* input = target->GetProperty(propName);
  111. if (input) {
  112. properties[propName] = input;
  113. }
  114. }
  115. void cmExportFileGenerator::PopulateInterfaceProperty(
  116. const std::string& propName, const std::string& outputName,
  117. cmGeneratorTarget* target,
  118. cmGeneratorExpression::PreprocessContext preprocessRule,
  119. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  120. {
  121. const char* input = target->GetProperty(propName);
  122. if (input) {
  123. if (!*input) {
  124. // Set to empty
  125. properties[outputName].clear();
  126. return;
  127. }
  128. std::string prepro =
  129. cmGeneratorExpression::Preprocess(input, preprocessRule);
  130. if (!prepro.empty()) {
  131. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  132. missingTargets);
  133. properties[outputName] = prepro;
  134. }
  135. }
  136. }
  137. void cmExportFileGenerator::GenerateRequiredCMakeVersion(
  138. std::ostream& os, const char* versionString)
  139. {
  140. /* clang-format off */
  141. os << "if(CMAKE_VERSION VERSION_LESS " << versionString << ")\n"
  142. " message(FATAL_ERROR \"This file relies on consumers using "
  143. "CMake " << versionString << " or greater.\")\n"
  144. "endif()\n\n";
  145. /* clang-format on */
  146. }
  147. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  148. cmGeneratorTarget* target,
  149. cmGeneratorExpression::PreprocessContext preprocessRule,
  150. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  151. {
  152. if (!target->IsLinkable()) {
  153. return false;
  154. }
  155. const char* input = target->GetProperty("INTERFACE_LINK_LIBRARIES");
  156. if (input) {
  157. std::string prepro =
  158. cmGeneratorExpression::Preprocess(input, preprocessRule);
  159. if (!prepro.empty()) {
  160. this->ResolveTargetsInGeneratorExpressions(
  161. prepro, target, missingTargets, ReplaceFreeTargets);
  162. properties["INTERFACE_LINK_LIBRARIES"] = prepro;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. static bool isSubDirectory(std::string const& a, std::string const& b)
  169. {
  170. return (cmSystemTools::ComparePath(a, b) ||
  171. cmSystemTools::IsSubDirectory(a, b));
  172. }
  173. static bool checkInterfaceDirs(const std::string& prepro,
  174. cmGeneratorTarget* target,
  175. const std::string& prop)
  176. {
  177. std::string const& installDir =
  178. target->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  179. std::string const& topSourceDir =
  180. target->GetLocalGenerator()->GetSourceDirectory();
  181. std::string const& topBinaryDir =
  182. target->GetLocalGenerator()->GetBinaryDirectory();
  183. std::vector<std::string> parts;
  184. cmGeneratorExpression::Split(prepro, parts);
  185. const bool inSourceBuild = topSourceDir == topBinaryDir;
  186. bool hadFatalError = false;
  187. for (std::string const& li : parts) {
  188. size_t genexPos = cmGeneratorExpression::Find(li);
  189. if (genexPos == 0) {
  190. continue;
  191. }
  192. MessageType messageType = MessageType::FATAL_ERROR;
  193. std::ostringstream e;
  194. if (genexPos != std::string::npos) {
  195. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  196. switch (target->GetPolicyStatusCMP0041()) {
  197. case cmPolicies::WARN:
  198. messageType = MessageType::WARNING;
  199. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0041) << "\n";
  200. break;
  201. case cmPolicies::OLD:
  202. continue;
  203. case cmPolicies::REQUIRED_IF_USED:
  204. case cmPolicies::REQUIRED_ALWAYS:
  205. case cmPolicies::NEW:
  206. hadFatalError = true;
  207. break; // Issue fatal message.
  208. }
  209. } else {
  210. hadFatalError = true;
  211. }
  212. }
  213. if (cmHasLiteralPrefix(li, "${_IMPORT_PREFIX}")) {
  214. continue;
  215. }
  216. if (!cmSystemTools::FileIsFullPath(li)) {
  217. /* clang-format off */
  218. e << "Target \"" << target->GetName() << "\" " << prop <<
  219. " property contains relative path:\n"
  220. " \"" << li << "\"";
  221. /* clang-format on */
  222. target->GetLocalGenerator()->IssueMessage(messageType, e.str());
  223. }
  224. bool inBinary = isSubDirectory(li, topBinaryDir);
  225. bool inSource = isSubDirectory(li, topSourceDir);
  226. if (isSubDirectory(li, installDir)) {
  227. // The include directory is inside the install tree. If the
  228. // install tree is not inside the source tree or build tree then
  229. // fall through to the checks below that the include directory is not
  230. // also inside the source tree or build tree.
  231. bool shouldContinue =
  232. (!inBinary || isSubDirectory(installDir, topBinaryDir)) &&
  233. (!inSource || isSubDirectory(installDir, topSourceDir));
  234. if (prop == "INTERFACE_INCLUDE_DIRECTORIES") {
  235. if (!shouldContinue) {
  236. switch (target->GetPolicyStatusCMP0052()) {
  237. case cmPolicies::WARN: {
  238. std::ostringstream s;
  239. s << cmPolicies::GetPolicyWarning(cmPolicies::CMP0052) << "\n";
  240. s << "Directory:\n \"" << li
  241. << "\"\nin "
  242. "INTERFACE_INCLUDE_DIRECTORIES of target \""
  243. << target->GetName()
  244. << "\" is a subdirectory of the install "
  245. "directory:\n \""
  246. << installDir
  247. << "\"\nhowever it is also "
  248. "a subdirectory of the "
  249. << (inBinary ? "build" : "source") << " tree:\n \""
  250. << (inBinary ? topBinaryDir : topSourceDir) << "\""
  251. << std::endl;
  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. const char* input = gt->GetProperty(propName);
  314. if (!input) {
  315. return;
  316. }
  317. if (!*input) {
  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. const char* 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(), "", false, 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) && 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. const char* input = gt->GetProperty(propName);
  388. if (!input) {
  389. return;
  390. }
  391. if (!*input) {
  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. const char* input = gt->GetProperty(propName);
  413. if (!input) {
  414. return;
  415. }
  416. if (!*input) {
  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. const char* p = tgt->GetProperty(prop);
  442. if (!p) {
  443. return;
  444. }
  445. std::vector<std::string> content;
  446. cmSystemTools::ExpandListArgument(p, content);
  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 = this->Namespace;
  518. targetName += 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 (cmGeneratorExpression::Find(li) == std::string::npos) {
  569. this->AddTargetNamespace(li, target, missingTargets);
  570. } else {
  571. this->ResolveTargetsInGeneratorExpression(li, target, missingTargets);
  572. }
  573. input += sep + li;
  574. sep = ";";
  575. }
  576. }
  577. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  578. std::string& input, cmGeneratorTarget* target,
  579. std::vector<std::string>& missingTargets)
  580. {
  581. std::string::size_type pos = 0;
  582. std::string::size_type lastPos = pos;
  583. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  584. std::string::npos) {
  585. std::string::size_type nameStartPos =
  586. pos + sizeof("$<TARGET_PROPERTY:") - 1;
  587. std::string::size_type closePos = input.find('>', nameStartPos);
  588. std::string::size_type commaPos = input.find(',', nameStartPos);
  589. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  590. if (commaPos == std::string::npos // Implied 'this' target
  591. || closePos == std::string::npos // Incomplete expression.
  592. || closePos < commaPos // Implied 'this' target
  593. || nextOpenPos < commaPos) // Non-literal
  594. {
  595. lastPos = nameStartPos;
  596. continue;
  597. }
  598. std::string targetName =
  599. input.substr(nameStartPos, commaPos - nameStartPos);
  600. if (this->AddTargetNamespace(targetName, target, missingTargets)) {
  601. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  602. }
  603. lastPos = nameStartPos + targetName.size() + 1;
  604. }
  605. std::string errorString;
  606. pos = 0;
  607. lastPos = pos;
  608. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  609. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  610. std::string::size_type endPos = input.find('>', nameStartPos);
  611. if (endPos == std::string::npos) {
  612. errorString = "$<TARGET_NAME:...> expression incomplete";
  613. break;
  614. }
  615. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  616. if (targetName.find("$<") != std::string::npos) {
  617. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  618. "literal.";
  619. break;
  620. }
  621. if (!this->AddTargetNamespace(targetName, target, missingTargets)) {
  622. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  623. "reachable target.";
  624. break;
  625. }
  626. input.replace(pos, endPos - pos + 1, targetName);
  627. lastPos = endPos;
  628. }
  629. pos = 0;
  630. lastPos = pos;
  631. while (errorString.empty() &&
  632. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  633. std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
  634. std::string::size_type endPos = input.find('>', nameStartPos);
  635. if (endPos == std::string::npos) {
  636. errorString = "$<LINK_ONLY:...> expression incomplete";
  637. break;
  638. }
  639. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  640. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  641. this->AddTargetNamespace(libName, target, missingTargets)) {
  642. input.replace(nameStartPos, endPos - nameStartPos, libName);
  643. }
  644. lastPos = nameStartPos + libName.size() + 1;
  645. }
  646. this->ReplaceInstallPrefix(input);
  647. if (!errorString.empty()) {
  648. target->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  649. errorString);
  650. }
  651. }
  652. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/)
  653. {
  654. // Do nothing
  655. }
  656. void cmExportFileGenerator::SetImportLinkInterface(
  657. const std::string& config, std::string const& suffix,
  658. cmGeneratorExpression::PreprocessContext preprocessRule,
  659. cmGeneratorTarget* target, ImportPropertyMap& properties,
  660. std::vector<std::string>& missingTargets)
  661. {
  662. // Add the transitive link dependencies for this configuration.
  663. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  664. if (!iface) {
  665. return;
  666. }
  667. if (iface->ImplementationIsInterface) {
  668. // Policy CMP0022 must not be NEW.
  669. this->SetImportLinkProperty(suffix, target,
  670. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  671. iface->Libraries, properties, missingTargets);
  672. return;
  673. }
  674. const char* propContent;
  675. if (const char* prop_suffixed =
  676. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  677. propContent = prop_suffixed;
  678. } else if (const char* prop =
  679. target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  680. propContent = prop;
  681. } else {
  682. return;
  683. }
  684. const bool newCMP0022Behavior =
  685. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  686. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  687. if (newCMP0022Behavior && !this->ExportOld) {
  688. cmLocalGenerator* lg = target->GetLocalGenerator();
  689. std::ostringstream e;
  690. e << "Target \"" << target->GetName()
  691. << "\" has policy CMP0022 enabled, "
  692. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  693. "populated, but it was exported without the "
  694. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  695. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  696. return;
  697. }
  698. if (!*propContent) {
  699. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  700. return;
  701. }
  702. std::string prepro =
  703. cmGeneratorExpression::Preprocess(propContent, preprocessRule);
  704. if (!prepro.empty()) {
  705. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets,
  706. ReplaceFreeTargets);
  707. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  708. }
  709. }
  710. void cmExportFileGenerator::SetImportDetailProperties(
  711. const std::string& config, std::string const& suffix,
  712. cmGeneratorTarget* target, ImportPropertyMap& properties,
  713. std::vector<std::string>& missingTargets)
  714. {
  715. // Get the makefile in which to lookup target information.
  716. cmMakefile* mf = target->Makefile;
  717. // Add the soname for unix shared libraries.
  718. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  719. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  720. if (!target->IsDLLPlatform()) {
  721. std::string prop;
  722. std::string value;
  723. if (target->HasSOName(config)) {
  724. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  725. value = this->InstallNameDir(target, config);
  726. }
  727. prop = "IMPORTED_SONAME";
  728. value += target->GetSOName(config);
  729. } else {
  730. prop = "IMPORTED_NO_SONAME";
  731. value = "TRUE";
  732. }
  733. prop += suffix;
  734. properties[prop] = value;
  735. }
  736. }
  737. // Add the transitive link dependencies for this configuration.
  738. if (cmLinkInterface const* iface =
  739. target->GetLinkInterface(config, target)) {
  740. this->SetImportLinkProperty(suffix, target,
  741. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  742. iface->Languages, properties, missingTargets);
  743. std::vector<std::string> dummy;
  744. this->SetImportLinkProperty(suffix, target,
  745. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  746. iface->SharedDeps, properties, dummy);
  747. if (iface->Multiplicity > 0) {
  748. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  749. prop += suffix;
  750. std::ostringstream m;
  751. m << iface->Multiplicity;
  752. properties[prop] = m.str();
  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 = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  759. prop += suffix;
  760. std::string propval;
  761. if (auto* p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  762. propval = p;
  763. } else if (target->IsCSharpOnly()) {
  764. // C# projects do not have the /clr flag, so we set the property
  765. // here to mark the target as (only) managed (i.e. no .lib file
  766. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  767. // property would have to be set manually for C# targets to make
  768. // exporting/importing work.
  769. propval = "CSharp";
  770. }
  771. properties[prop] = propval;
  772. }
  773. }
  774. static std::string const& asString(std::string const& l)
  775. {
  776. return l;
  777. }
  778. static std::string const& asString(cmLinkItem const& l)
  779. {
  780. return l.AsStr();
  781. }
  782. template <typename T>
  783. void cmExportFileGenerator::SetImportLinkProperty(
  784. std::string const& suffix, cmGeneratorTarget* target,
  785. const std::string& propName, std::vector<T> const& entries,
  786. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  787. {
  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. std::string temp = asString(l);
  800. this->AddTargetNamespace(temp, target, missingTargets);
  801. link_entries += temp;
  802. }
  803. // Store the property.
  804. std::string prop = propName;
  805. prop += suffix;
  806. properties[prop] = link_entries;
  807. }
  808. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  809. {
  810. // Protect that file against use with older CMake versions.
  811. /* clang-format off */
  812. os << "# Generated by CMake\n\n";
  813. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  814. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  815. << "endif()\n";
  816. /* clang-format on */
  817. // Isolate the file policy level.
  818. // We use 2.6 here instead of the current version because newer
  819. // versions of CMake should be able to export files imported by 2.6
  820. // until the import format changes.
  821. /* clang-format off */
  822. os << "cmake_policy(PUSH)\n"
  823. << "cmake_policy(VERSION 2.6)\n";
  824. /* clang-format on */
  825. }
  826. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  827. {
  828. os << "cmake_policy(POP)\n";
  829. }
  830. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  831. const std::string& config)
  832. {
  833. os << "#----------------------------------------------------------------\n"
  834. << "# Generated CMake target import file";
  835. if (!config.empty()) {
  836. os << " for configuration \"" << config << "\".\n";
  837. } else {
  838. os << ".\n";
  839. }
  840. os << "#----------------------------------------------------------------\n"
  841. << "\n";
  842. this->GenerateImportVersionCode(os);
  843. }
  844. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  845. {
  846. os << "# Commands beyond this point should not need to know the version.\n"
  847. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  848. }
  849. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  850. {
  851. // Store an import file format version. This will let us change the
  852. // format later while still allowing old import files to work.
  853. /* clang-format off */
  854. os << "# Commands may need to know the format version.\n"
  855. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  856. << "\n";
  857. /* clang-format on */
  858. }
  859. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  860. std::ostream& os, const std::string& expectedTargets)
  861. {
  862. /* clang-format off */
  863. os << "# Protect against multiple inclusion, which would fail when already "
  864. "imported targets are added once more.\n"
  865. "set(_targetsDefined)\n"
  866. "set(_targetsNotDefined)\n"
  867. "set(_expectedTargets)\n"
  868. "foreach(_expectedTarget " << expectedTargets << ")\n"
  869. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  870. " if(NOT TARGET ${_expectedTarget})\n"
  871. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  872. " endif()\n"
  873. " if(TARGET ${_expectedTarget})\n"
  874. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  875. " endif()\n"
  876. "endforeach()\n"
  877. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  878. " unset(_targetsDefined)\n"
  879. " unset(_targetsNotDefined)\n"
  880. " unset(_expectedTargets)\n"
  881. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  882. " cmake_policy(POP)\n"
  883. " return()\n"
  884. "endif()\n"
  885. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  886. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  887. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  888. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  889. "endif()\n"
  890. "unset(_targetsDefined)\n"
  891. "unset(_targetsNotDefined)\n"
  892. "unset(_expectedTargets)\n"
  893. "\n\n";
  894. /* clang-format on */
  895. }
  896. void cmExportFileGenerator::GenerateImportTargetCode(
  897. std::ostream& os, cmGeneratorTarget const* target,
  898. cmStateEnums::TargetType targetType)
  899. {
  900. // Construct the imported target name.
  901. std::string targetName = this->Namespace;
  902. targetName += target->GetExportName();
  903. // Create the imported target.
  904. os << "# Create imported target " << targetName << "\n";
  905. switch (targetType) {
  906. case cmStateEnums::EXECUTABLE:
  907. os << "add_executable(" << targetName << " IMPORTED)\n";
  908. break;
  909. case cmStateEnums::STATIC_LIBRARY:
  910. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  911. break;
  912. case cmStateEnums::SHARED_LIBRARY:
  913. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  914. break;
  915. case cmStateEnums::MODULE_LIBRARY:
  916. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  917. break;
  918. case cmStateEnums::UNKNOWN_LIBRARY:
  919. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  920. break;
  921. case cmStateEnums::OBJECT_LIBRARY:
  922. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  923. break;
  924. case cmStateEnums::INTERFACE_LIBRARY:
  925. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  926. break;
  927. default: // should never happen
  928. break;
  929. }
  930. // Mark the imported executable if it has exports.
  931. if (target->IsExecutableWithExports()) {
  932. os << "set_property(TARGET " << targetName
  933. << " PROPERTY ENABLE_EXPORTS 1)\n";
  934. }
  935. // Mark the imported library if it is a framework.
  936. if (target->IsFrameworkOnApple()) {
  937. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  938. }
  939. // Mark the imported executable if it is an application bundle.
  940. if (target->IsAppBundleOnApple()) {
  941. os << "set_property(TARGET " << targetName
  942. << " PROPERTY MACOSX_BUNDLE 1)\n";
  943. }
  944. if (target->IsCFBundleOnApple()) {
  945. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  946. }
  947. os << "\n";
  948. }
  949. void cmExportFileGenerator::GenerateImportPropertyCode(
  950. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  951. ImportPropertyMap const& properties)
  952. {
  953. // Construct the imported target name.
  954. std::string targetName = this->Namespace;
  955. targetName += target->GetExportName();
  956. // Set the import properties.
  957. os << "# Import target \"" << targetName << "\" for configuration \""
  958. << config << "\"\n";
  959. os << "set_property(TARGET " << targetName
  960. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  961. if (!config.empty()) {
  962. os << cmSystemTools::UpperCase(config);
  963. } else {
  964. os << "NOCONFIG";
  965. }
  966. os << ")\n";
  967. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  968. for (auto const& property : properties) {
  969. os << " " << property.first << " "
  970. << cmExportFileGeneratorEscape(property.second) << "\n";
  971. }
  972. os << " )\n"
  973. << "\n";
  974. }
  975. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  976. std::ostream& os, const std::vector<std::string>& missingTargets)
  977. {
  978. if (missingTargets.empty()) {
  979. /* clang-format off */
  980. os << "# This file does not depend on other imported targets which have\n"
  981. "# been exported from the same project but in a separate "
  982. "export set.\n\n";
  983. /* clang-format on */
  984. return;
  985. }
  986. /* clang-format off */
  987. os << "# Make sure the targets which have been exported in some other \n"
  988. "# export set exist.\n"
  989. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  990. "foreach(_target ";
  991. /* clang-format on */
  992. std::set<std::string> emitted;
  993. for (std::string const& missingTarget : missingTargets) {
  994. if (emitted.insert(missingTarget).second) {
  995. os << "\"" << missingTarget << "\" ";
  996. }
  997. }
  998. /* clang-format off */
  999. os << ")\n"
  1000. " if(NOT TARGET \"${_target}\" )\n"
  1001. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  1002. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  1003. "\n"
  1004. " endif()\n"
  1005. "endforeach()\n"
  1006. "\n"
  1007. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1008. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  1009. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  1010. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  1011. "\"The following imported targets are "
  1012. "referenced, but are missing: "
  1013. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1014. " else()\n"
  1015. " message(FATAL_ERROR \"The following imported targets are "
  1016. "referenced, but are missing: "
  1017. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1018. " endif()\n"
  1019. "endif()\n"
  1020. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1021. "\n";
  1022. /* clang-format on */
  1023. }
  1024. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  1025. {
  1026. // Add code which verifies at cmake time that the file which is being
  1027. // imported actually exists on disk. This should in theory always be theory
  1028. // case, but still when packages are split into normal and development
  1029. // packages this might get broken (e.g. the Config.cmake could be part of
  1030. // the non-development package, something similar happened to me without
  1031. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1032. // but the development package was not installed.).
  1033. /* clang-format off */
  1034. os << "# Loop over all imported files and verify that they actually exist\n"
  1035. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1036. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1037. " if(NOT EXISTS \"${file}\" )\n"
  1038. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1039. " references the file\n"
  1040. " \\\"${file}\\\"\n"
  1041. "but this file does not exist. Possible reasons include:\n"
  1042. "* The file was deleted, renamed, or moved to another location.\n"
  1043. "* An install or uninstall procedure did not complete successfully.\n"
  1044. "* The installation package was faulty and contained\n"
  1045. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1046. "but not all the files it references.\n"
  1047. "\")\n"
  1048. " endif()\n"
  1049. " endforeach()\n"
  1050. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1051. "endforeach()\n"
  1052. "unset(_IMPORT_CHECK_TARGETS)\n"
  1053. "\n";
  1054. /* clang-format on */
  1055. }
  1056. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1057. std::ostream& os, cmGeneratorTarget* target,
  1058. ImportPropertyMap const& properties,
  1059. const std::set<std::string>& importedLocations)
  1060. {
  1061. // Construct the imported target name.
  1062. std::string targetName = this->Namespace;
  1063. targetName += target->GetExportName();
  1064. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1065. << " )\n"
  1066. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1067. << targetName << " ";
  1068. for (std::string const& li : importedLocations) {
  1069. ImportPropertyMap::const_iterator pi = properties.find(li);
  1070. if (pi != properties.end()) {
  1071. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1072. }
  1073. }
  1074. os << ")\n\n";
  1075. }
  1076. bool cmExportFileGenerator::PopulateExportProperties(
  1077. cmGeneratorTarget* gte, ImportPropertyMap& properties,
  1078. std::string& errorMessage)
  1079. {
  1080. auto& targetProperties = gte->Target->GetProperties();
  1081. if (const char* exportProperties =
  1082. targetProperties.GetPropertyValue("EXPORT_PROPERTIES")) {
  1083. for (auto& prop : cmSystemTools::ExpandedListArgument(exportProperties)) {
  1084. /* Black list reserved properties */
  1085. if (cmSystemTools::StringStartsWith(prop, "IMPORTED_") ||
  1086. cmSystemTools::StringStartsWith(prop, "INTERFACE_")) {
  1087. std::ostringstream e;
  1088. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1089. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1090. << "properties are reserved.";
  1091. errorMessage = e.str();
  1092. return false;
  1093. }
  1094. auto propertyValue = targetProperties.GetPropertyValue(prop);
  1095. if (propertyValue == nullptr) {
  1096. // Asked to export a property that isn't defined on the target. Do not
  1097. // consider this an error, there's just nothing to export.
  1098. continue;
  1099. }
  1100. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1101. propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1102. if (evaluatedValue != propertyValue) {
  1103. std::ostringstream e;
  1104. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1105. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1106. << "generator expression. This is not allowed.";
  1107. errorMessage = e.str();
  1108. return false;
  1109. }
  1110. properties[prop] = propertyValue;
  1111. }
  1112. }
  1113. return true;
  1114. }