cmCommonTargetGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 "cmList.h"
  16. #include "cmLocalCommonGenerator.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmMessageType.h"
  20. #include "cmOutputConverter.h"
  21. #include "cmRange.h"
  22. #include "cmSourceFile.h"
  23. #include "cmState.h"
  24. #include "cmStateTypes.h"
  25. #include "cmStringAlgorithms.h"
  26. #include "cmSystemTools.h"
  27. #include "cmValue.h"
  28. cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
  29. : GeneratorTarget(gt)
  30. , Makefile(gt->Makefile)
  31. , LocalCommonGenerator(
  32. static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
  33. , GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>(
  34. gt->LocalGenerator->GetGlobalGenerator()))
  35. , ConfigNames(this->LocalCommonGenerator->GetConfigNames())
  36. {
  37. }
  38. cmCommonTargetGenerator::~cmCommonTargetGenerator() = default;
  39. std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const
  40. {
  41. return this->ConfigNames;
  42. }
  43. cmValue cmCommonTargetGenerator::GetFeature(const std::string& feature,
  44. const std::string& config)
  45. {
  46. return this->GeneratorTarget->GetFeature(feature, config);
  47. }
  48. void cmCommonTargetGenerator::AppendFortranFormatFlags(
  49. std::string& flags, cmSourceFile const& source)
  50. {
  51. const std::string srcfmt = source.GetSafeProperty("Fortran_FORMAT");
  52. cmOutputConverter::FortranFormat format =
  53. cmOutputConverter::GetFortranFormat(srcfmt);
  54. if (format == cmOutputConverter::FortranFormatNone) {
  55. std::string const& tgtfmt =
  56. this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT");
  57. format = cmOutputConverter::GetFortranFormat(tgtfmt);
  58. }
  59. const char* var = nullptr;
  60. switch (format) {
  61. case cmOutputConverter::FortranFormatFixed:
  62. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
  63. break;
  64. case cmOutputConverter::FortranFormatFree:
  65. var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
  66. break;
  67. default:
  68. break;
  69. }
  70. if (var) {
  71. this->LocalCommonGenerator->AppendFlags(
  72. flags, this->Makefile->GetSafeDefinition(var));
  73. }
  74. }
  75. void cmCommonTargetGenerator::AppendFortranPreprocessFlags(
  76. std::string& flags, cmSourceFile const& source,
  77. PreprocessFlagsRequired requires_pp)
  78. {
  79. const std::string srcpp = source.GetSafeProperty("Fortran_PREPROCESS");
  80. cmOutputConverter::FortranPreprocess preprocess =
  81. cmOutputConverter::GetFortranPreprocess(srcpp);
  82. if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
  83. std::string const& tgtpp =
  84. this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
  85. preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
  86. }
  87. const char* var = nullptr;
  88. switch (preprocess) {
  89. case cmOutputConverter::FortranPreprocess::Needed:
  90. if (requires_pp == PreprocessFlagsRequired::YES) {
  91. var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON";
  92. }
  93. break;
  94. case cmOutputConverter::FortranPreprocess::NotNeeded:
  95. var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF";
  96. break;
  97. default:
  98. break;
  99. }
  100. if (var) {
  101. this->LocalCommonGenerator->AppendCompileOptions(
  102. flags, this->Makefile->GetSafeDefinition(var));
  103. }
  104. }
  105. std::string cmCommonTargetGenerator::GetFlags(const std::string& l,
  106. const std::string& config,
  107. const std::string& arch)
  108. {
  109. const std::string key = config + arch;
  110. auto i = this->Configs[key].FlagsByLanguage.find(l);
  111. if (i == this->Configs[key].FlagsByLanguage.end()) {
  112. std::string flags;
  113. this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget,
  114. config, l, flags, arch);
  115. ByLanguageMap::value_type entry(l, flags);
  116. i = this->Configs[key].FlagsByLanguage.insert(entry).first;
  117. }
  118. return i->second;
  119. }
  120. std::string cmCommonTargetGenerator::GetDefines(const std::string& l,
  121. const std::string& config)
  122. {
  123. auto i = this->Configs[config].DefinesByLanguage.find(l);
  124. if (i == this->Configs[config].DefinesByLanguage.end()) {
  125. std::set<std::string> defines;
  126. this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config,
  127. l, defines);
  128. std::string definesString;
  129. this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
  130. ByLanguageMap::value_type entry(l, definesString);
  131. i = this->Configs[config].DefinesByLanguage.insert(entry).first;
  132. }
  133. return i->second;
  134. }
  135. std::string cmCommonTargetGenerator::GetIncludes(std::string const& l,
  136. const std::string& config)
  137. {
  138. auto i = this->Configs[config].IncludesByLanguage.find(l);
  139. if (i == this->Configs[config].IncludesByLanguage.end()) {
  140. std::string includes;
  141. this->AddIncludeFlags(includes, l, config);
  142. ByLanguageMap::value_type entry(l, includes);
  143. i = this->Configs[config].IncludesByLanguage.insert(entry).first;
  144. }
  145. return i->second;
  146. }
  147. std::vector<std::string> cmCommonTargetGenerator::GetLinkedTargetDirectories(
  148. const std::string& lang, const std::string& config) const
  149. {
  150. std::vector<std::string> dirs;
  151. std::set<cmGeneratorTarget const*> emitted;
  152. cmGlobalCommonGenerator* const gg = this->GlobalCommonGenerator;
  153. if (cmComputeLinkInformation* cli =
  154. this->GeneratorTarget->GetLinkInformation(config)) {
  155. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  156. for (auto const& item : items) {
  157. cmGeneratorTarget const* linkee = item.Target;
  158. if (linkee &&
  159. !linkee->IsImported()
  160. // Skip targets that build after this one in a static lib cycle.
  161. && gg->TargetOrderIndexLess(linkee, this->GeneratorTarget)
  162. // We can ignore the INTERFACE_LIBRARY items because
  163. // Target->GetLinkInformation already processed their
  164. // link interface and they don't have any output themselves.
  165. && (linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY
  166. // Synthesized targets may have relevant rules.
  167. || linkee->IsSynthetic()) &&
  168. ((lang == "CXX"_s && linkee->HaveCxx20ModuleSources()) ||
  169. (lang == "Fortran"_s && linkee->HaveFortranSources(config))) &&
  170. emitted.insert(linkee).second) {
  171. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  172. std::string di = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  173. lg->GetTargetDirectory(linkee));
  174. if (lg->GetGlobalGenerator()->IsMultiConfig()) {
  175. di = cmStrCat(di, '/', config);
  176. }
  177. dirs.push_back(std::move(di));
  178. }
  179. }
  180. }
  181. return dirs;
  182. }
  183. std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
  184. const std::string& config) const
  185. {
  186. std::string compilePdbPath;
  187. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  188. return compilePdbPath;
  189. }
  190. compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
  191. if (compilePdbPath.empty()) {
  192. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  193. // A trailing slash tells the toolchain to add its default file name.
  194. compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
  195. if (this->GlobalCommonGenerator->IsMultiConfig()) {
  196. compilePdbPath += "/";
  197. compilePdbPath += config;
  198. }
  199. compilePdbPath += "/";
  200. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  201. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  202. compilePdbPath += this->GeneratorTarget->GetName();
  203. compilePdbPath += ".pdb";
  204. }
  205. }
  206. return compilePdbPath;
  207. }
  208. std::string cmCommonTargetGenerator::GetManifests(const std::string& config)
  209. {
  210. std::vector<cmSourceFile const*> manifest_srcs;
  211. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  212. std::vector<std::string> manifests;
  213. manifests.reserve(manifest_srcs.size());
  214. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  215. std::string const& manifestFlag =
  216. this->Makefile->GetDefinition("CMAKE_" + lang + "_LINKER_MANIFEST_FLAG");
  217. for (cmSourceFile const* manifest_src : manifest_srcs) {
  218. manifests.push_back(manifestFlag +
  219. this->LocalCommonGenerator->ConvertToOutputFormat(
  220. this->LocalCommonGenerator->MaybeRelativeToWorkDir(
  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->IsAIX()) {
  230. if (cmValue 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. cmValue 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. }
  265. std::string cmCommonTargetGenerator::GetCompilerLauncher(
  266. std::string const& lang, std::string const& config)
  267. {
  268. std::string compilerLauncher;
  269. if (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
  270. lang == "HIP" || lang == "ISPC" || lang == "OBJC" || lang == "OBJCXX") {
  271. std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
  272. cmValue clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  273. std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate(
  274. *clauncher, this->GeneratorTarget->GetLocalGenerator(), config,
  275. this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
  276. if (!evaluatedClauncher.empty()) {
  277. compilerLauncher = evaluatedClauncher;
  278. }
  279. }
  280. return compilerLauncher;
  281. }
  282. std::string cmCommonTargetGenerator::GenerateCodeCheckRules(
  283. cmSourceFile const& source, std::string& compilerLauncher,
  284. std::string const& cmakeCmd, std::string const& config,
  285. std::function<std::string(std::string const&)> const& pathConverter)
  286. {
  287. auto const lang = source.GetLanguage();
  288. std::string tidy;
  289. std::string iwyu;
  290. std::string cpplint;
  291. std::string cppcheck;
  292. auto evaluateProp = [&](std::string const& prop) -> std::string {
  293. auto const value = this->GeneratorTarget->GetProperty(prop);
  294. if (!value) {
  295. return std::string{};
  296. }
  297. auto evaluatedProp = cmGeneratorExpression::Evaluate(
  298. *value, this->GeneratorTarget->GetLocalGenerator(), config,
  299. this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
  300. if (!evaluatedProp.empty()) {
  301. return evaluatedProp;
  302. }
  303. return *value;
  304. };
  305. std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY");
  306. tidy = evaluateProp(tidy_prop);
  307. if (lang == "C" || lang == "CXX") {
  308. std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE");
  309. iwyu = evaluateProp(iwyu_prop);
  310. std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT");
  311. cpplint = evaluateProp(cpplint_prop);
  312. std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK");
  313. cppcheck = evaluateProp(cppcheck_prop);
  314. }
  315. if (cmNonempty(iwyu) || cmNonempty(tidy) || cmNonempty(cpplint) ||
  316. cmNonempty(cppcheck)) {
  317. std::string code_check = cmakeCmd + " -E __run_co_compile";
  318. if (!compilerLauncher.empty()) {
  319. // In __run_co_compile case the launcher command is supplied
  320. // via --launcher=<maybe-list> and consumed
  321. code_check += " --launcher=";
  322. code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  323. compilerLauncher);
  324. compilerLauncher.clear();
  325. }
  326. if (cmNonempty(iwyu)) {
  327. code_check += " --iwyu=";
  328. // Only add --driver-mode if it is not already specified, as adding
  329. // it unconditionally might override a user-specified driver-mode
  330. if (iwyu.find("--driver-mode=") == std::string::npos) {
  331. cmValue const p = this->Makefile->GetDefinition(
  332. cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE"));
  333. std::string driverMode;
  334. if (cmNonempty(p)) {
  335. driverMode = *p;
  336. } else {
  337. driverMode = lang == "C" ? "gcc" : "g++";
  338. }
  339. code_check +=
  340. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  341. cmStrCat(iwyu, ";--driver-mode=", driverMode));
  342. } else {
  343. code_check +=
  344. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(iwyu);
  345. }
  346. }
  347. if (cmNonempty(tidy)) {
  348. code_check += " --tidy=";
  349. cmValue const p = this->Makefile->GetDefinition(
  350. "CMAKE_" + lang + "_CLANG_TIDY_DRIVER_MODE");
  351. std::string driverMode;
  352. if (cmNonempty(p)) {
  353. driverMode = *p;
  354. } else {
  355. driverMode = lang == "C" ? "gcc" : "g++";
  356. }
  357. auto const generatorName = this->GeneratorTarget->GetLocalGenerator()
  358. ->GetGlobalGenerator()
  359. ->GetName();
  360. auto const clangTidyExportFixedDir =
  361. this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang);
  362. auto fixesFile = this->GetClangTidyReplacementsFilePath(
  363. clangTidyExportFixedDir, source, config);
  364. std::string exportFixes;
  365. if (!clangTidyExportFixedDir.empty()) {
  366. this->GlobalCommonGenerator->AddClangTidyExportFixesDir(
  367. clangTidyExportFixedDir);
  368. }
  369. if (generatorName.find("Make") != std::string::npos) {
  370. if (!clangTidyExportFixedDir.empty()) {
  371. this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
  372. cmSystemTools::MakeDirectory(
  373. cmSystemTools::GetFilenamePath(fixesFile));
  374. fixesFile = this->GeneratorTarget->GetLocalGenerator()
  375. ->MaybeRelativeToCurBinDir(fixesFile);
  376. exportFixes = cmStrCat(";--export-fixes=", fixesFile);
  377. }
  378. code_check +=
  379. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  380. cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
  381. exportFixes));
  382. } else if (generatorName.find("Ninja") != std::string::npos) {
  383. if (!clangTidyExportFixedDir.empty()) {
  384. this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
  385. cmSystemTools::MakeDirectory(
  386. cmSystemTools::GetFilenamePath(fixesFile));
  387. if (!pathConverter) {
  388. fixesFile = pathConverter(fixesFile);
  389. }
  390. exportFixes = cmStrCat(";--export-fixes=", fixesFile);
  391. }
  392. code_check +=
  393. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  394. cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
  395. exportFixes));
  396. }
  397. }
  398. if (cmNonempty(cpplint)) {
  399. code_check += " --cpplint=";
  400. code_check +=
  401. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cpplint);
  402. }
  403. if (cmNonempty(cppcheck)) {
  404. code_check += " --cppcheck=";
  405. code_check +=
  406. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cppcheck);
  407. }
  408. if (cmNonempty(tidy) || (cmNonempty(cpplint)) || (cmNonempty(cppcheck))) {
  409. code_check += " --source=";
  410. code_check +=
  411. this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat(
  412. source.GetFullPath(), cmOutputConverter::SHELL);
  413. }
  414. code_check += " -- ";
  415. return code_check;
  416. }
  417. return "";
  418. }
  419. std::string cmCommonTargetGenerator::GetLinkerLauncher(
  420. const std::string& config)
  421. {
  422. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  423. std::string propName = lang + "_LINKER_LAUNCHER";
  424. cmValue launcherProp = this->GeneratorTarget->GetProperty(propName);
  425. if (cmNonempty(launcherProp)) {
  426. cmGeneratorExpressionDAGChecker dagChecker(this->GeneratorTarget, propName,
  427. nullptr, nullptr);
  428. std::string evaluatedLinklauncher = cmGeneratorExpression::Evaluate(
  429. *launcherProp, this->LocalCommonGenerator, config, this->GeneratorTarget,
  430. &dagChecker, this->GeneratorTarget, lang);
  431. // Convert ;-delimited list to single string
  432. cmList args{ evaluatedLinklauncher, cmList::EmptyElements::Yes };
  433. if (!args.empty()) {
  434. args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
  435. args[0], cmOutputConverter::SHELL);
  436. for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
  437. i = this->LocalCommonGenerator->EscapeForShell(i);
  438. }
  439. return cmJoin(args, " ");
  440. }
  441. }
  442. return std::string();
  443. }
  444. bool cmCommonTargetGenerator::HaveRequiredLanguages(
  445. const std::vector<cmSourceFile const*>& sources,
  446. std::set<std::string>& languagesNeeded) const
  447. {
  448. for (cmSourceFile const* sf : sources) {
  449. languagesNeeded.insert(sf->GetLanguage());
  450. }
  451. auto* makefile = this->Makefile;
  452. auto* state = makefile->GetState();
  453. auto unary = [&state, &makefile](const std::string& lang) -> bool {
  454. const bool valid = state->GetLanguageEnabled(lang);
  455. if (!valid) {
  456. makefile->IssueMessage(
  457. MessageType::FATAL_ERROR,
  458. cmStrCat("The language ", lang,
  459. " was requested for compilation but was not enabled."
  460. " To enable a language it needs to be specified in a"
  461. " 'project' or 'enable_language' command in the root"
  462. " CMakeLists.txt"));
  463. }
  464. return valid;
  465. };
  466. return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary);
  467. }