cmExportTryCompileFileGenerator.cxx 4.3 KB

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