cmExportBuildFileGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = CM_NULLPTR;
  26. this->ExportSet = CM_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::vector<std::string>::const_iterator tei = targets.begin();
  43. tei != targets.end(); ++tei) {
  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. cmake::FATAL_ERROR, e.str(),
  54. this->LG->GetMakefile()->GetBacktrace());
  55. return false;
  56. }
  57. if (te->GetType() == 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 (std::vector<cmGeneratorTarget*>::const_iterator tei =
  66. this->Exports.begin();
  67. tei != this->Exports.end(); ++tei) {
  68. cmGeneratorTarget* gte = *tei;
  69. this->GenerateImportTargetCode(os, 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_POSITION_INDEPENDENT_CODE", gte,
  91. properties);
  92. const bool newCMP0022Behavior =
  93. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  94. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  95. if (newCMP0022Behavior) {
  96. this->PopulateInterfaceLinkLibrariesProperty(
  97. gte, cmGeneratorExpression::BuildInterface, properties,
  98. missingTargets);
  99. }
  100. this->PopulateCompatibleInterfaceProperties(gte, properties);
  101. this->GenerateInterfaceProperties(gte, os, properties);
  102. }
  103. // Generate import file content for each configuration.
  104. for (std::vector<std::string>::const_iterator ci =
  105. this->Configurations.begin();
  106. ci != this->Configurations.end(); ++ci) {
  107. this->GenerateImportConfig(os, *ci, missingTargets);
  108. }
  109. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  110. return true;
  111. }
  112. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  113. std::ostream& os, const std::string& config, std::string const& suffix,
  114. std::vector<std::string>& missingTargets)
  115. {
  116. for (std::vector<cmGeneratorTarget*>::const_iterator tei =
  117. this->Exports.begin();
  118. tei != this->Exports.end(); ++tei) {
  119. // Collect import properties for this target.
  120. cmGeneratorTarget* target = *tei;
  121. ImportPropertyMap properties;
  122. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  123. this->SetImportLocationProperty(config, suffix, target, properties);
  124. }
  125. if (!properties.empty()) {
  126. // Get the rest of the target details.
  127. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  128. this->SetImportDetailProperties(config, suffix, target, properties,
  129. missingTargets);
  130. this->SetImportLinkInterface(config, suffix,
  131. cmGeneratorExpression::BuildInterface,
  132. target, properties, missingTargets);
  133. }
  134. // TOOD: PUBLIC_HEADER_LOCATION
  135. // This should wait until the build feature propagation stuff
  136. // is done. Then this can be a propagated include directory.
  137. // this->GenerateImportProperty(config, te->HeaderGenerator,
  138. // properties);
  139. // Generate code in the export file.
  140. this->GenerateImportPropertyCode(os, config, target, properties);
  141. }
  142. }
  143. }
  144. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  145. {
  146. this->ExportSet = exportSet;
  147. }
  148. void cmExportBuildFileGenerator::SetImportLocationProperty(
  149. const std::string& config, std::string const& suffix,
  150. cmGeneratorTarget* target, ImportPropertyMap& properties)
  151. {
  152. // Get the makefile in which to lookup target information.
  153. cmMakefile* mf = target->Makefile;
  154. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  155. std::string prop = "IMPORTED_OBJECTS";
  156. prop += suffix;
  157. // Compute all the object files inside this target and setup
  158. // IMPORTED_OBJECTS as a list of object files
  159. std::vector<cmSourceFile const*> objectSources;
  160. target->GetObjectSources(objectSources, config);
  161. std::string const obj_dir = target->GetObjectDirectory(config);
  162. std::vector<std::string> objects;
  163. for (std::vector<cmSourceFile const*>::const_iterator si =
  164. objectSources.begin();
  165. si != objectSources.end(); ++si) {
  166. const std::string& obj = target->GetObjectName(*si);
  167. objects.push_back(obj_dir + obj);
  168. }
  169. // Store the property.
  170. properties[prop] = cmJoin(objects, ";");
  171. } else {
  172. // Add the main target file.
  173. {
  174. std::string prop = "IMPORTED_LOCATION";
  175. prop += suffix;
  176. std::string value;
  177. if (target->IsAppBundleOnApple()) {
  178. value =
  179. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  180. } else {
  181. value = target->GetFullPath(config,
  182. cmStateEnums::RuntimeBinaryArtifact, true);
  183. }
  184. properties[prop] = value;
  185. }
  186. // Add the import library for windows DLLs.
  187. if (target->HasImportLibrary() &&
  188. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  189. std::string prop = "IMPORTED_IMPLIB";
  190. prop += suffix;
  191. std::string value =
  192. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact);
  193. target->GetImplibGNUtoMS(value, value, "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  194. properties[prop] = value;
  195. }
  196. }
  197. }
  198. void cmExportBuildFileGenerator::HandleMissingTarget(
  199. std::string& link_libs, std::vector<std::string>& missingTargets,
  200. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  201. {
  202. // The target is not in the export.
  203. if (!this->AppendMode) {
  204. const std::string name = dependee->GetName();
  205. cmGlobalGenerator* gg =
  206. dependee->GetLocalGenerator()->GetGlobalGenerator();
  207. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  208. int targetOccurrences = (int)namespaces.size();
  209. if (targetOccurrences == 1) {
  210. std::string missingTarget = namespaces[0];
  211. missingTarget += dependee->GetExportName();
  212. link_libs += missingTarget;
  213. missingTargets.push_back(missingTarget);
  214. return;
  215. }
  216. // We are not appending, so all exported targets should be
  217. // known here. This is probably user-error.
  218. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  219. }
  220. // Assume the target will be exported by another command.
  221. // Append it with the export namespace.
  222. link_libs += this->Namespace;
  223. link_libs += dependee->GetExportName();
  224. }
  225. void cmExportBuildFileGenerator::GetTargets(
  226. std::vector<std::string>& targets) const
  227. {
  228. if (this->ExportSet) {
  229. for (std::vector<cmTargetExport*>::const_iterator tei =
  230. this->ExportSet->GetTargetExports()->begin();
  231. tei != this->ExportSet->GetTargetExports()->end(); ++tei) {
  232. targets.push_back((*tei)->TargetName);
  233. }
  234. return;
  235. }
  236. targets = this->Targets;
  237. }
  238. std::vector<std::string> cmExportBuildFileGenerator::FindNamespaces(
  239. cmGlobalGenerator* gg, const std::string& name)
  240. {
  241. std::vector<std::string> namespaces;
  242. std::map<std::string, cmExportBuildFileGenerator*>& exportSets =
  243. gg->GetBuildExportSets();
  244. for (std::map<std::string, cmExportBuildFileGenerator*>::const_iterator
  245. expIt = exportSets.begin();
  246. expIt != exportSets.end(); ++expIt) {
  247. const cmExportBuildFileGenerator* exportSet = expIt->second;
  248. std::vector<std::string> targets;
  249. exportSet->GetTargets(targets);
  250. if (std::find(targets.begin(), targets.end(), name) != targets.end()) {
  251. namespaces.push_back(exportSet->GetNamespace());
  252. }
  253. }
  254. return namespaces;
  255. }
  256. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  257. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  258. {
  259. if (cmSystemTools::GetErrorOccuredFlag()) {
  260. return;
  261. }
  262. std::ostringstream e;
  263. e << "export called with target \"" << depender->GetName()
  264. << "\" which requires target \"" << dependee->GetName() << "\" ";
  265. if (occurrences == 0) {
  266. e << "that is not in the export set.\n";
  267. } else {
  268. e << "that is not in this export set, but " << occurrences
  269. << " times in others.\n";
  270. }
  271. e << "If the required target is not easy to reference in this call, "
  272. << "consider using the APPEND option with multiple separate calls.";
  273. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  274. cmake::FATAL_ERROR, e.str(), this->LG->GetMakefile()->GetBacktrace());
  275. }
  276. std::string cmExportBuildFileGenerator::InstallNameDir(
  277. cmGeneratorTarget* target, const std::string& config)
  278. {
  279. std::string install_name_dir;
  280. cmMakefile* mf = target->Target->GetMakefile();
  281. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  282. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  283. }
  284. return install_name_dir;
  285. }