cmExportBuildFileGenerator.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. this->SetImportLinkInterface(config, suffix,
  96. cmGeneratorExpression::BuildInterface,
  97. target, properties, missingTargets);
  98. // TOOD: PUBLIC_HEADER_LOCATION
  99. // This should wait until the build feature propagation stuff
  100. // is done. Then this can be a propagated include directory.
  101. // this->GenerateImportProperty(config, te->HeaderGenerator,
  102. // properties);
  103. // Generate code in the export file.
  104. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  105. this->GenerateImportPropertyCode(os, config, target, properties);
  106. }
  107. }
  108. }
  109. //----------------------------------------------------------------------------
  110. void
  111. cmExportBuildFileGenerator
  112. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  113. cmTarget* target, ImportPropertyMap& properties)
  114. {
  115. // Get the makefile in which to lookup target information.
  116. cmMakefile* mf = target->GetMakefile();
  117. // Add the main target file.
  118. {
  119. std::string prop = "IMPORTED_LOCATION";
  120. prop += suffix;
  121. std::string value;
  122. if(target->IsFrameworkOnApple() || target->IsAppBundleOnApple())
  123. {
  124. value = target->GetFullPath(config, false);
  125. }
  126. else
  127. {
  128. value = target->GetFullPath(config, false, true);
  129. }
  130. properties[prop] = value;
  131. }
  132. // Check whether this is a DLL platform.
  133. bool dll_platform =
  134. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  135. // Add the import library for windows DLLs.
  136. if(dll_platform &&
  137. (target->GetType() == cmTarget::SHARED_LIBRARY ||
  138. target->IsExecutableWithExports()) &&
  139. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  140. {
  141. std::string prop = "IMPORTED_IMPLIB";
  142. prop += suffix;
  143. std::string value = target->GetFullPath(config, true);
  144. target->GetImplibGNUtoMS(value, value,
  145. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  146. properties[prop] = value;
  147. }
  148. }
  149. //----------------------------------------------------------------------------
  150. void
  151. cmExportBuildFileGenerator::HandleMissingTarget(
  152. std::string& link_libs, std::vector<std::string>&,
  153. cmMakefile*, cmTarget* depender, cmTarget* dependee)
  154. {
  155. // The target is not in the export.
  156. if(!this->AppendMode)
  157. {
  158. // We are not appending, so all exported targets should be
  159. // known here. This is probably user-error.
  160. this->ComplainAboutMissingTarget(depender, dependee);
  161. }
  162. // Assume the target will be exported by another command.
  163. // Append it with the export namespace.
  164. link_libs += this->Namespace;
  165. link_libs += dependee->GetName();
  166. }
  167. //----------------------------------------------------------------------------
  168. void
  169. cmExportBuildFileGenerator
  170. ::ComplainAboutMissingTarget(cmTarget* depender,
  171. cmTarget* dependee)
  172. {
  173. if(!this->ExportCommand || !this->ExportCommand->ErrorMessage.empty())
  174. {
  175. return;
  176. }
  177. cmOStringStream e;
  178. e << "called with target \"" << depender->GetName()
  179. << "\" which requires target \"" << dependee->GetName()
  180. << "\" that is not in the export list.\n"
  181. << "If the required target is not easy to reference in this call, "
  182. << "consider using the APPEND option with multiple separate calls.";
  183. this->ExportCommand->ErrorMessage = e.str();
  184. }