cmExportInstallFileGenerator.cxx 27 KB

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