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