cmExportBuildFileGenerator.cxx 8.4 KB

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