cmCommonTargetGenerator.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. void cmCommonTargetGenerator::AddFeatureFlags(std::string& flags,
  40. const std::string& lang)
  41. {
  42. // Add language-specific flags.
  43. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  44. if (this->GeneratorTarget->IsIPOEnabled(this->ConfigName)) {
  45. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  46. }
  47. }
  48. void cmCommonTargetGenerator::AddModuleDefinitionFlag(
  49. cmLinkLineComputer* linkLineComputer, std::string& flags)
  50. {
  51. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  52. this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
  53. if (!mdi || mdi->DefFile.empty()) {
  54. return;
  55. }
  56. // TODO: Create a per-language flag variable.
  57. const char* defFileFlag =
  58. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  59. if (!defFileFlag) {
  60. return;
  61. }
  62. // Append the flag and value. Use ConvertToLinkReference to help
  63. // vs6's "cl -link" pass it to the linker.
  64. std::string flag = defFileFlag;
  65. flag += this->LocalGenerator->ConvertToOutputFormat(
  66. linkLineComputer->ConvertToLinkReference(mdi->DefFile),
  67. cmOutputConverter::SHELL);
  68. this->LocalGenerator->AppendFlags(flags, flag);
  69. }
  70. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  71. std::string& flags, cmSourceFile const& source)
  72. {
  73. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  74. cmOutputConverter::FortranFormat format =
  75. cmOutputConverter::GetFortranFormat(srcfmt);
  76. if (format == cmOutputConverter::FortranFormatNone) {
  77. const char* tgtfmt = this->GeneratorTarget->GetProperty("Fortran_FORMAT");
  78. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  79. }
  80. const char* var = CM_NULLPTR;
  81. switch (format) {
  82. case cmOutputConverter::FortranFormatFixed:
  83. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  84. break;
  85. case cmOutputConverter::FortranFormatFree:
  86. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  87. break;
  88. default:
  89. break;
  90. }
  91. if (var) {
  92. this->LocalGenerator->AppendFlags(flags,
  93. this->Makefile->GetDefinition(var));
  94. }
  95. }
  96. std::string cmCommonTargetGenerator::GetFlags(const std::string& l)
  97. {
  98. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  99. if (i == this->FlagsByLanguage.end()) {
  100. std::string flags;
  101. this->LocalGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  102. this->ConfigName, l, flags);
  103. ByLanguageMap::value_type entry(l, flags);
  104. i = this->FlagsByLanguage.insert(entry).first;
  105. }
  106. return i->second;
  107. }
  108. std::string cmCommonTargetGenerator::GetDefines(const std::string& l)
  109. {
  110. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  111. if (i == this->DefinesByLanguage.end()) {
  112. std::set<std::string> defines;
  113. this->LocalGenerator->GetTargetDefines(this->GeneratorTarget,
  114. this->ConfigName, l, defines);
  115. std::string definesString;
  116. this->LocalGenerator->JoinDefines(defines, definesString, l);
  117. ByLanguageMap::value_type entry(l, definesString);
  118. i = this->DefinesByLanguage.insert(entry).first;
  119. }
  120. return i->second;
  121. }
  122. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  123. {
  124. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  125. if (i == this->IncludesByLanguage.end()) {
  126. std::string includes;
  127. this->AddIncludeFlags(includes, l);
  128. ByLanguageMap::value_type entry(l, includes);
  129. i = this->IncludesByLanguage.insert(entry).first;
  130. }
  131. return i->second;
  132. }
  133. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories()
  134. const
  135. {
  136. std::vector<std::string> dirs;
  137. std::set<cmGeneratorTarget const*> emitted;
  138. if (cmComputeLinkInformation* cli =
  139. this->GeneratorTarget->GetLinkInformation(this->ConfigName)) {
  140. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  141. for (cmComputeLinkInformation::ItemVector::const_iterator i =
  142. items.begin();
  143. i != items.end(); ++i) {
  144. cmGeneratorTarget const* linkee = i->Target;
  145. if (linkee && !linkee->IsImported()
  146. // We can ignore the INTERFACE_LIBRARY items because
  147. // Target->GetLinkInformation already processed their
  148. // link interface and they don't have any output themselves.
  149. && linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  150. emitted.insert(linkee).second) {
  151. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  152. std::string di = lg->GetCurrentBinaryDirectory();
  153. di += "/";
  154. di += lg->GetTargetDirectory(linkee);
  155. dirs.push_back(di);
  156. }
  157. }
  158. }
  159. return dirs;
  160. }
  161. std::string cmCommonTargetGenerator::ComputeTargetCompilePDB() const
  162. {
  163. std::string compilePdbPath;
  164. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  165. return compilePdbPath;
  166. }
  167. compilePdbPath =
  168. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  169. if (compilePdbPath.empty()) {
  170. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  171. // A trailing slash tells the toolchain to add its default file name.
  172. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  173. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  174. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  175. compilePdbPath += this->GeneratorTarget->GetName();
  176. compilePdbPath += ".pdb";
  177. }
  178. }
  179. return compilePdbPath;
  180. }
  181. std::string cmCommonTargetGenerator::GetManifests()
  182. {
  183. std::vector<cmSourceFile const*> manifest_srcs;
  184. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  185. std::vector<std::string> manifests;
  186. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  187. mi != manifest_srcs.end(); ++mi) {
  188. manifests.push_back(this->LocalGenerator->ConvertToOutputFormat(
  189. this->LocalGenerator->ConvertToRelativePath(
  190. this->LocalGenerator->GetWorkingDirectory(), (*mi)->GetFullPath()),
  191. cmOutputConverter::SHELL));
  192. }
  193. return cmJoin(manifests, " ");
  194. }
  195. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  196. const std::string& lang,
  197. const char* name, bool so)
  198. {
  199. // Lookup the flag to specify the version.
  200. std::string fvar = "CMAKE_";
  201. fvar += lang;
  202. fvar += "_OSX_";
  203. fvar += name;
  204. fvar += "_VERSION_FLAG";
  205. const char* flag = this->Makefile->GetDefinition(fvar);
  206. // Skip if no such flag.
  207. if (!flag) {
  208. return;
  209. }
  210. // Lookup the target version information.
  211. int major;
  212. int minor;
  213. int patch;
  214. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  215. if (major > 0 || minor > 0 || patch > 0) {
  216. // Append the flag since a non-zero version is specified.
  217. std::ostringstream vflag;
  218. vflag << flag << major << "." << minor << "." << patch;
  219. this->LocalGenerator->AppendFlags(flags, vflag.str());
  220. }
  221. }