cmExportBuildFileGenerator.cxx 12 KB

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