cmExportBuildFileGenerator.cxx 11 KB

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