cmExportBuildFileGenerator.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmExportBuildFileGenerator.h"
  4. #include <algorithm>
  5. #include <map>
  6. #include <memory>
  7. #include <set>
  8. #include <sstream>
  9. #include <utility>
  10. #include "cmExportSet.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmList.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmTarget.h"
  20. #include "cmTargetExport.h"
  21. #include "cmValue.h"
  22. #include "cmake.h"
  23. class cmSourceFile;
  24. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  25. {
  26. this->LG = nullptr;
  27. this->ExportSet = nullptr;
  28. }
  29. void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg)
  30. {
  31. this->LG = lg;
  32. if (this->ExportSet) {
  33. this->ExportSet->Compute(lg);
  34. }
  35. }
  36. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  37. cmGeneratorTarget const* target) const
  38. {
  39. cmStateEnums::TargetType targetType = target->GetType();
  40. // An object library exports as an interface library if we cannot
  41. // tell clients where to find the objects. This is sufficient
  42. // to support transitive usage requirements on other targets that
  43. // use the object library.
  44. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  45. !target->Target->HasKnownObjectFileLocation(nullptr)) {
  46. targetType = cmStateEnums::INTERFACE_LIBRARY;
  47. }
  48. return targetType;
  49. }
  50. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  51. {
  52. this->ExportSet = exportSet;
  53. }
  54. void cmExportBuildFileGenerator::SetImportLocationProperty(
  55. std::string const& config, std::string const& suffix,
  56. cmGeneratorTarget* target, ImportPropertyMap& properties)
  57. {
  58. // Get the makefile in which to lookup target information.
  59. cmMakefile* mf = target->Makefile;
  60. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  61. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  62. // Compute all the object files inside this target and setup
  63. // IMPORTED_OBJECTS as a list of object files
  64. std::vector<cmSourceFile const*> objectSources;
  65. target->GetObjectSources(objectSources, config);
  66. std::string const obj_dir = target->GetObjectDirectory(config);
  67. std::vector<std::string> objects;
  68. for (cmSourceFile const* sf : objectSources) {
  69. std::string const& obj = target->GetObjectName(sf);
  70. objects.push_back(obj_dir + obj);
  71. }
  72. // Store the property.
  73. properties[prop] = cmList::to_string(objects);
  74. } else {
  75. // Add the main target file.
  76. {
  77. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  78. std::string value;
  79. if (target->IsAppBundleOnApple()) {
  80. value =
  81. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  82. } else {
  83. value = target->GetFullPath(config,
  84. cmStateEnums::RuntimeBinaryArtifact, true);
  85. }
  86. properties[prop] = value;
  87. }
  88. // Add the import library for windows DLLs.
  89. if (target->HasImportLibrary(config)) {
  90. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  91. std::string value =
  92. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact, true);
  93. if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  94. target->GetImplibGNUtoMS(config, value, value,
  95. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  96. }
  97. properties[prop] = value;
  98. }
  99. }
  100. }
  101. bool cmExportBuildFileGenerator::CollectExports(
  102. std::function<void(cmGeneratorTarget const*)> visitor)
  103. {
  104. auto pred = [&](cmExportBuildFileGenerator::TargetExport& tei) -> bool {
  105. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(tei.Name);
  106. if (this->ExportedTargets.insert(te).second) {
  107. this->Exports.emplace_back(te, tei.XcFrameworkLocation);
  108. visitor(te);
  109. return true;
  110. }
  111. this->ComplainAboutDuplicateTarget(te->GetName());
  112. return false;
  113. };
  114. std::vector<TargetExport> targets;
  115. this->GetTargets(targets);
  116. return std::all_of(targets.begin(), targets.end(), pred);
  117. }
  118. void cmExportBuildFileGenerator::HandleMissingTarget(
  119. std::string& link_libs, cmGeneratorTarget const* depender,
  120. cmGeneratorTarget* dependee)
  121. {
  122. // The target is not in the export.
  123. if (!this->AppendMode) {
  124. auto const& exportInfo = this->FindExportInfo(dependee);
  125. if (exportInfo.Namespaces.size() == 1 && exportInfo.Sets.size() == 1) {
  126. std::string missingTarget = *exportInfo.Namespaces.begin();
  127. missingTarget += dependee->GetExportName();
  128. link_libs += missingTarget;
  129. this->MissingTargets.emplace_back(std::move(missingTarget));
  130. return;
  131. }
  132. // We are not appending, so all exported targets should be
  133. // known here. This is probably user-error.
  134. this->ComplainAboutMissingTarget(depender, dependee, exportInfo);
  135. }
  136. // Assume the target will be exported by another command.
  137. // Append it with the export namespace.
  138. link_libs += this->Namespace;
  139. link_libs += dependee->GetExportName();
  140. }
  141. void cmExportBuildFileGenerator::GetTargets(
  142. std::vector<TargetExport>& targets) const
  143. {
  144. if (this->ExportSet) {
  145. for (std::unique_ptr<cmTargetExport> const& te :
  146. this->ExportSet->GetTargetExports()) {
  147. if (te->NamelinkOnly) {
  148. continue;
  149. }
  150. targets.emplace_back(te->TargetName, te->XcFrameworkLocation);
  151. }
  152. return;
  153. }
  154. targets = this->Targets;
  155. }
  156. cmExportFileGenerator::ExportInfo cmExportBuildFileGenerator::FindExportInfo(
  157. cmGeneratorTarget const* target) const
  158. {
  159. std::vector<std::string> exportFiles;
  160. std::set<std::string> exportSets;
  161. std::set<std::string> namespaces;
  162. auto const& name = target->GetName();
  163. auto& allExportSets =
  164. target->GetLocalGenerator()->GetGlobalGenerator()->GetBuildExportSets();
  165. for (auto const& exp : allExportSets) {
  166. cmExportBuildFileGenerator const* const bfg = exp.second;
  167. cmExportSet const* const exportSet = bfg->GetExportSet();
  168. std::vector<TargetExport> targets;
  169. bfg->GetTargets(targets);
  170. if (std::any_of(
  171. targets.begin(), targets.end(),
  172. [&name](TargetExport const& te) { return te.Name == name; })) {
  173. if (exportSet) {
  174. exportSets.insert(exportSet->GetName());
  175. } else {
  176. exportSets.insert(exp.first);
  177. }
  178. exportFiles.push_back(exp.first);
  179. namespaces.insert(bfg->GetNamespace());
  180. }
  181. }
  182. return { exportFiles, exportSets, namespaces };
  183. }
  184. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  185. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  186. ExportInfo const& exportInfo) const
  187. {
  188. std::ostringstream e;
  189. e << "export called with target \"" << depender->GetName()
  190. << "\" which requires target \"" << dependee->GetName() << "\" ";
  191. if (exportInfo.Sets.empty()) {
  192. e << "that is not in any export set.";
  193. } else {
  194. if (exportInfo.Sets.size() == 1) {
  195. e << "that is not in this export set, but in another export set which "
  196. "is "
  197. "exported multiple times with different namespaces: ";
  198. } else {
  199. e << "that is not in this export set, but in multiple other export "
  200. "sets: ";
  201. }
  202. e << cmJoin(exportInfo.Files, ", ") << ".\n"
  203. << "An exported target cannot depend upon another target which is "
  204. "exported in more than one export set or with more than one "
  205. "namespace. Consider consolidating the exports of the \""
  206. << dependee->GetName() << "\" target to a single export.";
  207. }
  208. this->ReportError(e.str());
  209. }
  210. void cmExportBuildFileGenerator::ComplainAboutDuplicateTarget(
  211. std::string const& targetName) const
  212. {
  213. std::ostringstream e;
  214. e << "given target \"" << targetName << "\" more than once.";
  215. this->ReportError(e.str());
  216. }
  217. void cmExportBuildFileGenerator::IssueMessage(MessageType type,
  218. std::string const& message) const
  219. {
  220. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  221. type, message, this->LG->GetMakefile()->GetBacktrace());
  222. }
  223. std::string cmExportBuildFileGenerator::InstallNameDir(
  224. cmGeneratorTarget const* target, std::string const& config)
  225. {
  226. std::string install_name_dir;
  227. cmMakefile* mf = target->Target->GetMakefile();
  228. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  229. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  230. }
  231. return install_name_dir;
  232. }
  233. bool cmExportBuildFileGenerator::PopulateInterfaceProperties(
  234. cmGeneratorTarget const* target, ImportPropertyMap& properties)
  235. {
  236. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", target,
  237. cmGeneratorExpression::BuildInterface,
  238. properties);
  239. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", target,
  240. cmGeneratorExpression::BuildInterface,
  241. properties);
  242. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", target,
  243. cmGeneratorExpression::BuildInterface,
  244. properties);
  245. this->PopulateInterfaceProperty("INTERFACE_SOURCES", target,
  246. cmGeneratorExpression::BuildInterface,
  247. properties);
  248. return this->PopulateInterfaceProperties(
  249. target, {}, cmGeneratorExpression::BuildInterface, properties);
  250. }