cmExportInstallFileGenerator.cxx 27 KB

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