cmExportFileGenerator.cxx 41 KB

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