cmExportInstallFileGenerator.cxx 24 KB

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