1
0

cmExportBuildFileGenerator.cxx 8.9 KB

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