cmExportBuildFileGenerator.cxx 11 KB

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