cmStandardLevelResolver.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 "cmStandardLevelResolver.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstddef>
  7. #include <sstream>
  8. #include <stdexcept>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/iterator>
  13. #include <cm/optional>
  14. #include <cm/string_view>
  15. #include <cmext/algorithm>
  16. #include <cmext/string_view>
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmList.h"
  21. #include "cmListFileCache.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmPolicies.h"
  25. #include "cmStandardLevel.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmTarget.h"
  28. #include "cmValue.h"
  29. #include "cmake.h"
  30. namespace {
  31. #define FEATURE_STRING(F) , #F
  32. const char* const C_FEATURES[] = { nullptr FOR_EACH_C_FEATURE(
  33. FEATURE_STRING) };
  34. const char* const CXX_FEATURES[] = { nullptr FOR_EACH_CXX_FEATURE(
  35. FEATURE_STRING) };
  36. const char* const CUDA_FEATURES[] = { nullptr FOR_EACH_CUDA_FEATURE(
  37. FEATURE_STRING) };
  38. const char* const HIP_FEATURES[] = { nullptr FOR_EACH_HIP_FEATURE(
  39. FEATURE_STRING) };
  40. #undef FEATURE_STRING
  41. int ParseStd(std::string const& level)
  42. {
  43. try {
  44. return std::stoi(level);
  45. } catch (std::invalid_argument&) {
  46. // Fall through to use an invalid value.
  47. }
  48. return -1;
  49. }
  50. struct StandardLevelComputer
  51. {
  52. explicit StandardLevelComputer(std::string lang, std::vector<int> levels,
  53. std::vector<std::string> levelsStr)
  54. : Language(std::move(lang))
  55. , Levels(std::move(levels))
  56. , LevelsAsStrings(std::move(levelsStr))
  57. {
  58. assert(this->Levels.size() == this->LevelsAsStrings.size());
  59. }
  60. // Note that the logic here is shadowed in `GetEffectiveStandard`; if one is
  61. // changed, the other needs changed as well.
  62. std::string GetCompileOptionDef(cmMakefile* makefile,
  63. cmGeneratorTarget const* target,
  64. std::string const& config) const
  65. {
  66. const auto& stds = this->Levels;
  67. const auto& stdsStrings = this->LevelsAsStrings;
  68. cmValue defaultStd = makefile->GetDefinition(
  69. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  70. if (!cmNonempty(defaultStd)) {
  71. // this compiler has no notion of language standard levels
  72. return std::string{};
  73. }
  74. cmPolicies::PolicyStatus const cmp0128{ makefile->GetPolicyStatus(
  75. cmPolicies::CMP0128) };
  76. bool const defaultExt{ cmIsOn(*makefile->GetDefinition(
  77. cmStrCat("CMAKE_", this->Language, "_EXTENSIONS_DEFAULT"))) };
  78. bool ext = true;
  79. if (cmp0128 == cmPolicies::NEW) {
  80. ext = defaultExt;
  81. }
  82. if (cmValue extPropValue = target->GetLanguageExtensions(this->Language)) {
  83. ext = cmIsOn(*extPropValue);
  84. }
  85. std::string const type{ ext ? "EXTENSION" : "STANDARD" };
  86. cmValue standardProp = target->GetLanguageStandard(this->Language, config);
  87. if (!standardProp) {
  88. if (cmp0128 == cmPolicies::NEW) {
  89. // Add extension flag if compiler's default doesn't match.
  90. if (ext != defaultExt) {
  91. return cmStrCat("CMAKE_", this->Language, *defaultStd, '_', type,
  92. "_COMPILE_OPTION");
  93. }
  94. } else {
  95. if (cmp0128 == cmPolicies::WARN &&
  96. makefile->PolicyOptionalWarningEnabled(
  97. "CMAKE_POLICY_WARNING_CMP0128") &&
  98. ext != defaultExt) {
  99. const char* state{};
  100. if (ext) {
  101. if (!makefile->GetDefinition(cmStrCat(
  102. "CMAKE_", this->Language, "_EXTENSION_COMPILE_OPTION"))) {
  103. state = "enabled";
  104. }
  105. } else {
  106. state = "disabled";
  107. }
  108. if (state) {
  109. makefile->IssueMessage(
  110. MessageType::AUTHOR_WARNING,
  111. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0128),
  112. "\nFor compatibility with older versions of CMake, "
  113. "compiler extensions won't be ",
  114. state, '.'));
  115. }
  116. }
  117. if (ext) {
  118. return cmStrCat("CMAKE_", this->Language,
  119. "_EXTENSION_COMPILE_OPTION");
  120. }
  121. }
  122. return std::string{};
  123. }
  124. if (target->GetLanguageStandardRequired(this->Language)) {
  125. std::string option_flag = cmStrCat(
  126. "CMAKE_", this->Language, *standardProp, '_', type, "_COMPILE_OPTION");
  127. cmValue opt = target->Target->GetMakefile()->GetDefinition(option_flag);
  128. if (!opt) {
  129. std::ostringstream e;
  130. e << "Target \"" << target->GetName()
  131. << "\" requires the language "
  132. "dialect \""
  133. << this->Language << *standardProp << "\" "
  134. << (ext ? "(with compiler extensions)" : "")
  135. << ". But the current compiler \""
  136. << makefile->GetSafeDefinition(
  137. cmStrCat("CMAKE_", this->Language, "_COMPILER_ID"))
  138. << "\" does not support this, or "
  139. "CMake does not know the flags to enable it.";
  140. makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  141. }
  142. return option_flag;
  143. }
  144. // If the request matches the compiler's defaults we don't need to add
  145. // anything.
  146. if (*standardProp == *defaultStd && ext == defaultExt) {
  147. if (cmp0128 == cmPolicies::NEW) {
  148. return std::string{};
  149. }
  150. if (cmp0128 == cmPolicies::WARN &&
  151. makefile->PolicyOptionalWarningEnabled(
  152. "CMAKE_POLICY_WARNING_CMP0128")) {
  153. makefile->IssueMessage(
  154. MessageType::AUTHOR_WARNING,
  155. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0128),
  156. "\nFor compatibility with older versions of CMake, "
  157. "unnecessary flags for language standard or compiler "
  158. "extensions may be added."));
  159. }
  160. }
  161. std::string standardStr(*standardProp);
  162. if (this->Language == "CUDA"_s && standardStr == "98"_s) {
  163. standardStr = "03";
  164. }
  165. auto stdIt =
  166. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(standardStr));
  167. if (stdIt == cm::cend(stds)) {
  168. std::string e =
  169. cmStrCat(this->Language, "_STANDARD is set to invalid value '",
  170. standardStr, '\'');
  171. makefile->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR, e,
  172. target->GetBacktrace());
  173. return std::string{};
  174. }
  175. auto defaultStdIt =
  176. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(*defaultStd));
  177. if (defaultStdIt == cm::cend(stds)) {
  178. std::string e = cmStrCat("CMAKE_", this->Language,
  179. "_STANDARD_DEFAULT is set to invalid value '",
  180. *defaultStd, '\'');
  181. makefile->IssueMessage(MessageType::INTERNAL_ERROR, e);
  182. return std::string{};
  183. }
  184. // If the standard requested is older than the compiler's default or the
  185. // extension mode doesn't match then we need to use a flag.
  186. if ((cmp0128 != cmPolicies::NEW && stdIt <= defaultStdIt) ||
  187. (cmp0128 == cmPolicies::NEW &&
  188. (stdIt < defaultStdIt || ext != defaultExt))) {
  189. auto offset = std::distance(cm::cbegin(stds), stdIt);
  190. return cmStrCat("CMAKE_", this->Language, stdsStrings[offset], '_', type,
  191. "_COMPILE_OPTION");
  192. }
  193. // The compiler's default is at least as new as the requested standard,
  194. // and the requested standard is not required. Decay to the newest
  195. // standard for which a flag is defined.
  196. for (; defaultStdIt < stdIt; --stdIt) {
  197. auto offset = std::distance(cm::cbegin(stds), stdIt);
  198. std::string option_flag =
  199. cmStrCat("CMAKE_", this->Language, stdsStrings[offset], '_', type,
  200. "_COMPILE_OPTION");
  201. if (target->Target->GetMakefile()->GetDefinition(option_flag)) {
  202. return option_flag;
  203. }
  204. }
  205. return std::string{};
  206. }
  207. std::string GetEffectiveStandard(cmMakefile* makefile,
  208. cmGeneratorTarget const* target,
  209. std::string const& config) const
  210. {
  211. const auto& stds = this->Levels;
  212. const auto& stdsStrings = this->LevelsAsStrings;
  213. cmValue defaultStd = makefile->GetDefinition(
  214. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  215. if (!cmNonempty(defaultStd)) {
  216. // this compiler has no notion of language standard levels
  217. return std::string{};
  218. }
  219. cmPolicies::PolicyStatus const cmp0128{ makefile->GetPolicyStatus(
  220. cmPolicies::CMP0128) };
  221. bool const defaultExt{ cmIsOn(*makefile->GetDefinition(
  222. cmStrCat("CMAKE_", this->Language, "_EXTENSIONS_DEFAULT"))) };
  223. bool ext = true;
  224. if (cmp0128 == cmPolicies::NEW) {
  225. ext = defaultExt;
  226. }
  227. if (cmValue extPropValue = target->GetLanguageExtensions(this->Language)) {
  228. ext = cmIsOn(*extPropValue);
  229. }
  230. std::string const type{ ext ? "EXTENSION" : "STANDARD" };
  231. cmValue standardProp = target->GetLanguageStandard(this->Language, config);
  232. if (!standardProp) {
  233. if (cmp0128 == cmPolicies::NEW) {
  234. // Add extension flag if compiler's default doesn't match.
  235. if (ext != defaultExt) {
  236. return *defaultStd;
  237. }
  238. } else {
  239. if (ext) {
  240. return *defaultStd;
  241. }
  242. }
  243. return std::string{};
  244. }
  245. if (target->GetLanguageStandardRequired(this->Language)) {
  246. return *standardProp;
  247. }
  248. // If the request matches the compiler's defaults we don't need to add
  249. // anything.
  250. if (*standardProp == *defaultStd && ext == defaultExt) {
  251. if (cmp0128 == cmPolicies::NEW) {
  252. return std::string{};
  253. }
  254. }
  255. std::string standardStr(*standardProp);
  256. if (this->Language == "CUDA"_s && standardStr == "98"_s) {
  257. standardStr = "03";
  258. }
  259. auto stdIt =
  260. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(standardStr));
  261. if (stdIt == cm::cend(stds)) {
  262. return std::string{};
  263. }
  264. auto defaultStdIt =
  265. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(*defaultStd));
  266. if (defaultStdIt == cm::cend(stds)) {
  267. return std::string{};
  268. }
  269. // If the standard requested is older than the compiler's default or the
  270. // extension mode doesn't match then we need to use a flag.
  271. if ((cmp0128 != cmPolicies::NEW && stdIt <= defaultStdIt) ||
  272. (cmp0128 == cmPolicies::NEW &&
  273. (stdIt < defaultStdIt || ext != defaultExt))) {
  274. auto offset = std::distance(cm::cbegin(stds), stdIt);
  275. return stdsStrings[offset];
  276. }
  277. // The compiler's default is at least as new as the requested standard,
  278. // and the requested standard is not required. Decay to the newest
  279. // standard for which a flag is defined.
  280. for (; defaultStdIt < stdIt; --stdIt) {
  281. auto offset = std::distance(cm::cbegin(stds), stdIt);
  282. std::string option_flag =
  283. cmStrCat("CMAKE_", this->Language, stdsStrings[offset], '_', type,
  284. "_COMPILE_OPTION");
  285. if (target->Target->GetMakefile()->GetDefinition(option_flag)) {
  286. return stdsStrings[offset];
  287. }
  288. }
  289. return std::string{};
  290. }
  291. bool GetNewRequiredStandard(cmMakefile* makefile,
  292. std::string const& targetName,
  293. const std::string& feature,
  294. cmValue currentLangStandardValue,
  295. std::string& newRequiredStandard,
  296. std::string* error) const
  297. {
  298. if (currentLangStandardValue) {
  299. newRequiredStandard = *currentLangStandardValue;
  300. } else {
  301. newRequiredStandard.clear();
  302. }
  303. cm::optional<cmStandardLevel> needed =
  304. this->HighestStandardNeeded(makefile, feature);
  305. cmValue existingStandard = currentLangStandardValue;
  306. if (!existingStandard) {
  307. cmValue defaultStandard = makefile->GetDefinition(
  308. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  309. if (cmNonempty(defaultStandard)) {
  310. existingStandard = defaultStandard;
  311. }
  312. }
  313. auto existingLevelIter = cm::cend(this->Levels);
  314. if (existingStandard) {
  315. existingLevelIter =
  316. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  317. ParseStd(*existingStandard));
  318. if (existingLevelIter == cm::cend(this->Levels)) {
  319. const std::string e =
  320. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  321. targetName, "\" contained an invalid value: \"",
  322. *existingStandard, "\".");
  323. if (error) {
  324. *error = e;
  325. } else {
  326. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  327. }
  328. return false;
  329. }
  330. }
  331. if (needed) {
  332. // Ensure the C++ language level is high enough to support
  333. // the needed C++ features.
  334. if (existingLevelIter == cm::cend(this->Levels) ||
  335. existingLevelIter < this->Levels.begin() + needed->Index()) {
  336. newRequiredStandard = this->LevelsAsStrings[needed->Index()];
  337. }
  338. }
  339. return true;
  340. }
  341. bool HaveStandardAvailable(cmMakefile* makefile,
  342. cmGeneratorTarget const* target,
  343. std::string const& config,
  344. std::string const& feature) const
  345. {
  346. cmValue defaultStandard = makefile->GetDefinition(
  347. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  348. if (!defaultStandard) {
  349. makefile->IssueMessage(
  350. MessageType::INTERNAL_ERROR,
  351. cmStrCat("CMAKE_", this->Language,
  352. "_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  353. "not fully configured for this compiler."));
  354. // Return true so the caller does not try to lookup the default standard.
  355. return true;
  356. }
  357. // convert defaultStandard to an integer
  358. if (std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  359. ParseStd(*defaultStandard)) == cm::cend(this->Levels)) {
  360. const std::string e = cmStrCat("The CMAKE_", this->Language,
  361. "_STANDARD_DEFAULT variable contains an "
  362. "invalid value: \"",
  363. *defaultStandard, "\".");
  364. makefile->IssueMessage(MessageType::INTERNAL_ERROR, e);
  365. return false;
  366. }
  367. cmValue existingStandard =
  368. target->GetLanguageStandard(this->Language, config);
  369. if (!existingStandard) {
  370. existingStandard = defaultStandard;
  371. }
  372. auto existingLevelIter =
  373. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  374. ParseStd(*existingStandard));
  375. if (existingLevelIter == cm::cend(this->Levels)) {
  376. const std::string e =
  377. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  378. target->GetName(), "\" contained an invalid value: \"",
  379. *existingStandard, "\".");
  380. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  381. return false;
  382. }
  383. cm::optional<cmStandardLevel> needed =
  384. this->HighestStandardNeeded(makefile, feature);
  385. return !needed ||
  386. (this->Levels.begin() + needed->Index()) <= existingLevelIter;
  387. }
  388. cm::optional<cmStandardLevel> HighestStandardNeeded(
  389. cmMakefile* makefile, std::string const& feature) const
  390. {
  391. std::string prefix = cmStrCat("CMAKE_", this->Language);
  392. cm::optional<cmStandardLevel> maxLevel;
  393. for (size_t i = 0; i < this->Levels.size(); ++i) {
  394. if (cmValue prop = makefile->GetDefinition(
  395. cmStrCat(prefix, this->LevelsAsStrings[i], "_COMPILE_FEATURES"))) {
  396. cmList props{ *prop };
  397. if (cm::contains(props, feature)) {
  398. maxLevel = cmStandardLevel(i);
  399. }
  400. }
  401. }
  402. return maxLevel;
  403. }
  404. bool IsLaterStandard(int lhs, int rhs) const
  405. {
  406. auto rhsIt =
  407. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels), rhs);
  408. return std::find(rhsIt, cm::cend(this->Levels), lhs) !=
  409. cm::cend(this->Levels);
  410. }
  411. std::string Language;
  412. std::vector<int> Levels;
  413. std::vector<std::string> LevelsAsStrings;
  414. };
  415. std::unordered_map<std::string,
  416. StandardLevelComputer> const StandardComputerMapping = {
  417. { "C",
  418. StandardLevelComputer{
  419. "C", std::vector<int>{ 90, 99, 11, 17, 23 },
  420. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  421. { "CXX",
  422. StandardLevelComputer{
  423. "CXX", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  424. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } },
  425. { "CUDA",
  426. StandardLevelComputer{
  427. "CUDA", std::vector<int>{ 03, 11, 14, 17, 20, 23, 26 },
  428. std::vector<std::string>{ "03", "11", "14", "17", "20", "23", "26" } } },
  429. { "OBJC",
  430. StandardLevelComputer{
  431. "OBJC", std::vector<int>{ 90, 99, 11, 17, 23 },
  432. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  433. { "OBJCXX",
  434. StandardLevelComputer{
  435. "OBJCXX", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  436. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } },
  437. { "HIP",
  438. StandardLevelComputer{
  439. "HIP", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  440. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } }
  441. };
  442. }
  443. std::string cmStandardLevelResolver::GetCompileOptionDef(
  444. cmGeneratorTarget const* target, std::string const& lang,
  445. std::string const& config) const
  446. {
  447. const auto& mapping = StandardComputerMapping.find(lang);
  448. if (mapping == cm::cend(StandardComputerMapping)) {
  449. return std::string{};
  450. }
  451. return mapping->second.GetCompileOptionDef(this->Makefile, target, config);
  452. }
  453. std::string cmStandardLevelResolver::GetEffectiveStandard(
  454. cmGeneratorTarget const* target, std::string const& lang,
  455. std::string const& config) const
  456. {
  457. const auto& mapping = StandardComputerMapping.find(lang);
  458. if (mapping == cm::cend(StandardComputerMapping)) {
  459. return std::string{};
  460. }
  461. return mapping->second.GetEffectiveStandard(this->Makefile, target, config);
  462. }
  463. bool cmStandardLevelResolver::AddRequiredTargetFeature(
  464. cmTarget* target, const std::string& feature, std::string* error) const
  465. {
  466. if (cmGeneratorExpression::Find(feature) != std::string::npos) {
  467. target->AppendProperty("COMPILE_FEATURES", feature,
  468. this->Makefile->GetBacktrace());
  469. return true;
  470. }
  471. std::string lang;
  472. if (!this->CheckCompileFeaturesAvailable(target->GetName(), feature, lang,
  473. error)) {
  474. return false;
  475. }
  476. target->AppendProperty("COMPILE_FEATURES", feature,
  477. this->Makefile->GetBacktrace());
  478. // FIXME: Add a policy to avoid updating the <LANG>_STANDARD target
  479. // property due to COMPILE_FEATURES. The language standard selection
  480. // should be done purely at generate time based on whatever the project
  481. // code put in these properties explicitly. That is mostly true now,
  482. // but for compatibility we need to continue updating the property here.
  483. std::string newRequiredStandard;
  484. bool succeeded = this->GetNewRequiredStandard(
  485. target->GetName(), feature,
  486. target->GetProperty(cmStrCat(lang, "_STANDARD")), newRequiredStandard,
  487. error);
  488. if (!newRequiredStandard.empty()) {
  489. target->SetProperty(cmStrCat(lang, "_STANDARD"), newRequiredStandard);
  490. }
  491. return succeeded;
  492. }
  493. bool cmStandardLevelResolver::CheckCompileFeaturesAvailable(
  494. const std::string& targetName, const std::string& feature, std::string& lang,
  495. std::string* error) const
  496. {
  497. if (!this->CompileFeatureKnown(targetName, feature, lang, error)) {
  498. return false;
  499. }
  500. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  501. return true;
  502. }
  503. cmValue features = this->CompileFeaturesAvailable(lang, error);
  504. if (!features) {
  505. return false;
  506. }
  507. cmList availableFeatures{ features };
  508. if (!cm::contains(availableFeatures, feature)) {
  509. std::ostringstream e;
  510. e << "The compiler feature \"" << feature << "\" is not known to " << lang
  511. << " compiler\n\""
  512. << this->Makefile->GetSafeDefinition(
  513. cmStrCat("CMAKE_", lang, "_COMPILER_ID"))
  514. << "\"\nversion "
  515. << this->Makefile->GetSafeDefinition(
  516. cmStrCat("CMAKE_", lang, "_COMPILER_VERSION"))
  517. << '.';
  518. if (error) {
  519. *error = e.str();
  520. } else {
  521. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  522. }
  523. return false;
  524. }
  525. return true;
  526. }
  527. bool cmStandardLevelResolver::CompileFeatureKnown(
  528. const std::string& targetName, const std::string& feature, std::string& lang,
  529. std::string* error) const
  530. {
  531. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  532. bool isCFeature =
  533. std::find_if(cm::cbegin(C_FEATURES) + 1, cm::cend(C_FEATURES),
  534. cmStrCmp(feature)) != cm::cend(C_FEATURES);
  535. if (isCFeature) {
  536. lang = "C";
  537. return true;
  538. }
  539. bool isCxxFeature =
  540. std::find_if(cm::cbegin(CXX_FEATURES) + 1, cm::cend(CXX_FEATURES),
  541. cmStrCmp(feature)) != cm::cend(CXX_FEATURES);
  542. if (isCxxFeature) {
  543. lang = "CXX";
  544. return true;
  545. }
  546. bool isCudaFeature =
  547. std::find_if(cm::cbegin(CUDA_FEATURES) + 1, cm::cend(CUDA_FEATURES),
  548. cmStrCmp(feature)) != cm::cend(CUDA_FEATURES);
  549. if (isCudaFeature) {
  550. lang = "CUDA";
  551. return true;
  552. }
  553. bool isHIPFeature =
  554. std::find_if(cm::cbegin(HIP_FEATURES) + 1, cm::cend(HIP_FEATURES),
  555. cmStrCmp(feature)) != cm::cend(HIP_FEATURES);
  556. if (isHIPFeature) {
  557. lang = "HIP";
  558. return true;
  559. }
  560. std::ostringstream e;
  561. if (error) {
  562. e << "specified";
  563. } else {
  564. e << "Specified";
  565. }
  566. e << " unknown feature \"" << feature
  567. << "\" for "
  568. "target \""
  569. << targetName << "\".";
  570. if (error) {
  571. *error = e.str();
  572. } else {
  573. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  574. }
  575. return false;
  576. }
  577. cmValue cmStandardLevelResolver::CompileFeaturesAvailable(
  578. const std::string& lang, std::string* error) const
  579. {
  580. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  581. std::ostringstream e;
  582. if (error) {
  583. e << "cannot";
  584. } else {
  585. e << "Cannot";
  586. }
  587. e << " use features from non-enabled language " << lang;
  588. if (error) {
  589. *error = e.str();
  590. } else {
  591. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  592. }
  593. return nullptr;
  594. }
  595. cmValue featuresKnown = this->Makefile->GetDefinition(
  596. cmStrCat("CMAKE_", lang, "_COMPILE_FEATURES"));
  597. if (!cmNonempty(featuresKnown)) {
  598. std::ostringstream e;
  599. if (error) {
  600. e << "no";
  601. } else {
  602. e << "No";
  603. }
  604. e << " known features for " << lang << " compiler\n\""
  605. << this->Makefile->GetSafeDefinition(
  606. cmStrCat("CMAKE_", lang, "_COMPILER_ID"))
  607. << "\"\nversion "
  608. << this->Makefile->GetSafeDefinition(
  609. cmStrCat("CMAKE_", lang, "_COMPILER_VERSION"))
  610. << '.';
  611. if (error) {
  612. *error = e.str();
  613. } else {
  614. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  615. }
  616. return nullptr;
  617. }
  618. return featuresKnown;
  619. }
  620. bool cmStandardLevelResolver::GetNewRequiredStandard(
  621. const std::string& targetName, const std::string& feature,
  622. cmValue currentLangStandardValue, std::string& newRequiredStandard,
  623. std::string* error) const
  624. {
  625. std::string lang;
  626. if (!this->CheckCompileFeaturesAvailable(targetName, feature, lang, error)) {
  627. return false;
  628. }
  629. auto mapping = StandardComputerMapping.find(lang);
  630. if (mapping != cm::cend(StandardComputerMapping)) {
  631. return mapping->second.GetNewRequiredStandard(
  632. this->Makefile, targetName, feature, currentLangStandardValue,
  633. newRequiredStandard, error);
  634. }
  635. return false;
  636. }
  637. bool cmStandardLevelResolver::HaveStandardAvailable(
  638. cmGeneratorTarget const* target, std::string const& lang,
  639. std::string const& config, const std::string& feature) const
  640. {
  641. auto mapping = StandardComputerMapping.find(lang);
  642. if (mapping != cm::cend(StandardComputerMapping)) {
  643. return mapping->second.HaveStandardAvailable(this->Makefile, target,
  644. config, feature);
  645. }
  646. return false;
  647. }
  648. bool cmStandardLevelResolver::IsLaterStandard(std::string const& lang,
  649. std::string const& lhs,
  650. std::string const& rhs) const
  651. {
  652. auto mapping = StandardComputerMapping.find(lang);
  653. if (mapping != cm::cend(StandardComputerMapping)) {
  654. return mapping->second.IsLaterStandard(std::stoi(lhs), std::stoi(rhs));
  655. }
  656. return false;
  657. }