cmExportBuildFileGenerator.cxx 12 KB

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