cmExportBuildFileGenerator.cxx 12 KB

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