cmExportInstallFileGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 "cmExportInstallFileGenerator.h"
  4. #include <algorithm>
  5. #include <memory>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/string_view>
  9. #include <cmext/string_view>
  10. #include "cmExportSet.h"
  11. #include "cmFileSet.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGeneratorExpression.h"
  14. #include "cmGeneratorTarget.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmInstallExportGenerator.h"
  17. #include "cmInstallFileSetGenerator.h"
  18. #include "cmInstallTargetGenerator.h"
  19. #include "cmList.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmMessageType.h"
  23. #include "cmOutputConverter.h"
  24. #include "cmPolicies.h"
  25. #include "cmStateTypes.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmSystemTools.h"
  28. #include "cmTarget.h"
  29. #include "cmTargetExport.h"
  30. #include "cmValue.h"
  31. cmExportInstallFileGenerator::cmExportInstallFileGenerator(
  32. cmInstallExportGenerator* iegen)
  33. : IEGen(iegen)
  34. {
  35. }
  36. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  37. {
  38. std::string glob = cmStrCat(this->FileBase, "-*", this->FileExt);
  39. return glob;
  40. }
  41. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  42. {
  43. std::vector<cmTargetExport*> allTargets;
  44. {
  45. std::string expectedTargets;
  46. std::string sep;
  47. for (std::unique_ptr<cmTargetExport> const& te :
  48. this->GetExportSet()->GetTargetExports()) {
  49. if (te->NamelinkOnly) {
  50. continue;
  51. }
  52. expectedTargets += sep + this->Namespace + te->Target->GetExportName();
  53. sep = " ";
  54. if (this->ExportedTargets.insert(te->Target).second) {
  55. allTargets.push_back(te.get());
  56. } else {
  57. std::ostringstream e;
  58. e << "install(EXPORT \"" << this->GetExportSet()->GetName()
  59. << "\" ...) "
  60. << "includes target \"" << te->Target->GetName()
  61. << "\" more than once in the export set.";
  62. cmSystemTools::Error(e.str());
  63. return false;
  64. }
  65. }
  66. this->GenerateExpectedTargetsCode(os, expectedTargets);
  67. }
  68. // Compute the relative import prefix for the file
  69. this->GenerateImportPrefix(os);
  70. bool requiresConfigFiles = false;
  71. // Create all the imported targets.
  72. for (cmTargetExport* te : allTargets) {
  73. cmGeneratorTarget* gt = te->Target;
  74. cmStateEnums::TargetType targetType = this->GetExportTargetType(te);
  75. requiresConfigFiles =
  76. requiresConfigFiles || targetType != cmStateEnums::INTERFACE_LIBRARY;
  77. this->GenerateImportTargetCode(os, gt, targetType);
  78. ImportPropertyMap properties;
  79. std::string includesDestinationDirs;
  80. this->PopulateIncludeDirectoriesInterface(
  81. gt, cmGeneratorExpression::InstallInterface, properties, *te,
  82. includesDestinationDirs);
  83. this->PopulateSourcesInterface(gt, cmGeneratorExpression::InstallInterface,
  84. properties);
  85. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES", gt,
  86. cmGeneratorExpression::InstallInterface,
  87. properties);
  88. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gt,
  89. cmGeneratorExpression::InstallInterface,
  90. properties);
  91. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gt,
  92. cmGeneratorExpression::InstallInterface,
  93. properties);
  94. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", gt,
  95. cmGeneratorExpression::InstallInterface,
  96. properties);
  97. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gt,
  98. cmGeneratorExpression::InstallInterface,
  99. properties);
  100. this->PopulateInterfaceProperty("INTERFACE_AUTOMOC_MACRO_NAMES", gt,
  101. cmGeneratorExpression::InstallInterface,
  102. properties);
  103. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gt,
  104. cmGeneratorExpression::InstallInterface,
  105. properties);
  106. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gt,
  107. cmGeneratorExpression::InstallInterface,
  108. properties);
  109. this->PopulateLinkDirectoriesInterface(
  110. gt, cmGeneratorExpression::InstallInterface, properties);
  111. this->PopulateLinkDependsInterface(
  112. gt, cmGeneratorExpression::InstallInterface, properties);
  113. std::string errorMessage;
  114. if (!this->PopulateCxxModuleExportProperties(
  115. gt, properties, cmGeneratorExpression::InstallInterface,
  116. includesDestinationDirs, errorMessage)) {
  117. cmSystemTools::Error(errorMessage);
  118. return false;
  119. }
  120. if (!this->PopulateExportProperties(gt, properties, errorMessage)) {
  121. cmSystemTools::Error(errorMessage);
  122. return false;
  123. }
  124. const bool newCMP0022Behavior =
  125. gt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  126. gt->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  127. if (newCMP0022Behavior) {
  128. if (this->PopulateInterfaceLinkLibrariesProperty(
  129. gt, cmGeneratorExpression::InstallInterface, properties) &&
  130. !this->ExportOld) {
  131. this->SetRequiredCMakeVersion(2, 8, 12);
  132. }
  133. }
  134. if (targetType == cmStateEnums::INTERFACE_LIBRARY) {
  135. this->SetRequiredCMakeVersion(3, 0, 0);
  136. }
  137. if (gt->GetProperty("INTERFACE_SOURCES")) {
  138. // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
  139. // can consume them.
  140. this->SetRequiredCMakeVersion(3, 1, 0);
  141. }
  142. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gt,
  143. properties);
  144. this->PopulateCompatibleInterfaceProperties(gt, properties);
  145. this->PopulateCustomTransitiveInterfaceProperties(
  146. gt, cmGeneratorExpression::InstallInterface, properties);
  147. this->GenerateInterfaceProperties(gt, os, properties);
  148. this->GenerateTargetFileSets(gt, os, te);
  149. }
  150. this->LoadConfigFiles(os);
  151. bool result = true;
  152. std::string cxx_modules_name = this->GetExportSet()->GetName();
  153. this->GenerateCxxModuleInformation(cxx_modules_name, os);
  154. if (requiresConfigFiles) {
  155. for (std::string const& c : this->Configurations) {
  156. if (!this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name,
  157. c)) {
  158. result = false;
  159. }
  160. }
  161. }
  162. this->CleanupTemporaryVariables(os);
  163. this->GenerateImportedFileCheckLoop(os);
  164. // Generate an import file for each configuration.
  165. // Don't do this if we only export INTERFACE_LIBRARY targets.
  166. if (requiresConfigFiles) {
  167. for (std::string const& c : this->Configurations) {
  168. if (!this->GenerateImportFileConfig(c)) {
  169. result = false;
  170. }
  171. }
  172. }
  173. this->GenerateMissingTargetsCheckCode(os);
  174. return result;
  175. }
  176. void cmExportInstallFileGenerator::GenerateImportPrefix(std::ostream& os)
  177. {
  178. // Set an _IMPORT_PREFIX variable for import location properties
  179. // to reference if they are relative to the install prefix.
  180. std::string installPrefix =
  181. this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition(
  182. "CMAKE_INSTALL_PREFIX");
  183. std::string const& expDest = this->IEGen->GetDestination();
  184. if (cmSystemTools::FileIsFullPath(expDest)) {
  185. // The export file is being installed to an absolute path so the
  186. // package is not relocatable. Use the configured install prefix.
  187. /* clang-format off */
  188. os <<
  189. "# The installation prefix configured by this project.\n"
  190. "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n"
  191. "\n";
  192. /* clang-format on */
  193. } else {
  194. // Add code to compute the installation prefix relative to the
  195. // import file location.
  196. std::string absDest = installPrefix + "/" + expDest;
  197. std::string absDestS = absDest + "/";
  198. os << "# Compute the installation prefix relative to this file.\n"
  199. << "get_filename_component(_IMPORT_PREFIX"
  200. << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  201. if (cmHasLiteralPrefix(absDestS, "/lib/") ||
  202. cmHasLiteralPrefix(absDestS, "/lib64/") ||
  203. cmHasLiteralPrefix(absDestS, "/libx32/") ||
  204. cmHasLiteralPrefix(absDestS, "/usr/lib/") ||
  205. cmHasLiteralPrefix(absDestS, "/usr/lib64/") ||
  206. cmHasLiteralPrefix(absDestS, "/usr/libx32/")) {
  207. // Handle "/usr move" symlinks created by some Linux distros.
  208. /* clang-format off */
  209. os <<
  210. "# Use original install prefix when loaded through a\n"
  211. "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
  212. "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
  213. "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
  214. "if(_realCurr STREQUAL _realOrig)\n"
  215. " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
  216. "endif()\n"
  217. "unset(_realOrig)\n"
  218. "unset(_realCurr)\n";
  219. /* clang-format on */
  220. }
  221. std::string dest = expDest;
  222. while (!dest.empty()) {
  223. os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" "
  224. "PATH)\n";
  225. dest = cmSystemTools::GetFilenamePath(dest);
  226. }
  227. os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
  228. << " set(_IMPORT_PREFIX \"\")\n"
  229. << "endif()\n"
  230. << "\n";
  231. }
  232. }
  233. void cmExportInstallFileGenerator::CleanupTemporaryVariables(std::ostream& os)
  234. {
  235. /* clang-format off */
  236. os << "# Cleanup temporary variables.\n"
  237. << "set(_IMPORT_PREFIX)\n"
  238. << "\n";
  239. /* clang-format on */
  240. }
  241. void cmExportInstallFileGenerator::LoadConfigFiles(std::ostream& os)
  242. {
  243. // Now load per-configuration properties for them.
  244. /* clang-format off */
  245. os << "# Load information for each installed configuration.\n"
  246. << "file(GLOB _cmake_config_files \"${CMAKE_CURRENT_LIST_DIR}/"
  247. << this->GetConfigImportFileGlob() << "\")\n"
  248. << "foreach(_cmake_config_file IN LISTS _cmake_config_files)\n"
  249. << " include(\"${_cmake_config_file}\")\n"
  250. << "endforeach()\n"
  251. << "unset(_cmake_config_file)\n"
  252. << "unset(_cmake_config_files)\n"
  253. << "\n";
  254. /* clang-format on */
  255. }
  256. void cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string& input)
  257. {
  258. cmGeneratorExpression::ReplaceInstallPrefix(input, "${_IMPORT_PREFIX}");
  259. }
  260. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  261. const std::string& config)
  262. {
  263. // Skip configurations not enabled for this export.
  264. if (!this->IEGen->InstallsForConfig(config)) {
  265. return true;
  266. }
  267. // Construct the name of the file to generate.
  268. std::string fileName = cmStrCat(this->FileDir, '/', this->FileBase, '-');
  269. if (!config.empty()) {
  270. fileName += cmSystemTools::LowerCase(config);
  271. } else {
  272. fileName += "noconfig";
  273. }
  274. fileName += this->FileExt;
  275. // Open the output file to generate it.
  276. cmGeneratedFileStream exportFileStream(fileName, true);
  277. if (!exportFileStream) {
  278. std::string se = cmSystemTools::GetLastSystemError();
  279. std::ostringstream e;
  280. e << "cannot write to file \"" << fileName << "\": " << se;
  281. cmSystemTools::Error(e.str());
  282. return false;
  283. }
  284. exportFileStream.SetCopyIfDifferent(true);
  285. std::ostream& os = exportFileStream;
  286. // Start with the import file header.
  287. this->GenerateImportHeaderCode(os, config);
  288. // Generate the per-config target information.
  289. this->GenerateImportConfig(os, config);
  290. // End with the import file footer.
  291. this->GenerateImportFooterCode(os);
  292. // Record this per-config import file.
  293. this->ConfigImportFiles[config] = fileName;
  294. return true;
  295. }
  296. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  297. std::ostream& os, const std::string& config, std::string const& suffix)
  298. {
  299. // Add each target in the set to the export.
  300. for (std::unique_ptr<cmTargetExport> const& te :
  301. this->GetExportSet()->GetTargetExports()) {
  302. // Collect import properties for this target.
  303. if (this->GetExportTargetType(te.get()) ==
  304. cmStateEnums::INTERFACE_LIBRARY) {
  305. continue;
  306. }
  307. ImportPropertyMap properties;
  308. std::set<std::string> importedLocations;
  309. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  310. properties, importedLocations);
  311. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  312. properties, importedLocations);
  313. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  314. properties, importedLocations);
  315. this->SetImportLocationProperty(config, suffix, te->ObjectsGenerator,
  316. properties, importedLocations);
  317. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  318. properties, importedLocations);
  319. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  320. properties, importedLocations);
  321. // If any file location was set for the target add it to the
  322. // import file.
  323. if (!properties.empty()) {
  324. // Get the rest of the target details.
  325. cmGeneratorTarget* gtgt = te->Target;
  326. this->SetImportDetailProperties(config, suffix, gtgt, properties);
  327. this->SetImportLinkInterface(config, suffix,
  328. cmGeneratorExpression::InstallInterface,
  329. gtgt, properties);
  330. // TODO: PUBLIC_HEADER_LOCATION
  331. // This should wait until the build feature propagation stuff
  332. // is done. Then this can be a propagated include directory.
  333. // this->GenerateImportProperty(config, te->HeaderGenerator,
  334. // properties);
  335. // Generate code in the export file.
  336. std::string importedXcFrameworkLocation = te->XcFrameworkLocation;
  337. if (!importedXcFrameworkLocation.empty()) {
  338. importedXcFrameworkLocation = cmGeneratorExpression::Preprocess(
  339. importedXcFrameworkLocation,
  340. cmGeneratorExpression::PreprocessContext::InstallInterface, true);
  341. importedXcFrameworkLocation = cmGeneratorExpression::Evaluate(
  342. importedXcFrameworkLocation, te->Target->GetLocalGenerator(), config,
  343. te->Target, nullptr, te->Target);
  344. if (!importedXcFrameworkLocation.empty() &&
  345. !cmSystemTools::FileIsFullPath(importedXcFrameworkLocation) &&
  346. !cmHasLiteralPrefix(importedXcFrameworkLocation,
  347. "${_IMPORT_PREFIX}/")) {
  348. importedXcFrameworkLocation =
  349. cmStrCat("${_IMPORT_PREFIX}/", importedXcFrameworkLocation);
  350. }
  351. }
  352. this->GenerateImportPropertyCode(os, config, suffix, gtgt, properties,
  353. importedXcFrameworkLocation);
  354. this->GenerateImportedFileChecksCode(
  355. os, gtgt, properties, importedLocations, importedXcFrameworkLocation);
  356. }
  357. }
  358. }
  359. void cmExportInstallFileGenerator::SetImportLocationProperty(
  360. const std::string& config, std::string const& suffix,
  361. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  362. std::set<std::string>& importedLocations)
  363. {
  364. // Skip rules that do not match this configuration.
  365. if (!(itgen && itgen->InstallsForConfig(config))) {
  366. return;
  367. }
  368. // Get the target to be installed.
  369. cmGeneratorTarget* target = itgen->GetTarget();
  370. // Construct the installed location of the target.
  371. std::string dest = itgen->GetDestination(config);
  372. std::string value;
  373. if (!cmSystemTools::FileIsFullPath(dest)) {
  374. // The target is installed relative to the installation prefix.
  375. value = "${_IMPORT_PREFIX}/";
  376. }
  377. value += dest;
  378. value += "/";
  379. if (itgen->IsImportLibrary()) {
  380. // Construct the property name.
  381. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  382. // Append the installed file name.
  383. value += cmInstallTargetGenerator::GetInstallFilename(
  384. target, config, cmInstallTargetGenerator::NameImplibReal);
  385. // Store the property.
  386. properties[prop] = value;
  387. importedLocations.insert(prop);
  388. } else if (itgen->GetTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  389. // Construct the property name.
  390. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  391. // Compute all the object files inside this target and setup
  392. // IMPORTED_OBJECTS as a list of object files
  393. std::vector<std::string> objects;
  394. itgen->GetInstallObjectNames(config, objects);
  395. for (std::string& obj : objects) {
  396. obj = cmStrCat(value, obj);
  397. }
  398. // Store the property.
  399. properties[prop] = cmList::to_string(objects);
  400. importedLocations.insert(prop);
  401. } else {
  402. if (target->IsFrameworkOnApple() && target->HasImportLibrary(config)) {
  403. // store as well IMPLIB value
  404. auto importProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  405. auto importValue =
  406. cmStrCat(value,
  407. cmInstallTargetGenerator::GetInstallFilename(
  408. target, config, cmInstallTargetGenerator::NameImplibReal));
  409. // Store the property.
  410. properties[importProp] = importValue;
  411. importedLocations.insert(importProp);
  412. }
  413. // Construct the property name.
  414. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  415. // Append the installed file name.
  416. if (target->IsAppBundleOnApple()) {
  417. value += cmInstallTargetGenerator::GetInstallFilename(target, config);
  418. value += ".app/";
  419. if (!target->Makefile->PlatformIsAppleEmbedded()) {
  420. value += "Contents/MacOS/";
  421. }
  422. value += cmInstallTargetGenerator::GetInstallFilename(target, config);
  423. } else {
  424. value += cmInstallTargetGenerator::GetInstallFilename(
  425. target, config, cmInstallTargetGenerator::NameReal);
  426. }
  427. // Store the property.
  428. properties[prop] = value;
  429. importedLocations.insert(prop);
  430. }
  431. }
  432. cmStateEnums::TargetType cmExportInstallFileGenerator::GetExportTargetType(
  433. cmTargetExport const* targetExport) const
  434. {
  435. cmStateEnums::TargetType targetType = targetExport->Target->GetType();
  436. // An OBJECT library installed with no OBJECTS DESTINATION
  437. // is transformed to an INTERFACE library.
  438. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  439. targetExport->ObjectsGenerator == nullptr) {
  440. targetType = cmStateEnums::INTERFACE_LIBRARY;
  441. }
  442. return targetType;
  443. }
  444. void cmExportInstallFileGenerator::HandleMissingTarget(
  445. std::string& link_libs, cmGeneratorTarget const* depender,
  446. cmGeneratorTarget* dependee)
  447. {
  448. const std::string name = dependee->GetName();
  449. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  450. auto exportInfo = this->FindNamespaces(gg, name);
  451. std::vector<std::string> const& exportFiles = exportInfo.first;
  452. if (exportFiles.size() == 1) {
  453. std::string missingTarget = exportInfo.second;
  454. missingTarget += dependee->GetExportName();
  455. link_libs += missingTarget;
  456. this->MissingTargets.emplace_back(std::move(missingTarget));
  457. } else {
  458. // All exported targets should be known here and should be unique.
  459. // This is probably user-error.
  460. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  461. }
  462. }
  463. std::pair<std::vector<std::string>, std::string>
  464. cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg,
  465. const std::string& name)
  466. {
  467. std::vector<std::string> exportFiles;
  468. std::string ns;
  469. const cmExportSetMap& exportSets = gg->GetExportSets();
  470. for (auto const& expIt : exportSets) {
  471. const cmExportSet& exportSet = expIt.second;
  472. bool containsTarget = false;
  473. for (auto const& target : exportSet.GetTargetExports()) {
  474. if (name == target->TargetName) {
  475. containsTarget = true;
  476. break;
  477. }
  478. }
  479. if (containsTarget) {
  480. std::vector<cmInstallExportGenerator const*> const* installs =
  481. exportSet.GetInstallations();
  482. for (cmInstallExportGenerator const* install : *installs) {
  483. exportFiles.push_back(install->GetDestinationFile());
  484. ns = install->GetNamespace();
  485. }
  486. }
  487. }
  488. return { exportFiles, ns };
  489. }
  490. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  491. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  492. std::vector<std::string> const& exportFiles)
  493. {
  494. std::ostringstream e;
  495. e << "install(EXPORT \"" << this->GetExportSet()->GetName() << "\" ...) "
  496. << "includes target \"" << depender->GetName()
  497. << "\" which requires target \"" << dependee->GetName() << "\" ";
  498. if (exportFiles.empty()) {
  499. e << "that is not in any export set.";
  500. } else {
  501. e << "that is not in this export set, but in multiple other export sets: "
  502. << cmJoin(exportFiles, ", ") << ".\n";
  503. e << "An exported target cannot depend upon another target which is "
  504. "exported multiple times. Consider consolidating the exports of the "
  505. "\""
  506. << dependee->GetName() << "\" target to a single export.";
  507. }
  508. cmSystemTools::Error(e.str());
  509. }
  510. std::string cmExportInstallFileGenerator::InstallNameDir(
  511. cmGeneratorTarget const* target, const std::string& config)
  512. {
  513. std::string install_name_dir;
  514. cmMakefile* mf = target->Target->GetMakefile();
  515. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  516. install_name_dir =
  517. target->GetInstallNameDirForInstallTree(config, "${_IMPORT_PREFIX}");
  518. }
  519. return install_name_dir;
  520. }
  521. namespace {
  522. bool EntryIsContextSensitive(
  523. const std::unique_ptr<cmCompiledGeneratorExpression>& cge)
  524. {
  525. return cge->GetHadContextSensitiveCondition();
  526. }
  527. }
  528. std::string cmExportInstallFileGenerator::GetFileSetDirectories(
  529. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te)
  530. {
  531. std::vector<std::string> resultVector;
  532. auto configs =
  533. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  534. cmGeneratorExpression ge(*gte->Makefile->GetCMakeInstance());
  535. auto cge = ge.Parse(te->FileSetGenerators.at(fileSet)->GetDestination());
  536. for (auto const& config : configs) {
  537. auto unescapedDest = cge->Evaluate(gte->LocalGenerator, config, gte);
  538. auto dest = cmOutputConverter::EscapeForCMake(
  539. unescapedDest, cmOutputConverter::WrapQuotes::NoWrap);
  540. if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
  541. dest = cmStrCat("${_IMPORT_PREFIX}/", dest);
  542. }
  543. auto const& type = fileSet->GetType();
  544. // C++ modules do not support interface file sets which are dependent upon
  545. // the configuration.
  546. if (cge->GetHadContextSensitiveCondition() && type == "CXX_MODULES"_s) {
  547. auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
  548. std::ostringstream e;
  549. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  550. << fileSet->GetName() << "\" of type \"" << type
  551. << "\" contains context-sensitive base file entries which is not "
  552. "supported.";
  553. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  554. return std::string{};
  555. }
  556. if (cge->GetHadContextSensitiveCondition() && configs.size() != 1) {
  557. resultVector.push_back(
  558. cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
  559. } else {
  560. resultVector.emplace_back(cmStrCat('"', dest, '"'));
  561. break;
  562. }
  563. }
  564. return cmJoin(resultVector, " ");
  565. }
  566. std::string cmExportInstallFileGenerator::GetFileSetFiles(
  567. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te)
  568. {
  569. std::vector<std::string> resultVector;
  570. auto configs =
  571. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  572. auto fileEntries = fileSet->CompileFileEntries();
  573. auto directoryEntries = fileSet->CompileDirectoryEntries();
  574. cmGeneratorExpression destGe(*gte->Makefile->GetCMakeInstance());
  575. auto destCge =
  576. destGe.Parse(te->FileSetGenerators.at(fileSet)->GetDestination());
  577. for (auto const& config : configs) {
  578. auto directories = fileSet->EvaluateDirectoryEntries(
  579. directoryEntries, gte->LocalGenerator, config, gte);
  580. std::map<std::string, std::vector<std::string>> files;
  581. for (auto const& entry : fileEntries) {
  582. fileSet->EvaluateFileEntry(directories, files, entry,
  583. gte->LocalGenerator, config, gte);
  584. }
  585. auto unescapedDest = destCge->Evaluate(gte->LocalGenerator, config, gte);
  586. auto dest =
  587. cmStrCat(cmOutputConverter::EscapeForCMake(
  588. unescapedDest, cmOutputConverter::WrapQuotes::NoWrap),
  589. '/');
  590. if (!cmSystemTools::FileIsFullPath(unescapedDest)) {
  591. dest = cmStrCat("${_IMPORT_PREFIX}/", dest);
  592. }
  593. bool const contextSensitive = destCge->GetHadContextSensitiveCondition() ||
  594. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  595. EntryIsContextSensitive) ||
  596. std::any_of(fileEntries.begin(), fileEntries.end(),
  597. EntryIsContextSensitive);
  598. auto const& type = fileSet->GetType();
  599. // C++ modules do not support interface file sets which are dependent upon
  600. // the configuration.
  601. if (contextSensitive && type == "CXX_MODULES"_s) {
  602. auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
  603. std::ostringstream e;
  604. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  605. << fileSet->GetName() << "\" of type \"" << type
  606. << "\" contains context-sensitive base file entries which is not "
  607. "supported.";
  608. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  609. return std::string{};
  610. }
  611. for (auto const& it : files) {
  612. auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/');
  613. for (auto const& filename : it.second) {
  614. auto relFile =
  615. cmStrCat(prefix, cmSystemTools::GetFilenameName(filename));
  616. auto escapedFile =
  617. cmStrCat(dest,
  618. cmOutputConverter::EscapeForCMake(
  619. relFile, cmOutputConverter::WrapQuotes::NoWrap));
  620. if (contextSensitive && configs.size() != 1) {
  621. resultVector.push_back(
  622. cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
  623. } else {
  624. resultVector.emplace_back(cmStrCat('"', escapedFile, '"'));
  625. }
  626. }
  627. }
  628. if (!(contextSensitive && configs.size() != 1)) {
  629. break;
  630. }
  631. }
  632. return cmJoin(resultVector, " ");
  633. }
  634. std::string cmExportInstallFileGenerator::GetCxxModulesDirectory() const
  635. {
  636. return IEGen->GetCxxModuleDirectory();
  637. }
  638. void cmExportInstallFileGenerator::GenerateCxxModuleConfigInformation(
  639. std::string const& name, std::ostream& os) const
  640. {
  641. // Now load per-configuration properties for them.
  642. /* clang-format off */
  643. os << "# Load information for each installed configuration.\n"
  644. "file(GLOB _cmake_cxx_module_includes \"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << "-*.cmake\")\n"
  645. "foreach(_cmake_cxx_module_include IN LISTS _cmake_cxx_module_includes)\n"
  646. " include(\"${_cmake_cxx_module_include}\")\n"
  647. "endforeach()\n"
  648. "unset(_cmake_cxx_module_include)\n"
  649. "unset(_cmake_cxx_module_includes)\n";
  650. /* clang-format on */
  651. }
  652. std::string cmExportInstallFileGenerator::GetCxxModuleFile() const
  653. {
  654. return this->GetCxxModuleFile(this->GetExportSet()->GetName());
  655. }
  656. bool cmExportInstallFileGenerator::
  657. GenerateImportCxxModuleConfigTargetInclusion(std::string const& name,
  658. std::string const& config)
  659. {
  660. auto cxx_modules_dirname = this->GetCxxModulesDirectory();
  661. if (cxx_modules_dirname.empty()) {
  662. return true;
  663. }
  664. std::string filename_config = config;
  665. if (filename_config.empty()) {
  666. filename_config = "noconfig";
  667. }
  668. std::string const dest =
  669. cmStrCat(this->FileDir, '/', cxx_modules_dirname, '/');
  670. std::string fileName =
  671. cmStrCat(dest, "cxx-modules-", name, '-', filename_config, ".cmake");
  672. cmGeneratedFileStream os(fileName, true);
  673. if (!os) {
  674. std::string se = cmSystemTools::GetLastSystemError();
  675. std::ostringstream e;
  676. e << "cannot write to file \"" << fileName << "\": " << se;
  677. cmSystemTools::Error(e.str());
  678. return false;
  679. }
  680. os.SetCopyIfDifferent(true);
  681. // Record this per-config import file.
  682. this->ConfigCxxModuleFiles[config] = fileName;
  683. auto& prop_files = this->ConfigCxxModuleTargetFiles[config];
  684. for (auto const* tgt : this->ExportedTargets) {
  685. // Only targets with C++ module sources will have a
  686. // collator-generated install script.
  687. if (!tgt->HaveCxx20ModuleSources()) {
  688. continue;
  689. }
  690. auto prop_filename = cmStrCat("target-", tgt->GetFilesystemExportName(),
  691. '-', filename_config, ".cmake");
  692. prop_files.emplace_back(cmStrCat(dest, prop_filename));
  693. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/" << prop_filename << "\")\n";
  694. }
  695. return true;
  696. }