cmExportBuildFileGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // Check whether this is a DLL platform.
  192. bool dll_platform =
  193. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  194. // Add the import library for windows DLLs.
  195. if(dll_platform &&
  196. (target->GetType() == cmState::SHARED_LIBRARY ||
  197. target->IsExecutableWithExports()) &&
  198. mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  199. {
  200. std::string prop = "IMPORTED_IMPLIB";
  201. prop += suffix;
  202. std::string value = target->GetFullPath(config, true);
  203. target->GetImplibGNUtoMS(value, value,
  204. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  205. properties[prop] = value;
  206. }
  207. }
  208. //----------------------------------------------------------------------------
  209. void
  210. cmExportBuildFileGenerator::HandleMissingTarget(
  211. std::string& link_libs,
  212. std::vector<std::string>& missingTargets,
  213. cmGeneratorTarget* depender,
  214. cmGeneratorTarget* dependee)
  215. {
  216. // The target is not in the export.
  217. if(!this->AppendMode)
  218. {
  219. const std::string name = dependee->GetName();
  220. cmGlobalGenerator* gg =
  221. dependee->GetLocalGenerator()->GetGlobalGenerator();
  222. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  223. int targetOccurrences = (int)namespaces.size();
  224. if (targetOccurrences == 1)
  225. {
  226. std::string missingTarget = namespaces[0];
  227. missingTarget += dependee->GetExportName();
  228. link_libs += missingTarget;
  229. missingTargets.push_back(missingTarget);
  230. return;
  231. }
  232. else
  233. {
  234. // We are not appending, so all exported targets should be
  235. // known here. This is probably user-error.
  236. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  237. }
  238. }
  239. // Assume the target will be exported by another command.
  240. // Append it with the export namespace.
  241. link_libs += this->Namespace;
  242. link_libs += dependee->GetExportName();
  243. }
  244. //----------------------------------------------------------------------------
  245. void cmExportBuildFileGenerator
  246. ::GetTargets(std::vector<std::string> &targets) const
  247. {
  248. if (this->ExportSet)
  249. {
  250. for(std::vector<cmTargetExport*>::const_iterator
  251. tei = this->ExportSet->GetTargetExports()->begin();
  252. tei != this->ExportSet->GetTargetExports()->end(); ++tei)
  253. {
  254. targets.push_back((*tei)->TargetName);
  255. }
  256. return;
  257. }
  258. targets = this->Targets;
  259. }
  260. //----------------------------------------------------------------------------
  261. std::vector<std::string>
  262. cmExportBuildFileGenerator
  263. ::FindNamespaces(cmGlobalGenerator* gg, const std::string& name)
  264. {
  265. std::vector<std::string> namespaces;
  266. std::map<std::string, cmExportBuildFileGenerator*>& exportSets
  267. = gg->GetBuildExportSets();
  268. for(std::map<std::string, cmExportBuildFileGenerator*>::const_iterator
  269. expIt = exportSets.begin(); expIt != exportSets.end(); ++expIt)
  270. {
  271. const cmExportBuildFileGenerator* exportSet = expIt->second;
  272. std::vector<std::string> targets;
  273. exportSet->GetTargets(targets);
  274. if (std::find(targets.begin(), targets.end(), name) != targets.end())
  275. {
  276. namespaces.push_back(exportSet->GetNamespace());
  277. }
  278. }
  279. return namespaces;
  280. }
  281. //----------------------------------------------------------------------------
  282. void
  283. cmExportBuildFileGenerator
  284. ::ComplainAboutMissingTarget(cmGeneratorTarget* depender,
  285. cmGeneratorTarget* dependee,
  286. int occurrences)
  287. {
  288. if(cmSystemTools::GetErrorOccuredFlag())
  289. {
  290. return;
  291. }
  292. std::ostringstream e;
  293. e << "export called with target \"" << depender->GetName()
  294. << "\" which requires target \"" << dependee->GetName() << "\" ";
  295. if (occurrences == 0)
  296. {
  297. e << "that is not in the export set.\n";
  298. }
  299. else
  300. {
  301. e << "that is not in this export set, but " << occurrences
  302. << " times in others.\n";
  303. }
  304. e << "If the required target is not easy to reference in this call, "
  305. << "consider using the APPEND option with multiple separate calls.";
  306. this->LG->GetGlobalGenerator()->GetCMakeInstance()
  307. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  308. this->LG->GetMakefile()->GetBacktrace());
  309. }
  310. std::string
  311. cmExportBuildFileGenerator::InstallNameDir(cmGeneratorTarget* target,
  312. const std::string& config)
  313. {
  314. std::string install_name_dir;
  315. cmMakefile* mf = target->Target->GetMakefile();
  316. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  317. {
  318. install_name_dir =
  319. target->GetInstallNameDirForBuildTree(config);
  320. }
  321. return install_name_dir;
  322. }