cmExportBuildFileGenerator.cxx 13 KB

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