cmCommonTargetGenerator.cxx 7.6 KB

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