cmExportBuildFileGenerator.cxx 11 KB

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