cmCommonTargetGenerator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 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 "cmCommonTargetGenerator.h"
  11. #include <algorithm>
  12. #include <cmConfigure.h>
  13. #include <set>
  14. #include <sstream>
  15. #include <utility>
  16. #include "cmAlgorithms.h"
  17. #include "cmComputeLinkInformation.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalCommonGenerator.h"
  20. #include "cmLocalCommonGenerator.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmSourceFile.h"
  24. #include "cmState.h"
  25. cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
  26. : GeneratorTarget(gt)
  27. , Makefile(gt->Makefile)
  28. , LocalGenerator(static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  29. , GlobalGenerator(static_cast<cmGlobalCommonGenerator*>(
  30. gt->LocalGenerator->GetGlobalGenerator()))
  31. , ConfigName(LocalGenerator->GetConfigName())
  32. , ModuleDefinitionFile(GeneratorTarget->GetModuleDefinitionFile(ConfigName))
  33. {
  34. }
  35. cmCommonTargetGenerator::~cmCommonTargetGenerator()
  36. {
  37. }
  38. std::string const& cmCommonTargetGenerator::GetConfigName() const
  39. {
  40. return this->ConfigName;
  41. }
  42. std::string cmCommonTargetGenerator::Convert(
  43. std::string const& source, cmOutputConverter::RelativeRoot relative,
  44. cmOutputConverter::OutputFormat output)
  45. {
  46. return this->LocalGenerator->Convert(source, relative, output);
  47. }
  48. const char* cmCommonTargetGenerator::GetFeature(const std::string& feature)
  49. {
  50. return this->GeneratorTarget->GetFeature(feature, this->ConfigName);
  51. }
  52. bool cmCommonTargetGenerator::GetFeatureAsBool(const std::string& feature)
  53. {
  54. return this->GeneratorTarget->GetFeatureAsBool(feature, this->ConfigName);
  55. }
  56. void cmCommonTargetGenerator::AddFeatureFlags(std::string& flags,
  57. const std::string& lang)
  58. {
  59. // Add language-specific flags.
  60. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  61. if (this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION")) {
  62. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  63. }
  64. }
  65. void cmCommonTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  66. {
  67. if (!this->ModuleDefinitionFile) {
  68. return;
  69. }
  70. // TODO: Create a per-language flag variable.
  71. const char* defFileFlag =
  72. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  73. if (!defFileFlag) {
  74. return;
  75. }
  76. // Append the flag and value. Use ConvertToLinkReference to help
  77. // vs6's "cl -link" pass it to the linker.
  78. std::string flag = defFileFlag;
  79. flag += (this->LocalGenerator->ConvertToLinkReference(
  80. this->ModuleDefinitionFile->GetFullPath()));
  81. this->LocalGenerator->AppendFlags(flags, flag);
  82. }
  83. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  84. std::string& flags, cmSourceFile const& source)
  85. {
  86. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  87. cmOutputConverter::FortranFormat format =
  88. cmOutputConverter::GetFortranFormat(srcfmt);
  89. if (format == cmOutputConverter::FortranFormatNone) {
  90. const char* tgtfmt = this->GeneratorTarget->GetProperty("Fortran_FORMAT");
  91. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  92. }
  93. const char* var = CM_NULLPTR;
  94. switch (format) {
  95. case cmOutputConverter::FortranFormatFixed:
  96. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  97. break;
  98. case cmOutputConverter::FortranFormatFree:
  99. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  100. break;
  101. default:
  102. break;
  103. }
  104. if (var) {
  105. this->LocalGenerator->AppendFlags(flags,
  106. this->Makefile->GetDefinition(var));
  107. }
  108. }
  109. std::string cmCommonTargetGenerator::GetFlags(const std::string& l)
  110. {
  111. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  112. if (i == this->FlagsByLanguage.end()) {
  113. std::string flags;
  114. this->LocalGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  115. this->ConfigName, l, flags);
  116. ByLanguageMap::value_type entry(l, flags);
  117. i = this->FlagsByLanguage.insert(entry).first;
  118. }
  119. return i->second;
  120. }
  121. std::string cmCommonTargetGenerator::GetDefines(const std::string& l)
  122. {
  123. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  124. if (i == this->DefinesByLanguage.end()) {
  125. std::set<std::string> defines;
  126. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget,
  127. this->ConfigName, l, defines);
  128. std::string definesString;
  129. this->LocalGenerator->JoinDefines(defines, definesString, l);
  130. ByLanguageMap::value_type entry(l, definesString);
  131. i = this->DefinesByLanguage.insert(entry).first;
  132. }
  133. return i->second;
  134. }
  135. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  136. {
  137. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  138. if (i == this->IncludesByLanguage.end()) {
  139. std::string includes;
  140. this->AddIncludeFlags(includes, l);
  141. ByLanguageMap::value_type entry(l, includes);
  142. i = this->IncludesByLanguage.insert(entry).first;
  143. }
  144. return i->second;
  145. }
  146. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories()
  147. const
  148. {
  149. std::vector<std::string> dirs;
  150. std::set<cmGeneratorTarget const*> emitted;
  151. if (cmComputeLinkInformation* cli =
  152. this->GeneratorTarget->GetLinkInformation(this->ConfigName)) {
  153. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  154. for (cmComputeLinkInformation::ItemVector::const_iterator i =
  155. items.begin();
  156. i != items.end(); ++i) {
  157. cmGeneratorTarget const* linkee = i->Target;
  158. if (linkee && !linkee->IsImported()
  159. // We can ignore the INTERFACE_LIBRARY items because
  160. // Target->GetLinkInformation already processed their
  161. // link interface and they don't have any output themselves.
  162. && linkee->GetType() != cmState::INTERFACE_LIBRARY &&
  163. emitted.insert(linkee).second) {
  164. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  165. std::string di = lg->GetCurrentBinaryDirectory();
  166. di += "/";
  167. di += lg->GetTargetDirectory(linkee);
  168. dirs.push_back(di);
  169. }
  170. }
  171. }
  172. return dirs;
  173. }
  174. std::string cmCommonTargetGenerator::GetManifests()
  175. {
  176. std::vector<cmSourceFile const*> manifest_srcs;
  177. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  178. std::vector<std::string> manifests;
  179. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  180. mi != manifest_srcs.end(); ++mi) {
  181. manifests.push_back(this->LocalGenerator->ConvertToOutputFormat(
  182. this->LocalGenerator->ConvertToRelativePath(
  183. this->LocalGenerator->GetWorkingDirectory(), (*mi)->GetFullPath()),
  184. cmOutputConverter::SHELL));
  185. }
  186. return cmJoin(manifests, " ");
  187. }
  188. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  189. const std::string& lang,
  190. const char* name, bool so)
  191. {
  192. // Lookup the flag to specify the version.
  193. std::string fvar = "CMAKE_";
  194. fvar += lang;
  195. fvar += "_OSX_";
  196. fvar += name;
  197. fvar += "_VERSION_FLAG";
  198. const char* flag = this->Makefile->GetDefinition(fvar);
  199. // Skip if no such flag.
  200. if (!flag) {
  201. return;
  202. }
  203. // Lookup the target version information.
  204. int major;
  205. int minor;
  206. int patch;
  207. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  208. if (major > 0 || minor > 0 || patch > 0) {
  209. // Append the flag since a non-zero version is specified.
  210. std::ostringstream vflag;
  211. vflag << flag << major << "." << minor << "." << patch;
  212. this->LocalGenerator->AppendFlags(flags, vflag.str());
  213. }
  214. }