cmExportBuildFileGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "cmAlgorithms.h"
  5. #include "cmExportSet.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmPolicies.h"
  13. #include "cmStateTypes.h"
  14. #include "cmSystemTools.h"
  15. #include "cmTarget.h"
  16. #include "cmTargetExport.h"
  17. #include "cmake.h"
  18. #include <algorithm>
  19. #include <map>
  20. #include <set>
  21. #include <sstream>
  22. #include <utility>
  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_AUTOUIC_OPTIONS", gte,
  85. cmGeneratorExpression::BuildInterface,
  86. properties, missingTargets);
  87. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  88. cmGeneratorExpression::BuildInterface,
  89. properties, missingTargets);
  90. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  91. cmGeneratorExpression::BuildInterface,
  92. properties, missingTargets);
  93. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  94. cmGeneratorExpression::BuildInterface,
  95. properties, missingTargets);
  96. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  97. cmGeneratorExpression::BuildInterface,
  98. properties, missingTargets);
  99. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  100. properties);
  101. std::string errorMessage;
  102. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  103. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  104. MessageType::FATAL_ERROR, errorMessage,
  105. this->LG->GetMakefile()->GetBacktrace());
  106. return false;
  107. }
  108. const bool newCMP0022Behavior =
  109. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  110. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  111. if (newCMP0022Behavior) {
  112. this->PopulateInterfaceLinkLibrariesProperty(
  113. gte, cmGeneratorExpression::BuildInterface, properties,
  114. missingTargets);
  115. }
  116. this->PopulateCompatibleInterfaceProperties(gte, properties);
  117. this->GenerateInterfaceProperties(gte, os, properties);
  118. }
  119. // Generate import file content for each configuration.
  120. for (std::string const& c : this->Configurations) {
  121. this->GenerateImportConfig(os, c, missingTargets);
  122. }
  123. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  124. return true;
  125. }
  126. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  127. std::ostream& os, const std::string& config, std::string const& suffix,
  128. std::vector<std::string>& missingTargets)
  129. {
  130. for (cmGeneratorTarget* target : this->Exports) {
  131. // Collect import properties for this target.
  132. ImportPropertyMap properties;
  133. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  134. this->SetImportLocationProperty(config, suffix, target, properties);
  135. }
  136. if (!properties.empty()) {
  137. // Get the rest of the target details.
  138. if (this->GetExportTargetType(target) !=
  139. cmStateEnums::INTERFACE_LIBRARY) {
  140. this->SetImportDetailProperties(config, suffix, target, properties,
  141. missingTargets);
  142. this->SetImportLinkInterface(config, suffix,
  143. cmGeneratorExpression::BuildInterface,
  144. target, properties, missingTargets);
  145. }
  146. // TODO: PUBLIC_HEADER_LOCATION
  147. // This should wait until the build feature propagation stuff
  148. // is done. Then this can be a propagated include directory.
  149. // this->GenerateImportProperty(config, te->HeaderGenerator,
  150. // properties);
  151. // Generate code in the export file.
  152. this->GenerateImportPropertyCode(os, config, target, properties);
  153. }
  154. }
  155. }
  156. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  157. cmGeneratorTarget const* target) const
  158. {
  159. cmStateEnums::TargetType targetType = target->GetType();
  160. // An object library exports as an interface library if we cannot
  161. // tell clients where to find the objects. This is sufficient
  162. // to support transitive usage requirements on other targets that
  163. // use the object library.
  164. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  165. !this->LG->GetGlobalGenerator()->HasKnownObjectFileLocation(nullptr)) {
  166. targetType = cmStateEnums::INTERFACE_LIBRARY;
  167. }
  168. return targetType;
  169. }
  170. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  171. {
  172. this->ExportSet = exportSet;
  173. }
  174. void cmExportBuildFileGenerator::SetImportLocationProperty(
  175. const std::string& config, std::string const& suffix,
  176. cmGeneratorTarget* target, ImportPropertyMap& properties)
  177. {
  178. // Get the makefile in which to lookup target information.
  179. cmMakefile* mf = target->Makefile;
  180. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  181. std::string prop = "IMPORTED_OBJECTS";
  182. prop += 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 = "IMPORTED_LOCATION";
  199. prop += 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. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  213. std::string prop = "IMPORTED_IMPLIB";
  214. prop += suffix;
  215. std::string value =
  216. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact);
  217. target->GetImplibGNUtoMS(config, value, value,
  218. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  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. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  233. int targetOccurrences = static_cast<int>(namespaces.size());
  234. if (targetOccurrences == 1) {
  235. std::string missingTarget = namespaces[0];
  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, targetOccurrences);
  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::vector<std::string> cmExportBuildFileGenerator::FindNamespaces(
  262. cmGlobalGenerator* gg, const std::string& name)
  263. {
  264. std::vector<std::string> namespaces;
  265. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  266. gg->GetBuildExportSets();
  267. for (auto const& exp : exportSets) {
  268. const cmExportBuildFileGenerator* exportSet = exp.second;
  269. std::vector<std::string> targets;
  270. exportSet->GetTargets(targets);
  271. if (std::find(targets.begin(), targets.end(), name) != targets.end()) {
  272. namespaces.push_back(exportSet->GetNamespace());
  273. }
  274. }
  275. return namespaces;
  276. }
  277. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  278. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  279. {
  280. if (cmSystemTools::GetErrorOccuredFlag()) {
  281. return;
  282. }
  283. std::ostringstream e;
  284. e << "export called with target \"" << depender->GetName()
  285. << "\" which requires target \"" << dependee->GetName() << "\" ";
  286. if (occurrences == 0) {
  287. e << "that is not in the export set.\n";
  288. } else {
  289. e << "that is not in this export set, but " << occurrences
  290. << " times in others.\n";
  291. }
  292. e << "If the required target is not easy to reference in this call, "
  293. << "consider using the APPEND option with multiple separate calls.";
  294. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  295. MessageType::FATAL_ERROR, e.str(),
  296. this->LG->GetMakefile()->GetBacktrace());
  297. }
  298. std::string cmExportBuildFileGenerator::InstallNameDir(
  299. cmGeneratorTarget* target, const std::string& config)
  300. {
  301. std::string install_name_dir;
  302. cmMakefile* mf = target->Target->GetMakefile();
  303. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  304. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  305. }
  306. return install_name_dir;
  307. }