cmExportBuildCMakeConfigGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmExportBuildCMakeConfigGenerator.h"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <sstream>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/string_view>
  14. #include <cmext/string_view>
  15. #include "cmCryptoHash.h"
  16. #include "cmExportSet.h"
  17. #include "cmFileSet.h"
  18. #include "cmGenExContext.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmGeneratorExpression.h"
  21. #include "cmGeneratorTarget.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmMakefile.h"
  24. #include "cmMessageType.h"
  25. #include "cmOutputConverter.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. #include "cmTarget.h"
  30. cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator()
  31. {
  32. this->LG = nullptr;
  33. this->ExportSet = nullptr;
  34. }
  35. bool cmExportBuildCMakeConfigGenerator::GenerateMainFile(std::ostream& os)
  36. {
  37. {
  38. std::string expectedTargets;
  39. std::string sep;
  40. bool generatedInterfaceRequired = false;
  41. auto visitor = [&](cmGeneratorTarget const* te) {
  42. expectedTargets += sep + this->Namespace + te->GetExportName();
  43. sep = " ";
  44. generatedInterfaceRequired |=
  45. this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY;
  46. };
  47. if (!this->CollectExports(visitor)) {
  48. return false;
  49. }
  50. if (generatedInterfaceRequired) {
  51. this->SetRequiredCMakeVersion(3, 0, 0);
  52. }
  53. this->GenerateExpectedTargetsCode(os, expectedTargets);
  54. }
  55. // Create all the imported targets.
  56. for (auto const& exp : this->Exports) {
  57. cmGeneratorTarget* gte = exp.Target;
  58. this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
  59. gte->Target->AppendBuildInterfaceIncludes();
  60. ImportPropertyMap properties;
  61. if (!this->PopulateInterfaceProperties(gte, properties)) {
  62. return false;
  63. }
  64. this->PopulateInterfaceLinkLibrariesProperty(
  65. gte, cmGeneratorExpression::BuildInterface, properties);
  66. this->GenerateInterfaceProperties(gte, os, properties);
  67. this->GenerateTargetFileSets(gte, os);
  68. }
  69. std::string cxx_modules_name;
  70. if (this->ExportSet) {
  71. cxx_modules_name = this->ExportSet->GetName();
  72. } else {
  73. cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512);
  74. constexpr std::size_t HASH_TRUNCATION = 12;
  75. for (auto const& target : this->Targets) {
  76. hasher.Append(target.Name);
  77. }
  78. cxx_modules_name = hasher.FinalizeHex().substr(0, HASH_TRUNCATION);
  79. }
  80. this->GenerateCxxModuleInformation(cxx_modules_name, os);
  81. // Generate import file content for each configuration.
  82. for (std::string const& c : this->Configurations) {
  83. this->GenerateImportConfig(os, c);
  84. }
  85. // Generate import file content for each configuration.
  86. for (std::string const& c : this->Configurations) {
  87. this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name, c);
  88. }
  89. this->GenerateMissingTargetsCheckCode(os);
  90. return true;
  91. }
  92. void cmExportBuildCMakeConfigGenerator::GenerateImportTargetsConfig(
  93. std::ostream& os, std::string const& config, std::string const& suffix)
  94. {
  95. for (auto const& exp : this->Exports) {
  96. cmGeneratorTarget* target = exp.Target;
  97. // Collect import properties for this target.
  98. ImportPropertyMap properties;
  99. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  100. this->SetImportLocationProperty(config, suffix, target, properties);
  101. }
  102. if (!properties.empty()) {
  103. // Get the rest of the target details.
  104. if (this->GetExportTargetType(target) !=
  105. cmStateEnums::INTERFACE_LIBRARY) {
  106. this->SetImportDetailProperties(config, suffix, target, properties);
  107. this->SetImportLinkInterface(config, suffix,
  108. cmGeneratorExpression::BuildInterface,
  109. target, properties);
  110. }
  111. // TODO: PUBLIC_HEADER_LOCATION
  112. // This should wait until the build feature propagation stuff
  113. // is done. Then this can be a propagated include directory.
  114. // this->GenerateImportProperty(config, te->HeaderGenerator,
  115. // properties);
  116. // Generate code in the export file.
  117. std::string importedXcFrameworkLocation = exp.XcFrameworkLocation;
  118. if (!importedXcFrameworkLocation.empty()) {
  119. importedXcFrameworkLocation = cmGeneratorExpression::Preprocess(
  120. importedXcFrameworkLocation,
  121. cmGeneratorExpression::PreprocessContext::BuildInterface);
  122. importedXcFrameworkLocation = cmGeneratorExpression::Evaluate(
  123. importedXcFrameworkLocation, exp.Target->GetLocalGenerator(), config,
  124. exp.Target, nullptr, exp.Target);
  125. if (!importedXcFrameworkLocation.empty() &&
  126. !cmSystemTools::FileIsFullPath(importedXcFrameworkLocation)) {
  127. importedXcFrameworkLocation =
  128. cmStrCat(this->LG->GetCurrentBinaryDirectory(), '/',
  129. importedXcFrameworkLocation);
  130. }
  131. }
  132. this->GenerateImportPropertyCode(os, config, suffix, target, properties,
  133. importedXcFrameworkLocation);
  134. }
  135. }
  136. }
  137. namespace {
  138. bool EntryIsContextSensitive(
  139. std::unique_ptr<cmCompiledGeneratorExpression> const& cge)
  140. {
  141. return cge->GetHadContextSensitiveCondition();
  142. }
  143. }
  144. std::string cmExportBuildCMakeConfigGenerator::GetFileSetDirectories(
  145. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  146. {
  147. std::vector<std::string> resultVector;
  148. auto configs =
  149. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  150. auto directoryEntries = fileSet->CompileDirectoryEntries();
  151. for (auto const& config : configs) {
  152. cm::GenEx::Context context(gte->LocalGenerator, config);
  153. auto directories =
  154. fileSet->EvaluateDirectoryEntries(directoryEntries, context, gte);
  155. bool const contextSensitive =
  156. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  157. EntryIsContextSensitive);
  158. auto const& type = fileSet->GetType();
  159. // C++ modules do not support interface file sets which are dependent upon
  160. // the configuration.
  161. if (contextSensitive && type == "CXX_MODULES"_s) {
  162. auto* mf = this->LG->GetMakefile();
  163. std::ostringstream e;
  164. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  165. << fileSet->GetName() << "\" of type \"" << type
  166. << "\" contains context-sensitive base directory entries which is not "
  167. "supported.";
  168. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  169. return std::string{};
  170. }
  171. for (auto const& directory : directories) {
  172. auto dest = cmOutputConverter::EscapeForCMake(
  173. directory, cmOutputConverter::WrapQuotes::NoWrap);
  174. if (contextSensitive && configs.size() != 1) {
  175. resultVector.push_back(
  176. cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
  177. } else {
  178. resultVector.emplace_back(cmStrCat('"', dest, '"'));
  179. break;
  180. }
  181. }
  182. }
  183. return cmJoin(resultVector, " ");
  184. }
  185. std::string cmExportBuildCMakeConfigGenerator::GetFileSetFiles(
  186. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  187. {
  188. std::vector<std::string> resultVector;
  189. auto configs =
  190. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  191. auto fileEntries = fileSet->CompileFileEntries();
  192. auto directoryEntries = fileSet->CompileDirectoryEntries();
  193. for (auto const& config : configs) {
  194. cm::GenEx::Context context(gte->LocalGenerator, config);
  195. auto directories =
  196. fileSet->EvaluateDirectoryEntries(directoryEntries, context, gte);
  197. std::map<std::string, std::vector<std::string>> files;
  198. for (auto const& entry : fileEntries) {
  199. fileSet->EvaluateFileEntry(directories, files, entry, context, gte);
  200. }
  201. bool const contextSensitive =
  202. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  203. EntryIsContextSensitive) ||
  204. std::any_of(fileEntries.begin(), fileEntries.end(),
  205. EntryIsContextSensitive);
  206. auto const& type = fileSet->GetType();
  207. // C++ modules do not support interface file sets which are dependent upon
  208. // the configuration.
  209. if (contextSensitive && type == "CXX_MODULES"_s) {
  210. auto* mf = this->LG->GetMakefile();
  211. std::ostringstream e;
  212. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  213. << fileSet->GetName() << "\" of type \"" << type
  214. << "\" contains context-sensitive file entries which is not "
  215. "supported.";
  216. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  217. return std::string{};
  218. }
  219. for (auto const& it : files) {
  220. for (auto const& filename : it.second) {
  221. auto escapedFile = cmOutputConverter::EscapeForCMake(
  222. filename, cmOutputConverter::WrapQuotes::NoWrap);
  223. if (contextSensitive && configs.size() != 1) {
  224. resultVector.push_back(
  225. cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
  226. } else {
  227. resultVector.emplace_back(cmStrCat('"', escapedFile, '"'));
  228. }
  229. }
  230. }
  231. if (!(contextSensitive && configs.size() != 1)) {
  232. break;
  233. }
  234. }
  235. return cmJoin(resultVector, " ");
  236. }
  237. void cmExportBuildCMakeConfigGenerator::GenerateCxxModuleConfigInformation(
  238. std::string const& name, std::ostream& os) const
  239. {
  240. char const* opt = "";
  241. if (this->Configurations.size() > 1) {
  242. // With more than one configuration, each individual file is optional.
  243. opt = " OPTIONAL";
  244. }
  245. // Generate import file content for each configuration.
  246. for (std::string c : this->Configurations) {
  247. if (c.empty()) {
  248. c = "noconfig";
  249. }
  250. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << '-'
  251. << c << ".cmake\"" << opt << ")\n";
  252. }
  253. }
  254. bool cmExportBuildCMakeConfigGenerator::
  255. GenerateImportCxxModuleConfigTargetInclusion(std::string const& name,
  256. std::string config) const
  257. {
  258. auto cxx_modules_dirname = this->GetCxxModulesDirectory();
  259. if (cxx_modules_dirname.empty()) {
  260. return true;
  261. }
  262. if (config.empty()) {
  263. config = "noconfig";
  264. }
  265. std::string fileName =
  266. cmStrCat(this->FileDir, '/', cxx_modules_dirname, "/cxx-modules-", name,
  267. '-', config, ".cmake");
  268. cmGeneratedFileStream os(fileName, true);
  269. if (!os) {
  270. std::string se = cmSystemTools::GetLastSystemError();
  271. std::ostringstream e;
  272. e << "cannot write to file \"" << fileName << "\": " << se;
  273. cmSystemTools::Error(e.str());
  274. return false;
  275. }
  276. os.SetCopyIfDifferent(true);
  277. for (auto const* tgt : this->ExportedTargets) {
  278. // Only targets with C++ module sources will have a
  279. // collator-generated install script.
  280. if (!tgt->HaveCxx20ModuleSources()) {
  281. continue;
  282. }
  283. os << "include(\"${CMAKE_CURRENT_LIST_DIR}/target-"
  284. << tgt->GetFilesystemExportName() << '-' << config << ".cmake\")\n";
  285. }
  286. return true;
  287. }