cmCommonTargetGenerator.cxx 8.0 KB

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