cmExportBuildFileGenerator.cxx 12 KB

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