cmExportBuildFileGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <map>
  18. #include <memory>
  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_PRECOMPILE_HEADERS", gte,
  84. cmGeneratorExpression::BuildInterface,
  85. properties, missingTargets);
  86. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  87. cmGeneratorExpression::BuildInterface,
  88. properties, missingTargets);
  89. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  90. cmGeneratorExpression::BuildInterface,
  91. properties, missingTargets);
  92. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  93. cmGeneratorExpression::BuildInterface,
  94. properties, missingTargets);
  95. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  96. cmGeneratorExpression::BuildInterface,
  97. properties, missingTargets);
  98. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  99. cmGeneratorExpression::BuildInterface,
  100. properties, missingTargets);
  101. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  102. properties);
  103. std::string errorMessage;
  104. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  105. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  106. MessageType::FATAL_ERROR, errorMessage,
  107. this->LG->GetMakefile()->GetBacktrace());
  108. return false;
  109. }
  110. const bool newCMP0022Behavior =
  111. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  112. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  113. if (newCMP0022Behavior) {
  114. this->PopulateInterfaceLinkLibrariesProperty(
  115. gte, cmGeneratorExpression::BuildInterface, properties,
  116. missingTargets);
  117. }
  118. this->PopulateCompatibleInterfaceProperties(gte, properties);
  119. this->GenerateInterfaceProperties(gte, os, properties);
  120. }
  121. // Generate import file content for each configuration.
  122. for (std::string const& c : this->Configurations) {
  123. this->GenerateImportConfig(os, c, missingTargets);
  124. }
  125. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  126. return true;
  127. }
  128. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  129. std::ostream& os, const std::string& config, std::string const& suffix,
  130. std::vector<std::string>& missingTargets)
  131. {
  132. for (cmGeneratorTarget* target : this->Exports) {
  133. // Collect import properties for this target.
  134. ImportPropertyMap properties;
  135. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  136. this->SetImportLocationProperty(config, suffix, target, properties);
  137. }
  138. if (!properties.empty()) {
  139. // Get the rest of the target details.
  140. if (this->GetExportTargetType(target) !=
  141. cmStateEnums::INTERFACE_LIBRARY) {
  142. this->SetImportDetailProperties(config, suffix, target, properties,
  143. missingTargets);
  144. this->SetImportLinkInterface(config, suffix,
  145. cmGeneratorExpression::BuildInterface,
  146. target, properties, missingTargets);
  147. }
  148. // TODO: PUBLIC_HEADER_LOCATION
  149. // This should wait until the build feature propagation stuff
  150. // is done. Then this can be a propagated include directory.
  151. // this->GenerateImportProperty(config, te->HeaderGenerator,
  152. // properties);
  153. // Generate code in the export file.
  154. this->GenerateImportPropertyCode(os, config, target, properties);
  155. }
  156. }
  157. }
  158. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  159. cmGeneratorTarget const* target) const
  160. {
  161. cmStateEnums::TargetType targetType = target->GetType();
  162. // An object library exports as an interface library if we cannot
  163. // tell clients where to find the objects. This is sufficient
  164. // to support transitive usage requirements on other targets that
  165. // use the object library.
  166. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  167. !this->LG->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  168. targetType = cmStateEnums::INTERFACE_LIBRARY;
  169. }
  170. return targetType;
  171. }
  172. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  173. {
  174. this->ExportSet = exportSet;
  175. }
  176. void cmExportBuildFileGenerator::SetImportLocationProperty(
  177. const std::string& config, std::string const& suffix,
  178. cmGeneratorTarget* target, ImportPropertyMap& properties)
  179. {
  180. // Get the makefile in which to lookup target information.
  181. cmMakefile* mf = target->Makefile;
  182. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  183. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  184. // Compute all the object files inside this target and setup
  185. // IMPORTED_OBJECTS as a list of object files
  186. std::vector<cmSourceFile const*> objectSources;
  187. target->GetObjectSources(objectSources, config);
  188. std::string const obj_dir = target->GetObjectDirectory(config);
  189. std::vector<std::string> objects;
  190. for (cmSourceFile const* sf : objectSources) {
  191. const std::string& obj = target->GetObjectName(sf);
  192. objects.push_back(obj_dir + obj);
  193. }
  194. // Store the property.
  195. properties[prop] = cmJoin(objects, ";");
  196. } else {
  197. // Add the main target file.
  198. {
  199. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  200. std::string value;
  201. if (target->IsAppBundleOnApple()) {
  202. value =
  203. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  204. } else {
  205. value = target->GetFullPath(config,
  206. cmStateEnums::RuntimeBinaryArtifact, true);
  207. }
  208. properties[prop] = value;
  209. }
  210. // Add the import library for windows DLLs.
  211. if (target->HasImportLibrary(config)) {
  212. std::string prop = cmStrCat("IMPORTED_IMPLIB", 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 (std::unique_ptr<cmTargetExport> const& te :
  255. this->ExportSet->GetTargetExports()) {
  256. targets.push_back(te->TargetName);
  257. }
  258. return;
  259. }
  260. targets = this->Targets;
  261. }
  262. std::pair<std::vector<std::string>, std::string>
  263. cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
  264. const std::string& name)
  265. {
  266. std::vector<std::string> exportFiles;
  267. std::string ns;
  268. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  269. gg->GetBuildExportSets();
  270. for (auto const& exp : exportSets) {
  271. const cmExportBuildFileGenerator* exportSet = exp.second;
  272. std::vector<std::string> targets;
  273. exportSet->GetTargets(targets);
  274. if (cmContains(targets, name)) {
  275. exportFiles.push_back(exp.first);
  276. ns = exportSet->GetNamespace();
  277. }
  278. }
  279. return { exportFiles, ns };
  280. }
  281. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  282. cmGeneratorTarget* depender, cmGeneratorTarget* dependee,
  283. std::vector<std::string> const& exportFiles)
  284. {
  285. std::ostringstream e;
  286. e << "export called with target \"" << depender->GetName()
  287. << "\" which requires target \"" << dependee->GetName() << "\" ";
  288. if (exportFiles.empty()) {
  289. e << "that is not in any export set.";
  290. } else {
  291. e << "that is not in this export set, but in multiple other export sets: "
  292. << cmJoin(exportFiles, ", ") << ".\n";
  293. e << "An exported target cannot depend upon another target which is "
  294. "exported multiple times. Consider consolidating the exports of the "
  295. "\""
  296. << dependee->GetName() << "\" target to a single export.";
  297. }
  298. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  299. MessageType::FATAL_ERROR, e.str(),
  300. this->LG->GetMakefile()->GetBacktrace());
  301. }
  302. std::string cmExportBuildFileGenerator::InstallNameDir(
  303. cmGeneratorTarget* target, const std::string& config)
  304. {
  305. std::string install_name_dir;
  306. cmMakefile* mf = target->Target->GetMakefile();
  307. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  308. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  309. }
  310. return install_name_dir;
  311. }