cmCommonTargetGenerator.cxx 19 KB

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