cmExportBuildFileGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "cmPolicies.h"
  11. #include "cmStateTypes.h"
  12. #include "cmSystemTools.h"
  13. #include "cmTarget.h"
  14. #include "cmTargetExport.h"
  15. #include "cmake.h"
  16. #include <algorithm>
  17. #include <map>
  18. #include <set>
  19. #include <sstream>
  20. #include <utility>
  21. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  22. {
  23. this->LG = CM_NULLPTR;
  24. this->ExportSet = CM_NULLPTR;
  25. }
  26. void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg)
  27. {
  28. this->LG = lg;
  29. if (this->ExportSet) {
  30. this->ExportSet->Compute(lg);
  31. }
  32. }
  33. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  34. {
  35. {
  36. std::string expectedTargets;
  37. std::string sep;
  38. std::vector<std::string> targets;
  39. this->GetTargets(targets);
  40. for (std::vector<std::string>::const_iterator tei = targets.begin();
  41. tei != targets.end(); ++tei) {
  42. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(*tei);
  43. expectedTargets += sep + this->Namespace + te->GetExportName();
  44. sep = " ";
  45. if (this->ExportedTargets.insert(te).second) {
  46. this->Exports.push_back(te);
  47. } else {
  48. std::ostringstream e;
  49. e << "given target \"" << te->GetName() << "\" more than once.";
  50. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  51. cmake::FATAL_ERROR, e.str(),
  52. this->LG->GetMakefile()->GetBacktrace());
  53. return false;
  54. }
  55. if (te->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  56. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  57. }
  58. }
  59. this->GenerateExpectedTargetsCode(os, expectedTargets);
  60. }
  61. std::vector<std::string> missingTargets;
  62. // Create all the imported targets.
  63. for (std::vector<cmGeneratorTarget*>::const_iterator tei =
  64. this->Exports.begin();
  65. tei != this->Exports.end(); ++tei) {
  66. cmGeneratorTarget* gte = *tei;
  67. this->GenerateImportTargetCode(os, 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_AUTOUIC_OPTIONS", gte,
  83. cmGeneratorExpression::BuildInterface,
  84. properties, missingTargets);
  85. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  86. cmGeneratorExpression::BuildInterface,
  87. properties, missingTargets);
  88. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  89. properties);
  90. const bool newCMP0022Behavior =
  91. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  92. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  93. if (newCMP0022Behavior) {
  94. this->PopulateInterfaceLinkLibrariesProperty(
  95. gte, cmGeneratorExpression::BuildInterface, properties,
  96. missingTargets);
  97. }
  98. this->PopulateCompatibleInterfaceProperties(gte, properties);
  99. this->GenerateInterfaceProperties(gte, os, properties);
  100. }
  101. // Generate import file content for each configuration.
  102. for (std::vector<std::string>::const_iterator ci =
  103. this->Configurations.begin();
  104. ci != this->Configurations.end(); ++ci) {
  105. this->GenerateImportConfig(os, *ci, missingTargets);
  106. }
  107. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  108. return true;
  109. }
  110. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  111. std::ostream& os, const std::string& config, std::string const& suffix,
  112. std::vector<std::string>& missingTargets)
  113. {
  114. for (std::vector<cmGeneratorTarget*>::const_iterator tei =
  115. this->Exports.begin();
  116. tei != this->Exports.end(); ++tei) {
  117. // Collect import properties for this target.
  118. cmGeneratorTarget* target = *tei;
  119. ImportPropertyMap properties;
  120. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  121. this->SetImportLocationProperty(config, suffix, target, properties);
  122. }
  123. if (!properties.empty()) {
  124. // Get the rest of the target details.
  125. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  126. this->SetImportDetailProperties(config, suffix, target, properties,
  127. missingTargets);
  128. this->SetImportLinkInterface(config, suffix,
  129. cmGeneratorExpression::BuildInterface,
  130. target, properties, missingTargets);
  131. }
  132. // TOOD: PUBLIC_HEADER_LOCATION
  133. // This should wait until the build feature propagation stuff
  134. // is done. Then this can be a propagated include directory.
  135. // this->GenerateImportProperty(config, te->HeaderGenerator,
  136. // properties);
  137. // Generate code in the export file.
  138. this->GenerateImportPropertyCode(os, config, target, properties);
  139. }
  140. }
  141. }
  142. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  143. {
  144. this->ExportSet = exportSet;
  145. }
  146. void cmExportBuildFileGenerator::SetImportLocationProperty(
  147. const std::string& config, std::string const& suffix,
  148. cmGeneratorTarget* target, ImportPropertyMap& properties)
  149. {
  150. // Get the makefile in which to lookup target information.
  151. cmMakefile* mf = target->Makefile;
  152. // Add the main target file.
  153. {
  154. std::string prop = "IMPORTED_LOCATION";
  155. prop += suffix;
  156. std::string value;
  157. if (target->IsAppBundleOnApple()) {
  158. value = target->GetFullPath(config, false);
  159. } else {
  160. value = target->GetFullPath(config, false, true);
  161. }
  162. properties[prop] = value;
  163. }
  164. // Add the import library for windows DLLs.
  165. if (target->HasImportLibrary() &&
  166. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  167. std::string prop = "IMPORTED_IMPLIB";
  168. prop += suffix;
  169. std::string value = target->GetFullPath(config, true);
  170. target->GetImplibGNUtoMS(value, value, "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  171. properties[prop] = value;
  172. }
  173. }
  174. void cmExportBuildFileGenerator::HandleMissingTarget(
  175. std::string& link_libs, std::vector<std::string>& missingTargets,
  176. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  177. {
  178. // The target is not in the export.
  179. if (!this->AppendMode) {
  180. const std::string name = dependee->GetName();
  181. cmGlobalGenerator* gg =
  182. dependee->GetLocalGenerator()->GetGlobalGenerator();
  183. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  184. int targetOccurrences = (int)namespaces.size();
  185. if (targetOccurrences == 1) {
  186. std::string missingTarget = namespaces[0];
  187. missingTarget += dependee->GetExportName();
  188. link_libs += missingTarget;
  189. missingTargets.push_back(missingTarget);
  190. return;
  191. }
  192. // We are not appending, so all exported targets should be
  193. // known here. This is probably user-error.
  194. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  195. }
  196. // Assume the target will be exported by another command.
  197. // Append it with the export namespace.
  198. link_libs += this->Namespace;
  199. link_libs += dependee->GetExportName();
  200. }
  201. void cmExportBuildFileGenerator::GetTargets(
  202. std::vector<std::string>& targets) const
  203. {
  204. if (this->ExportSet) {
  205. for (std::vector<cmTargetExport*>::const_iterator tei =
  206. this->ExportSet->GetTargetExports()->begin();
  207. tei != this->ExportSet->GetTargetExports()->end(); ++tei) {
  208. targets.push_back((*tei)->TargetName);
  209. }
  210. return;
  211. }
  212. targets = this->Targets;
  213. }
  214. std::vector<std::string> cmExportBuildFileGenerator::FindNamespaces(
  215. cmGlobalGenerator* gg, const std::string& name)
  216. {
  217. std::vector<std::string> namespaces;
  218. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  219. gg->GetBuildExportSets();
  220. for (std::map<std::string, cmExportBuildFileGenerator*>::const_iterator
  221. expIt = exportSets.begin();
  222. expIt != exportSets.end(); ++expIt) {
  223. const cmExportBuildFileGenerator* exportSet = expIt->second;
  224. std::vector<std::string> targets;
  225. exportSet->GetTargets(targets);
  226. if (std::find(targets.begin(), targets.end(), name) != targets.end()) {
  227. namespaces.push_back(exportSet->GetNamespace());
  228. }
  229. }
  230. return namespaces;
  231. }
  232. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  233. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  234. {
  235. if (cmSystemTools::GetErrorOccuredFlag()) {
  236. return;
  237. }
  238. std::ostringstream e;
  239. e << "export called with target \"" << depender->GetName()
  240. << "\" which requires target \"" << dependee->GetName() << "\" ";
  241. if (occurrences == 0) {
  242. e << "that is not in the export set.\n";
  243. } else {
  244. e << "that is not in this export set, but " << occurrences
  245. << " times in others.\n";
  246. }
  247. e << "If the required target is not easy to reference in this call, "
  248. << "consider using the APPEND option with multiple separate calls.";
  249. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  250. cmake::FATAL_ERROR, e.str(), this->LG->GetMakefile()->GetBacktrace());
  251. }
  252. std::string cmExportBuildFileGenerator::InstallNameDir(
  253. cmGeneratorTarget* target, const std::string& config)
  254. {
  255. std::string install_name_dir;
  256. cmMakefile* mf = target->Target->GetMakefile();
  257. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  258. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  259. }
  260. return install_name_dir;
  261. }