cmExportBuildFileGenerator.cxx 7.0 KB

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