cmExportBuildFileGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <map>
  5. #include <memory>
  6. #include <set>
  7. #include <sstream>
  8. #include <utility>
  9. #include "cmAlgorithms.h"
  10. #include "cmExportSet.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmMessageType.h"
  17. #include "cmPolicies.h"
  18. #include "cmStateTypes.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmTarget.h"
  21. #include "cmTargetExport.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. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  37. {
  38. {
  39. std::string expectedTargets;
  40. std::string sep;
  41. std::vector<std::string> targets;
  42. bool generatedInterfaceRequired = false;
  43. this->GetTargets(targets);
  44. for (std::string const& tei : targets) {
  45. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(tei);
  46. expectedTargets += sep + this->Namespace + te->GetExportName();
  47. sep = " ";
  48. if (this->ExportedTargets.insert(te).second) {
  49. this->Exports.push_back(te);
  50. } else {
  51. std::ostringstream e;
  52. e << "given target \"" << te->GetName() << "\" more than once.";
  53. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  54. MessageType::FATAL_ERROR, e.str(),
  55. this->LG->GetMakefile()->GetBacktrace());
  56. return false;
  57. }
  58. generatedInterfaceRequired |=
  59. this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY;
  60. }
  61. if (generatedInterfaceRequired) {
  62. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  63. }
  64. this->GenerateExpectedTargetsCode(os, expectedTargets);
  65. }
  66. std::vector<std::string> missingTargets;
  67. // Create all the imported targets.
  68. for (cmGeneratorTarget* gte : this->Exports) {
  69. this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
  70. gte->Target->AppendBuildInterfaceIncludes();
  71. ImportPropertyMap properties;
  72. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", gte,
  73. cmGeneratorExpression::BuildInterface,
  74. properties, missingTargets);
  75. this->PopulateInterfaceProperty("INTERFACE_SOURCES", gte,
  76. cmGeneratorExpression::BuildInterface,
  77. properties, missingTargets);
  78. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gte,
  79. cmGeneratorExpression::BuildInterface,
  80. properties, missingTargets);
  81. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gte,
  82. cmGeneratorExpression::BuildInterface,
  83. properties, missingTargets);
  84. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", gte,
  85. cmGeneratorExpression::BuildInterface,
  86. properties, missingTargets);
  87. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  88. cmGeneratorExpression::BuildInterface,
  89. properties, missingTargets);
  90. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  91. cmGeneratorExpression::BuildInterface,
  92. properties, missingTargets);
  93. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  94. cmGeneratorExpression::BuildInterface,
  95. properties, missingTargets);
  96. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  97. cmGeneratorExpression::BuildInterface,
  98. properties, missingTargets);
  99. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  100. cmGeneratorExpression::BuildInterface,
  101. properties, missingTargets);
  102. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  103. properties);
  104. std::string errorMessage;
  105. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  106. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  107. MessageType::FATAL_ERROR, errorMessage,
  108. this->LG->GetMakefile()->GetBacktrace());
  109. return false;
  110. }
  111. const bool newCMP0022Behavior =
  112. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  113. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  114. if (newCMP0022Behavior) {
  115. this->PopulateInterfaceLinkLibrariesProperty(
  116. gte, cmGeneratorExpression::BuildInterface, properties,
  117. missingTargets);
  118. }
  119. this->PopulateCompatibleInterfaceProperties(gte, properties);
  120. this->GenerateInterfaceProperties(gte, os, properties);
  121. }
  122. // Generate import file content for each configuration.
  123. for (std::string const& c : this->Configurations) {
  124. this->GenerateImportConfig(os, c, missingTargets);
  125. }
  126. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  127. return true;
  128. }
  129. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  130. std::ostream& os, const std::string& config, std::string const& suffix,
  131. std::vector<std::string>& missingTargets)
  132. {
  133. for (cmGeneratorTarget* target : this->Exports) {
  134. // Collect import properties for this target.
  135. ImportPropertyMap properties;
  136. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  137. this->SetImportLocationProperty(config, suffix, target, properties);
  138. }
  139. if (!properties.empty()) {
  140. // Get the rest of the target details.
  141. if (this->GetExportTargetType(target) !=
  142. cmStateEnums::INTERFACE_LIBRARY) {
  143. this->SetImportDetailProperties(config, suffix, target, properties,
  144. missingTargets);
  145. this->SetImportLinkInterface(config, suffix,
  146. cmGeneratorExpression::BuildInterface,
  147. target, properties, missingTargets);
  148. }
  149. // TODO: PUBLIC_HEADER_LOCATION
  150. // This should wait until the build feature propagation stuff
  151. // is done. Then this can be a propagated include directory.
  152. // this->GenerateImportProperty(config, te->HeaderGenerator,
  153. // properties);
  154. // Generate code in the export file.
  155. this->GenerateImportPropertyCode(os, config, target, properties);
  156. }
  157. }
  158. }
  159. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  160. cmGeneratorTarget const* target) const
  161. {
  162. cmStateEnums::TargetType targetType = target->GetType();
  163. // An object library exports as an interface library if we cannot
  164. // tell clients where to find the objects. This is sufficient
  165. // to support transitive usage requirements on other targets that
  166. // use the object library.
  167. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  168. !this->LG->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  169. targetType = cmStateEnums::INTERFACE_LIBRARY;
  170. }
  171. return targetType;
  172. }
  173. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  174. {
  175. this->ExportSet = exportSet;
  176. }
  177. void cmExportBuildFileGenerator::SetImportLocationProperty(
  178. const std::string& config, std::string const& suffix,
  179. cmGeneratorTarget* target, ImportPropertyMap& properties)
  180. {
  181. // Get the makefile in which to lookup target information.
  182. cmMakefile* mf = target->Makefile;
  183. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  184. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  185. // Compute all the object files inside this target and setup
  186. // IMPORTED_OBJECTS as a list of object files
  187. std::vector<cmSourceFile const*> objectSources;
  188. target->GetObjectSources(objectSources, config);
  189. std::string const obj_dir = target->GetObjectDirectory(config);
  190. std::vector<std::string> objects;
  191. for (cmSourceFile const* sf : objectSources) {
  192. const std::string& obj = target->GetObjectName(sf);
  193. objects.push_back(obj_dir + obj);
  194. }
  195. // Store the property.
  196. properties[prop] = cmJoin(objects, ";");
  197. } else {
  198. // Add the main target file.
  199. {
  200. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  201. std::string value;
  202. if (target->IsAppBundleOnApple()) {
  203. value =
  204. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  205. } else {
  206. value = target->GetFullPath(config,
  207. cmStateEnums::RuntimeBinaryArtifact, true);
  208. }
  209. properties[prop] = value;
  210. }
  211. // Add the import library for windows DLLs.
  212. if (target->HasImportLibrary(config)) {
  213. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  214. std::string value =
  215. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact);
  216. if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  217. target->GetImplibGNUtoMS(config, value, value,
  218. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  219. }
  220. properties[prop] = value;
  221. }
  222. }
  223. }
  224. void cmExportBuildFileGenerator::HandleMissingTarget(
  225. std::string& link_libs, std::vector<std::string>& missingTargets,
  226. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  227. {
  228. // The target is not in the export.
  229. if (!this->AppendMode) {
  230. const std::string name = dependee->GetName();
  231. cmGlobalGenerator* gg =
  232. dependee->GetLocalGenerator()->GetGlobalGenerator();
  233. auto exportInfo = this->FindBuildExportInfo(gg, name);
  234. std::vector<std::string> const& exportFiles = exportInfo.first;
  235. if (exportFiles.size() == 1) {
  236. std::string missingTarget = exportInfo.second;
  237. missingTarget += dependee->GetExportName();
  238. link_libs += missingTarget;
  239. missingTargets.push_back(std::move(missingTarget));
  240. return;
  241. }
  242. // We are not appending, so all exported targets should be
  243. // known here. This is probably user-error.
  244. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  245. }
  246. // Assume the target will be exported by another command.
  247. // Append it with the export namespace.
  248. link_libs += this->Namespace;
  249. link_libs += dependee->GetExportName();
  250. }
  251. void cmExportBuildFileGenerator::GetTargets(
  252. std::vector<std::string>& targets) const
  253. {
  254. if (this->ExportSet) {
  255. for (std::unique_ptr<cmTargetExport> const& te :
  256. this->ExportSet->GetTargetExports()) {
  257. targets.push_back(te->TargetName);
  258. }
  259. return;
  260. }
  261. targets = this->Targets;
  262. }
  263. std::pair<std::vector<std::string>, std::string>
  264. cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
  265. const std::string& name)
  266. {
  267. std::vector<std::string> exportFiles;
  268. std::string ns;
  269. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  270. gg->GetBuildExportSets();
  271. for (auto const& exp : exportSets) {
  272. const cmExportBuildFileGenerator* exportSet = exp.second;
  273. std::vector<std::string> targets;
  274. exportSet->GetTargets(targets);
  275. if (cmContains(targets, name)) {
  276. exportFiles.push_back(exp.first);
  277. ns = exportSet->GetNamespace();
  278. }
  279. }
  280. return { exportFiles, ns };
  281. }
  282. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  283. cmGeneratorTarget* depender, cmGeneratorTarget* dependee,
  284. std::vector<std::string> const& exportFiles)
  285. {
  286. std::ostringstream e;
  287. e << "export called with target \"" << depender->GetName()
  288. << "\" which requires target \"" << dependee->GetName() << "\" ";
  289. if (exportFiles.empty()) {
  290. e << "that is not in any export set.";
  291. } else {
  292. e << "that is not in this export set, but in multiple other export sets: "
  293. << cmJoin(exportFiles, ", ") << ".\n";
  294. e << "An exported target cannot depend upon another target which is "
  295. "exported multiple times. Consider consolidating the exports of the "
  296. "\""
  297. << dependee->GetName() << "\" target to a single export.";
  298. }
  299. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  300. MessageType::FATAL_ERROR, e.str(),
  301. this->LG->GetMakefile()->GetBacktrace());
  302. }
  303. std::string cmExportBuildFileGenerator::InstallNameDir(
  304. cmGeneratorTarget* target, const std::string& config)
  305. {
  306. std::string install_name_dir;
  307. cmMakefile* mf = target->Target->GetMakefile();
  308. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  309. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  310. }
  311. return install_name_dir;
  312. }