cmExportBuildFileGenerator.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 "cmExportBuildFileGenerator.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <sstream>
  10. #include <utility>
  11. #include <cm/string_view>
  12. #include <cmext/string_view>
  13. #include "cmCryptoHash.h"
  14. #include "cmExportSet.h"
  15. #include "cmFileSet.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmList.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmOutputConverter.h"
  25. #include "cmPolicies.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. #include "cmTarget.h"
  30. #include "cmTargetExport.h"
  31. #include "cmValue.h"
  32. #include "cmake.h"
  33. class cmSourceFile;
  34. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  35. {
  36. this->LG = nullptr;
  37. this->ExportSet = nullptr;
  38. }
  39. void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg)
  40. {
  41. this->LG = lg;
  42. if (this->ExportSet) {
  43. this->ExportSet->Compute(lg);
  44. }
  45. }
  46. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  47. {
  48. {
  49. std::string expectedTargets;
  50. std::string sep;
  51. std::vector<TargetExport> targets;
  52. bool generatedInterfaceRequired = false;
  53. this->GetTargets(targets);
  54. for (auto const& tei : targets) {
  55. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(tei.Name);
  56. expectedTargets += sep + this->Namespace + te->GetExportName();
  57. sep = " ";
  58. if (this->ExportedTargets.insert(te).second) {
  59. this->Exports.emplace_back(te, tei.XcFrameworkLocation);
  60. } else {
  61. std::ostringstream e;
  62. e << "given target \"" << te->GetName() << "\" more than once.";
  63. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  64. MessageType::FATAL_ERROR, e.str(),
  65. this->LG->GetMakefile()->GetBacktrace());
  66. return false;
  67. }
  68. generatedInterfaceRequired |=
  69. this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY;
  70. }
  71. if (generatedInterfaceRequired) {
  72. this->SetRequiredCMakeVersion(3, 0, 0);
  73. }
  74. this->GenerateExpectedTargetsCode(os, expectedTargets);
  75. }
  76. // Create all the imported targets.
  77. for (auto const& exp : this->Exports) {
  78. cmGeneratorTarget* gte = exp.Target;
  79. this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
  80. gte->Target->AppendBuildInterfaceIncludes();
  81. ImportPropertyMap properties;
  82. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", gte,
  83. cmGeneratorExpression::BuildInterface,
  84. properties);
  85. this->PopulateInterfaceProperty("INTERFACE_SOURCES", gte,
  86. cmGeneratorExpression::BuildInterface,
  87. properties);
  88. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gte,
  89. cmGeneratorExpression::BuildInterface,
  90. properties);
  91. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gte,
  92. cmGeneratorExpression::BuildInterface,
  93. properties);
  94. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", gte,
  95. cmGeneratorExpression::BuildInterface,
  96. properties);
  97. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  98. cmGeneratorExpression::BuildInterface,
  99. properties);
  100. this->PopulateInterfaceProperty("INTERFACE_AUTOMOC_MACRO_NAMES", gte,
  101. cmGeneratorExpression::BuildInterface,
  102. properties);
  103. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  104. cmGeneratorExpression::BuildInterface,
  105. properties);
  106. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  107. cmGeneratorExpression::BuildInterface,
  108. properties);
  109. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  110. cmGeneratorExpression::BuildInterface,
  111. properties);
  112. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  113. cmGeneratorExpression::BuildInterface,
  114. properties);
  115. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  116. properties);
  117. std::string errorMessage;
  118. if (!this->PopulateCxxModuleExportProperties(
  119. gte, properties, cmGeneratorExpression::BuildInterface, {},
  120. errorMessage)) {
  121. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  122. MessageType::FATAL_ERROR, errorMessage,
  123. this->LG->GetMakefile()->GetBacktrace());
  124. return false;
  125. }
  126. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  127. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  128. MessageType::FATAL_ERROR, errorMessage,
  129. this->LG->GetMakefile()->GetBacktrace());
  130. return false;
  131. }
  132. const bool newCMP0022Behavior =
  133. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  134. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  135. if (newCMP0022Behavior) {
  136. this->PopulateInterfaceLinkLibrariesProperty(
  137. gte, cmGeneratorExpression::BuildInterface, properties);
  138. }
  139. this->PopulateCompatibleInterfaceProperties(gte, properties);
  140. this->PopulateCustomTransitiveInterfaceProperties(
  141. gte, cmGeneratorExpression::BuildInterface, properties);
  142. this->GenerateInterfaceProperties(gte, os, properties);
  143. this->GenerateTargetFileSets(gte, os);
  144. }
  145. std::string cxx_modules_name;
  146. if (this->ExportSet) {
  147. cxx_modules_name = this->ExportSet->GetName();
  148. } else {
  149. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
  150. constexpr std::size_t HASH_TRUNCATION = 12;
  151. for (auto const& target : this->Targets) {
  152. hasher.Append(target.Name);
  153. }
  154. cxx_modules_name = hasher.FinalizeHex().substr(0, HASH_TRUNCATION);
  155. }
  156. this->GenerateCxxModuleInformation(cxx_modules_name, os);
  157. // Generate import file content for each configuration.
  158. for (std::string const& c : this->Configurations) {
  159. this->GenerateImportConfig(os, c);
  160. }
  161. // Generate import file content for each configuration.
  162. for (std::string const& c : this->Configurations) {
  163. this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name, c);
  164. }
  165. this->GenerateMissingTargetsCheckCode(os);
  166. return true;
  167. }
  168. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  169. std::ostream& os, const std::string& config, std::string const& suffix)
  170. {
  171. for (auto const& exp : this->Exports) {
  172. cmGeneratorTarget* target = exp.Target;
  173. // Collect import properties for this target.
  174. ImportPropertyMap properties;
  175. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  176. this->SetImportLocationProperty(config, suffix, target, properties);
  177. }
  178. if (!properties.empty()) {
  179. // Get the rest of the target details.
  180. if (this->GetExportTargetType(target) !=
  181. cmStateEnums::INTERFACE_LIBRARY) {
  182. this->SetImportDetailProperties(config, suffix, target, properties);
  183. this->SetImportLinkInterface(config, suffix,
  184. cmGeneratorExpression::BuildInterface,
  185. target, properties);
  186. }
  187. // TODO: PUBLIC_HEADER_LOCATION
  188. // This should wait until the build feature propagation stuff
  189. // is done. Then this can be a propagated include directory.
  190. // this->GenerateImportProperty(config, te->HeaderGenerator,
  191. // properties);
  192. // Generate code in the export file.
  193. std::string importedXcFrameworkLocation = exp.XcFrameworkLocation;
  194. if (!importedXcFrameworkLocation.empty()) {
  195. importedXcFrameworkLocation = cmGeneratorExpression::Preprocess(
  196. importedXcFrameworkLocation,
  197. cmGeneratorExpression::PreprocessContext::BuildInterface);
  198. importedXcFrameworkLocation = cmGeneratorExpression::Evaluate(
  199. importedXcFrameworkLocation, exp.Target->GetLocalGenerator(), config,
  200. exp.Target, nullptr, exp.Target);
  201. if (!importedXcFrameworkLocation.empty() &&
  202. !cmSystemTools::FileIsFullPath(importedXcFrameworkLocation)) {
  203. importedXcFrameworkLocation =
  204. cmStrCat(this->LG->GetCurrentBinaryDirectory(), '/',
  205. importedXcFrameworkLocation);
  206. }
  207. }
  208. this->GenerateImportPropertyCode(os, config, suffix, target, properties,
  209. importedXcFrameworkLocation);
  210. }
  211. }
  212. }
  213. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  214. cmGeneratorTarget const* target) const
  215. {
  216. cmStateEnums::TargetType targetType = target->GetType();
  217. // An object library exports as an interface library if we cannot
  218. // tell clients where to find the objects. This is sufficient
  219. // to support transitive usage requirements on other targets that
  220. // use the object library.
  221. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  222. !target->Target->HasKnownObjectFileLocation(nullptr)) {
  223. targetType = cmStateEnums::INTERFACE_LIBRARY;
  224. }
  225. return targetType;
  226. }
  227. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  228. {
  229. this->ExportSet = exportSet;
  230. }
  231. void cmExportBuildFileGenerator::SetImportLocationProperty(
  232. const std::string& config, std::string const& suffix,
  233. cmGeneratorTarget* target, ImportPropertyMap& properties)
  234. {
  235. // Get the makefile in which to lookup target information.
  236. cmMakefile* mf = target->Makefile;
  237. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  238. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  239. // Compute all the object files inside this target and setup
  240. // IMPORTED_OBJECTS as a list of object files
  241. std::vector<cmSourceFile const*> objectSources;
  242. target->GetObjectSources(objectSources, config);
  243. std::string const obj_dir = target->GetObjectDirectory(config);
  244. std::vector<std::string> objects;
  245. for (cmSourceFile const* sf : objectSources) {
  246. const std::string& obj = target->GetObjectName(sf);
  247. objects.push_back(obj_dir + obj);
  248. }
  249. // Store the property.
  250. properties[prop] = cmList::to_string(objects);
  251. } else {
  252. // Add the main target file.
  253. {
  254. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  255. std::string value;
  256. if (target->IsAppBundleOnApple()) {
  257. value =
  258. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  259. } else {
  260. value = target->GetFullPath(config,
  261. cmStateEnums::RuntimeBinaryArtifact, true);
  262. }
  263. properties[prop] = value;
  264. }
  265. // Add the import library for windows DLLs.
  266. if (target->HasImportLibrary(config)) {
  267. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  268. std::string value =
  269. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact, true);
  270. if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  271. target->GetImplibGNUtoMS(config, value, value,
  272. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  273. }
  274. properties[prop] = value;
  275. }
  276. }
  277. }
  278. void cmExportBuildFileGenerator::HandleMissingTarget(
  279. std::string& link_libs, cmGeneratorTarget const* depender,
  280. cmGeneratorTarget* dependee)
  281. {
  282. // The target is not in the export.
  283. if (!this->AppendMode) {
  284. const std::string name = dependee->GetName();
  285. cmGlobalGenerator* gg =
  286. dependee->GetLocalGenerator()->GetGlobalGenerator();
  287. auto exportInfo = this->FindBuildExportInfo(gg, name);
  288. std::vector<std::string> const& exportFiles = exportInfo.first;
  289. if (exportFiles.size() == 1) {
  290. std::string missingTarget = exportInfo.second;
  291. missingTarget += dependee->GetExportName();
  292. link_libs += missingTarget;
  293. this->MissingTargets.emplace_back(std::move(missingTarget));
  294. return;
  295. }
  296. // We are not appending, so all exported targets should be
  297. // known here. This is probably user-error.
  298. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  299. }
  300. // Assume the target will be exported by another command.
  301. // Append it with the export namespace.
  302. link_libs += this->Namespace;
  303. link_libs += dependee->GetExportName();
  304. }
  305. void cmExportBuildFileGenerator::GetTargets(
  306. std::vector<TargetExport>& targets) const
  307. {
  308. if (this->ExportSet) {
  309. for (std::unique_ptr<cmTargetExport> const& te :
  310. this->ExportSet->GetTargetExports()) {
  311. if (te->NamelinkOnly) {
  312. continue;
  313. }
  314. targets.emplace_back(te->TargetName, te->XcFrameworkLocation);
  315. }
  316. return;
  317. }
  318. targets = this->Targets;
  319. }
  320. std::pair<std::vector<std::string>, std::string>
  321. cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
  322. const std::string& name)
  323. {
  324. std::vector<std::string> exportFiles;
  325. std::string ns;
  326. auto& exportSets = gg->GetBuildExportSets();
  327. for (auto const& exp : exportSets) {
  328. const auto& exportSet = exp.second;
  329. std::vector<TargetExport> targets;
  330. exportSet->GetTargets(targets);
  331. if (std::any_of(
  332. targets.begin(), targets.end(),
  333. [&name](const TargetExport& te) { return te.Name == name; })) {
  334. exportFiles.push_back(exp.first);
  335. ns = exportSet->GetNamespace();
  336. }
  337. }
  338. return { exportFiles, ns };
  339. }
  340. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  341. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  342. std::vector<std::string> const& exportFiles)
  343. {
  344. std::ostringstream e;
  345. e << "export called with target \"" << depender->GetName()
  346. << "\" which requires target \"" << dependee->GetName() << "\" ";
  347. if (exportFiles.empty()) {
  348. e << "that is not in any export set.";
  349. } else {
  350. e << "that is not in this export set, but in multiple other export sets: "
  351. << cmJoin(exportFiles, ", ") << ".\n";
  352. e << "An exported target cannot depend upon another target which is "
  353. "exported multiple times. Consider consolidating the exports of the "
  354. "\""
  355. << dependee->GetName() << "\" target to a single export.";
  356. }
  357. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  358. MessageType::FATAL_ERROR, e.str(),
  359. this->LG->GetMakefile()->GetBacktrace());
  360. }
  361. std::string cmExportBuildFileGenerator::InstallNameDir(
  362. cmGeneratorTarget const* target, const std::string& config)
  363. {
  364. std::string install_name_dir;
  365. cmMakefile* mf = target->Target->GetMakefile();
  366. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  367. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  368. }
  369. return install_name_dir;
  370. }
  371. namespace {
  372. bool EntryIsContextSensitive(
  373. const std::unique_ptr<cmCompiledGeneratorExpression>& cge)
  374. {
  375. return cge->GetHadContextSensitiveCondition();
  376. }
  377. }
  378. std::string cmExportBuildFileGenerator::GetFileSetDirectories(
  379. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* /*te*/)
  380. {
  381. std::vector<std::string> resultVector;
  382. auto configs =
  383. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  384. auto directoryEntries = fileSet->CompileDirectoryEntries();
  385. for (auto const& config : configs) {
  386. auto directories = fileSet->EvaluateDirectoryEntries(
  387. directoryEntries, gte->LocalGenerator, config, gte);
  388. bool const contextSensitive =
  389. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  390. EntryIsContextSensitive);
  391. auto const& type = fileSet->GetType();
  392. // C++ modules do not support interface file sets which are dependent upon
  393. // the configuration.
  394. if (contextSensitive && type == "CXX_MODULES"_s) {
  395. auto* mf = this->LG->GetMakefile();
  396. std::ostringstream e;
  397. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  398. << fileSet->GetName() << "\" of type \"" << type
  399. << "\" contains context-sensitive base directory entries which is not "
  400. "supported.";
  401. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  402. return std::string{};
  403. }
  404. for (auto const& directory : directories) {
  405. auto dest = cmOutputConverter::EscapeForCMake(
  406. directory, cmOutputConverter::WrapQuotes::NoWrap);
  407. if (contextSensitive && configs.size() != 1) {
  408. resultVector.push_back(
  409. cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
  410. } else {
  411. resultVector.emplace_back(cmStrCat('"', dest, '"'));
  412. break;
  413. }
  414. }
  415. }
  416. return cmJoin(resultVector, " ");
  417. }
  418. std::string cmExportBuildFileGenerator::GetFileSetFiles(cmGeneratorTarget* gte,
  419. cmFileSet* fileSet,
  420. cmTargetExport* /*te*/)
  421. {
  422. std::vector<std::string> resultVector;
  423. auto configs =
  424. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  425. auto fileEntries = fileSet->CompileFileEntries();
  426. auto directoryEntries = fileSet->CompileDirectoryEntries();
  427. for (auto const& config : configs) {
  428. auto directories = fileSet->EvaluateDirectoryEntries(
  429. directoryEntries, gte->LocalGenerator, config, gte);
  430. std::map<std::string, std::vector<std::string>> files;
  431. for (auto const& entry : fileEntries) {
  432. fileSet->EvaluateFileEntry(directories, files, entry,
  433. gte->LocalGenerator, config, gte);
  434. }
  435. bool const contextSensitive =
  436. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  437. EntryIsContextSensitive) ||
  438. std::any_of(fileEntries.begin(), fileEntries.end(),
  439. EntryIsContextSensitive);
  440. auto const& type = fileSet->GetType();
  441. // C++ modules do not support interface file sets which are dependent upon
  442. // the configuration.
  443. if (contextSensitive && type == "CXX_MODULES"_s) {
  444. auto* mf = this->LG->GetMakefile();
  445. std::ostringstream e;
  446. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  447. << fileSet->GetName() << "\" of type \"" << type
  448. << "\" contains context-sensitive file entries which is not "
  449. "supported.";
  450. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  451. return std::string{};
  452. }
  453. for (auto const& it : files) {
  454. for (auto const& filename : it.second) {
  455. auto escapedFile = cmOutputConverter::EscapeForCMake(
  456. filename, cmOutputConverter::WrapQuotes::NoWrap);
  457. if (contextSensitive && configs.size() != 1) {
  458. resultVector.push_back(
  459. cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
  460. } else {
  461. resultVector.emplace_back(cmStrCat('"', escapedFile, '"'));
  462. }
  463. }
  464. }
  465. if (!(contextSensitive && configs.size() != 1)) {
  466. break;
  467. }
  468. }
  469. return cmJoin(resultVector, " ");
  470. }
  471. std::string cmExportBuildFileGenerator::GetCxxModulesDirectory() const
  472. {
  473. return this->CxxModulesDirectory;
  474. }
  475. void cmExportBuildFileGenerator::GenerateCxxModuleConfigInformation(
  476. std::string const& name, std::ostream& os) const
  477. {
  478. const char* opt = "";
  479. if (this->Configurations.size() > 1) {
  480. // With more than one configuration, each individual file is optional.
  481. opt = " OPTIONAL";
  482. }
  483. // Generate import file content for each configuration.
  484. for (std::string c : this->Configurations) {
  485. if (c.empty()) {
  486. c = "noconfig";
  487. }
  488. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << '-'
  489. << c << ".cmake\"" << opt << ")\n";
  490. }
  491. }
  492. bool cmExportBuildFileGenerator::GenerateImportCxxModuleConfigTargetInclusion(
  493. std::string const& name, std::string config) const
  494. {
  495. auto cxx_modules_dirname = this->GetCxxModulesDirectory();
  496. if (cxx_modules_dirname.empty()) {
  497. return true;
  498. }
  499. if (config.empty()) {
  500. config = "noconfig";
  501. }
  502. std::string fileName =
  503. cmStrCat(this->FileDir, '/', cxx_modules_dirname, "/cxx-modules-", name,
  504. '-', config, ".cmake");
  505. cmGeneratedFileStream os(fileName, true);
  506. if (!os) {
  507. std::string se = cmSystemTools::GetLastSystemError();
  508. std::ostringstream e;
  509. e << "cannot write to file \"" << fileName << "\": " << se;
  510. cmSystemTools::Error(e.str());
  511. return false;
  512. }
  513. os.SetCopyIfDifferent(true);
  514. for (auto const* tgt : this->ExportedTargets) {
  515. // Only targets with C++ module sources will have a
  516. // collator-generated install script.
  517. if (!tgt->HaveCxx20ModuleSources()) {
  518. continue;
  519. }
  520. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/target-"
  521. << tgt->GetFilesystemExportName() << '-' << config << ".cmake\")\n";
  522. }
  523. return true;
  524. }