cmExportBuildFileGenerator.cxx 12 KB

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