cmExportBuildFileGenerator.cxx 12 KB

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