cmCommonTargetGenerator.cxx 7.7 KB

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