cmExportBuildFileGenerator.cxx 11 KB

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