cmExportTryCompileFileGenerator.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "cmGeneratedFileStream.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmGeneratorExpressionDAGChecker.h"
  14. //----------------------------------------------------------------------------
  15. cmExportTryCompileFileGenerator::cmExportTryCompileFileGenerator(
  16. cmGlobalGenerator* gg)
  17. {
  18. gg->CreateGenerationObjects(cmGlobalGenerator::ImportedOnly);
  19. }
  20. bool cmExportTryCompileFileGenerator::GenerateMainFile(std::ostream& os)
  21. {
  22. std::set<cmTarget const*> emitted;
  23. std::set<cmTarget const*> emittedDeps;
  24. while(!this->Exports.empty())
  25. {
  26. cmTarget const* te = this->Exports.back();
  27. this->Exports.pop_back();
  28. if (emitted.insert(te).second)
  29. {
  30. emittedDeps.insert(te);
  31. this->GenerateImportTargetCode(os, te);
  32. ImportPropertyMap properties;
  33. #define FIND_TARGETS(PROPERTY) \
  34. this->FindTargets("INTERFACE_" #PROPERTY, te, emittedDeps);
  35. CM_FOR_EACH_TRANSITIVE_PROPERTY_NAME(FIND_TARGETS)
  36. #undef FIND_TARGETS
  37. this->PopulateProperties(te, properties, emittedDeps);
  38. this->GenerateInterfaceProperties(te, os, properties);
  39. }
  40. }
  41. return true;
  42. }
  43. std::string cmExportTryCompileFileGenerator::FindTargets(
  44. const std::string& propName,
  45. cmTarget const* tgt,
  46. std::set<cmTarget const*> &emitted)
  47. {
  48. const char *prop = tgt->GetProperty(propName);
  49. if(!prop)
  50. {
  51. return std::string();
  52. }
  53. cmGeneratorExpression ge;
  54. cmGeneratorExpressionDAGChecker dagChecker(
  55. tgt->GetName(),
  56. propName, 0, 0);
  57. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
  58. cmTarget dummyHead;
  59. dummyHead.SetType(cmTarget::EXECUTABLE, "try_compile_dummy_exe");
  60. dummyHead.SetMakefile(tgt->GetMakefile());
  61. cmGeneratorTarget* gtgt =
  62. tgt->GetMakefile()->GetGlobalGenerator()->GetGeneratorTarget(tgt);
  63. std::string result = cge->Evaluate(gtgt->Target->GetMakefile(), this->Config,
  64. false, &dummyHead,
  65. gtgt->Target, &dagChecker);
  66. const std::set<cmTarget const*> &allTargets = cge->GetAllTargetsSeen();
  67. for(std::set<cmTarget const*>::const_iterator li = allTargets.begin();
  68. li != allTargets.end(); ++li)
  69. {
  70. if(emitted.insert(*li).second)
  71. {
  72. this->Exports.push_back(*li);
  73. }
  74. }
  75. return result;
  76. }
  77. //----------------------------------------------------------------------------
  78. void
  79. cmExportTryCompileFileGenerator::PopulateProperties(cmTarget const* target,
  80. ImportPropertyMap& properties,
  81. std::set<cmTarget const*> &emitted)
  82. {
  83. cmPropertyMap props = target->GetProperties();
  84. for(cmPropertyMap::const_iterator i = props.begin(); i != props.end(); ++i)
  85. {
  86. properties[i->first] = i->second.GetValue();
  87. if(i->first.find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0
  88. || i->first.find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0
  89. || i->first.find("INTERFACE_LINK_LIBRARIES") == 0)
  90. {
  91. std::string evalResult = this->FindTargets(i->first,
  92. target, emitted);
  93. std::vector<std::string> depends;
  94. cmSystemTools::ExpandListArgument(evalResult, depends);
  95. for(std::vector<std::string>::const_iterator li = depends.begin();
  96. li != depends.end(); ++li)
  97. {
  98. cmTarget *tgt = target->GetMakefile()->FindTargetToUse(*li);
  99. if(tgt && emitted.insert(tgt).second)
  100. {
  101. this->Exports.push_back(tgt);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. std::string
  108. cmExportTryCompileFileGenerator::InstallNameDir(cmGeneratorTarget* target,
  109. const std::string& config)
  110. {
  111. std::string install_name_dir;
  112. cmMakefile* mf = target->Target->GetMakefile();
  113. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  114. {
  115. install_name_dir =
  116. target->GetInstallNameDirForBuildTree(config);
  117. }
  118. return install_name_dir;
  119. }