cmCommonTargetGenerator.cxx 8.9 KB

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