cmExportTryCompileFileGenerator.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2013 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmExportTryCompileFileGenerator.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmGeneratorExpressionDAGChecker.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmState.h"
  18. #include "cmSystemTools.h"
  19. #include "cmTarget.h"
  20. #include "cm_auto_ptr.hxx"
  21. #include <map>
  22. #include <utility>
  23. cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
  24. cmGlobalGenerator* gg, const std::vector<std::string>& targets,
  25. cmMakefile* mf)
  26. {
  27. gg->CreateImportedGenerationObjects(mf, targets, this->Exports);
  28. }
  29. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  30. {
  31. std::set<cmGeneratorTarget const*> emitted;
  32. std::set<cmGeneratorTarget const*> emittedDeps;
  33. while (!this->Exports.empty()) {
  34. cmGeneratorTarget const* te = this->Exports.back();
  35. this->Exports.pop_back();
  36. if (emitted.insert(te).second) {
  37. emittedDeps.insert(te);
  38. this->GenerateImportTargetCode(os, te);
  39. ImportPropertyMap properties;
  40. #define FIND_TARGETS(PROPERTY) \
  41. this->FindTargets("INTERFACE_" #PROPERTY, te, emittedDeps);
  42. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(FIND_TARGETS)
  43. #undef FIND_TARGETS
  44. this->PopulateProperties(te, properties, emittedDeps);
  45. this->GenerateInterfaceProperties(te, os, properties);
  46. }
  47. }
  48. return true;
  49. }
  50. std::string cmExportTryCompileFileGenerator::FindTargets(
  51. const std::string& propName, cmGeneratorTarget const* tgt,
  52. std::set<cmGeneratorTarget const*>& emitted)
  53. {
  54. const char* prop = tgt->GetProperty(propName);
  55. if (!prop) {
  56. return std::string();
  57. }
  58. cmGeneratorExpression ge;
  59. cmGeneratorExpressionDAGChecker dagChecker(tgt->GetName(), propName,
  60. CM_NULLPTR, CM_NULLPTR);
  61. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  62. cmTarget dummyHead;
  63. dummyHead.SetType(cmState::EXECUTABLE, "try_compile_dummy_exe");
  64. dummyHead.SetMakefile(tgt->Target->GetMakefile());
  65. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  66. std::string result = cge->Evaluate(tgt->GetLocalGenerator(), this->Config,
  67. false, &gDummyHead, tgt, &dagChecker);
  68. const std::set<cmGeneratorTarget const*>& allTargets =
  69. cge->GetAllTargetsSeen();
  70. for (std::set<cmGeneratorTarget const*>::const_iterator li =
  71. allTargets.begin();
  72. li != allTargets.end(); ++li) {
  73. if (emitted.insert(*li).second) {
  74. this->Exports.push_back(*li);
  75. }
  76. }
  77. return result;
  78. }
  79. void cmExportTryCompileFileGenerator::PopulateProperties(
  80. const cmGeneratorTarget* target, ImportPropertyMap& properties,
  81. std::set<cmGeneratorTarget const*>& emitted)
  82. {
  83. std::vector<std::string> props = target->GetPropertyKeys();
  84. for (std::vector<std::string>::const_iterator i = props.begin();
  85. i != props.end(); ++i) {
  86. properties[*i] = target->GetProperty(*i);
  87. if (i->find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  88. i->find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0 ||
  89. i->find("INTERFACE_LINK_LIBRARIES") == 0) {
  90. std::string evalResult = this->FindTargets(*i, target, emitted);
  91. std::vector<std::string> depends;
  92. cmSystemTools::ExpandListArgument(evalResult, depends);
  93. for (std::vector<std::string>::const_iterator li = depends.begin();
  94. li != depends.end(); ++li) {
  95. cmGeneratorTarget* tgt =
  96. target->GetLocalGenerator()->FindGeneratorTargetToUse(*li);
  97. if (tgt && emitted.insert(tgt).second) {
  98. this->Exports.push_back(tgt);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  105. cmGeneratorTarget* target, const std::string& config)
  106. {
  107. std::string install_name_dir;
  108. cmMakefile* mf = target->Target->GetMakefile();
  109. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  110. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  111. }
  112. return install_name_dir;
  113. }