cmCommonTargetGenerator.cxx 12 KB

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