cmExportBuildFileGenerator.cxx 12 KB

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