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