cmExportInstallFileGenerator.cxx 27 KB

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