cmExportInstallFileGenerator.cxx 27 KB

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