cmExportBuildFileGenerator.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. auto const& exportInfo = this->FindExportInfo(dependee);
  126. if (exportInfo.Files.size() == 1) {
  127. std::string missingTarget = exportInfo.Namespace;
  128. missingTarget += dependee->GetExportName();
  129. link_libs += missingTarget;
  130. this->MissingTargets.emplace_back(std::move(missingTarget));
  131. return;
  132. }
  133. // We are not appending, so all exported targets should be
  134. // known here. This is probably user-error.
  135. this->ComplainAboutMissingTarget(depender, dependee, exportInfo.Files);
  136. }
  137. // Assume the target will be exported by another command.
  138. // Append it with the export namespace.
  139. link_libs += this->Namespace;
  140. link_libs += dependee->GetExportName();
  141. }
  142. void cmExportBuildFileGenerator::GetTargets(
  143. std::vector<TargetExport>& targets) const
  144. {
  145. if (this->ExportSet) {
  146. for (std::unique_ptr<cmTargetExport> const& te :
  147. this->ExportSet->GetTargetExports()) {
  148. if (te->NamelinkOnly) {
  149. continue;
  150. }
  151. targets.emplace_back(te->TargetName, te->XcFrameworkLocation);
  152. }
  153. return;
  154. }
  155. targets = this->Targets;
  156. }
  157. cmExportFileGenerator::ExportInfo cmExportBuildFileGenerator::FindExportInfo(
  158. cmGeneratorTarget const* target) const
  159. {
  160. std::vector<std::string> exportFiles;
  161. std::string ns;
  162. auto const& name = target->GetName();
  163. auto& exportSets =
  164. target->GetLocalGenerator()->GetGlobalGenerator()->GetBuildExportSets();
  165. for (auto const& exp : exportSets) {
  166. auto const& exportSet = exp.second;
  167. std::vector<TargetExport> targets;
  168. exportSet->GetTargets(targets);
  169. if (std::any_of(
  170. targets.begin(), targets.end(),
  171. [&name](TargetExport const& te) { return te.Name == name; })) {
  172. exportFiles.push_back(exp.first);
  173. ns = exportSet->GetNamespace();
  174. }
  175. }
  176. return { exportFiles, exportFiles.size() == 1 ? ns : std::string{} };
  177. }
  178. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  179. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  180. std::vector<std::string> const& exportFiles) const
  181. {
  182. std::ostringstream e;
  183. e << "export called with target \"" << depender->GetName()
  184. << "\" which requires target \"" << dependee->GetName() << "\" ";
  185. if (exportFiles.empty()) {
  186. e << "that is not in any export set.";
  187. } else {
  188. e << "that is not in this export set, but in multiple other export sets: "
  189. << cmJoin(exportFiles, ", ") << ".\n";
  190. e << "An exported target cannot depend upon another target which is "
  191. "exported multiple times. Consider consolidating the exports of the "
  192. "\""
  193. << dependee->GetName() << "\" target to a single export.";
  194. }
  195. this->ReportError(e.str());
  196. }
  197. void cmExportBuildFileGenerator::ComplainAboutDuplicateTarget(
  198. std::string const& targetName) const
  199. {
  200. std::ostringstream e;
  201. e << "given target \"" << targetName << "\" more than once.";
  202. this->ReportError(e.str());
  203. }
  204. void cmExportBuildFileGenerator::ReportError(
  205. std::string const& errorMessage) const
  206. {
  207. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  208. MessageType::FATAL_ERROR, errorMessage,
  209. this->LG->GetMakefile()->GetBacktrace());
  210. }
  211. std::string cmExportBuildFileGenerator::InstallNameDir(
  212. cmGeneratorTarget const* target, std::string const& config)
  213. {
  214. std::string install_name_dir;
  215. cmMakefile* mf = target->Target->GetMakefile();
  216. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  217. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  218. }
  219. return install_name_dir;
  220. }
  221. bool cmExportBuildFileGenerator::PopulateInterfaceProperties(
  222. cmGeneratorTarget const* target, ImportPropertyMap& properties)
  223. {
  224. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", target,
  225. cmGeneratorExpression::BuildInterface,
  226. properties);
  227. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", target,
  228. cmGeneratorExpression::BuildInterface,
  229. properties);
  230. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", target,
  231. cmGeneratorExpression::BuildInterface,
  232. properties);
  233. this->PopulateInterfaceProperty("INTERFACE_SOURCES", target,
  234. cmGeneratorExpression::BuildInterface,
  235. properties);
  236. return this->PopulateInterfaceProperties(
  237. target, {}, cmGeneratorExpression::BuildInterface, properties);
  238. }