1
0

cmCommonTargetGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. auto addLinkedTarget = [this, &lang, &config, &dirs, &emitted,
  156. gg](cmGeneratorTarget const* linkee) {
  157. if (linkee &&
  158. !linkee->IsImported()
  159. // Skip targets that build after this one in a static lib cycle.
  160. && gg->TargetOrderIndexLess(linkee, this->GeneratorTarget)
  161. // We can ignore the INTERFACE_LIBRARY items because
  162. // Target->GetLinkInformation already processed their
  163. // link interface and they don't have any output themselves.
  164. && (linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY
  165. // Synthesized targets may have relevant rules.
  166. || linkee->IsSynthetic()) &&
  167. ((lang == "CXX"_s && linkee->HaveCxx20ModuleSources()) ||
  168. (lang == "Fortran"_s && linkee->HaveFortranSources(config))) &&
  169. emitted.insert(linkee).second) {
  170. cmLocalGenerator* lg = linkee->GetLocalGenerator();
  171. std::string di = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  172. lg->GetTargetDirectory(linkee));
  173. if (lg->GetGlobalGenerator()->IsMultiConfig()) {
  174. di = cmStrCat(di, '/', config);
  175. }
  176. dirs.push_back(std::move(di));
  177. }
  178. };
  179. for (auto const& item : cli->GetItems()) {
  180. addLinkedTarget(item.Target);
  181. }
  182. for (cmGeneratorTarget const* target : cli->GetObjectLibrariesLinked()) {
  183. addLinkedTarget(target);
  184. }
  185. for (cmGeneratorTarget const* target : cli->GetExternalObjectTargets()) {
  186. addLinkedTarget(target);
  187. }
  188. }
  189. return dirs;
  190. }
  191. std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
  192. const std::string& config) const
  193. {
  194. std::string compilePdbPath;
  195. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  196. return compilePdbPath;
  197. }
  198. compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
  199. if (compilePdbPath.empty()) {
  200. // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
  201. // A trailing slash tells the toolchain to add its default file name.
  202. compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
  203. if (this->GlobalCommonGenerator->IsMultiConfig()) {
  204. compilePdbPath += "/";
  205. compilePdbPath += config;
  206. }
  207. compilePdbPath += "/";
  208. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  209. // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
  210. compilePdbPath += this->GeneratorTarget->GetName();
  211. compilePdbPath += ".pdb";
  212. }
  213. }
  214. return compilePdbPath;
  215. }
  216. std::string cmCommonTargetGenerator::GetManifests(const std::string& config)
  217. {
  218. std::vector<cmSourceFile const*> manifest_srcs;
  219. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  220. std::vector<std::string> manifests;
  221. manifests.reserve(manifest_srcs.size());
  222. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  223. std::string manifestFlag =
  224. this->Makefile->GetDefinition("CMAKE_" + lang + "_LINKER_MANIFEST_FLAG");
  225. for (cmSourceFile const* manifest_src : manifest_srcs) {
  226. manifests.push_back(manifestFlag +
  227. this->LocalCommonGenerator->ConvertToOutputFormat(
  228. this->LocalCommonGenerator->MaybeRelativeToWorkDir(
  229. manifest_src->GetFullPath()),
  230. cmOutputConverter::SHELL));
  231. }
  232. return cmJoin(manifests, " ");
  233. }
  234. std::string cmCommonTargetGenerator::GetAIXExports(std::string const&)
  235. {
  236. std::string aixExports;
  237. if (this->GeneratorTarget->IsAIX()) {
  238. if (cmValue exportAll =
  239. this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) {
  240. if (cmIsOff(*exportAll)) {
  241. aixExports = "-n";
  242. }
  243. }
  244. }
  245. return aixExports;
  246. }
  247. void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
  248. const std::string& lang,
  249. const char* name, bool so)
  250. {
  251. // Lookup the flag to specify the version.
  252. std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG");
  253. cmValue flag = this->Makefile->GetDefinition(fvar);
  254. // Skip if no such flag.
  255. if (!flag) {
  256. return;
  257. }
  258. // Lookup the target version information.
  259. int major;
  260. int minor;
  261. int patch;
  262. std::string prop = cmStrCat("MACHO_", name, "_VERSION");
  263. std::string fallback_prop = so ? "SOVERSION" : "VERSION";
  264. this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major,
  265. minor, patch);
  266. if (major > 0 || minor > 0 || patch > 0) {
  267. // Append the flag since a non-zero version is specified.
  268. std::ostringstream vflag;
  269. vflag << *flag << major << "." << minor << "." << patch;
  270. this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
  271. }
  272. }
  273. std::string cmCommonTargetGenerator::GetCompilerLauncher(
  274. std::string const& lang, std::string const& config)
  275. {
  276. std::string compilerLauncher;
  277. if (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
  278. lang == "HIP" || lang == "ISPC" || lang == "OBJC" || lang == "OBJCXX") {
  279. std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
  280. cmValue clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
  281. std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate(
  282. *clauncher, this->GeneratorTarget->GetLocalGenerator(), config,
  283. this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
  284. if (!evaluatedClauncher.empty()) {
  285. compilerLauncher = evaluatedClauncher;
  286. }
  287. }
  288. return compilerLauncher;
  289. }
  290. std::string cmCommonTargetGenerator::GenerateCodeCheckRules(
  291. cmSourceFile const& source, std::string& compilerLauncher,
  292. std::string const& cmakeCmd, std::string const& config,
  293. std::function<std::string(std::string const&)> const& pathConverter)
  294. {
  295. auto const lang = source.GetLanguage();
  296. std::string tidy;
  297. std::string iwyu;
  298. std::string cpplint;
  299. std::string cppcheck;
  300. auto evaluateProp = [&](std::string const& prop) -> std::string {
  301. auto const value = this->GeneratorTarget->GetProperty(prop);
  302. if (!value) {
  303. return std::string{};
  304. }
  305. auto evaluatedProp = cmGeneratorExpression::Evaluate(
  306. *value, this->GeneratorTarget->GetLocalGenerator(), config,
  307. this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
  308. return evaluatedProp;
  309. };
  310. std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY");
  311. tidy = evaluateProp(tidy_prop);
  312. if (lang == "C" || lang == "CXX") {
  313. std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE");
  314. iwyu = evaluateProp(iwyu_prop);
  315. std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT");
  316. cpplint = evaluateProp(cpplint_prop);
  317. std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK");
  318. cppcheck = evaluateProp(cppcheck_prop);
  319. }
  320. if (cmNonempty(iwyu) || cmNonempty(tidy) || cmNonempty(cpplint) ||
  321. cmNonempty(cppcheck)) {
  322. std::string code_check = cmakeCmd + " -E __run_co_compile";
  323. if (!compilerLauncher.empty()) {
  324. // In __run_co_compile case the launcher command is supplied
  325. // via --launcher=<maybe-list> and consumed
  326. code_check += " --launcher=";
  327. code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  328. compilerLauncher);
  329. compilerLauncher.clear();
  330. }
  331. if (cmNonempty(iwyu)) {
  332. code_check += " --iwyu=";
  333. // Only add --driver-mode if it is not already specified, as adding
  334. // it unconditionally might override a user-specified driver-mode
  335. if (iwyu.find("--driver-mode=") == std::string::npos) {
  336. cmValue const p = this->Makefile->GetDefinition(
  337. cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE"));
  338. std::string driverMode;
  339. if (cmNonempty(p)) {
  340. driverMode = *p;
  341. } else {
  342. driverMode = lang == "C" ? "gcc" : "g++";
  343. }
  344. code_check +=
  345. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  346. cmStrCat(iwyu, ";--driver-mode=", driverMode));
  347. } else {
  348. code_check +=
  349. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(iwyu);
  350. }
  351. }
  352. if (cmNonempty(tidy)) {
  353. code_check += " --tidy=";
  354. cmValue const p = this->Makefile->GetDefinition(
  355. "CMAKE_" + lang + "_CLANG_TIDY_DRIVER_MODE");
  356. std::string driverMode;
  357. if (cmNonempty(p)) {
  358. driverMode = *p;
  359. } else {
  360. driverMode = lang == "C" ? "gcc" : "g++";
  361. }
  362. auto const generatorName = this->GeneratorTarget->GetLocalGenerator()
  363. ->GetGlobalGenerator()
  364. ->GetName();
  365. auto const clangTidyExportFixedDir =
  366. this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang);
  367. auto fixesFile = this->GetClangTidyReplacementsFilePath(
  368. clangTidyExportFixedDir, source, config);
  369. std::string exportFixes;
  370. if (!clangTidyExportFixedDir.empty()) {
  371. this->GlobalCommonGenerator->AddClangTidyExportFixesDir(
  372. clangTidyExportFixedDir);
  373. }
  374. if (generatorName.find("Make") != std::string::npos) {
  375. if (!clangTidyExportFixedDir.empty()) {
  376. this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
  377. cmSystemTools::MakeDirectory(
  378. cmSystemTools::GetFilenamePath(fixesFile));
  379. fixesFile = this->GeneratorTarget->GetLocalGenerator()
  380. ->MaybeRelativeToCurBinDir(fixesFile);
  381. exportFixes = cmStrCat(";--export-fixes=", fixesFile);
  382. }
  383. code_check +=
  384. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  385. cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
  386. exportFixes));
  387. } else if (generatorName.find("Ninja") != std::string::npos) {
  388. if (!clangTidyExportFixedDir.empty()) {
  389. this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
  390. cmSystemTools::MakeDirectory(
  391. cmSystemTools::GetFilenamePath(fixesFile));
  392. if (!pathConverter) {
  393. fixesFile = pathConverter(fixesFile);
  394. }
  395. exportFixes = cmStrCat(";--export-fixes=", fixesFile);
  396. }
  397. code_check +=
  398. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
  399. cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
  400. exportFixes));
  401. }
  402. }
  403. if (cmNonempty(cpplint)) {
  404. code_check += " --cpplint=";
  405. code_check +=
  406. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cpplint);
  407. }
  408. if (cmNonempty(cppcheck)) {
  409. code_check += " --cppcheck=";
  410. code_check +=
  411. this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cppcheck);
  412. }
  413. if (cmNonempty(tidy) || (cmNonempty(cpplint)) || (cmNonempty(cppcheck))) {
  414. code_check += " --source=";
  415. code_check +=
  416. this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat(
  417. source.GetFullPath(), cmOutputConverter::SHELL);
  418. }
  419. code_check += " -- ";
  420. return code_check;
  421. }
  422. return "";
  423. }
  424. std::string cmCommonTargetGenerator::GetLinkerLauncher(
  425. const std::string& config)
  426. {
  427. std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
  428. std::string propName = lang + "_LINKER_LAUNCHER";
  429. cmValue launcherProp = this->GeneratorTarget->GetProperty(propName);
  430. if (cmNonempty(launcherProp)) {
  431. cmGeneratorExpressionDAGChecker dagChecker(this->GeneratorTarget, propName,
  432. nullptr, nullptr);
  433. std::string evaluatedLinklauncher = cmGeneratorExpression::Evaluate(
  434. *launcherProp, this->LocalCommonGenerator, config, this->GeneratorTarget,
  435. &dagChecker, this->GeneratorTarget, lang);
  436. // Convert ;-delimited list to single string
  437. cmList args{ evaluatedLinklauncher, cmList::EmptyElements::Yes };
  438. if (!args.empty()) {
  439. args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
  440. args[0], cmOutputConverter::SHELL);
  441. for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
  442. i = this->LocalCommonGenerator->EscapeForShell(i);
  443. }
  444. return cmJoin(args, " ");
  445. }
  446. }
  447. return std::string();
  448. }
  449. bool cmCommonTargetGenerator::HaveRequiredLanguages(
  450. const std::vector<cmSourceFile const*>& sources,
  451. std::set<std::string>& languagesNeeded) const
  452. {
  453. for (cmSourceFile const* sf : sources) {
  454. languagesNeeded.insert(sf->GetLanguage());
  455. }
  456. auto* makefile = this->Makefile;
  457. auto* state = makefile->GetState();
  458. auto unary = [&state, &makefile](const std::string& lang) -> bool {
  459. const bool valid = state->GetLanguageEnabled(lang);
  460. if (!valid) {
  461. makefile->IssueMessage(
  462. MessageType::FATAL_ERROR,
  463. cmStrCat("The language ", lang,
  464. " was requested for compilation but was not enabled."
  465. " To enable a language it needs to be specified in a"
  466. " 'project' or 'enable_language' command in the root"
  467. " CMakeLists.txt"));
  468. }
  469. return valid;
  470. };
  471. return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary);
  472. }