cmExportBuildFileGenerator.cxx 7.5 KB

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