cmExportBuildFileGenerator.cxx 10 KB

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