cmExportBuildFileGenerator.cxx 11 KB

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