1
0

cmExportBuildFileGenerator.cxx 11 KB

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