cmExportBuildFileGenerator.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. std::vector<std::string> missingTargets;
  49. // Create all the imported targets.
  50. for(std::vector<cmTarget*>::const_iterator
  51. tei = allTargets.begin();
  52. tei != allTargets.end(); ++tei)
  53. {
  54. cmTarget* te = *tei;
  55. this->GenerateImportTargetCode(os, te);
  56. ImportPropertyMap properties;
  57. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", te,
  58. cmGeneratorExpression::BuildInterface,
  59. properties, missingTargets);
  60. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", te,
  61. cmGeneratorExpression::BuildInterface,
  62. properties, missingTargets);
  63. this->GenerateInterfaceProperties(te, os, properties);
  64. }
  65. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  66. // Generate import file content for each configuration.
  67. for(std::vector<std::string>::const_iterator
  68. ci = this->Configurations.begin();
  69. ci != this->Configurations.end(); ++ci)
  70. {
  71. this->GenerateImportConfig(os, ci->c_str());
  72. }
  73. return true;
  74. }
  75. //----------------------------------------------------------------------------
  76. void
  77. cmExportBuildFileGenerator
  78. ::GenerateImportTargetsConfig(std::ostream& os,
  79. const char* config, std::string const& suffix)
  80. {
  81. for(std::vector<cmTarget*>::const_iterator
  82. tei = this->Exports->begin();
  83. tei != this->Exports->end(); ++tei)
  84. {
  85. // Collect import properties for this target.
  86. cmTarget* target = *tei;
  87. ImportPropertyMap properties;
  88. this->SetImportLocationProperty(config, suffix, target, properties);
  89. if(!properties.empty())
  90. {
  91. // Get the rest of the target details.
  92. std::vector<std::string> missingTargets;
  93. this->SetImportDetailProperties(config, suffix,
  94. target, properties, missingTargets);
  95. // TOOD: PUBLIC_HEADER_LOCATION
  96. // This should wait until the build feature propagation stuff
  97. // is done. Then this can be a propagated include directory.
  98. // this->GenerateImportProperty(config, te->HeaderGenerator,
  99. // properties);
  100. // Generate code in the export file.
  101. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  102. this->GenerateImportPropertyCode(os, config, target, properties);
  103. }
  104. }
  105. }
  106. //----------------------------------------------------------------------------
  107. void
  108. cmExportBuildFileGenerator
  109. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  110. cmTarget* target, ImportPropertyMap& properties)
  111. {
  112. // Get the makefile in which to lookup target information.
  113. cmMakefile* mf = target->GetMakefile();
  114. // Add the main target file.
  115. {
  116. std::string prop = "IMPORTED_LOCATION";
  117. prop += suffix;
  118. std::string value;
  119. if(target->IsFrameworkOnApple() || target->IsAppBundleOnApple())
  120. {
  121. value = target->GetFullPath(config, false);
  122. }
  123. else
  124. {
  125. value = target->GetFullPath(config, false, true);
  126. }
  127. properties[prop] = value;
  128. }
  129. // Check whether this is a DLL platform.
  130. bool dll_platform =
  131. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  132. // Add the import library for windows DLLs.
  133. if(dll_platform &&
  134. (target->GetType() == cmTarget::SHARED_LIBRARY ||
  135. target->IsExecutableWithExports()) &&
  136. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  137. {
  138. std::string prop = "IMPORTED_IMPLIB";
  139. prop += suffix;
  140. std::string value = target->GetFullPath(config, true);
  141. target->GetImplibGNUtoMS(value, value,
  142. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  143. properties[prop] = value;
  144. }
  145. }
  146. //----------------------------------------------------------------------------
  147. void
  148. cmExportBuildFileGenerator::HandleMissingTarget(
  149. std::string& link_libs, std::vector<std::string>&,
  150. cmMakefile*, cmTarget* depender, cmTarget* dependee)
  151. {
  152. // The target is not in the export.
  153. if(!this->AppendMode)
  154. {
  155. // We are not appending, so all exported targets should be
  156. // known here. This is probably user-error.
  157. this->ComplainAboutMissingTarget(depender, dependee);
  158. }
  159. // Assume the target will be exported by another command.
  160. // Append it with the export namespace.
  161. link_libs += this->Namespace;
  162. link_libs += dependee->GetName();
  163. }
  164. //----------------------------------------------------------------------------
  165. void
  166. cmExportBuildFileGenerator
  167. ::ComplainAboutMissingTarget(cmTarget* depender,
  168. cmTarget* dependee)
  169. {
  170. if(!this->ExportCommand || !this->ExportCommand->ErrorMessage.empty())
  171. {
  172. return;
  173. }
  174. cmOStringStream e;
  175. e << "called with target \"" << depender->GetName()
  176. << "\" which requires target \"" << dependee->GetName()
  177. << "\" that is not in the export list.\n"
  178. << "If the required target is not easy to reference in this call, "
  179. << "consider using the APPEND option with multiple separate calls.";
  180. this->ExportCommand->ErrorMessage = e.str();
  181. }