cmExportBuildFileGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "cmExportSet.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmPolicies.h"
  18. #include "cmState.h"
  19. #include "cmSystemTools.h"
  20. #include "cmTarget.h"
  21. #include "cmTargetExport.h"
  22. #include "cmake.h"
  23. #include <algorithm>
  24. #include <map>
  25. #include <set>
  26. #include <sstream>
  27. #include <utility>
  28. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  29. {
  30. this->LG = CM_NULLPTR;
  31. this->ExportSet = CM_NULLPTR;
  32. }
  33. void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg)
  34. {
  35. this->LG = lg;
  36. if (this->ExportSet) {
  37. this->ExportSet->Compute(lg);
  38. }
  39. }
  40. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  41. {
  42. {
  43. std::string expectedTargets;
  44. std::string sep;
  45. std::vector<std::string> targets;
  46. this->GetTargets(targets);
  47. for (std::vector<std::string>::const_iterator tei = targets.begin();
  48. tei != targets.end(); ++tei) {
  49. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(*tei);
  50. expectedTargets += sep + this->Namespace + te->GetExportName();
  51. sep = " ";
  52. if (this->ExportedTargets.insert(te).second) {
  53. this->Exports.push_back(te);
  54. } else {
  55. std::ostringstream e;
  56. e << "given target \"" << te->GetName() << "\" more than once.";
  57. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  58. cmake::FATAL_ERROR, e.str(),
  59. this->LG->GetMakefile()->GetBacktrace());
  60. return false;
  61. }
  62. if (te->GetType() == cmState::INTERFACE_LIBRARY) {
  63. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  64. }
  65. }
  66. this->GenerateExpectedTargetsCode(os, expectedTargets);
  67. }
  68. std::vector<std::string> missingTargets;
  69. // Create all the imported targets.
  70. for (std::vector<cmGeneratorTarget*>::const_iterator tei =
  71. this->Exports.begin();
  72. tei != this->Exports.end(); ++tei) {
  73. cmGeneratorTarget* gte = *tei;
  74. this->GenerateImportTargetCode(os, gte);
  75. gte->Target->AppendBuildInterfaceIncludes();
  76. ImportPropertyMap properties;
  77. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", gte,
  78. cmGeneratorExpression::BuildInterface,
  79. properties, missingTargets);
  80. this->PopulateInterfaceProperty("INTERFACE_SOURCES", gte,
  81. cmGeneratorExpression::BuildInterface,
  82. properties, missingTargets);
  83. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gte,
  84. cmGeneratorExpression::BuildInterface,
  85. properties, missingTargets);
  86. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gte,
  87. cmGeneratorExpression::BuildInterface,
  88. properties, missingTargets);
  89. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  90. cmGeneratorExpression::BuildInterface,
  91. properties, missingTargets);
  92. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  93. cmGeneratorExpression::BuildInterface,
  94. properties, missingTargets);
  95. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  96. properties);
  97. const bool newCMP0022Behavior =
  98. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  99. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  100. if (newCMP0022Behavior) {
  101. this->PopulateInterfaceLinkLibrariesProperty(
  102. gte, cmGeneratorExpression::BuildInterface, properties,
  103. missingTargets);
  104. }
  105. this->PopulateCompatibleInterfaceProperties(gte, properties);
  106. this->GenerateInterfaceProperties(gte, os, properties);
  107. }
  108. // Generate import file content for each configuration.
  109. for (std::vector<std::string>::const_iterator ci =
  110. this->Configurations.begin();
  111. ci != this->Configurations.end(); ++ci) {
  112. this->GenerateImportConfig(os, *ci, missingTargets);
  113. }
  114. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  115. return true;
  116. }
  117. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  118. std::ostream& os, const std::string& config, std::string const& suffix,
  119. std::vector<std::string>& missingTargets)
  120. {
  121. for (std::vector<cmGeneratorTarget*>::const_iterator tei =
  122. this->Exports.begin();
  123. tei != this->Exports.end(); ++tei) {
  124. // Collect import properties for this target.
  125. cmGeneratorTarget* target = *tei;
  126. ImportPropertyMap properties;
  127. if (target->GetType() != cmState::INTERFACE_LIBRARY) {
  128. this->SetImportLocationProperty(config, suffix, target, properties);
  129. }
  130. if (!properties.empty()) {
  131. // Get the rest of the target details.
  132. if (target->GetType() != cmState::INTERFACE_LIBRARY) {
  133. this->SetImportDetailProperties(config, suffix, target, properties,
  134. missingTargets);
  135. this->SetImportLinkInterface(config, suffix,
  136. cmGeneratorExpression::BuildInterface,
  137. target, properties, missingTargets);
  138. }
  139. // TOOD: PUBLIC_HEADER_LOCATION
  140. // This should wait until the build feature propagation stuff
  141. // is done. Then this can be a propagated include directory.
  142. // this->GenerateImportProperty(config, te->HeaderGenerator,
  143. // properties);
  144. // Generate code in the export file.
  145. this->GenerateImportPropertyCode(os, config, target, properties);
  146. }
  147. }
  148. }
  149. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  150. {
  151. this->ExportSet = exportSet;
  152. }
  153. void cmExportBuildFileGenerator::SetImportLocationProperty(
  154. const std::string& config, std::string const& suffix,
  155. cmGeneratorTarget* target, ImportPropertyMap& properties)
  156. {
  157. // Get the makefile in which to lookup target information.
  158. cmMakefile* mf = target->Makefile;
  159. // Add the main target file.
  160. {
  161. std::string prop = "IMPORTED_LOCATION";
  162. prop += suffix;
  163. std::string value;
  164. if (target->IsAppBundleOnApple()) {
  165. value = target->GetFullPath(config, false);
  166. } else {
  167. value = target->GetFullPath(config, false, true);
  168. }
  169. properties[prop] = value;
  170. }
  171. // Add the import library for windows DLLs.
  172. if (target->IsDLLPlatform() &&
  173. (target->GetType() == cmState::SHARED_LIBRARY ||
  174. target->IsExecutableWithExports()) &&
  175. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  176. std::string prop = "IMPORTED_IMPLIB";
  177. prop += suffix;
  178. std::string value = target->GetFullPath(config, true);
  179. target->GetImplibGNUtoMS(value, value, "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  180. properties[prop] = value;
  181. }
  182. }
  183. void cmExportBuildFileGenerator::HandleMissingTarget(
  184. std::string& link_libs, std::vector<std::string>& missingTargets,
  185. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  186. {
  187. // The target is not in the export.
  188. if (!this->AppendMode) {
  189. const std::string name = dependee->GetName();
  190. cmGlobalGenerator* gg =
  191. dependee->GetLocalGenerator()->GetGlobalGenerator();
  192. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  193. int targetOccurrences = (int)namespaces.size();
  194. if (targetOccurrences == 1) {
  195. std::string missingTarget = namespaces[0];
  196. missingTarget += dependee->GetExportName();
  197. link_libs += missingTarget;
  198. missingTargets.push_back(missingTarget);
  199. return;
  200. }
  201. // We are not appending, so all exported targets should be
  202. // known here. This is probably user-error.
  203. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  204. }
  205. // Assume the target will be exported by another command.
  206. // Append it with the export namespace.
  207. link_libs += this->Namespace;
  208. link_libs += dependee->GetExportName();
  209. }
  210. void cmExportBuildFileGenerator::GetTargets(
  211. std::vector<std::string>& targets) const
  212. {
  213. if (this->ExportSet) {
  214. for (std::vector<cmTargetExport*>::const_iterator tei =
  215. this->ExportSet->GetTargetExports()->begin();
  216. tei != this->ExportSet->GetTargetExports()->end(); ++tei) {
  217. targets.push_back((*tei)->TargetName);
  218. }
  219. return;
  220. }
  221. targets = this->Targets;
  222. }
  223. std::vector<std::string> cmExportBuildFileGenerator::FindNamespaces(
  224. cmGlobalGenerator* gg, const std::string& name)
  225. {
  226. std::vector<std::string> namespaces;
  227. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  228. gg->GetBuildExportSets();
  229. for (std::map<std::string, cmExportBuildFileGenerator*>::const_iterator
  230. expIt = exportSets.begin();
  231. expIt != exportSets.end(); ++expIt) {
  232. const cmExportBuildFileGenerator* exportSet = expIt->second;
  233. std::vector<std::string> targets;
  234. exportSet->GetTargets(targets);
  235. if (std::find(targets.begin(), targets.end(), name) != targets.end()) {
  236. namespaces.push_back(exportSet->GetNamespace());
  237. }
  238. }
  239. return namespaces;
  240. }
  241. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  242. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  243. {
  244. if (cmSystemTools::GetErrorOccuredFlag()) {
  245. return;
  246. }
  247. std::ostringstream e;
  248. e << "export called with target \"" << depender->GetName()
  249. << "\" which requires target \"" << dependee->GetName() << "\" ";
  250. if (occurrences == 0) {
  251. e << "that is not in the export set.\n";
  252. } else {
  253. e << "that is not in this export set, but " << occurrences
  254. << " times in others.\n";
  255. }
  256. e << "If the required target is not easy to reference in this call, "
  257. << "consider using the APPEND option with multiple separate calls.";
  258. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  259. cmake::FATAL_ERROR, e.str(), this->LG->GetMakefile()->GetBacktrace());
  260. }
  261. std::string cmExportBuildFileGenerator::InstallNameDir(
  262. cmGeneratorTarget* target, const std::string& config)
  263. {
  264. std::string install_name_dir;
  265. cmMakefile* mf = target->Target->GetMakefile();
  266. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  267. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  268. }
  269. return install_name_dir;
  270. }