cmExportBuildFileGenerator.cxx 13 KB

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