cmExportInstallFileGenerator.cxx 26 KB

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