cmExportBuildFileGenerator.cxx 8.4 KB

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