cmCommonTargetGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <algorithm>
  5. #include <sstream>
  6. #include <utility>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmComputeLinkInformation.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalCommonGenerator.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalCommonGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmMessageType.h"
  17. #include "cmOutputConverter.h"
  18. #include "cmRange.h"
  19. #include "cmSourceFile.h"
  20. #include "cmState.h"
  21. #include "cmStateTypes.h"
  22. #include "cmStringAlgorithms.h"
  23. #include "cmTarget.h"
  24. #include "cmValue.h"
  25. cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
  26. : GeneratorTarget(gt)
  27. , Makefile(gt->Makefile)
  28. , LocalCommonGenerator(
  29. static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  30. , GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>(
  31. gt->LocalGenerator->GetGlobalGenerator()))
  32. , ConfigNames(this->LocalCommonGenerator->GetConfigNames())
  33. {
  34. }
  35. cmCommonTargetGenerator::~cmCommonTargetGenerator() = default;
  36. std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const
  37. {
  38. return this->ConfigNames;
  39. }
  40. cmValue cmCommonTargetGenerator::GetFeature(const std::string& feature,
  41. const std::string& config)
  42. {
  43. return this->GeneratorTarget->GetFeature(feature, config);
  44. }
  45. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  46. std::string& flags, cmSourceFile const& source)
  47. {
  48. const std::string srcfmt = source.GetSafeProperty("Fortran_FORMAT");
  49. cmOutputConverter::FortranFormat format =
  50. cmOutputConverter::GetFortranFormat(srcfmt);
  51. if (format == cmOutputConverter::FortranFormatNone) {
  52. std::string const& tgtfmt =
  53. this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT");
  54. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  55. }
  56. const char* var = nullptr;
  57. switch (format) {
  58. case cmOutputConverter::FortranFormatFixed:
  59. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  60. break;
  61. case cmOutputConverter::FortranFormatFree:
  62. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  63. break;
  64. default:
  65. break;
  66. }
  67. if (var) {
  68. this->LocalCommonGenerator->AppendFlags(
  69. flags, this->Makefile->GetSafeDefinition(var));
  70. }
  71. }
  72. void cmCommonTargetGenerator::AppendFortranPreprocessFlags(
  73. std::string& flags, cmSourceFile const& source,
  74. PreprocessFlagsRequired requires_pp)
  75. {
  76. const std::string srcpp = source.GetSafeProperty("Fortran_PREPROCESS");
  77. cmOutputConverter::FortranPreprocess preprocess =
  78. cmOutputConverter::GetFortranPreprocess(srcpp);
  79. if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
  80. std::string const& tgtpp =
  81. this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
  82. preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
  83. }
  84. const char* var = nullptr;
  85. switch (preprocess) {
  86. case cmOutputConverter::FortranPreprocess::Needed:
  87. if (requires_pp == PreprocessFlagsRequired::YES) {
  88. var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON";
  89. }
  90. break;
  91. case cmOutputConverter::FortranPreprocess::NotNeeded:
  92. var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF";
  93. break;
  94. default:
  95. break;
  96. }
  97. if (var) {
  98. this->LocalCommonGenerator->AppendCompileOptions(
  99. flags, this->Makefile->GetSafeDefinition(var));
  100. }
  101. }
  102. std::string cmCommonTargetGenerator::GetFlags(const std::string& l,
  103. const std::string& config,
  104. const std::string& arch)
  105. {
  106. const std::string key = config + arch;
  107. auto i = this->Configs[key].FlagsByLanguage.find(l);
  108. if (i == this->Configs[key].FlagsByLanguage.end()) {
  109. std::string flags;
  110. this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  111. config, l, flags, arch);
  112. ByLanguageMap::value_type entry(l, flags);
  113. i = this->Configs[key].FlagsByLanguage.insert(entry).first;
  114. }
  115. return i->second;
  116. }
  117. std::string cmCommonTargetGenerator::GetDefines(const std::string& l,
  118. const std::string& config)
  119. {
  120. auto i = this->Configs[config].DefinesByLanguage.find(l);
  121. if (i == this->Configs[config].DefinesByLanguage.end()) {
  122. std::set<std::string> defines;
  123. this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config,
  124. l, defines);
  125. std::string definesString;
  126. this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
  127. ByLanguageMap::value_type entry(l, definesString);
  128. i = this->Configs[config].DefinesByLanguage.insert(entry).first;
  129. }
  130. return i->second;
  131. }
  132. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l,
  133. const std::string& config)
  134. {
  135. auto i = this->Configs[config].IncludesByLanguage.find(l);
  136. if (i == this->Configs[config].IncludesByLanguage.end()) {
  137. std::string includes;
  138. this->AddIncludeFlags(includes, l, config);
  139. ByLanguageMap::value_type entry(l, includes);
  140. i = this->Configs[config].IncludesByLanguage.insert(entry).first;
  141. }
  142. return i->second;
  143. }
  144. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories(
  145. const std::string& lang, const std::string& config) const
  146. {
  147. std::vector<std::string> dirs;
  148. std::set<cmGeneratorTarget const*> emitted;
  149. if (cmComputeLinkInformation* cli =
  150. this->GeneratorTarget->GetLinkInformation(config)) {
  151. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  152. for (auto const& item : items) {
  153. cmGeneratorTarget const* linkee = item.Target;
  154. if (linkee &&
  155. !linkee->IsImported()
  156. // We can ignore the INTERFACE_LIBRARY items because
  157. // Target->GetLinkInformation already processed their
  158. // link interface and they don't have any output themselves.
  159. && linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  160. ((lang == "CXX"_s && linkee->HaveCxx20ModuleSources()) ||
  161. (lang == "Fortran"_s && linkee->HaveFortranSources(config))) &&
  162. emitted.insert(linkee).second) {
  163. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  164. std::string di = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  165. lg->GetTargetDirectory(linkee));
  166. if (lg->GetGlobalGenerator()->IsMultiConfig()) {
  167. di = cmStrCat(di, '/', config);
  168. }
  169. dirs.push_back(std::move(di));
  170. }
  171. }
  172. }
  173. return dirs;
  174. }
  175. std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
  176. const std::string& config) const
  177. {
  178. std::string compilePdbPath;
  179. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  180. return compilePdbPath;
  181. }
  182. compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
  183. if (compilePdbPath.empty()) {
  184. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  185. // A trailing slash tells the toolchain to add its default file name.
  186. compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
  187. if (this->GlobalCommonGenerator->IsMultiConfig()) {
  188. compilePdbPath += "/";
  189. compilePdbPath += config;
  190. }
  191. compilePdbPath += "/";
  192. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  193. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  194. compilePdbPath += this->GeneratorTarget->GetName();
  195. compilePdbPath += ".pdb";
  196. }
  197. }
  198. return compilePdbPath;
  199. }
  200. std::string cmCommonTargetGenerator::GetManifests(const std::string& config)
  201. {
  202. std::vector<cmSourceFile const*> manifest_srcs;
  203. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  204. std::vector<std::string> manifests;
  205. manifests.reserve(manifest_srcs.size());
  206. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  207. std::string const& manifestFlag =
  208. this->Makefile->GetDefinition("CMAKE_" + lang + "_LINKER_MANIFEST_FLAG");
  209. for (cmSourceFile const* manifest_src : manifest_srcs) {
  210. manifests.push_back(manifestFlag +
  211. this->LocalCommonGenerator->ConvertToOutputFormat(
  212. this->LocalCommonGenerator->MaybeRelativeToWorkDir(
  213. manifest_src->GetFullPath()),
  214. cmOutputConverter::SHELL));
  215. }
  216. return cmJoin(manifests, " ");
  217. }
  218. std::string cmCommonTargetGenerator::GetAIXExports(std::string const&)
  219. {
  220. std::string aixExports;
  221. if (this->GeneratorTarget->Target->IsAIX()) {
  222. if (cmValue exportAll =
  223. this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) {
  224. if (cmIsOff(*exportAll)) {
  225. aixExports = "-n";
  226. }
  227. }
  228. }
  229. return aixExports;
  230. }
  231. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  232. const std::string& lang,
  233. const char* name, bool so)
  234. {
  235. // Lookup the flag to specify the version.
  236. std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG");
  237. cmValue flag = this->Makefile->GetDefinition(fvar);
  238. // Skip if no such flag.
  239. if (!flag) {
  240. return;
  241. }
  242. // Lookup the target version information.
  243. int major;
  244. int minor;
  245. int patch;
  246. std::string prop = cmStrCat("MACHO_", name, "_VERSION");
  247. std::string fallback_prop = so ? "SOVERSION" : "VERSION";
  248. this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major,
  249. minor, patch);
  250. if (major > 0 || minor > 0 || patch > 0) {
  251. // Append the flag since a non-zero version is specified.
  252. std::ostringstream vflag;
  253. vflag << *flag << major << "." << minor << "." << patch;
  254. this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
  255. }
  256. }
  257. std::string cmCommonTargetGenerator::GetLinkerLauncher(
  258. const std::string& config)
  259. {
  260. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  261. cmValue launcherProp =
  262. this->GeneratorTarget->GetProperty(lang + "_LINKER_LAUNCHER");
  263. if (cmNonempty(launcherProp)) {
  264. // Convert ;-delimited list to single string
  265. std::vector<std::string> args = cmExpandedList(*launcherProp, true);
  266. if (!args.empty()) {
  267. args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
  268. args[0], cmOutputConverter::SHELL);
  269. for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
  270. i = this->LocalCommonGenerator->EscapeForShell(i);
  271. }
  272. return cmJoin(args, " ");
  273. }
  274. }
  275. return std::string();
  276. }
  277. bool cmCommonTargetGenerator::HaveRequiredLanguages(
  278. const std::vector<cmSourceFile const*>& sources,
  279. std::set<std::string>& languagesNeeded) const
  280. {
  281. for (cmSourceFile const* sf : sources) {
  282. languagesNeeded.insert(sf->GetLanguage());
  283. }
  284. auto* makefile = this->Makefile;
  285. auto* state = makefile->GetState();
  286. auto unary = [&state, &makefile](const std::string& lang) -> bool {
  287. const bool valid = state->GetLanguageEnabled(lang);
  288. if (!valid) {
  289. makefile->IssueMessage(
  290. MessageType::FATAL_ERROR,
  291. cmStrCat("The language ", lang,
  292. " was requested for compilation but was not enabled."
  293. " To enable a language it needs to be specified in a"
  294. " 'project' or 'enable_language' command in the root"
  295. " CMakeLists.txt"));
  296. }
  297. return valid;
  298. };
  299. return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary);
  300. }