cmCommonTargetGenerator.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. , LocalGenerator(static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  23. , GlobalGenerator(static_cast<cmGlobalCommonGenerator*>(
  24. gt->LocalGenerator->GetGlobalGenerator()))
  25. , ConfigName(LocalGenerator->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->LocalGenerator->ConvertToOutputFormat(
  57. linkLineComputer->ConvertToLinkReference(mdi->DefFile),
  58. cmOutputConverter::SHELL);
  59. this->LocalGenerator->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 = CM_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->LocalGenerator->AppendFlags(flags,
  84. 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->LocalGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  93. 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->LocalGenerator->GetTargetDefines(this->GeneratorTarget,
  105. this->ConfigName, l, defines);
  106. std::string definesString;
  107. this->LocalGenerator->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 (cmComputeLinkInformation::ItemVector::const_iterator i =
  133. items.begin();
  134. i != items.end(); ++i) {
  135. cmGeneratorTarget const* linkee = i->Target;
  136. if (linkee && !linkee->IsImported()
  137. // We can ignore the INTERFACE_LIBRARY items because
  138. // Target->GetLinkInformation already processed their
  139. // link interface and they don't have any output themselves.
  140. && linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  141. emitted.insert(linkee).second) {
  142. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  143. std::string di = lg->GetCurrentBinaryDirectory();
  144. di += "/";
  145. di += lg->GetTargetDirectory(linkee);
  146. dirs.push_back(di);
  147. }
  148. }
  149. }
  150. return dirs;
  151. }
  152. std::string cmCommonTargetGenerator::ComputeTargetCompilePDB() const
  153. {
  154. std::string compilePdbPath;
  155. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  156. return compilePdbPath;
  157. }
  158. compilePdbPath =
  159. this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
  160. if (compilePdbPath.empty()) {
  161. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  162. // A trailing slash tells the toolchain to add its default file name.
  163. compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
  164. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  165. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  166. compilePdbPath += this->GeneratorTarget->GetName();
  167. compilePdbPath += ".pdb";
  168. }
  169. }
  170. return compilePdbPath;
  171. }
  172. std::string cmCommonTargetGenerator::GetManifests()
  173. {
  174. std::vector<cmSourceFile const*> manifest_srcs;
  175. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  176. std::vector<std::string> manifests;
  177. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  178. mi != manifest_srcs.end(); ++mi) {
  179. manifests.push_back(this->LocalGenerator->ConvertToOutputFormat(
  180. this->LocalGenerator->ConvertToRelativePath(
  181. this->LocalGenerator->GetWorkingDirectory(), (*mi)->GetFullPath()),
  182. cmOutputConverter::SHELL));
  183. }
  184. return cmJoin(manifests, " ");
  185. }
  186. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  187. const std::string& lang,
  188. const char* name, bool so)
  189. {
  190. // Lookup the flag to specify the version.
  191. std::string fvar = "CMAKE_";
  192. fvar += lang;
  193. fvar += "_OSX_";
  194. fvar += name;
  195. fvar += "_VERSION_FLAG";
  196. const char* flag = this->Makefile->GetDefinition(fvar);
  197. // Skip if no such flag.
  198. if (!flag) {
  199. return;
  200. }
  201. // Lookup the target version information.
  202. int major;
  203. int minor;
  204. int patch;
  205. this->GeneratorTarget->GetTargetVersion(so, major, minor, patch);
  206. if (major > 0 || minor > 0 || patch > 0) {
  207. // Append the flag since a non-zero version is specified.
  208. std::ostringstream vflag;
  209. vflag << flag << major << "." << minor << "." << patch;
  210. this->LocalGenerator->AppendFlags(flags, vflag.str());
  211. }
  212. }