cmCommonTargetGenerator.cxx 7.4 KB

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