cmStandardLevelResolver.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 <cmext/algorithm>
  14. #include "cmGeneratorExpression.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmProperty.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmTarget.h"
  22. #include "cmake.h"
  23. namespace {
  24. #define FEATURE_STRING(F) , #F
  25. const char* const C_FEATURES[] = { nullptr FOR_EACH_C_FEATURE(
  26. FEATURE_STRING) };
  27. const char* const CXX_FEATURES[] = { nullptr FOR_EACH_CXX_FEATURE(
  28. FEATURE_STRING) };
  29. const char* const CUDA_FEATURES[] = { nullptr FOR_EACH_CUDA_FEATURE(
  30. FEATURE_STRING) };
  31. const char* const HIP_FEATURES[] = { nullptr FOR_EACH_HIP_FEATURE(
  32. FEATURE_STRING) };
  33. #undef FEATURE_STRING
  34. struct StandardNeeded
  35. {
  36. int index;
  37. int value;
  38. };
  39. int ParseStd(std::string const& level)
  40. {
  41. try {
  42. return std::stoi(level);
  43. } catch (std::invalid_argument&) {
  44. // Fall through to use an invalid value.
  45. }
  46. return -1;
  47. }
  48. struct StanardLevelComputer
  49. {
  50. explicit StanardLevelComputer(std::string lang, std::vector<int> levels,
  51. std::vector<std::string> levelsStr)
  52. : Language(std::move(lang))
  53. , Levels(std::move(levels))
  54. , LevelsAsStrings(std::move(levelsStr))
  55. {
  56. assert(this->Levels.size() == this->LevelsAsStrings.size());
  57. }
  58. std::string GetCompileOptionDef(cmMakefile* makefile,
  59. cmGeneratorTarget const* target,
  60. std::string const& config) const
  61. {
  62. const auto& stds = this->Levels;
  63. const auto& stdsStrings = this->LevelsAsStrings;
  64. cmProp defaultStd = makefile->GetDefinition(
  65. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  66. if (!cmNonempty(defaultStd)) {
  67. // this compiler has no notion of language standard levels
  68. return std::string{};
  69. }
  70. bool ext = true;
  71. if (cmProp extPropValue = target->GetLanguageExtensions(this->Language)) {
  72. if (cmIsOff(*extPropValue)) {
  73. ext = false;
  74. }
  75. }
  76. cmProp standardProp = target->GetLanguageStandard(this->Language, config);
  77. if (!standardProp) {
  78. if (ext) {
  79. // No language standard is specified and extensions are not disabled.
  80. // Check if this compiler needs a flag to enable extensions.
  81. return cmStrCat("CMAKE_", this->Language, "_EXTENSION_COMPILE_OPTION");
  82. }
  83. return std::string{};
  84. }
  85. std::string const type = ext ? "EXTENSION" : "STANDARD";
  86. if (target->GetLanguageStandardRequired(this->Language)) {
  87. std::string option_flag = cmStrCat(
  88. "CMAKE_", this->Language, *standardProp, "_", type, "_COMPILE_OPTION");
  89. cmProp opt = target->Target->GetMakefile()->GetDefinition(option_flag);
  90. if (!opt) {
  91. std::ostringstream e;
  92. e << "Target \"" << target->GetName()
  93. << "\" requires the language "
  94. "dialect \""
  95. << this->Language << *standardProp << "\" "
  96. << (ext ? "(with compiler extensions)" : "")
  97. << ", but CMake "
  98. "does not know the compile flags to use to enable it.";
  99. makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  100. }
  101. return option_flag;
  102. }
  103. std::string standardStr(*standardProp);
  104. if (this->Language == "CUDA" && standardStr == "98") {
  105. standardStr = "03";
  106. }
  107. auto stdIt =
  108. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(standardStr));
  109. if (stdIt == cm::cend(stds)) {
  110. std::string e =
  111. cmStrCat(this->Language, "_STANDARD is set to invalid value '",
  112. standardStr, "'");
  113. makefile->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR, e,
  114. target->GetBacktrace());
  115. return std::string{};
  116. }
  117. auto defaultStdIt =
  118. std::find(cm::cbegin(stds), cm::cend(stds), ParseStd(*defaultStd));
  119. if (defaultStdIt == cm::cend(stds)) {
  120. std::string e = cmStrCat("CMAKE_", this->Language,
  121. "_STANDARD_DEFAULT is set to invalid value '",
  122. *defaultStd, "'");
  123. makefile->IssueMessage(MessageType::INTERNAL_ERROR, e);
  124. return std::string{};
  125. }
  126. // If the standard requested is older than the compiler's default
  127. // then we need to use a flag to change it.
  128. if (stdIt <= defaultStdIt) {
  129. auto offset = std::distance(cm::cbegin(stds), stdIt);
  130. return cmStrCat("CMAKE_", this->Language, stdsStrings[offset], "_", type,
  131. "_COMPILE_OPTION");
  132. }
  133. // The standard requested is at least as new as the compiler's default,
  134. // and the standard request is not required. Decay to the newest standard
  135. // for which a flag is defined.
  136. for (; defaultStdIt < stdIt; --stdIt) {
  137. auto offset = std::distance(cm::cbegin(stds), stdIt);
  138. std::string option_flag =
  139. cmStrCat("CMAKE_", this->Language, stdsStrings[offset], "_", type,
  140. "_COMPILE_OPTION");
  141. if (target->Target->GetMakefile()->GetDefinition(option_flag)) {
  142. return option_flag;
  143. }
  144. }
  145. return std::string{};
  146. }
  147. bool GetNewRequiredStandard(cmMakefile* makefile,
  148. std::string const& targetName,
  149. const std::string& feature,
  150. cmProp currentLangStandardValue,
  151. std::string& newRequiredStandard,
  152. std::string* error) const
  153. {
  154. if (currentLangStandardValue) {
  155. newRequiredStandard = *currentLangStandardValue;
  156. } else {
  157. newRequiredStandard.clear();
  158. }
  159. auto needed = this->HighestStandardNeeded(makefile, feature);
  160. cmProp existingStandard = currentLangStandardValue;
  161. if (!existingStandard) {
  162. cmProp defaultStandard = makefile->GetDefinition(
  163. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  164. if (cmNonempty(defaultStandard)) {
  165. existingStandard = defaultStandard;
  166. }
  167. }
  168. auto existingLevelIter = cm::cend(this->Levels);
  169. if (existingStandard) {
  170. existingLevelIter =
  171. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  172. ParseStd(*existingStandard));
  173. if (existingLevelIter == cm::cend(this->Levels)) {
  174. const std::string e =
  175. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  176. targetName, "\" contained an invalid value: \"",
  177. *existingStandard, "\".");
  178. if (error) {
  179. *error = e;
  180. } else {
  181. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  182. }
  183. return false;
  184. }
  185. }
  186. if (needed.index != -1) {
  187. // Ensure the C++ language level is high enough to support
  188. // the needed C++ features.
  189. if (existingLevelIter == cm::cend(this->Levels) ||
  190. existingLevelIter < this->Levels.begin() + needed.index) {
  191. newRequiredStandard = this->LevelsAsStrings[needed.index];
  192. }
  193. }
  194. return true;
  195. }
  196. bool HaveStandardAvailable(cmMakefile* makefile,
  197. cmGeneratorTarget const* target,
  198. std::string const& config,
  199. std::string const& feature) const
  200. {
  201. cmProp defaultStandard = makefile->GetDefinition(
  202. cmStrCat("CMAKE_", this->Language, "_STANDARD_DEFAULT"));
  203. if (!defaultStandard) {
  204. makefile->IssueMessage(
  205. MessageType::INTERNAL_ERROR,
  206. cmStrCat("CMAKE_", this->Language,
  207. "_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  208. "not fully configured for this compiler."));
  209. // Return true so the caller does not try to lookup the default standard.
  210. return true;
  211. }
  212. // convert defaultStandard to an integer
  213. if (std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  214. ParseStd(*defaultStandard)) == cm::cend(this->Levels)) {
  215. const std::string e = cmStrCat("The CMAKE_", this->Language,
  216. "_STANDARD_DEFAULT variable contains an "
  217. "invalid value: \"",
  218. *defaultStandard, "\".");
  219. makefile->IssueMessage(MessageType::INTERNAL_ERROR, e);
  220. return false;
  221. }
  222. cmProp existingStandard =
  223. target->GetLanguageStandard(this->Language, config);
  224. if (!existingStandard) {
  225. existingStandard = defaultStandard;
  226. }
  227. auto existingLevelIter =
  228. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels),
  229. ParseStd(*existingStandard));
  230. if (existingLevelIter == cm::cend(this->Levels)) {
  231. const std::string e =
  232. cmStrCat("The ", this->Language, "_STANDARD property on target \"",
  233. target->GetName(), "\" contained an invalid value: \"",
  234. *existingStandard, "\".");
  235. makefile->IssueMessage(MessageType::FATAL_ERROR, e);
  236. return false;
  237. }
  238. auto needed = this->HighestStandardNeeded(makefile, feature);
  239. return (needed.index == -1) ||
  240. (this->Levels.begin() + needed.index) <= existingLevelIter;
  241. }
  242. StandardNeeded HighestStandardNeeded(cmMakefile* makefile,
  243. std::string const& feature) const
  244. {
  245. std::string prefix = cmStrCat("CMAKE_", this->Language);
  246. StandardNeeded maxLevel = { -1, -1 };
  247. for (size_t i = 0; i < this->Levels.size(); ++i) {
  248. if (cmProp prop = makefile->GetDefinition(
  249. cmStrCat(prefix, this->LevelsAsStrings[i], "_COMPILE_FEATURES"))) {
  250. std::vector<std::string> props = cmExpandedList(*prop);
  251. if (cm::contains(props, feature)) {
  252. maxLevel = { static_cast<int>(i), this->Levels[i] };
  253. }
  254. }
  255. }
  256. return maxLevel;
  257. }
  258. bool IsLaterStandard(int lhs, int rhs) const
  259. {
  260. auto rhsIt =
  261. std::find(cm::cbegin(this->Levels), cm::cend(this->Levels), rhs);
  262. return std::find(rhsIt, cm::cend(this->Levels), lhs) !=
  263. cm::cend(this->Levels);
  264. }
  265. std::string Language;
  266. std::vector<int> Levels;
  267. std::vector<std::string> LevelsAsStrings;
  268. };
  269. std::unordered_map<std::string, StanardLevelComputer> StandardComputerMapping =
  270. { { "C",
  271. StanardLevelComputer{
  272. "C", std::vector<int>{ 90, 99, 11, 17, 23 },
  273. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  274. { "CXX",
  275. StanardLevelComputer{
  276. "CXX", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  277. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } },
  278. { "CUDA",
  279. StanardLevelComputer{
  280. "CUDA", std::vector<int>{ 03, 11, 14, 17, 20, 23 },
  281. std::vector<std::string>{ "03", "11", "14", "17", "20", "23" } } },
  282. { "OBJC",
  283. StanardLevelComputer{ "OBJC", std::vector<int>{ 90, 99, 11 },
  284. std::vector<std::string>{ "90", "99", "11" } } },
  285. { "OBJCXX",
  286. StanardLevelComputer{
  287. "OBJCXX", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  288. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } },
  289. { "HIP",
  290. StanardLevelComputer{
  291. "HIP", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  292. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } } };
  293. }
  294. std::string cmStandardLevelResolver::GetCompileOptionDef(
  295. cmGeneratorTarget const* target, std::string const& lang,
  296. std::string const& config) const
  297. {
  298. const auto& mapping = StandardComputerMapping.find(lang);
  299. if (mapping == cm::cend(StandardComputerMapping)) {
  300. return std::string{};
  301. }
  302. return mapping->second.GetCompileOptionDef(this->Makefile, target, config);
  303. }
  304. bool cmStandardLevelResolver::AddRequiredTargetFeature(
  305. cmTarget* target, const std::string& feature, std::string* error) const
  306. {
  307. if (cmGeneratorExpression::Find(feature) != std::string::npos) {
  308. target->AppendProperty("COMPILE_FEATURES", feature);
  309. return true;
  310. }
  311. std::string lang;
  312. if (!this->CheckCompileFeaturesAvailable(target->GetName(), feature, lang,
  313. error)) {
  314. return false;
  315. }
  316. target->AppendProperty("COMPILE_FEATURES", feature);
  317. // FIXME: Add a policy to avoid updating the <LANG>_STANDARD target
  318. // property due to COMPILE_FEATURES. The language standard selection
  319. // should be done purely at generate time based on whatever the project
  320. // code put in these properties explicitly. That is mostly true now,
  321. // but for compatibility we need to continue updating the property here.
  322. std::string newRequiredStandard;
  323. bool newRequired = this->GetNewRequiredStandard(
  324. target->GetName(), feature,
  325. target->GetProperty(cmStrCat(lang, "_STANDARD")), newRequiredStandard,
  326. error);
  327. if (!newRequiredStandard.empty()) {
  328. target->SetProperty(cmStrCat(lang, "_STANDARD"), newRequiredStandard);
  329. }
  330. return newRequired;
  331. }
  332. bool cmStandardLevelResolver::CheckCompileFeaturesAvailable(
  333. const std::string& targetName, const std::string& feature, std::string& lang,
  334. std::string* error) const
  335. {
  336. if (!this->CompileFeatureKnown(targetName, feature, lang, error)) {
  337. return false;
  338. }
  339. const char* features = this->CompileFeaturesAvailable(lang, error);
  340. if (!features) {
  341. return false;
  342. }
  343. std::vector<std::string> availableFeatures = cmExpandedList(features);
  344. if (!cm::contains(availableFeatures, feature)) {
  345. std::ostringstream e;
  346. e << "The compiler feature \"" << feature << "\" is not known to " << lang
  347. << " compiler\n\""
  348. << this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID")
  349. << "\"\nversion "
  350. << this->Makefile->GetSafeDefinition("CMAKE_" + lang +
  351. "_COMPILER_VERSION")
  352. << ".";
  353. if (error) {
  354. *error = e.str();
  355. } else {
  356. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  357. }
  358. return false;
  359. }
  360. return true;
  361. }
  362. bool cmStandardLevelResolver::CompileFeatureKnown(
  363. const std::string& targetName, const std::string& feature, std::string& lang,
  364. std::string* error) const
  365. {
  366. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  367. bool isCFeature =
  368. std::find_if(cm::cbegin(C_FEATURES) + 1, cm::cend(C_FEATURES),
  369. cmStrCmp(feature)) != cm::cend(C_FEATURES);
  370. if (isCFeature) {
  371. lang = "C";
  372. return true;
  373. }
  374. bool isCxxFeature =
  375. std::find_if(cm::cbegin(CXX_FEATURES) + 1, cm::cend(CXX_FEATURES),
  376. cmStrCmp(feature)) != cm::cend(CXX_FEATURES);
  377. if (isCxxFeature) {
  378. lang = "CXX";
  379. return true;
  380. }
  381. bool isCudaFeature =
  382. std::find_if(cm::cbegin(CUDA_FEATURES) + 1, cm::cend(CUDA_FEATURES),
  383. cmStrCmp(feature)) != cm::cend(CUDA_FEATURES);
  384. if (isCudaFeature) {
  385. lang = "CUDA";
  386. return true;
  387. }
  388. bool isHIPFeature =
  389. std::find_if(cm::cbegin(HIP_FEATURES) + 1, cm::cend(HIP_FEATURES),
  390. cmStrCmp(feature)) != cm::cend(HIP_FEATURES);
  391. if (isHIPFeature) {
  392. lang = "HIP";
  393. return true;
  394. }
  395. std::ostringstream e;
  396. if (error) {
  397. e << "specified";
  398. } else {
  399. e << "Specified";
  400. }
  401. e << " unknown feature \"" << feature
  402. << "\" for "
  403. "target \""
  404. << targetName << "\".";
  405. if (error) {
  406. *error = e.str();
  407. } else {
  408. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  409. }
  410. return false;
  411. }
  412. const char* cmStandardLevelResolver::CompileFeaturesAvailable(
  413. const std::string& lang, std::string* error) const
  414. {
  415. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  416. std::ostringstream e;
  417. if (error) {
  418. e << "cannot";
  419. } else {
  420. e << "Cannot";
  421. }
  422. e << " use features from non-enabled language " << lang;
  423. if (error) {
  424. *error = e.str();
  425. } else {
  426. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  427. }
  428. return nullptr;
  429. }
  430. cmProp featuresKnown =
  431. this->Makefile->GetDefinition("CMAKE_" + lang + "_COMPILE_FEATURES");
  432. if (!cmNonempty(featuresKnown)) {
  433. std::ostringstream e;
  434. if (error) {
  435. e << "no";
  436. } else {
  437. e << "No";
  438. }
  439. e << " known features for " << lang << " compiler\n\""
  440. << this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID")
  441. << "\"\nversion "
  442. << this->Makefile->GetSafeDefinition("CMAKE_" + lang +
  443. "_COMPILER_VERSION")
  444. << ".";
  445. if (error) {
  446. *error = e.str();
  447. } else {
  448. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  449. }
  450. return nullptr;
  451. }
  452. return cmToCStr(featuresKnown);
  453. }
  454. bool cmStandardLevelResolver::GetNewRequiredStandard(
  455. const std::string& targetName, const std::string& feature,
  456. cmProp currentLangStandardValue, std::string& newRequiredStandard,
  457. std::string* error) const
  458. {
  459. std::string lang;
  460. if (!this->CheckCompileFeaturesAvailable(targetName, feature, lang, error)) {
  461. return false;
  462. }
  463. auto mapping = StandardComputerMapping.find(lang);
  464. if (mapping != cm::cend(StandardComputerMapping)) {
  465. return mapping->second.GetNewRequiredStandard(
  466. this->Makefile, targetName, feature, currentLangStandardValue,
  467. newRequiredStandard, error);
  468. }
  469. return false;
  470. }
  471. bool cmStandardLevelResolver::HaveStandardAvailable(
  472. cmGeneratorTarget const* target, std::string const& lang,
  473. std::string const& config, const std::string& feature) const
  474. {
  475. auto mapping = StandardComputerMapping.find(lang);
  476. if (mapping != cm::cend(StandardComputerMapping)) {
  477. return mapping->second.HaveStandardAvailable(this->Makefile, target,
  478. config, feature);
  479. }
  480. return false;
  481. }
  482. bool cmStandardLevelResolver::IsLaterStandard(std::string const& lang,
  483. std::string const& lhs,
  484. std::string const& rhs) const
  485. {
  486. auto mapping = StandardComputerMapping.find(lang);
  487. if (mapping != cm::cend(StandardComputerMapping)) {
  488. return mapping->second.IsLaterStandard(std::stoi(lhs), std::stoi(rhs));
  489. }
  490. return false;
  491. }