cmCommonTargetGenerator.cxx 7.3 KB

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