cmCommonTargetGenerator.cxx 10.0 KB

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