cmExportFileGenerator.cxx 41 KB

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