cmExportCMakeConfigGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 "cmExportCMakeConfigGenerator.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <sstream>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/optional>
  10. #include <cm/string_view>
  11. #include <cmext/string_view>
  12. #include "cmExportSet.h"
  13. #include "cmFileSet.h"
  14. #include "cmFindPackageStack.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmLinkItem.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmMessageType.h"
  21. #include "cmOutputConverter.h"
  22. #include "cmPolicies.h"
  23. #include "cmStateTypes.h"
  24. #include "cmStringAlgorithms.h"
  25. #include "cmSystemTools.h"
  26. #include "cmTarget.h"
  27. #include "cmValue.h"
  28. #include "cmVersion.h"
  29. static std::string cmExportFileGeneratorEscape(std::string const& str)
  30. {
  31. // Escape a property value for writing into a .cmake file.
  32. std::string result = cmOutputConverter::EscapeForCMake(str);
  33. // Un-escape variable references generated by our own export code.
  34. cmSystemTools::ReplaceString(result, "\\${_IMPORT_PREFIX}",
  35. "${_IMPORT_PREFIX}");
  36. cmSystemTools::ReplaceString(result, "\\${CMAKE_IMPORT_LIBRARY_SUFFIX}",
  37. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  38. return result;
  39. }
  40. cmExportCMakeConfigGenerator::cmExportCMakeConfigGenerator() = default;
  41. cm::string_view cmExportCMakeConfigGenerator::GetImportPrefixWithSlash() const
  42. {
  43. return "${_IMPORT_PREFIX}/"_s;
  44. }
  45. bool cmExportCMakeConfigGenerator::GenerateImportFile(std::ostream& os)
  46. {
  47. std::stringstream mainFileWithHeadersAndFootersBuffer;
  48. // Start with the import file header.
  49. this->GenerateImportHeaderCode(mainFileWithHeadersAndFootersBuffer);
  50. // Create all the imported targets.
  51. std::stringstream mainFileBuffer;
  52. bool result = this->GenerateMainFile(mainFileBuffer);
  53. // Export find_dependency() calls. Must be done after GenerateMainFile(),
  54. // because that's when target dependencies are gathered, which we need for
  55. // the find_dependency() calls.
  56. if (!this->AppendMode && this->GetExportSet() &&
  57. this->ExportPackageDependencies) {
  58. this->SetRequiredCMakeVersion(3, 9, 0);
  59. this->GenerateFindDependencyCalls(mainFileWithHeadersAndFootersBuffer);
  60. }
  61. // Write cached import code.
  62. mainFileWithHeadersAndFootersBuffer << mainFileBuffer.rdbuf();
  63. // End with the import file footer.
  64. this->GenerateImportFooterCode(mainFileWithHeadersAndFootersBuffer);
  65. this->GeneratePolicyFooterCode(mainFileWithHeadersAndFootersBuffer);
  66. // This has to be done last, after the minimum CMake version has been
  67. // determined.
  68. this->GeneratePolicyHeaderCode(os);
  69. os << mainFileWithHeadersAndFootersBuffer.rdbuf();
  70. return result;
  71. }
  72. void cmExportCMakeConfigGenerator::GenerateInterfaceProperties(
  73. cmGeneratorTarget const* target, std::ostream& os,
  74. ImportPropertyMap const& properties)
  75. {
  76. if (!properties.empty()) {
  77. std::string targetName =
  78. cmStrCat(this->Namespace, target->GetExportName());
  79. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  80. for (auto const& property : properties) {
  81. os << " " << property.first << ' '
  82. << cmExportFileGeneratorEscape(property.second) << '\n';
  83. }
  84. os << ")\n\n";
  85. }
  86. }
  87. void cmExportCMakeConfigGenerator::SetImportLinkInterface(
  88. std::string const& config, std::string const& suffix,
  89. cmGeneratorExpression::PreprocessContext preprocessRule,
  90. cmGeneratorTarget const* target, ImportPropertyMap& properties)
  91. {
  92. // Add the transitive link dependencies for this configuration.
  93. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  94. if (!iface) {
  95. return;
  96. }
  97. if (iface->ImplementationIsInterface) {
  98. // Policy CMP0022 must not be NEW.
  99. this->SetImportLinkProperty(
  100. suffix, target, "IMPORTED_LINK_INTERFACE_LIBRARIES", iface->Libraries,
  101. properties, ImportLinkPropertyTargetNames::Yes);
  102. return;
  103. }
  104. cmValue propContent;
  105. if (cmValue prop_suffixed =
  106. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  107. propContent = prop_suffixed;
  108. } else if (cmValue prop = target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  109. propContent = prop;
  110. } else {
  111. return;
  112. }
  113. bool const newCMP0022Behavior =
  114. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  115. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  116. if (newCMP0022Behavior && !this->ExportOld) {
  117. cmLocalGenerator* lg = target->GetLocalGenerator();
  118. std::ostringstream e;
  119. e << "Target \"" << target->GetName()
  120. << "\" has policy CMP0022 enabled, "
  121. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  122. "populated, but it was exported without the "
  123. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  124. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  125. return;
  126. }
  127. if (propContent->empty()) {
  128. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  129. return;
  130. }
  131. std::string prepro =
  132. cmGeneratorExpression::Preprocess(*propContent, preprocessRule);
  133. if (!prepro.empty()) {
  134. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  135. ReplaceFreeTargets);
  136. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  137. }
  138. }
  139. void cmExportCMakeConfigGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  140. {
  141. // Protect that file against use with older CMake versions.
  142. /* clang-format off */
  143. os << "# Generated by CMake\n\n"
  144. "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.8)\n"
  145. " message(FATAL_ERROR \"CMake >= "
  146. << this->RequiredCMakeVersionMajor << '.'
  147. << this->RequiredCMakeVersionMinor << '.'
  148. << this->RequiredCMakeVersionPatch << " required\")\n"
  149. "endif()\n"
  150. "if(CMAKE_VERSION VERSION_LESS \""
  151. << this->RequiredCMakeVersionMajor << '.'
  152. << this->RequiredCMakeVersionMinor << '.'
  153. << this->RequiredCMakeVersionPatch << "\")\n"
  154. " message(FATAL_ERROR \"CMake >= "
  155. << this->RequiredCMakeVersionMajor << '.'
  156. << this->RequiredCMakeVersionMinor << '.'
  157. << this->RequiredCMakeVersionPatch << " required\")\n"
  158. "endif()\n";
  159. /* clang-format on */
  160. // Isolate the file policy level.
  161. // Support CMake versions as far back as the
  162. // RequiredCMakeVersion{Major,Minor,Patch}, but also support using NEW
  163. // policy settings for up to CMake 3.30 (this upper limit may be reviewed
  164. // and increased from time to time). This reduces the opportunity for CMake
  165. // warnings when an older export file is later used with newer CMake
  166. // versions.
  167. /* clang-format off */
  168. os << "cmake_policy(PUSH)\n"
  169. "cmake_policy(VERSION "
  170. << this->RequiredCMakeVersionMajor << '.'
  171. << this->RequiredCMakeVersionMinor << '.'
  172. << this->RequiredCMakeVersionPatch << "...3.30)\n";
  173. /* clang-format on */
  174. }
  175. void cmExportCMakeConfigGenerator::GeneratePolicyFooterCode(std::ostream& os)
  176. {
  177. os << "cmake_policy(POP)\n";
  178. }
  179. void cmExportCMakeConfigGenerator::GenerateImportHeaderCode(
  180. std::ostream& os, std::string const& config)
  181. {
  182. os << "#----------------------------------------------------------------\n"
  183. "# Generated CMake target import file";
  184. if (!config.empty()) {
  185. os << " for configuration \"" << config << "\".\n";
  186. } else {
  187. os << ".\n";
  188. }
  189. os << "#----------------------------------------------------------------\n"
  190. "\n";
  191. this->GenerateImportVersionCode(os);
  192. }
  193. void cmExportCMakeConfigGenerator::GenerateImportFooterCode(std::ostream& os)
  194. {
  195. os << "# Commands beyond this point should not need to know the version.\n"
  196. "set(CMAKE_IMPORT_FILE_VERSION)\n";
  197. }
  198. void cmExportCMakeConfigGenerator::GenerateImportVersionCode(std::ostream& os)
  199. {
  200. // Store an import file format version. This will let us change the
  201. // format later while still allowing old import files to work.
  202. os << "# Commands may need to know the format version.\n"
  203. "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  204. "\n";
  205. }
  206. void cmExportCMakeConfigGenerator::GenerateExpectedTargetsCode(
  207. std::ostream& os, std::string const& expectedTargets)
  208. {
  209. /* clang-format off */
  210. os << "# Protect against multiple inclusion, which would fail when already "
  211. "imported targets are added once more.\n"
  212. "set(_cmake_targets_defined \"\")\n"
  213. "set(_cmake_targets_not_defined \"\")\n"
  214. "set(_cmake_expected_targets \"\")\n"
  215. "foreach(_cmake_expected_target IN ITEMS " << expectedTargets << ")\n"
  216. " list(APPEND _cmake_expected_targets \"${_cmake_expected_target}\")\n"
  217. " if(TARGET \"${_cmake_expected_target}\")\n"
  218. " list(APPEND _cmake_targets_defined \"${_cmake_expected_target}\")\n"
  219. " else()\n"
  220. " list(APPEND _cmake_targets_not_defined \"${_cmake_expected_target}\")\n"
  221. " endif()\n"
  222. "endforeach()\n"
  223. "unset(_cmake_expected_target)\n"
  224. "if(_cmake_targets_defined STREQUAL _cmake_expected_targets)\n"
  225. " unset(_cmake_targets_defined)\n"
  226. " unset(_cmake_targets_not_defined)\n"
  227. " unset(_cmake_expected_targets)\n"
  228. " unset(CMAKE_IMPORT_FILE_VERSION)\n"
  229. " cmake_policy(POP)\n"
  230. " return()\n"
  231. "endif()\n"
  232. "if(NOT _cmake_targets_defined STREQUAL \"\")\n"
  233. " string(REPLACE \";\" \", \" _cmake_targets_defined_text \"${_cmake_targets_defined}\")\n"
  234. " string(REPLACE \";\" \", \" _cmake_targets_not_defined_text \"${_cmake_targets_not_defined}\")\n"
  235. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  236. "set were already defined.\\nTargets Defined: ${_cmake_targets_defined_text}\\n"
  237. "Targets not yet defined: ${_cmake_targets_not_defined_text}\\n\")\n"
  238. "endif()\n"
  239. "unset(_cmake_targets_defined)\n"
  240. "unset(_cmake_targets_not_defined)\n"
  241. "unset(_cmake_expected_targets)\n"
  242. "\n\n";
  243. /* clang-format on */
  244. }
  245. void cmExportCMakeConfigGenerator::GenerateImportTargetCode(
  246. std::ostream& os, cmGeneratorTarget const* target,
  247. cmStateEnums::TargetType targetType)
  248. {
  249. // Construct the imported target name.
  250. std::string targetName = this->Namespace;
  251. targetName += target->GetExportName();
  252. // Create the imported target.
  253. os << "# Create imported target " << targetName << "\n";
  254. switch (targetType) {
  255. case cmStateEnums::EXECUTABLE:
  256. os << "add_executable(" << targetName << " IMPORTED)\n";
  257. break;
  258. case cmStateEnums::STATIC_LIBRARY:
  259. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  260. break;
  261. case cmStateEnums::SHARED_LIBRARY:
  262. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  263. break;
  264. case cmStateEnums::MODULE_LIBRARY:
  265. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  266. break;
  267. case cmStateEnums::UNKNOWN_LIBRARY:
  268. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  269. break;
  270. case cmStateEnums::OBJECT_LIBRARY:
  271. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  272. break;
  273. case cmStateEnums::INTERFACE_LIBRARY:
  274. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  275. break;
  276. default: // should never happen
  277. break;
  278. }
  279. // Mark the imported executable if it has exports.
  280. if (target->IsExecutableWithExports() ||
  281. (target->IsSharedLibraryWithExports() && target->HasImportLibrary(""))) {
  282. os << "set_property(TARGET " << targetName
  283. << " PROPERTY ENABLE_EXPORTS 1)\n";
  284. }
  285. // Mark the imported library if it is a framework.
  286. if (target->IsFrameworkOnApple()) {
  287. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  288. }
  289. // Mark the imported executable if it is an application bundle.
  290. if (target->IsAppBundleOnApple()) {
  291. os << "set_property(TARGET " << targetName
  292. << " PROPERTY MACOSX_BUNDLE 1)\n";
  293. }
  294. if (target->IsCFBundleOnApple()) {
  295. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  296. }
  297. // Mark the imported library if it is an AIX shared library archive.
  298. if (target->IsArchivedAIXSharedLibrary()) {
  299. os << "set_property(TARGET " << targetName
  300. << " PROPERTY AIX_SHARED_LIBRARY_ARCHIVE 1)\n";
  301. }
  302. // generate DEPRECATION
  303. if (target->IsDeprecated()) {
  304. os << "set_property(TARGET " << targetName << " PROPERTY DEPRECATION "
  305. << cmExportFileGeneratorEscape(target->GetDeprecation()) << ")\n";
  306. }
  307. if (target->GetPropertyAsBool("IMPORTED_NO_SYSTEM")) {
  308. os << "set_property(TARGET " << targetName
  309. << " PROPERTY IMPORTED_NO_SYSTEM 1)\n";
  310. }
  311. if (target->GetPropertyAsBool("EXPORT_NO_SYSTEM")) {
  312. os << "set_property(TARGET " << targetName << " PROPERTY SYSTEM 0)\n";
  313. }
  314. os << '\n';
  315. }
  316. void cmExportCMakeConfigGenerator::GenerateImportPropertyCode(
  317. std::ostream& os, std::string const& config, std::string const& suffix,
  318. cmGeneratorTarget const* target, ImportPropertyMap const& properties,
  319. std::string const& importedXcFrameworkLocation)
  320. {
  321. // Construct the imported target name.
  322. std::string targetName = this->Namespace;
  323. targetName += target->GetExportName();
  324. // Set the import properties.
  325. os << "# Import target \"" << targetName << "\" for configuration \""
  326. << config
  327. << "\"\n"
  328. "set_property(TARGET "
  329. << targetName << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  330. if (!config.empty()) {
  331. os << cmSystemTools::UpperCase(config);
  332. } else {
  333. os << "NOCONFIG";
  334. }
  335. os << ")\n"
  336. "set_target_properties("
  337. << targetName << " PROPERTIES\n";
  338. std::string importedLocationProp = cmStrCat("IMPORTED_LOCATION", suffix);
  339. for (auto const& property : properties) {
  340. if (importedXcFrameworkLocation.empty() ||
  341. property.first != importedLocationProp) {
  342. os << " " << property.first << ' '
  343. << cmExportFileGeneratorEscape(property.second) << '\n';
  344. }
  345. }
  346. os << " )\n";
  347. if (!importedXcFrameworkLocation.empty()) {
  348. auto importedLocationIt = properties.find(importedLocationProp);
  349. if (importedLocationIt != properties.end()) {
  350. os << "if(NOT CMAKE_VERSION VERSION_LESS \"3.28\" AND IS_DIRECTORY "
  351. << cmExportFileGeneratorEscape(importedXcFrameworkLocation)
  352. << ")\n"
  353. " set_property(TARGET "
  354. << targetName << " PROPERTY " << importedLocationProp << ' '
  355. << cmExportFileGeneratorEscape(importedXcFrameworkLocation)
  356. << ")\nelse()\n set_property(TARGET " << targetName << " PROPERTY "
  357. << importedLocationProp << ' '
  358. << cmExportFileGeneratorEscape(importedLocationIt->second)
  359. << ")\nendif()\n";
  360. }
  361. }
  362. os << '\n';
  363. }
  364. void cmExportCMakeConfigGenerator::GenerateFindDependencyCalls(
  365. std::ostream& os)
  366. {
  367. os << "include(CMakeFindDependencyMacro)\n";
  368. std::map<std::string, cmExportSet::PackageDependency> packageDependencies;
  369. auto* exportSet = this->GetExportSet();
  370. if (exportSet) {
  371. packageDependencies = exportSet->GetPackageDependencies();
  372. }
  373. for (cmGeneratorTarget const* gt : this->ExternalTargets) {
  374. std::string findPackageName;
  375. auto exportFindPackageName = gt->GetProperty("EXPORT_FIND_PACKAGE_NAME");
  376. cmFindPackageStack pkgStack = gt->Target->GetFindPackageStack();
  377. if (!exportFindPackageName.IsEmpty()) {
  378. findPackageName = *exportFindPackageName;
  379. } else {
  380. if (!pkgStack.Empty()) {
  381. cmFindPackageCall const& fpc = pkgStack.Top();
  382. findPackageName = fpc.Name;
  383. }
  384. }
  385. if (!findPackageName.empty()) {
  386. auto& dep = packageDependencies[findPackageName];
  387. if (!pkgStack.Empty()) {
  388. dep.FindPackageIndex = pkgStack.Top().Index;
  389. }
  390. if (dep.Enabled == cmExportSet::PackageDependencyExportEnabled::Auto) {
  391. dep.Enabled = cmExportSet::PackageDependencyExportEnabled::On;
  392. }
  393. }
  394. }
  395. std::vector<std::pair<std::string, cmExportSet::PackageDependency>>
  396. packageDependenciesSorted(packageDependencies.begin(),
  397. packageDependencies.end());
  398. std::sort(
  399. packageDependenciesSorted.begin(), packageDependenciesSorted.end(),
  400. [](std::pair<std::string, cmExportSet::PackageDependency> const& lhs,
  401. std::pair<std::string, cmExportSet::PackageDependency> const& rhs)
  402. -> bool {
  403. if (lhs.second.SpecifiedIndex) {
  404. if (rhs.second.SpecifiedIndex) {
  405. return lhs.second.SpecifiedIndex < rhs.second.SpecifiedIndex;
  406. }
  407. assert(rhs.second.FindPackageIndex);
  408. return true;
  409. }
  410. assert(lhs.second.FindPackageIndex);
  411. if (rhs.second.SpecifiedIndex) {
  412. return false;
  413. }
  414. assert(rhs.second.FindPackageIndex);
  415. return lhs.second.FindPackageIndex < rhs.second.FindPackageIndex;
  416. });
  417. for (auto const& it : packageDependenciesSorted) {
  418. if (it.second.Enabled == cmExportSet::PackageDependencyExportEnabled::On) {
  419. os << "find_dependency(" << it.first;
  420. for (auto const& arg : it.second.ExtraArguments) {
  421. os << ' ' << cmOutputConverter::EscapeForCMake(arg);
  422. }
  423. os << ")\n";
  424. }
  425. }
  426. os << "\n\n";
  427. }
  428. void cmExportCMakeConfigGenerator::GenerateMissingTargetsCheckCode(
  429. std::ostream& os)
  430. {
  431. if (this->MissingTargets.empty()) {
  432. os << "# This file does not depend on other imported targets which have\n"
  433. "# been exported from the same project but in a separate "
  434. "export set.\n\n";
  435. return;
  436. }
  437. os << "# Make sure the targets which have been exported in some other\n"
  438. "# export set exist.\n"
  439. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  440. "foreach(_target ";
  441. std::set<std::string> emitted;
  442. for (std::string const& missingTarget : this->MissingTargets) {
  443. if (emitted.insert(missingTarget).second) {
  444. os << '"' << missingTarget << "\" ";
  445. }
  446. }
  447. os << ")\n"
  448. " if(NOT TARGET \"${_target}\" )\n"
  449. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  450. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  451. "\n"
  452. " endif()\n"
  453. "endforeach()\n"
  454. "\n"
  455. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  456. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  457. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  458. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  459. "\"The following imported targets are "
  460. "referenced, but are missing: "
  461. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  462. " else()\n"
  463. " message(FATAL_ERROR \"The following imported targets are "
  464. "referenced, but are missing: "
  465. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  466. " endif()\n"
  467. "endif()\n"
  468. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  469. "\n";
  470. }
  471. void cmExportCMakeConfigGenerator::GenerateImportedFileCheckLoop(
  472. std::ostream& os)
  473. {
  474. // Add code which verifies at cmake time that the file which is being
  475. // imported actually exists on disk. This should in theory always be theory
  476. // case, but still when packages are split into normal and development
  477. // packages this might get broken (e.g. the Config.cmake could be part of
  478. // the non-development package, something similar happened to me without
  479. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  480. // but the development package was not installed.).
  481. os << "# Loop over all imported files and verify that they actually exist\n"
  482. "foreach(_cmake_target IN LISTS _cmake_import_check_targets)\n"
  483. " if(CMAKE_VERSION VERSION_LESS \"3.28\"\n"
  484. " OR NOT DEFINED "
  485. "_cmake_import_check_xcframework_for_${_cmake_target}\n"
  486. " OR NOT IS_DIRECTORY "
  487. "\"${_cmake_import_check_xcframework_for_${_cmake_target}}\")\n"
  488. " foreach(_cmake_file IN LISTS "
  489. "\"_cmake_import_check_files_for_${_cmake_target}\")\n"
  490. " if(NOT EXISTS \"${_cmake_file}\")\n"
  491. " message(FATAL_ERROR \"The imported target "
  492. "\\\"${_cmake_target}\\\" references the file\n"
  493. " \\\"${_cmake_file}\\\"\n"
  494. "but this file does not exist. Possible reasons include:\n"
  495. "* The file was deleted, renamed, or moved to another location.\n"
  496. "* An install or uninstall procedure did not complete successfully.\n"
  497. "* The installation package was faulty and contained\n"
  498. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  499. "but not all the files it references.\n"
  500. "\")\n"
  501. " endif()\n"
  502. " endforeach()\n"
  503. " endif()\n"
  504. " unset(_cmake_file)\n"
  505. " unset(\"_cmake_import_check_files_for_${_cmake_target}\")\n"
  506. "endforeach()\n"
  507. "unset(_cmake_target)\n"
  508. "unset(_cmake_import_check_targets)\n"
  509. "\n";
  510. }
  511. void cmExportCMakeConfigGenerator::GenerateImportedFileChecksCode(
  512. std::ostream& os, cmGeneratorTarget const* target,
  513. ImportPropertyMap const& properties,
  514. std::set<std::string> const& importedLocations,
  515. std::string const& importedXcFrameworkLocation)
  516. {
  517. // Construct the imported target name.
  518. std::string targetName = cmStrCat(this->Namespace, target->GetExportName());
  519. os << "list(APPEND _cmake_import_check_targets " << targetName << " )\n";
  520. if (!importedXcFrameworkLocation.empty()) {
  521. os << "set(_cmake_import_check_xcframework_for_" << targetName << ' '
  522. << cmExportFileGeneratorEscape(importedXcFrameworkLocation) << ")\n";
  523. }
  524. os << "list(APPEND _cmake_import_check_files_for_" << targetName << ' ';
  525. for (std::string const& li : importedLocations) {
  526. auto pi = properties.find(li);
  527. if (pi != properties.end()) {
  528. os << cmExportFileGeneratorEscape(pi->second) << ' ';
  529. }
  530. }
  531. os << ")\n\n";
  532. }
  533. void cmExportCMakeConfigGenerator::GenerateTargetFileSets(
  534. cmGeneratorTarget* gte, std::ostream& os, cmTargetExport const* te)
  535. {
  536. auto interfaceFileSets = gte->Target->GetAllInterfaceFileSets();
  537. if (!interfaceFileSets.empty()) {
  538. std::string targetName = cmStrCat(this->Namespace, gte->GetExportName());
  539. os << "if(NOT CMAKE_VERSION VERSION_LESS \"3.23.0\")\n"
  540. " target_sources("
  541. << targetName << '\n';
  542. for (auto const& name : interfaceFileSets) {
  543. auto* fileSet = gte->Target->GetFileSet(name);
  544. if (!fileSet) {
  545. gte->Makefile->IssueMessage(
  546. MessageType::FATAL_ERROR,
  547. cmStrCat("File set \"", name,
  548. "\" is listed in interface file sets of ", gte->GetName(),
  549. " but has not been created"));
  550. return;
  551. }
  552. os << " INTERFACE"
  553. << "\n FILE_SET " << cmOutputConverter::EscapeForCMake(name)
  554. << "\n TYPE "
  555. << cmOutputConverter::EscapeForCMake(fileSet->GetType())
  556. << "\n BASE_DIRS "
  557. << this->GetFileSetDirectories(gte, fileSet, te) << "\n FILES "
  558. << this->GetFileSetFiles(gte, fileSet, te) << '\n';
  559. }
  560. os << " )\nelse()\n set_property(TARGET " << targetName
  561. << "\n APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES";
  562. for (auto const& name : interfaceFileSets) {
  563. auto* fileSet = gte->Target->GetFileSet(name);
  564. if (!fileSet) {
  565. gte->Makefile->IssueMessage(
  566. MessageType::FATAL_ERROR,
  567. cmStrCat("File set \"", name,
  568. "\" is listed in interface file sets of ", gte->GetName(),
  569. " but has not been created"));
  570. return;
  571. }
  572. if (fileSet->GetType() == "HEADERS"_s) {
  573. os << "\n " << this->GetFileSetDirectories(gte, fileSet, te);
  574. }
  575. }
  576. os << "\n )\nendif()\n\n";
  577. }
  578. }
  579. std::string cmExportCMakeConfigGenerator::GetCxxModuleFile(
  580. std::string const& name) const
  581. {
  582. auto const& cxxModuleDirname = this->GetCxxModulesDirectory();
  583. if (cxxModuleDirname.empty()) {
  584. return {};
  585. }
  586. return cmStrCat(cmSystemTools::GetFilenamePath(this->MainImportFile), '/',
  587. cxxModuleDirname, "/cxx-modules-", name, ".cmake");
  588. }
  589. void cmExportCMakeConfigGenerator::GenerateCxxModuleInformation(
  590. std::string const& name, std::ostream& os)
  591. {
  592. auto const cxx_module_dirname = this->GetCxxModulesDirectory();
  593. if (cxx_module_dirname.empty()) {
  594. return;
  595. }
  596. // Write the include.
  597. os << "# Include C++ module properties\n"
  598. "include(\"${CMAKE_CURRENT_LIST_DIR}/"
  599. << cxx_module_dirname << "/cxx-modules-" << name << ".cmake\")\n\n";
  600. // Include all configuration-specific include files.
  601. cmGeneratedFileStream ap(this->GetCxxModuleFile(name), true);
  602. ap.SetCopyIfDifferent(true);
  603. this->GenerateCxxModuleConfigInformation(name, ap);
  604. }
  605. void cmExportCMakeConfigGenerator::SetRequiredCMakeVersion(unsigned int major,
  606. unsigned int minor,
  607. unsigned int patch)
  608. {
  609. if (CMake_VERSION_ENCODE(major, minor, patch) >
  610. CMake_VERSION_ENCODE(this->RequiredCMakeVersionMajor,
  611. this->RequiredCMakeVersionMinor,
  612. this->RequiredCMakeVersionPatch)) {
  613. this->RequiredCMakeVersionMajor = major;
  614. this->RequiredCMakeVersionMinor = minor;
  615. this->RequiredCMakeVersionPatch = patch;
  616. }
  617. }