cmCommonTargetGenerator.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. {
  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(
  53. cmLinkLineComputer* linkLineComputer, std::string& flags)
  54. {
  55. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  56. this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
  57. if (!mdi || mdi->DefFile.empty()) {
  58. return;
  59. }
  60. // TODO: Create a per-language flag variable.
  61. const char* defFileFlag =
  62. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  63. if (!defFileFlag) {
  64. return;
  65. }
  66. // Append the flag and value. Use ConvertToLinkReference to help
  67. // vs6's "cl -link" pass it to the linker.
  68. std::string flag = defFileFlag;
  69. flag += this->LocalGenerator->ConvertToOutputFormat(
  70. linkLineComputer->ConvertToLinkReference(mdi->DefFile),
  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::ComputeTargetCompilePDB() const
  166. {
  167. std::string compilePdbPath;
  168. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  169. return compilePdbPath;
  170. }
  171. compilePdbPath =
  172. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  173. if (compilePdbPath.empty()) {
  174. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  175. // A trailing slash tells the toolchain to add its default file name.
  176. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  177. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  178. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  179. compilePdbPath += this->GeneratorTarget->GetName();
  180. compilePdbPath += ".pdb";
  181. }
  182. }
  183. return compilePdbPath;
  184. }
  185. std::string cmCommonTargetGenerator::GetManifests()
  186. {
  187. std::vector<cmSourceFile const*> manifest_srcs;
  188. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  189. std::vector<std::string> manifests;
  190. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  191. mi != manifest_srcs.end(); ++mi) {
  192. manifests.push_back(this->LocalGenerator->ConvertToOutputFormat(
  193. this->LocalGenerator->ConvertToRelativePath(
  194. this->LocalGenerator->GetWorkingDirectory(), (*mi)->GetFullPath()),
  195. cmOutputConverter::SHELL));
  196. }
  197. return cmJoin(manifests, " ");
  198. }
  199. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  200. const std::string& lang,
  201. const char* name, bool so)
  202. {
  203. // Lookup the flag to specify the version.
  204. std::string fvar = "CMAKE_";
  205. fvar += lang;
  206. fvar += "_OSX_";
  207. fvar += name;
  208. fvar += "_VERSION_FLAG";
  209. const char* flag = this->Makefile->GetDefinition(fvar);
  210. // Skip if no such flag.
  211. if (!flag) {
  212. return;
  213. }
  214. // Lookup the target version information.
  215. int major;
  216. int minor;
  217. int patch;
  218. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  219. if (major > 0 || minor > 0 || patch > 0) {
  220. // Append the flag since a non-zero version is specified.
  221. std::ostringstream vflag;
  222. vflag << flag << major << "." << minor << "." << patch;
  223. this->LocalGenerator->AppendFlags(flags, vflag.str());
  224. }
  225. }