cmExportFileGenerator.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmExportFileGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLinkItem.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmPolicies.h"
  13. #include "cmProperty.h"
  14. #include "cmPropertyMap.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmTargetExport.h"
  19. #include "cmake.h"
  20. #include "cmsys/FStream.hxx"
  21. #include <assert.h>
  22. #include <memory> // IWYU pragma: keep
  23. #include <sstream>
  24. #include <string.h>
  25. #include <utility>
  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 char* cmExportFileGenerator::GetMainExportFileName() const
  56. {
  57. return this->MainImportFile.c_str();
  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. foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
  66. std::ios::app);
  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.get() || !*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().c_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. cmake::MessageType messageType = cmake::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 = cmake::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(cmake::AUTHOR_WARNING,
  253. 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(cmake::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::PopulateInterfaceProperty(
  406. const std::string& propName, cmGeneratorTarget* target,
  407. cmGeneratorExpression::PreprocessContext preprocessRule,
  408. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  409. {
  410. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  411. properties, missingTargets);
  412. }
  413. void getPropertyContents(cmGeneratorTarget const* tgt, const std::string& prop,
  414. std::set<std::string>& ifaceProperties)
  415. {
  416. const char* p = tgt->GetProperty(prop);
  417. if (!p) {
  418. return;
  419. }
  420. std::vector<std::string> content;
  421. cmSystemTools::ExpandListArgument(p, content);
  422. ifaceProperties.insert(content.begin(), content.end());
  423. }
  424. void getCompatibleInterfaceProperties(cmGeneratorTarget* target,
  425. std::set<std::string>& ifaceProperties,
  426. const std::string& config)
  427. {
  428. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  429. // object libraries have no link information, so nothing to compute
  430. return;
  431. }
  432. cmComputeLinkInformation* info = target->GetLinkInformation(config);
  433. if (!info) {
  434. cmLocalGenerator* lg = target->GetLocalGenerator();
  435. std::ostringstream e;
  436. e << "Exporting the target \"" << target->GetName()
  437. << "\" is not "
  438. "allowed since its linker language cannot be determined";
  439. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  440. return;
  441. }
  442. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  443. for (auto const& dep : deps) {
  444. if (!dep.Target) {
  445. continue;
  446. }
  447. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_BOOL",
  448. ifaceProperties);
  449. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_STRING",
  450. ifaceProperties);
  451. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  452. ifaceProperties);
  453. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  454. ifaceProperties);
  455. }
  456. }
  457. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  458. cmGeneratorTarget* gtarget, ImportPropertyMap& properties)
  459. {
  460. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL", gtarget,
  461. properties);
  462. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING", gtarget,
  463. properties);
  464. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN", gtarget,
  465. properties);
  466. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX", gtarget,
  467. properties);
  468. std::set<std::string> ifaceProperties;
  469. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  470. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  471. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  472. ifaceProperties);
  473. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  474. ifaceProperties);
  475. if (gtarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  476. getCompatibleInterfaceProperties(gtarget, ifaceProperties, "");
  477. std::vector<std::string> configNames;
  478. gtarget->Target->GetMakefile()->GetConfigurations(configNames);
  479. for (std::string const& cn : configNames) {
  480. getCompatibleInterfaceProperties(gtarget, ifaceProperties, cn);
  481. }
  482. }
  483. for (std::string const& ip : ifaceProperties) {
  484. this->PopulateInterfaceProperty("INTERFACE_" + ip, gtarget, properties);
  485. }
  486. }
  487. void cmExportFileGenerator::GenerateInterfaceProperties(
  488. const cmGeneratorTarget* target, std::ostream& os,
  489. const ImportPropertyMap& properties)
  490. {
  491. if (!properties.empty()) {
  492. std::string targetName = this->Namespace;
  493. targetName += target->GetExportName();
  494. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  495. for (auto const& property : properties) {
  496. os << " " << property.first << " "
  497. << cmExportFileGeneratorEscape(property.second) << "\n";
  498. }
  499. os << ")\n\n";
  500. }
  501. }
  502. bool cmExportFileGenerator::AddTargetNamespace(
  503. std::string& input, cmGeneratorTarget* target,
  504. std::vector<std::string>& missingTargets)
  505. {
  506. cmGeneratorTarget::TargetOrString resolved =
  507. target->ResolveTargetReference(input);
  508. cmGeneratorTarget* tgt = resolved.Target;
  509. if (!tgt) {
  510. input = resolved.String;
  511. return false;
  512. }
  513. if (tgt->IsImported()) {
  514. input = tgt->GetName();
  515. return true;
  516. }
  517. if (this->ExportedTargets.find(tgt) != this->ExportedTargets.end()) {
  518. input = this->Namespace + tgt->GetExportName();
  519. } else {
  520. std::string namespacedTarget;
  521. this->HandleMissingTarget(namespacedTarget, missingTargets, target, tgt);
  522. if (!namespacedTarget.empty()) {
  523. input = namespacedTarget;
  524. } else {
  525. input = tgt->GetName();
  526. }
  527. }
  528. return true;
  529. }
  530. void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  531. std::string& input, cmGeneratorTarget* target,
  532. std::vector<std::string>& missingTargets, FreeTargetsReplace replace)
  533. {
  534. if (replace == NoReplaceFreeTargets) {
  535. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  536. return;
  537. }
  538. std::vector<std::string> parts;
  539. cmGeneratorExpression::Split(input, parts);
  540. std::string sep;
  541. input.clear();
  542. for (std::string& li : parts) {
  543. if (cmGeneratorExpression::Find(li) == std::string::npos) {
  544. this->AddTargetNamespace(li, target, missingTargets);
  545. } else {
  546. this->ResolveTargetsInGeneratorExpression(li, target, missingTargets);
  547. }
  548. input += sep + li;
  549. sep = ";";
  550. }
  551. }
  552. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  553. std::string& input, cmGeneratorTarget* target,
  554. std::vector<std::string>& missingTargets)
  555. {
  556. std::string::size_type pos = 0;
  557. std::string::size_type lastPos = pos;
  558. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  559. std::string::npos) {
  560. std::string::size_type nameStartPos =
  561. pos + sizeof("$<TARGET_PROPERTY:") - 1;
  562. std::string::size_type closePos = input.find('>', nameStartPos);
  563. std::string::size_type commaPos = input.find(',', nameStartPos);
  564. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  565. if (commaPos == std::string::npos // Implied 'this' target
  566. || closePos == std::string::npos // Incomplete expression.
  567. || closePos < commaPos // Implied 'this' target
  568. || nextOpenPos < commaPos) // Non-literal
  569. {
  570. lastPos = nameStartPos;
  571. continue;
  572. }
  573. std::string targetName =
  574. input.substr(nameStartPos, commaPos - nameStartPos);
  575. if (this->AddTargetNamespace(targetName, target, missingTargets)) {
  576. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  577. }
  578. lastPos = nameStartPos + targetName.size() + 1;
  579. }
  580. std::string errorString;
  581. pos = 0;
  582. lastPos = pos;
  583. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  584. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  585. std::string::size_type endPos = input.find('>', nameStartPos);
  586. if (endPos == std::string::npos) {
  587. errorString = "$<TARGET_NAME:...> expression incomplete";
  588. break;
  589. }
  590. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  591. if (targetName.find("$<") != std::string::npos) {
  592. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  593. "literal.";
  594. break;
  595. }
  596. if (!this->AddTargetNamespace(targetName, target, missingTargets)) {
  597. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  598. "reachable target.";
  599. break;
  600. }
  601. input.replace(pos, endPos - pos + 1, targetName);
  602. lastPos = endPos;
  603. }
  604. pos = 0;
  605. lastPos = pos;
  606. while (errorString.empty() &&
  607. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  608. std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
  609. std::string::size_type endPos = input.find('>', nameStartPos);
  610. if (endPos == std::string::npos) {
  611. errorString = "$<LINK_ONLY:...> expression incomplete";
  612. break;
  613. }
  614. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  615. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  616. this->AddTargetNamespace(libName, target, missingTargets)) {
  617. input.replace(nameStartPos, endPos - nameStartPos, libName);
  618. }
  619. lastPos = nameStartPos + libName.size() + 1;
  620. }
  621. this->ReplaceInstallPrefix(input);
  622. if (!errorString.empty()) {
  623. target->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, errorString);
  624. }
  625. }
  626. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/)
  627. {
  628. // Do nothing
  629. }
  630. void cmExportFileGenerator::SetImportLinkInterface(
  631. const std::string& config, std::string const& suffix,
  632. cmGeneratorExpression::PreprocessContext preprocessRule,
  633. cmGeneratorTarget* target, ImportPropertyMap& properties,
  634. std::vector<std::string>& missingTargets)
  635. {
  636. // Add the transitive link dependencies for this configuration.
  637. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  638. if (!iface) {
  639. return;
  640. }
  641. if (iface->ImplementationIsInterface) {
  642. // Policy CMP0022 must not be NEW.
  643. this->SetImportLinkProperty(suffix, target,
  644. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  645. iface->Libraries, properties, missingTargets);
  646. return;
  647. }
  648. const char* propContent;
  649. if (const char* prop_suffixed =
  650. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  651. propContent = prop_suffixed;
  652. } else if (const char* prop =
  653. target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  654. propContent = prop;
  655. } else {
  656. return;
  657. }
  658. const bool newCMP0022Behavior =
  659. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  660. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  661. if (newCMP0022Behavior && !this->ExportOld) {
  662. cmLocalGenerator* lg = target->GetLocalGenerator();
  663. std::ostringstream e;
  664. e << "Target \"" << target->GetName()
  665. << "\" has policy CMP0022 enabled, "
  666. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  667. "populated, but it was exported without the "
  668. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  669. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  670. return;
  671. }
  672. if (!*propContent) {
  673. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  674. return;
  675. }
  676. std::string prepro =
  677. cmGeneratorExpression::Preprocess(propContent, preprocessRule);
  678. if (!prepro.empty()) {
  679. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets,
  680. ReplaceFreeTargets);
  681. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  682. }
  683. }
  684. void cmExportFileGenerator::SetImportDetailProperties(
  685. const std::string& config, std::string const& suffix,
  686. cmGeneratorTarget* target, ImportPropertyMap& properties,
  687. std::vector<std::string>& missingTargets)
  688. {
  689. // Get the makefile in which to lookup target information.
  690. cmMakefile* mf = target->Makefile;
  691. // Add the soname for unix shared libraries.
  692. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  693. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  694. if (!target->IsDLLPlatform()) {
  695. std::string prop;
  696. std::string value;
  697. if (target->HasSOName(config)) {
  698. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  699. value = this->InstallNameDir(target, config);
  700. }
  701. prop = "IMPORTED_SONAME";
  702. value += target->GetSOName(config);
  703. } else {
  704. prop = "IMPORTED_NO_SONAME";
  705. value = "TRUE";
  706. }
  707. prop += suffix;
  708. properties[prop] = value;
  709. }
  710. }
  711. // Add the transitive link dependencies for this configuration.
  712. if (cmLinkInterface const* iface =
  713. target->GetLinkInterface(config, target)) {
  714. this->SetImportLinkProperty(suffix, target,
  715. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  716. iface->Languages, properties, missingTargets);
  717. std::vector<std::string> dummy;
  718. this->SetImportLinkProperty(suffix, target,
  719. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  720. iface->SharedDeps, properties, dummy);
  721. if (iface->Multiplicity > 0) {
  722. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  723. prop += suffix;
  724. std::ostringstream m;
  725. m << iface->Multiplicity;
  726. properties[prop] = m.str();
  727. }
  728. }
  729. // Add information if this target is a managed target
  730. if (target->GetManagedType(config) !=
  731. cmGeneratorTarget::ManagedType::Native) {
  732. std::string prop = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  733. prop += suffix;
  734. std::string propval;
  735. if (auto* p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  736. propval = p;
  737. } else if (target->HasLanguage("CSharp", config)) {
  738. // C# projects do not have the /clr flag, so we set the property
  739. // here to mark the target as (only) managed (i.e. no .lib file
  740. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  741. // property would have to be set manually for C# targets to make
  742. // exporting/importing work.
  743. propval = "CSharp";
  744. }
  745. properties[prop] = propval;
  746. }
  747. }
  748. static std::string const& asString(std::string const& l)
  749. {
  750. return l;
  751. }
  752. static std::string const& asString(cmLinkItem const& l)
  753. {
  754. return l.AsStr();
  755. }
  756. template <typename T>
  757. void cmExportFileGenerator::SetImportLinkProperty(
  758. std::string const& suffix, cmGeneratorTarget* target,
  759. const std::string& propName, std::vector<T> const& entries,
  760. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  761. {
  762. // Skip the property if there are no entries.
  763. if (entries.empty()) {
  764. return;
  765. }
  766. // Construct the property value.
  767. std::string link_entries;
  768. const char* sep = "";
  769. for (T const& l : entries) {
  770. // Separate this from the previous entry.
  771. link_entries += sep;
  772. sep = ";";
  773. std::string temp = asString(l);
  774. this->AddTargetNamespace(temp, target, missingTargets);
  775. link_entries += temp;
  776. }
  777. // Store the property.
  778. std::string prop = propName;
  779. prop += suffix;
  780. properties[prop] = link_entries;
  781. }
  782. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  783. {
  784. // Protect that file against use with older CMake versions.
  785. /* clang-format off */
  786. os << "# Generated by CMake\n\n";
  787. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  788. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  789. << "endif()\n";
  790. /* clang-format on */
  791. // Isolate the file policy level.
  792. // We use 2.6 here instead of the current version because newer
  793. // versions of CMake should be able to export files imported by 2.6
  794. // until the import format changes.
  795. /* clang-format off */
  796. os << "cmake_policy(PUSH)\n"
  797. << "cmake_policy(VERSION 2.6)\n";
  798. /* clang-format on */
  799. }
  800. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  801. {
  802. os << "cmake_policy(POP)\n";
  803. }
  804. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  805. const std::string& config)
  806. {
  807. os << "#----------------------------------------------------------------\n"
  808. << "# Generated CMake target import file";
  809. if (!config.empty()) {
  810. os << " for configuration \"" << config << "\".\n";
  811. } else {
  812. os << ".\n";
  813. }
  814. os << "#----------------------------------------------------------------\n"
  815. << "\n";
  816. this->GenerateImportVersionCode(os);
  817. }
  818. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  819. {
  820. os << "# Commands beyond this point should not need to know the version.\n"
  821. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  822. }
  823. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  824. {
  825. // Store an import file format version. This will let us change the
  826. // format later while still allowing old import files to work.
  827. /* clang-format off */
  828. os << "# Commands may need to know the format version.\n"
  829. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  830. << "\n";
  831. /* clang-format on */
  832. }
  833. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  834. std::ostream& os, const std::string& expectedTargets)
  835. {
  836. /* clang-format off */
  837. os << "# Protect against multiple inclusion, which would fail when already "
  838. "imported targets are added once more.\n"
  839. "set(_targetsDefined)\n"
  840. "set(_targetsNotDefined)\n"
  841. "set(_expectedTargets)\n"
  842. "foreach(_expectedTarget " << expectedTargets << ")\n"
  843. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  844. " if(NOT TARGET ${_expectedTarget})\n"
  845. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  846. " endif()\n"
  847. " if(TARGET ${_expectedTarget})\n"
  848. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  849. " endif()\n"
  850. "endforeach()\n"
  851. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  852. " unset(_targetsDefined)\n"
  853. " unset(_targetsNotDefined)\n"
  854. " unset(_expectedTargets)\n"
  855. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  856. " cmake_policy(POP)\n"
  857. " return()\n"
  858. "endif()\n"
  859. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  860. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  861. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  862. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  863. "endif()\n"
  864. "unset(_targetsDefined)\n"
  865. "unset(_targetsNotDefined)\n"
  866. "unset(_expectedTargets)\n"
  867. "\n\n";
  868. /* clang-format on */
  869. }
  870. void cmExportFileGenerator::GenerateImportTargetCode(
  871. std::ostream& os, cmGeneratorTarget const* target,
  872. cmStateEnums::TargetType targetType)
  873. {
  874. // Construct the imported target name.
  875. std::string targetName = this->Namespace;
  876. targetName += target->GetExportName();
  877. // Create the imported target.
  878. os << "# Create imported target " << targetName << "\n";
  879. switch (targetType) {
  880. case cmStateEnums::EXECUTABLE:
  881. os << "add_executable(" << targetName << " IMPORTED)\n";
  882. break;
  883. case cmStateEnums::STATIC_LIBRARY:
  884. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  885. break;
  886. case cmStateEnums::SHARED_LIBRARY:
  887. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  888. break;
  889. case cmStateEnums::MODULE_LIBRARY:
  890. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  891. break;
  892. case cmStateEnums::UNKNOWN_LIBRARY:
  893. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  894. break;
  895. case cmStateEnums::OBJECT_LIBRARY:
  896. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  897. break;
  898. case cmStateEnums::INTERFACE_LIBRARY:
  899. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  900. break;
  901. default: // should never happen
  902. break;
  903. }
  904. // Mark the imported executable if it has exports.
  905. if (target->IsExecutableWithExports()) {
  906. os << "set_property(TARGET " << targetName
  907. << " PROPERTY ENABLE_EXPORTS 1)\n";
  908. }
  909. // Mark the imported library if it is a framework.
  910. if (target->IsFrameworkOnApple()) {
  911. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  912. }
  913. // Mark the imported executable if it is an application bundle.
  914. if (target->IsAppBundleOnApple()) {
  915. os << "set_property(TARGET " << targetName
  916. << " PROPERTY MACOSX_BUNDLE 1)\n";
  917. }
  918. if (target->IsCFBundleOnApple()) {
  919. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  920. }
  921. os << "\n";
  922. }
  923. void cmExportFileGenerator::GenerateImportPropertyCode(
  924. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  925. ImportPropertyMap const& properties)
  926. {
  927. // Construct the imported target name.
  928. std::string targetName = this->Namespace;
  929. targetName += target->GetExportName();
  930. // Set the import properties.
  931. os << "# Import target \"" << targetName << "\" for configuration \""
  932. << config << "\"\n";
  933. os << "set_property(TARGET " << targetName
  934. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  935. if (!config.empty()) {
  936. os << cmSystemTools::UpperCase(config);
  937. } else {
  938. os << "NOCONFIG";
  939. }
  940. os << ")\n";
  941. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  942. for (auto const& property : properties) {
  943. os << " " << property.first << " "
  944. << cmExportFileGeneratorEscape(property.second) << "\n";
  945. }
  946. os << " )\n"
  947. << "\n";
  948. }
  949. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  950. std::ostream& os, const std::vector<std::string>& missingTargets)
  951. {
  952. if (missingTargets.empty()) {
  953. /* clang-format off */
  954. os << "# This file does not depend on other imported targets which have\n"
  955. "# been exported from the same project but in a separate "
  956. "export set.\n\n";
  957. /* clang-format on */
  958. return;
  959. }
  960. /* clang-format off */
  961. os << "# Make sure the targets which have been exported in some other \n"
  962. "# export set exist.\n"
  963. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  964. "foreach(_target ";
  965. /* clang-format on */
  966. std::set<std::string> emitted;
  967. for (std::string const& missingTarget : missingTargets) {
  968. if (emitted.insert(missingTarget).second) {
  969. os << "\"" << missingTarget << "\" ";
  970. }
  971. }
  972. /* clang-format off */
  973. os << ")\n"
  974. " if(NOT TARGET \"${_target}\" )\n"
  975. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  976. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  977. "\n"
  978. " endif()\n"
  979. "endforeach()\n"
  980. "\n"
  981. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  982. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  983. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  984. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  985. "\"The following imported targets are "
  986. "referenced, but are missing: "
  987. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  988. " else()\n"
  989. " message(FATAL_ERROR \"The following imported targets are "
  990. "referenced, but are missing: "
  991. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  992. " endif()\n"
  993. "endif()\n"
  994. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  995. "\n";
  996. /* clang-format on */
  997. }
  998. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  999. {
  1000. // Add code which verifies at cmake time that the file which is being
  1001. // imported actually exists on disk. This should in theory always be theory
  1002. // case, but still when packages are split into normal and development
  1003. // packages this might get broken (e.g. the Config.cmake could be part of
  1004. // the non-development package, something similar happened to me without
  1005. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1006. // but the development package was not installed.).
  1007. /* clang-format off */
  1008. os << "# Loop over all imported files and verify that they actually exist\n"
  1009. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1010. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1011. " if(NOT EXISTS \"${file}\" )\n"
  1012. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1013. " references the file\n"
  1014. " \\\"${file}\\\"\n"
  1015. "but this file does not exist. Possible reasons include:\n"
  1016. "* The file was deleted, renamed, or moved to another location.\n"
  1017. "* An install or uninstall procedure did not complete successfully.\n"
  1018. "* The installation package was faulty and contained\n"
  1019. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1020. "but not all the files it references.\n"
  1021. "\")\n"
  1022. " endif()\n"
  1023. " endforeach()\n"
  1024. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1025. "endforeach()\n"
  1026. "unset(_IMPORT_CHECK_TARGETS)\n"
  1027. "\n";
  1028. /* clang-format on */
  1029. }
  1030. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1031. std::ostream& os, cmGeneratorTarget* target,
  1032. ImportPropertyMap const& properties,
  1033. const std::set<std::string>& importedLocations)
  1034. {
  1035. // Construct the imported target name.
  1036. std::string targetName = this->Namespace;
  1037. targetName += target->GetExportName();
  1038. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1039. << " )\n"
  1040. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1041. << targetName << " ";
  1042. for (std::string const& li : importedLocations) {
  1043. ImportPropertyMap::const_iterator pi = properties.find(li);
  1044. if (pi != properties.end()) {
  1045. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1046. }
  1047. }
  1048. os << ")\n\n";
  1049. }
  1050. bool cmExportFileGenerator::PopulateExportProperties(
  1051. cmGeneratorTarget* gte, ImportPropertyMap& properties,
  1052. std::string& errorMessage)
  1053. {
  1054. auto& targetProperties = gte->Target->GetProperties();
  1055. const auto& exportProperties = targetProperties.find("EXPORT_PROPERTIES");
  1056. if (exportProperties != targetProperties.end()) {
  1057. std::vector<std::string> propsToExport;
  1058. cmSystemTools::ExpandListArgument(exportProperties->second.GetValue(),
  1059. propsToExport);
  1060. for (auto& prop : propsToExport) {
  1061. /* Black list reserved properties */
  1062. if (cmSystemTools::StringStartsWith(prop, "IMPORTED_") ||
  1063. cmSystemTools::StringStartsWith(prop, "INTERFACE_")) {
  1064. std::ostringstream e;
  1065. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1066. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1067. << "properties are reserved.";
  1068. errorMessage = e.str();
  1069. return false;
  1070. }
  1071. auto propertyValue = targetProperties.GetPropertyValue(prop);
  1072. if (propertyValue == nullptr) {
  1073. // Asked to export a property that isn't defined on the target. Do not
  1074. // consider this an error, there's just nothing to export.
  1075. continue;
  1076. }
  1077. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1078. propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1079. if (evaluatedValue != propertyValue) {
  1080. std::ostringstream e;
  1081. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1082. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1083. << "generator expression. This is not allowed.";
  1084. errorMessage = e.str();
  1085. return false;
  1086. }
  1087. properties[prop] = propertyValue;
  1088. }
  1089. }
  1090. return true;
  1091. }