cmExportBuildFileGenerator.cxx 12 KB

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