cmStandardLevelResolver.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. cm::optional<cmStandardLevel> featureLevel,
  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. cmValue existingStandard = currentLangStandardValue;
  304. if (!existingStandard) {
  305. cmValue defaultStandard = makefile->GetDefinition(
  306. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  307. if (cmNonempty(defaultStandard)) {
  308. existingStandard = defaultStandard;
  309. }
  310. }
  311. auto existingLevelIter = cm::cend(this->Levels);
  312. if (existingStandard) {
  313. existingLevelIter =
  314. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  315. ParseStd(*existingStandard));
  316. if (existingLevelIter == cm::cend(this->Levels)) {
  317. const std::string e =
  318. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  319. targetName, "\" contained an invalid value: \"",
  320. *existingStandard, "\".");
  321. if (error) {
  322. *error = e;
  323. } else {
  324. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  325. }
  326. return false;
  327. }
  328. }
  329. if (featureLevel) {
  330. // Ensure the C++ language level is high enough to support
  331. // the needed C++ features.
  332. if (existingLevelIter == cm::cend(this->Levels) ||
  333. existingLevelIter < this->Levels.begin() + featureLevel->Index()) {
  334. newRequiredStandard = this->LevelsAsStrings[featureLevel->Index()];
  335. }
  336. }
  337. return true;
  338. }
  339. bool HaveStandardAvailable(cmMakefile* makefile,
  340. cmGeneratorTarget const* target,
  341. std::string const& config,
  342. std::string const& feature) const
  343. {
  344. cmValue defaultStandard = makefile->GetDefinition(
  345. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  346. if (!defaultStandard) {
  347. makefile->IssueMessage(
  348. MessageType::INTERNAL_ERROR,
  349. cmStrCat("CMAKE_", this->Language,
  350. "_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  351. "not fully configured for this compiler."));
  352. // Return true so the caller does not try to lookup the default standard.
  353. return true;
  354. }
  355. // convert defaultStandard to an integer
  356. if (std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  357. ParseStd(*defaultStandard)) == cm::cend(this->Levels)) {
  358. const std::string e = cmStrCat("The CMAKE_", this->Language,
  359. "_STANDARD_DEFAULT variable contains an "
  360. "invalid value: \"",
  361. *defaultStandard, "\".");
  362. makefile->IssueMessage(MessageType::INTERNAL_ERROR, e);
  363. return false;
  364. }
  365. cmValue existingStandard =
  366. target->GetLanguageStandard(this->Language, config);
  367. if (!existingStandard) {
  368. existingStandard = defaultStandard;
  369. }
  370. auto existingLevelIter =
  371. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  372. ParseStd(*existingStandard));
  373. if (existingLevelIter == cm::cend(this->Levels)) {
  374. const std::string e =
  375. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  376. target->GetName(), "\" contained an invalid value: \"",
  377. *existingStandard, "\".");
  378. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  379. return false;
  380. }
  381. cm::optional<cmStandardLevel> needed =
  382. this->CompileFeatureStandardLevel(makefile, feature);
  383. return !needed ||
  384. (this->Levels.begin() + needed->Index()) <= existingLevelIter;
  385. }
  386. cm::optional<cmStandardLevel> CompileFeatureStandardLevel(
  387. cmMakefile* makefile, std::string const& feature) const
  388. {
  389. std::string prefix = cmStrCat("CMAKE_", this->Language);
  390. cm::optional<cmStandardLevel> maxLevel;
  391. for (size_t i = 0; i < this->Levels.size(); ++i) {
  392. if (cmValue prop = makefile->GetDefinition(
  393. cmStrCat(prefix, this->LevelsAsStrings[i], "_COMPILE_FEATURES"))) {
  394. cmList props{ *prop };
  395. if (cm::contains(props, feature)) {
  396. maxLevel = cmStandardLevel(i);
  397. }
  398. }
  399. }
  400. return maxLevel;
  401. }
  402. bool IsLaterStandard(int lhs, int rhs) const
  403. {
  404. auto rhsIt =
  405. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels), rhs);
  406. return std::find(rhsIt, cm::cend(this->Levels), lhs) !=
  407. cm::cend(this->Levels);
  408. }
  409. std::string Language;
  410. std::vector<int> Levels;
  411. std::vector<std::string> LevelsAsStrings;
  412. };
  413. std::unordered_map<std::string,
  414. StandardLevelComputer> const StandardComputerMapping = {
  415. { "C",
  416. StandardLevelComputer{
  417. "C", std::vector<int>{ 90, 99, 11, 17, 23 },
  418. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  419. { "CXX",
  420. StandardLevelComputer{
  421. "CXX", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  422. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } },
  423. { "CUDA",
  424. StandardLevelComputer{
  425. "CUDA", std::vector<int>{ 03, 11, 14, 17, 20, 23, 26 },
  426. std::vector<std::string>{ "03", "11", "14", "17", "20", "23", "26" } } },
  427. { "OBJC",
  428. StandardLevelComputer{
  429. "OBJC", std::vector<int>{ 90, 99, 11, 17, 23 },
  430. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  431. { "OBJCXX",
  432. StandardLevelComputer{
  433. "OBJCXX", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  434. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } },
  435. { "HIP",
  436. StandardLevelComputer{
  437. "HIP", std::vector<int>{ 98, 11, 14, 17, 20, 23, 26 },
  438. std::vector<std::string>{ "98", "11", "14", "17", "20", "23", "26" } } }
  439. };
  440. }
  441. std::string cmStandardLevelResolver::GetCompileOptionDef(
  442. cmGeneratorTarget const* target, std::string const& lang,
  443. std::string const& config) const
  444. {
  445. const auto& mapping = StandardComputerMapping.find(lang);
  446. if (mapping == cm::cend(StandardComputerMapping)) {
  447. return std::string{};
  448. }
  449. return mapping->second.GetCompileOptionDef(this->Makefile, target, config);
  450. }
  451. std::string cmStandardLevelResolver::GetEffectiveStandard(
  452. cmGeneratorTarget const* target, std::string const& lang,
  453. std::string const& config) const
  454. {
  455. const auto& mapping = StandardComputerMapping.find(lang);
  456. if (mapping == cm::cend(StandardComputerMapping)) {
  457. return std::string{};
  458. }
  459. return mapping->second.GetEffectiveStandard(this->Makefile, target, config);
  460. }
  461. bool cmStandardLevelResolver::AddRequiredTargetFeature(
  462. cmTarget* target, const std::string& feature, std::string* error) const
  463. {
  464. if (cmGeneratorExpression::Find(feature) != std::string::npos) {
  465. target->AppendProperty("COMPILE_FEATURES", feature,
  466. this->Makefile->GetBacktrace());
  467. return true;
  468. }
  469. std::string lang;
  470. if (!this->CheckCompileFeaturesAvailable(target->GetName(), feature, lang,
  471. error)) {
  472. return false;
  473. }
  474. target->AppendProperty("COMPILE_FEATURES", feature,
  475. this->Makefile->GetBacktrace());
  476. // FIXME: Add a policy to avoid updating the <LANG>_STANDARD target
  477. // property due to COMPILE_FEATURES. The language standard selection
  478. // should be done purely at generate time based on whatever the project
  479. // code put in these properties explicitly. That is mostly true now,
  480. // but for compatibility we need to continue updating the property here.
  481. cm::optional<cmStandardLevel> featureLevel;
  482. std::string newRequiredStandard;
  483. bool succeeded = this->GetNewRequiredStandard(
  484. target->GetName(), feature,
  485. target->GetProperty(cmStrCat(lang, "_STANDARD")), featureLevel,
  486. newRequiredStandard, error);
  487. if (!newRequiredStandard.empty()) {
  488. target->SetProperty(cmStrCat(lang, "_STANDARD"), newRequiredStandard);
  489. }
  490. return succeeded;
  491. }
  492. bool cmStandardLevelResolver::CheckCompileFeaturesAvailable(
  493. const std::string& targetName, const std::string& feature, std::string& lang,
  494. std::string* error) const
  495. {
  496. if (!this->CompileFeatureKnown(targetName, feature, lang, error)) {
  497. return false;
  498. }
  499. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  500. return true;
  501. }
  502. cmValue features = this->CompileFeaturesAvailable(lang, error);
  503. if (!features) {
  504. return false;
  505. }
  506. cmList availableFeatures{ features };
  507. if (!cm::contains(availableFeatures, feature)) {
  508. std::ostringstream e;
  509. e << "The compiler feature \"" << feature << "\" is not known to " << lang
  510. << " compiler\n\""
  511. << this->Makefile->GetSafeDefinition(
  512. cmStrCat("CMAKE_", lang, "_COMPILER_ID"))
  513. << "\"\nversion "
  514. << this->Makefile->GetSafeDefinition(
  515. cmStrCat("CMAKE_", lang, "_COMPILER_VERSION"))
  516. << '.';
  517. if (error) {
  518. *error = e.str();
  519. } else {
  520. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  521. }
  522. return false;
  523. }
  524. return true;
  525. }
  526. bool cmStandardLevelResolver::CompileFeatureKnown(
  527. const std::string& targetName, const std::string& feature, std::string& lang,
  528. std::string* error) const
  529. {
  530. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  531. bool isCFeature =
  532. std::find_if(cm::cbegin(C_FEATURES) + 1, cm::cend(C_FEATURES),
  533. cmStrCmp(feature)) != cm::cend(C_FEATURES);
  534. if (isCFeature) {
  535. lang = "C";
  536. return true;
  537. }
  538. bool isCxxFeature =
  539. std::find_if(cm::cbegin(CXX_FEATURES) + 1, cm::cend(CXX_FEATURES),
  540. cmStrCmp(feature)) != cm::cend(CXX_FEATURES);
  541. if (isCxxFeature) {
  542. lang = "CXX";
  543. return true;
  544. }
  545. bool isCudaFeature =
  546. std::find_if(cm::cbegin(CUDA_FEATURES) + 1, cm::cend(CUDA_FEATURES),
  547. cmStrCmp(feature)) != cm::cend(CUDA_FEATURES);
  548. if (isCudaFeature) {
  549. lang = "CUDA";
  550. return true;
  551. }
  552. bool isHIPFeature =
  553. std::find_if(cm::cbegin(HIP_FEATURES) + 1, cm::cend(HIP_FEATURES),
  554. cmStrCmp(feature)) != cm::cend(HIP_FEATURES);
  555. if (isHIPFeature) {
  556. lang = "HIP";
  557. return true;
  558. }
  559. std::ostringstream e;
  560. if (error) {
  561. e << "specified";
  562. } else {
  563. e << "Specified";
  564. }
  565. e << " unknown feature \"" << feature
  566. << "\" for "
  567. "target \""
  568. << targetName << "\".";
  569. if (error) {
  570. *error = e.str();
  571. } else {
  572. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  573. }
  574. return false;
  575. }
  576. cm::optional<cmStandardLevel>
  577. cmStandardLevelResolver::CompileFeatureStandardLevel(
  578. std::string const& lang, std::string const& feature) const
  579. {
  580. auto mapping = StandardComputerMapping.find(lang);
  581. if (mapping == cm::cend(StandardComputerMapping)) {
  582. return cm::nullopt;
  583. }
  584. return mapping->second.CompileFeatureStandardLevel(this->Makefile, feature);
  585. }
  586. cmValue cmStandardLevelResolver::CompileFeaturesAvailable(
  587. const std::string& lang, std::string* error) const
  588. {
  589. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  590. std::ostringstream e;
  591. if (error) {
  592. e << "cannot";
  593. } else {
  594. e << "Cannot";
  595. }
  596. e << " use features from non-enabled language " << lang;
  597. if (error) {
  598. *error = e.str();
  599. } else {
  600. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  601. }
  602. return nullptr;
  603. }
  604. cmValue featuresKnown = this->Makefile->GetDefinition(
  605. cmStrCat("CMAKE_", lang, "_COMPILE_FEATURES"));
  606. if (!cmNonempty(featuresKnown)) {
  607. std::ostringstream e;
  608. if (error) {
  609. e << "no";
  610. } else {
  611. e << "No";
  612. }
  613. e << " known features for " << lang << " compiler\n\""
  614. << this->Makefile->GetSafeDefinition(
  615. cmStrCat("CMAKE_", lang, "_COMPILER_ID"))
  616. << "\"\nversion "
  617. << this->Makefile->GetSafeDefinition(
  618. cmStrCat("CMAKE_", lang, "_COMPILER_VERSION"))
  619. << '.';
  620. if (error) {
  621. *error = e.str();
  622. } else {
  623. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  624. }
  625. return nullptr;
  626. }
  627. return featuresKnown;
  628. }
  629. bool cmStandardLevelResolver::GetNewRequiredStandard(
  630. const std::string& targetName, const std::string& feature,
  631. cmValue currentLangStandardValue,
  632. cm::optional<cmStandardLevel>& featureLevel,
  633. std::string& newRequiredStandard, std::string* error) const
  634. {
  635. std::string lang;
  636. if (!this->CheckCompileFeaturesAvailable(targetName, feature, lang, error)) {
  637. return false;
  638. }
  639. featureLevel = this->CompileFeatureStandardLevel(lang, feature);
  640. auto mapping = StandardComputerMapping.find(lang);
  641. if (mapping != cm::cend(StandardComputerMapping)) {
  642. return mapping->second.GetNewRequiredStandard(
  643. this->Makefile, targetName, featureLevel, currentLangStandardValue,
  644. newRequiredStandard, error);
  645. }
  646. return false;
  647. }
  648. bool cmStandardLevelResolver::HaveStandardAvailable(
  649. cmGeneratorTarget const* target, std::string const& lang,
  650. std::string const& config, const std::string& feature) const
  651. {
  652. auto mapping = StandardComputerMapping.find(lang);
  653. if (mapping != cm::cend(StandardComputerMapping)) {
  654. return mapping->second.HaveStandardAvailable(this->Makefile, target,
  655. config, feature);
  656. }
  657. return false;
  658. }
  659. bool cmStandardLevelResolver::IsLaterStandard(std::string const& lang,
  660. std::string const& lhs,
  661. std::string const& rhs) const
  662. {
  663. auto mapping = StandardComputerMapping.find(lang);
  664. if (mapping != cm::cend(StandardComputerMapping)) {
  665. return mapping->second.IsLaterStandard(std::stoi(lhs), std::stoi(rhs));
  666. }
  667. return false;
  668. }