cmCommonTargetGenerator.cxx 11 KB

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