cmExportBuildFileGenerator.cxx 13 KB

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