cmExportFileGenerator.cxx 43 KB

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