cmCommonTargetGenerator.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "cmAlgorithms.h"
  8. #include "cmComputeLinkInformation.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalCommonGenerator.h"
  11. #include "cmLinkLineComputer.h"
  12. #include "cmLocalCommonGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmOutputConverter.h"
  16. #include "cmSourceFile.h"
  17. #include "cmStateTypes.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 = defFileFlag;
  54. flag += this->LocalCommonGenerator->ConvertToOutputFormat(
  55. linkLineComputer->ConvertToLinkReference(mdi->DefFile),
  56. cmOutputConverter::SHELL);
  57. this->LocalCommonGenerator->AppendFlags(flags, flag);
  58. }
  59. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  60. std::string& flags, cmSourceFile const& source)
  61. {
  62. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  63. cmOutputConverter::FortranFormat format =
  64. cmOutputConverter::GetFortranFormat(srcfmt);
  65. if (format == cmOutputConverter::FortranFormatNone) {
  66. const char* tgtfmt = this->GeneratorTarget->GetProperty("Fortran_FORMAT");
  67. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  68. }
  69. const char* var = nullptr;
  70. switch (format) {
  71. case cmOutputConverter::FortranFormatFixed:
  72. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  73. break;
  74. case cmOutputConverter::FortranFormatFree:
  75. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  76. break;
  77. default:
  78. break;
  79. }
  80. if (var) {
  81. this->LocalCommonGenerator->AppendFlags(
  82. flags, this->Makefile->GetDefinition(var));
  83. }
  84. }
  85. std::string cmCommonTargetGenerator::GetFlags(const std::string& l)
  86. {
  87. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  88. if (i == this->FlagsByLanguage.end()) {
  89. std::string flags;
  90. this->LocalCommonGenerator->GetTargetCompileFlags(
  91. this->GeneratorTarget, this->ConfigName, l, flags);
  92. ByLanguageMap::value_type entry(l, flags);
  93. i = this->FlagsByLanguage.insert(entry).first;
  94. }
  95. return i->second;
  96. }
  97. std::string cmCommonTargetGenerator::GetDefines(const std::string& l)
  98. {
  99. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  100. if (i == this->DefinesByLanguage.end()) {
  101. std::set<std::string> defines;
  102. this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget,
  103. this->ConfigName, l, defines);
  104. std::string definesString;
  105. this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
  106. ByLanguageMap::value_type entry(l, definesString);
  107. i = this->DefinesByLanguage.insert(entry).first;
  108. }
  109. return i->second;
  110. }
  111. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l)
  112. {
  113. ByLanguageMap::iterator i = this->IncludesByLanguage.find(l);
  114. if (i == this->IncludesByLanguage.end()) {
  115. std::string includes;
  116. this->AddIncludeFlags(includes, l);
  117. ByLanguageMap::value_type entry(l, includes);
  118. i = this->IncludesByLanguage.insert(entry).first;
  119. }
  120. return i->second;
  121. }
  122. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories()
  123. const
  124. {
  125. std::vector<std::string> dirs;
  126. std::set<cmGeneratorTarget const*> emitted;
  127. if (cmComputeLinkInformation* cli =
  128. this->GeneratorTarget->GetLinkInformation(this->ConfigName)) {
  129. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  130. for (auto const& item : items) {
  131. cmGeneratorTarget const* linkee = item.Target;
  132. if (linkee &&
  133. !linkee->IsImported()
  134. // We can ignore the INTERFACE_LIBRARY items because
  135. // Target->GetLinkInformation already processed their
  136. // link interface and they don't have any output themselves.
  137. && linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  138. emitted.insert(linkee).second) {
  139. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  140. std::string di = lg->GetCurrentBinaryDirectory();
  141. di += "/";
  142. di += 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 = "CMAKE_";
  190. fvar += lang;
  191. fvar += "_OSX_";
  192. fvar += name;
  193. fvar += "_VERSION_FLAG";
  194. const char* flag = this->Makefile->GetDefinition(fvar);
  195. // Skip if no such flag.
  196. if (!flag) {
  197. return;
  198. }
  199. // Lookup the target version information.
  200. int major;
  201. int minor;
  202. int patch;
  203. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  204. if (major > 0 || minor > 0 || patch > 0) {
  205. // Append the flag since a non-zero version is specified.
  206. std::ostringstream vflag;
  207. vflag << flag << major << "." << minor << "." << patch;
  208. this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
  209. }
  210. }