cmExportInstallFileGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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/";
  385. if (!target->Makefile->PlatformIsAppleEmbedded()) {
  386. value += "Contents/MacOS/";
  387. }
  388. value += cmInstallTargetGenerator::GetInstallFilename(target, config);
  389. } else {
  390. value += cmInstallTargetGenerator::GetInstallFilename(
  391. target, config, cmInstallTargetGenerator::NameReal);
  392. }
  393. // Store the property.
  394. properties[prop] = value;
  395. importedLocations.insert(prop);
  396. }
  397. }
  398. cmStateEnums::TargetType cmExportInstallFileGenerator::GetExportTargetType(
  399. cmTargetExport const* targetExport) const
  400. {
  401. cmStateEnums::TargetType targetType = targetExport->Target->GetType();
  402. // An OBJECT library installed with no OBJECTS DESTINATION
  403. // is transformed to an INTERFACE library.
  404. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  405. targetExport->ObjectsGenerator == nullptr) {
  406. targetType = cmStateEnums::INTERFACE_LIBRARY;
  407. }
  408. return targetType;
  409. }
  410. void cmExportInstallFileGenerator::HandleMissingTarget(
  411. std::string& link_libs, cmGeneratorTarget const* depender,
  412. cmGeneratorTarget* dependee)
  413. {
  414. const std::string name = dependee->GetName();
  415. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  416. auto exportInfo = this->FindNamespaces(gg, name);
  417. std::vector<std::string> const& exportFiles = exportInfo.first;
  418. if (exportFiles.size() == 1) {
  419. std::string missingTarget = exportInfo.second;
  420. missingTarget += dependee->GetExportName();
  421. link_libs += missingTarget;
  422. this->MissingTargets.emplace_back(std::move(missingTarget));
  423. } else {
  424. // All exported targets should be known here and should be unique.
  425. // This is probably user-error.
  426. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  427. }
  428. }
  429. std::pair<std::vector<std::string>, std::string>
  430. cmExportInstallFileGenerator::FindNamespaces(cmGlobalGenerator* gg,
  431. const std::string& name)
  432. {
  433. std::vector<std::string> exportFiles;
  434. std::string ns;
  435. const cmExportSetMap& exportSets = gg->GetExportSets();
  436. for (auto const& expIt : exportSets) {
  437. const cmExportSet& exportSet = expIt.second;
  438. bool containsTarget = false;
  439. for (auto const& target : exportSet.GetTargetExports()) {
  440. if (name == target->TargetName) {
  441. containsTarget = true;
  442. break;
  443. }
  444. }
  445. if (containsTarget) {
  446. std::vector<cmInstallExportGenerator const*> const* installs =
  447. exportSet.GetInstallations();
  448. for (cmInstallExportGenerator const* install : *installs) {
  449. exportFiles.push_back(install->GetDestinationFile());
  450. ns = install->GetNamespace();
  451. }
  452. }
  453. }
  454. return { exportFiles, ns };
  455. }
  456. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  457. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  458. std::vector<std::string> const& exportFiles)
  459. {
  460. std::ostringstream e;
  461. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  462. << "\" ...) "
  463. << "includes target \"" << depender->GetName()
  464. << "\" which requires target \"" << dependee->GetName() << "\" ";
  465. if (exportFiles.empty()) {
  466. e << "that is not in any export set.";
  467. } else {
  468. e << "that is not in this export set, but in multiple other export sets: "
  469. << cmJoin(exportFiles, ", ") << ".\n";
  470. e << "An exported target cannot depend upon another target which is "
  471. "exported multiple times. Consider consolidating the exports of the "
  472. "\""
  473. << dependee->GetName() << "\" target to a single export.";
  474. }
  475. cmSystemTools::Error(e.str());
  476. }
  477. std::string cmExportInstallFileGenerator::InstallNameDir(
  478. cmGeneratorTarget const* target, const std::string& config)
  479. {
  480. std::string install_name_dir;
  481. cmMakefile* mf = target->Target->GetMakefile();
  482. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  483. install_name_dir =
  484. target->GetInstallNameDirForInstallTree(config, "${_IMPORT_PREFIX}");
  485. }
  486. return install_name_dir;
  487. }
  488. namespace {
  489. bool EntryIsContextSensitive(
  490. const std::unique_ptr<cmCompiledGeneratorExpression>& cge)
  491. {
  492. return cge->GetHadContextSensitiveCondition();
  493. }
  494. }
  495. std::string cmExportInstallFileGenerator::GetFileSetDirectories(
  496. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te)
  497. {
  498. std::vector<std::string> resultVector;
  499. auto configs =
  500. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  501. cmGeneratorExpression ge(*gte->Makefile->GetCMakeInstance());
  502. auto cge = ge.Parse(te->FileSetGenerators.at(fileSet)->GetDestination());
  503. for (auto const& config : configs) {
  504. auto dest = cmStrCat("${_IMPORT_PREFIX}/",
  505. cmOutputConverter::EscapeForCMake(
  506. cge->Evaluate(gte->LocalGenerator, config, gte),
  507. cmOutputConverter::WrapQuotes::NoWrap));
  508. auto const& type = fileSet->GetType();
  509. // C++ modules do not support interface file sets which are dependent upon
  510. // the configuration.
  511. if (cge->GetHadContextSensitiveCondition() &&
  512. (type == "CXX_MODULES"_s || type == "CXX_MODULE_HEADER_UNITS"_s)) {
  513. auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
  514. std::ostringstream e;
  515. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  516. << fileSet->GetName() << "\" of type \"" << type
  517. << "\" contains context-sensitive base file entries which is not "
  518. "supported.";
  519. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  520. return std::string{};
  521. }
  522. if (cge->GetHadContextSensitiveCondition() && configs.size() != 1) {
  523. resultVector.push_back(
  524. cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
  525. } else {
  526. resultVector.push_back(cmStrCat('"', dest, '"'));
  527. break;
  528. }
  529. }
  530. return cmJoin(resultVector, " ");
  531. }
  532. std::string cmExportInstallFileGenerator::GetFileSetFiles(
  533. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* te)
  534. {
  535. std::vector<std::string> resultVector;
  536. auto configs =
  537. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  538. auto fileEntries = fileSet->CompileFileEntries();
  539. auto directoryEntries = fileSet->CompileDirectoryEntries();
  540. cmGeneratorExpression destGe(*gte->Makefile->GetCMakeInstance());
  541. auto destCge =
  542. destGe.Parse(te->FileSetGenerators.at(fileSet)->GetDestination());
  543. for (auto const& config : configs) {
  544. auto directories = fileSet->EvaluateDirectoryEntries(
  545. directoryEntries, gte->LocalGenerator, config, gte);
  546. std::map<std::string, std::vector<std::string>> files;
  547. for (auto const& entry : fileEntries) {
  548. fileSet->EvaluateFileEntry(directories, files, entry,
  549. gte->LocalGenerator, config, gte);
  550. }
  551. auto dest = cmStrCat("${_IMPORT_PREFIX}/",
  552. cmOutputConverter::EscapeForCMake(
  553. destCge->Evaluate(gte->LocalGenerator, config, gte),
  554. cmOutputConverter::WrapQuotes::NoWrap),
  555. '/');
  556. bool const contextSensitive = destCge->GetHadContextSensitiveCondition() ||
  557. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  558. EntryIsContextSensitive) ||
  559. std::any_of(fileEntries.begin(), fileEntries.end(),
  560. EntryIsContextSensitive);
  561. auto const& type = fileSet->GetType();
  562. // C++ modules do not support interface file sets which are dependent upon
  563. // the configuration.
  564. if (contextSensitive &&
  565. (type == "CXX_MODULES"_s || type == "CXX_MODULE_HEADER_UNITS"_s)) {
  566. auto* mf = this->IEGen->GetLocalGenerator()->GetMakefile();
  567. std::ostringstream e;
  568. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  569. << fileSet->GetName() << "\" of type \"" << type
  570. << "\" contains context-sensitive base file entries which is not "
  571. "supported.";
  572. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  573. return std::string{};
  574. }
  575. for (auto const& it : files) {
  576. auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/');
  577. for (auto const& filename : it.second) {
  578. auto relFile =
  579. cmStrCat(prefix, cmSystemTools::GetFilenameName(filename));
  580. auto escapedFile =
  581. cmStrCat(dest,
  582. cmOutputConverter::EscapeForCMake(
  583. relFile, cmOutputConverter::WrapQuotes::NoWrap));
  584. if (contextSensitive && configs.size() != 1) {
  585. resultVector.push_back(
  586. cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
  587. } else {
  588. resultVector.push_back(cmStrCat('"', escapedFile, '"'));
  589. }
  590. }
  591. }
  592. if (!(contextSensitive && configs.size() != 1)) {
  593. break;
  594. }
  595. }
  596. return cmJoin(resultVector, " ");
  597. }
  598. std::string cmExportInstallFileGenerator::GetCxxModulesDirectory() const
  599. {
  600. return IEGen->GetCxxModuleDirectory();
  601. }
  602. void cmExportInstallFileGenerator::GenerateCxxModuleConfigInformation(
  603. std::ostream& os) const
  604. {
  605. // Now load per-configuration properties for them.
  606. /* clang-format off */
  607. os << "# Load information for each installed configuration.\n"
  608. "file(GLOB _cmake_cxx_module_includes \"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-*.cmake\")\n"
  609. "foreach(_cmake_cxx_module_include IN LISTS _cmake_cxx_module_includes)\n"
  610. " include(\"${_cmake_cxx_module_include}\")\n"
  611. "endforeach()\n"
  612. "unset(_cmake_cxx_module_include)\n"
  613. "unset(_cmake_cxx_module_includes)\n";
  614. /* clang-format on */
  615. }
  616. bool cmExportInstallFileGenerator::
  617. GenerateImportCxxModuleConfigTargetInclusion(std::string const& config)
  618. {
  619. auto cxx_modules_dirname = this->GetCxxModulesDirectory();
  620. if (cxx_modules_dirname.empty()) {
  621. return true;
  622. }
  623. std::string filename_config = config;
  624. if (filename_config.empty()) {
  625. filename_config = "noconfig";
  626. }
  627. std::string const dest =
  628. cmStrCat(this->FileDir, '/', cxx_modules_dirname, '/');
  629. std::string fileName =
  630. cmStrCat(dest, "cxx-modules-", filename_config, ".cmake");
  631. cmGeneratedFileStream os(fileName, true);
  632. if (!os) {
  633. std::string se = cmSystemTools::GetLastSystemError();
  634. std::ostringstream e;
  635. e << "cannot write to file \"" << fileName << "\": " << se;
  636. cmSystemTools::Error(e.str());
  637. return false;
  638. }
  639. os.SetCopyIfDifferent(true);
  640. // Record this per-config import file.
  641. this->ConfigCxxModuleFiles[config] = fileName;
  642. auto& prop_files = this->ConfigCxxModuleTargetFiles[config];
  643. for (auto const* tgt : this->ExportedTargets) {
  644. auto prop_filename = cmStrCat("target-", tgt->GetExportName(), '-',
  645. filename_config, ".cmake");
  646. prop_files.emplace_back(cmStrCat(dest, prop_filename));
  647. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/" << prop_filename << "\")\n";
  648. }
  649. return true;
  650. }