cmExportBuildFileGenerator.cxx 10 KB

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