cmExportTryCompileFileGenerator.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "cmExportTryCompileFileGenerator.h"
  4. #include <map>
  5. #include <utility>
  6. #include <cm/memory>
  7. #include "cmFileSet.h"
  8. #include "cmGenExContext.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmGeneratorExpressionDAGChecker.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmList.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmOutputConverter.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h"
  20. #include "cmTarget.h"
  21. #include "cmValue.h"
  22. cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
  23. cmGlobalGenerator* gg, std::vector<std::string> const& targets,
  24. cmMakefile* mf, std::set<std::string> const& langs)
  25. : Languages(langs.begin(), langs.end())
  26. {
  27. gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
  28. }
  29. void cmExportTryCompileFileGenerator::ReportError(
  30. std::string const& errorMessage) const
  31. {
  32. cmSystemTools::Error(errorMessage);
  33. }
  34. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  35. {
  36. std::set<cmGeneratorTarget const*> emitted;
  37. std::set<cmGeneratorTarget const*> emittedDeps;
  38. while (!this->Exports.empty()) {
  39. cmGeneratorTarget const* te = this->Exports.back();
  40. this->Exports.pop_back();
  41. if (emitted.insert(te).second) {
  42. emittedDeps.insert(te);
  43. this->GenerateImportTargetCode(os, te, te->GetType());
  44. ImportPropertyMap properties;
  45. for (std::string const& lang : this->Languages) {
  46. for (auto i : cmGeneratorTarget::BuiltinTransitiveProperties) {
  47. this->FindTargets(std::string(i.second.InterfaceName), te, lang,
  48. emittedDeps);
  49. }
  50. }
  51. this->PopulateProperties(te, properties, emittedDeps);
  52. this->GenerateInterfaceProperties(te, os, properties);
  53. }
  54. }
  55. return true;
  56. }
  57. std::string cmExportTryCompileFileGenerator::FindTargets(
  58. std::string const& propName, cmGeneratorTarget const* tgt,
  59. std::string const& language, std::set<cmGeneratorTarget const*>& emitted)
  60. {
  61. cmValue prop = tgt->GetProperty(propName);
  62. if (!prop) {
  63. return std::string();
  64. }
  65. cm::GenEx::Context context(tgt->LocalGenerator, this->Config, language);
  66. cmGeneratorExpression ge(*tgt->Makefile->GetCMakeInstance());
  67. std::unique_ptr<cmGeneratorExpressionDAGChecker> parentDagChecker;
  68. if (propName == "INTERFACE_LINK_OPTIONS") {
  69. // To please constraint checks of DAGChecker, this property must have
  70. // LINK_OPTIONS property as parent
  71. parentDagChecker = cm::make_unique<cmGeneratorExpressionDAGChecker>(
  72. tgt, "LINK_OPTIONS", nullptr, nullptr, context);
  73. }
  74. cmGeneratorExpressionDAGChecker dagChecker{
  75. tgt, propName, nullptr, parentDagChecker.get(), context,
  76. };
  77. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*prop);
  78. cmTarget dummyHead("try_compile_dummy_exe", cmStateEnums::EXECUTABLE,
  79. cmTarget::Visibility::Normal, tgt->Target->GetMakefile(),
  80. cmTarget::PerConfig::Yes);
  81. dummyHead.SetIsForTryCompile();
  82. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  83. std::string result = cge->Evaluate(context, &dagChecker, &gDummyHead, tgt);
  84. std::set<cmGeneratorTarget const*> const& allTargets =
  85. cge->GetAllTargetsSeen();
  86. for (cmGeneratorTarget const* target : allTargets) {
  87. if (emitted.insert(target).second) {
  88. this->Exports.push_back(target);
  89. }
  90. }
  91. return result;
  92. }
  93. void cmExportTryCompileFileGenerator::PopulateProperties(
  94. cmGeneratorTarget const* target, ImportPropertyMap& properties,
  95. std::set<cmGeneratorTarget const*>& emitted)
  96. {
  97. // Look through all non-special properties.
  98. std::vector<std::string> props = target->GetPropertyKeys();
  99. // Include special properties that might be relevant here.
  100. props.emplace_back("INTERFACE_LINK_LIBRARIES");
  101. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT");
  102. props.emplace_back("INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE");
  103. for (std::string const& p : props) {
  104. cmValue v = target->GetProperty(p);
  105. if (!v) {
  106. continue;
  107. }
  108. properties[p] = *v;
  109. if (cmHasLiteralPrefix(p, "IMPORTED_LINK_INTERFACE_LIBRARIES") ||
  110. cmHasLiteralPrefix(p, "IMPORTED_LINK_DEPENDENT_LIBRARIES") ||
  111. cmHasLiteralPrefix(p, "INTERFACE_LINK_LIBRARIES")) {
  112. std::string evalResult =
  113. this->FindTargets(p, target, std::string(), emitted);
  114. cmList depends{ evalResult };
  115. for (std::string const& li : depends) {
  116. cmGeneratorTarget* tgt =
  117. target->GetLocalGenerator()->FindGeneratorTargetToUse(li);
  118. if (tgt && emitted.insert(tgt).second) {
  119. this->Exports.push_back(tgt);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  126. cmGeneratorTarget const* target, std::string const& config)
  127. {
  128. std::string install_name_dir;
  129. cmMakefile* mf = target->Target->GetMakefile();
  130. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  131. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  132. }
  133. return install_name_dir;
  134. }
  135. std::string cmExportTryCompileFileGenerator::GetFileSetDirectories(
  136. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  137. {
  138. return cmOutputConverter::EscapeForCMake(
  139. cmList::to_string(fileSet->GetDirectoryEntries()));
  140. }
  141. std::string cmExportTryCompileFileGenerator::GetFileSetFiles(
  142. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport const* /*te*/)
  143. {
  144. return cmOutputConverter::EscapeForCMake(
  145. cmList::to_string(fileSet->GetFileEntries()));
  146. }