cmExportBuildFileGenerator.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  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 "cmExportBuildFileGenerator.h"
  11. #include "cmExportCommand.h"
  12. //----------------------------------------------------------------------------
  13. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  14. {
  15. this->ExportCommand = 0;
  16. }
  17. //----------------------------------------------------------------------------
  18. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  19. {
  20. std::vector<cmTarget*> allTargets;
  21. {
  22. std::string expectedTargets;
  23. std::string sep;
  24. for(std::vector<cmTarget*>::const_iterator
  25. tei = this->Exports->begin();
  26. tei != this->Exports->end(); ++tei)
  27. {
  28. expectedTargets += sep + this->Namespace + (*tei)->GetName();
  29. sep = " ";
  30. cmTarget* te = *tei;
  31. if(this->ExportedTargets.insert(te).second)
  32. {
  33. allTargets.push_back(te);
  34. }
  35. else
  36. {
  37. if(this->ExportCommand && this->ExportCommand->ErrorMessage.empty())
  38. {
  39. cmOStringStream e;
  40. e << "given target \"" << te->GetName() << "\" more than once.";
  41. this->ExportCommand->ErrorMessage = e.str();
  42. }
  43. return false;
  44. }
  45. }
  46. this->GenerateExpectedTargetsCode(os, expectedTargets);
  47. }
  48. // Create all the imported targets.
  49. for(std::vector<cmTarget*>::const_iterator
  50. tei = allTargets.begin();
  51. tei != allTargets.end(); ++tei)
  52. {
  53. cmTarget* te = *tei;
  54. this->GenerateImportTargetCode(os, te);
  55. }
  56. // Generate import file content for each configuration.
  57. for(std::vector<std::string>::const_iterator
  58. ci = this->Configurations.begin();
  59. ci != this->Configurations.end(); ++ci)
  60. {
  61. this->GenerateImportConfig(os, ci->c_str());
  62. }
  63. return true;
  64. }
  65. //----------------------------------------------------------------------------
  66. void
  67. cmExportBuildFileGenerator
  68. ::GenerateImportTargetsConfig(std::ostream& os,
  69. const char* config, std::string const& suffix)
  70. {
  71. for(std::vector<cmTarget*>::const_iterator
  72. tei = this->Exports->begin();
  73. tei != this->Exports->end(); ++tei)
  74. {
  75. // Collect import properties for this target.
  76. cmTarget* target = *tei;
  77. ImportPropertyMap properties;
  78. this->SetImportLocationProperty(config, suffix, target, properties);
  79. if(!properties.empty())
  80. {
  81. // Get the rest of the target details.
  82. std::vector<std::string> missingTargets;
  83. this->SetImportDetailProperties(config, suffix,
  84. target, properties, missingTargets);
  85. // TOOD: PUBLIC_HEADER_LOCATION
  86. // This should wait until the build feature propagation stuff
  87. // is done. Then this can be a propagated include directory.
  88. // this->GenerateImportProperty(config, te->HeaderGenerator,
  89. // properties);
  90. // Generate code in the export file.
  91. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  92. this->GenerateImportPropertyCode(os, config, target, properties);
  93. }
  94. }
  95. }
  96. //----------------------------------------------------------------------------
  97. void
  98. cmExportBuildFileGenerator
  99. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  100. cmTarget* target, ImportPropertyMap& properties)
  101. {
  102. // Get the makefile in which to lookup target information.
  103. cmMakefile* mf = target->GetMakefile();
  104. // Add the main target file.
  105. {
  106. std::string prop = "IMPORTED_LOCATION";
  107. prop += suffix;
  108. std::string value;
  109. if(target->IsFrameworkOnApple() || target->IsAppBundleOnApple())
  110. {
  111. value = target->GetFullPath(config, false);
  112. }
  113. else
  114. {
  115. value = target->GetFullPath(config, false, true);
  116. }
  117. properties[prop] = value;
  118. }
  119. // Check whether this is a DLL platform.
  120. bool dll_platform =
  121. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  122. // Add the import library for windows DLLs.
  123. if(dll_platform &&
  124. (target->GetType() == cmTarget::SHARED_LIBRARY ||
  125. target->IsExecutableWithExports()) &&
  126. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  127. {
  128. std::string prop = "IMPORTED_IMPLIB";
  129. prop += suffix;
  130. std::string value = target->GetFullPath(config, true);
  131. target->GetImplibGNUtoMS(value, value,
  132. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  133. properties[prop] = value;
  134. }
  135. }
  136. //----------------------------------------------------------------------------
  137. void
  138. cmExportBuildFileGenerator::HandleMissingTarget(
  139. std::string& link_libs, std::vector<std::string>&,
  140. cmMakefile*, cmTarget* depender, cmTarget* dependee)
  141. {
  142. // The target is not in the export.
  143. if(!this->AppendMode)
  144. {
  145. // We are not appending, so all exported targets should be
  146. // known here. This is probably user-error.
  147. this->ComplainAboutMissingTarget(depender, dependee);
  148. }
  149. // Assume the target will be exported by another command.
  150. // Append it with the export namespace.
  151. link_libs += this->Namespace;
  152. link_libs += dependee->GetName();
  153. }
  154. //----------------------------------------------------------------------------
  155. void
  156. cmExportBuildFileGenerator
  157. ::ComplainAboutMissingTarget(cmTarget* depender,
  158. cmTarget* dependee)
  159. {
  160. if(!this->ExportCommand || !this->ExportCommand->ErrorMessage.empty())
  161. {
  162. return;
  163. }
  164. cmOStringStream e;
  165. e << "called with target \"" << depender->GetName()
  166. << "\" which requires target \"" << dependee->GetName()
  167. << "\" that is not in the export list.\n"
  168. << "If the required target is not easy to reference in this call, "
  169. << "consider using the APPEND option with multiple separate calls.";
  170. this->ExportCommand->ErrorMessage = e.str();
  171. }