cmExportFileGenerator.cxx 41 KB

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