cmExportFileGenerator.cxx 43 KB

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