cmExportTryCompileFileGenerator.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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("try_compile_dummy_exe", cmState::EXECUTABLE,
  63. cmTarget::VisibilityNormal, tgt->Target->GetMakefile());
  64. cmGeneratorTarget gDummyHead(&dummyHead, tgt->GetLocalGenerator());
  65. std::string result = cge->Evaluate(tgt->GetLocalGenerator(), this->Config,
  66. false, &gDummyHead, tgt, &dagChecker);
  67. const std::set<cmGeneratorTarget const*>& allTargets =
  68. cge->GetAllTargetsSeen();
  69. for (std::set<cmGeneratorTarget const*>::const_iterator li =
  70. allTargets.begin();
  71. li != allTargets.end(); ++li) {
  72. if (emitted.insert(*li).second) {
  73. this->Exports.push_back(*li);
  74. }
  75. }
  76. return result;
  77. }
  78. void cmExportTryCompileFileGenerator::PopulateProperties(
  79. const cmGeneratorTarget* target, ImportPropertyMap& properties,
  80. std::set<cmGeneratorTarget const*>& emitted)
  81. {
  82. std::vector<std::string> props = target->GetPropertyKeys();
  83. for (std::vector<std::string>::const_iterator i = props.begin();
  84. i != props.end(); ++i) {
  85. properties[*i] = target->GetProperty(*i);
  86. if (i->find("IMPORTED_LINK_INTERFACE_LIBRARIES") == 0 ||
  87. i->find("IMPORTED_LINK_DEPENDENT_LIBRARIES") == 0 ||
  88. i->find("INTERFACE_LINK_LIBRARIES") == 0) {
  89. std::string evalResult = this->FindTargets(*i, target, emitted);
  90. std::vector<std::string> depends;
  91. cmSystemTools::ExpandListArgument(evalResult, depends);
  92. for (std::vector<std::string>::const_iterator li = depends.begin();
  93. li != depends.end(); ++li) {
  94. cmGeneratorTarget* tgt =
  95. target->GetLocalGenerator()->FindGeneratorTargetToUse(*li);
  96. if (tgt && emitted.insert(tgt).second) {
  97. this->Exports.push_back(tgt);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. std::string cmExportTryCompileFileGenerator::InstallNameDir(
  104. cmGeneratorTarget* target, const std::string& config)
  105. {
  106. std::string install_name_dir;
  107. cmMakefile* mf = target->Target->GetMakefile();
  108. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  109. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  110. }
  111. return install_name_dir;
  112. }