1
0

cmCommonTargetGenerator.cxx 19 KB

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