cmExportInstallFileGenerator.cxx 27 KB

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