cmExportBuildFileGenerator.cxx 8.1 KB

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