cmExportInstallFileGenerator.cxx 27 KB

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