cmStandardLevelResolver.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 StandardLevelComputer
  49. {
  50. explicit StandardLevelComputer(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, StandardLevelComputer>
  270. StandardComputerMapping = {
  271. { "C",
  272. StandardLevelComputer{
  273. "C", std::vector<int>{ 90, 99, 11, 17, 23 },
  274. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  275. { "CXX",
  276. StandardLevelComputer{
  277. "CXX", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  278. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } },
  279. { "CUDA",
  280. StandardLevelComputer{
  281. "CUDA", std::vector<int>{ 03, 11, 14, 17, 20, 23 },
  282. std::vector<std::string>{ "03", "11", "14", "17", "20", "23" } } },
  283. { "OBJC",
  284. StandardLevelComputer{
  285. "OBJC", std::vector<int>{ 90, 99, 11, 17, 23 },
  286. std::vector<std::string>{ "90", "99", "11", "17", "23" } } },
  287. { "OBJCXX",
  288. StandardLevelComputer{
  289. "OBJCXX", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  290. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } },
  291. { "HIP",
  292. StandardLevelComputer{
  293. "HIP", std::vector<int>{ 98, 11, 14, 17, 20, 23 },
  294. std::vector<std::string>{ "98", "11", "14", "17", "20", "23" } } }
  295. };
  296. }
  297. std::string cmStandardLevelResolver::GetCompileOptionDef(
  298. cmGeneratorTarget const* target, std::string const& lang,
  299. std::string const& config) const
  300. {
  301. const auto& mapping = StandardComputerMapping.find(lang);
  302. if (mapping == cm::cend(StandardComputerMapping)) {
  303. return std::string{};
  304. }
  305. return mapping->second.GetCompileOptionDef(this->Makefile, target, config);
  306. }
  307. bool cmStandardLevelResolver::AddRequiredTargetFeature(
  308. cmTarget* target, const std::string& feature, std::string* error) const
  309. {
  310. if (cmGeneratorExpression::Find(feature) != std::string::npos) {
  311. target->AppendProperty("COMPILE_FEATURES", feature);
  312. return true;
  313. }
  314. std::string lang;
  315. if (!this->CheckCompileFeaturesAvailable(target->GetName(), feature, lang,
  316. error)) {
  317. return false;
  318. }
  319. target->AppendProperty("COMPILE_FEATURES", feature);
  320. // FIXME: Add a policy to avoid updating the <LANG>_STANDARD target
  321. // property due to COMPILE_FEATURES. The language standard selection
  322. // should be done purely at generate time based on whatever the project
  323. // code put in these properties explicitly. That is mostly true now,
  324. // but for compatibility we need to continue updating the property here.
  325. std::string newRequiredStandard;
  326. bool newRequired = this->GetNewRequiredStandard(
  327. target->GetName(), feature,
  328. target->GetProperty(cmStrCat(lang, "_STANDARD")), newRequiredStandard,
  329. error);
  330. if (!newRequiredStandard.empty()) {
  331. target->SetProperty(cmStrCat(lang, "_STANDARD"), newRequiredStandard);
  332. }
  333. return newRequired;
  334. }
  335. bool cmStandardLevelResolver::CheckCompileFeaturesAvailable(
  336. const std::string& targetName, const std::string& feature, std::string& lang,
  337. std::string* error) const
  338. {
  339. if (!this->CompileFeatureKnown(targetName, feature, lang, error)) {
  340. return false;
  341. }
  342. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  343. return true;
  344. }
  345. cmProp features = this->CompileFeaturesAvailable(lang, error);
  346. if (!features) {
  347. return false;
  348. }
  349. std::vector<std::string> availableFeatures = cmExpandedList(features);
  350. if (!cm::contains(availableFeatures, feature)) {
  351. std::ostringstream e;
  352. e << "The compiler feature \"" << feature << "\" is not known to " << lang
  353. << " compiler\n\""
  354. << this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID")
  355. << "\"\nversion "
  356. << this->Makefile->GetSafeDefinition("CMAKE_" + lang +
  357. "_COMPILER_VERSION")
  358. << ".";
  359. if (error) {
  360. *error = e.str();
  361. } else {
  362. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  363. }
  364. return false;
  365. }
  366. return true;
  367. }
  368. bool cmStandardLevelResolver::CompileFeatureKnown(
  369. const std::string& targetName, const std::string& feature, std::string& lang,
  370. std::string* error) const
  371. {
  372. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  373. bool isCFeature =
  374. std::find_if(cm::cbegin(C_FEATURES) + 1, cm::cend(C_FEATURES),
  375. cmStrCmp(feature)) != cm::cend(C_FEATURES);
  376. if (isCFeature) {
  377. lang = "C";
  378. return true;
  379. }
  380. bool isCxxFeature =
  381. std::find_if(cm::cbegin(CXX_FEATURES) + 1, cm::cend(CXX_FEATURES),
  382. cmStrCmp(feature)) != cm::cend(CXX_FEATURES);
  383. if (isCxxFeature) {
  384. lang = "CXX";
  385. return true;
  386. }
  387. bool isCudaFeature =
  388. std::find_if(cm::cbegin(CUDA_FEATURES) + 1, cm::cend(CUDA_FEATURES),
  389. cmStrCmp(feature)) != cm::cend(CUDA_FEATURES);
  390. if (isCudaFeature) {
  391. lang = "CUDA";
  392. return true;
  393. }
  394. bool isHIPFeature =
  395. std::find_if(cm::cbegin(HIP_FEATURES) + 1, cm::cend(HIP_FEATURES),
  396. cmStrCmp(feature)) != cm::cend(HIP_FEATURES);
  397. if (isHIPFeature) {
  398. lang = "HIP";
  399. return true;
  400. }
  401. std::ostringstream e;
  402. if (error) {
  403. e << "specified";
  404. } else {
  405. e << "Specified";
  406. }
  407. e << " unknown feature \"" << feature
  408. << "\" for "
  409. "target \""
  410. << targetName << "\".";
  411. if (error) {
  412. *error = e.str();
  413. } else {
  414. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  415. }
  416. return false;
  417. }
  418. cmProp cmStandardLevelResolver::CompileFeaturesAvailable(
  419. const std::string& lang, std::string* error) const
  420. {
  421. if (!this->Makefile->GetGlobalGenerator()->GetLanguageEnabled(lang)) {
  422. std::ostringstream e;
  423. if (error) {
  424. e << "cannot";
  425. } else {
  426. e << "Cannot";
  427. }
  428. e << " use features from non-enabled language " << lang;
  429. if (error) {
  430. *error = e.str();
  431. } else {
  432. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  433. }
  434. return nullptr;
  435. }
  436. cmProp featuresKnown =
  437. this->Makefile->GetDefinition("CMAKE_" + lang + "_COMPILE_FEATURES");
  438. if (!cmNonempty(featuresKnown)) {
  439. std::ostringstream e;
  440. if (error) {
  441. e << "no";
  442. } else {
  443. e << "No";
  444. }
  445. e << " known features for " << lang << " compiler\n\""
  446. << this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID")
  447. << "\"\nversion "
  448. << this->Makefile->GetSafeDefinition("CMAKE_" + lang +
  449. "_COMPILER_VERSION")
  450. << ".";
  451. if (error) {
  452. *error = e.str();
  453. } else {
  454. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  455. }
  456. return nullptr;
  457. }
  458. return featuresKnown;
  459. }
  460. bool cmStandardLevelResolver::GetNewRequiredStandard(
  461. const std::string& targetName, const std::string& feature,
  462. cmProp currentLangStandardValue, std::string& newRequiredStandard,
  463. std::string* error) const
  464. {
  465. std::string lang;
  466. if (!this->CheckCompileFeaturesAvailable(targetName, feature, lang, error)) {
  467. return false;
  468. }
  469. auto mapping = StandardComputerMapping.find(lang);
  470. if (mapping != cm::cend(StandardComputerMapping)) {
  471. return mapping->second.GetNewRequiredStandard(
  472. this->Makefile, targetName, feature, currentLangStandardValue,
  473. newRequiredStandard, error);
  474. }
  475. return false;
  476. }
  477. bool cmStandardLevelResolver::HaveStandardAvailable(
  478. cmGeneratorTarget const* target, std::string const& lang,
  479. std::string const& config, const std::string& feature) const
  480. {
  481. auto mapping = StandardComputerMapping.find(lang);
  482. if (mapping != cm::cend(StandardComputerMapping)) {
  483. return mapping->second.HaveStandardAvailable(this->Makefile, target,
  484. config, feature);
  485. }
  486. return false;
  487. }
  488. bool cmStandardLevelResolver::IsLaterStandard(std::string const& lang,
  489. std::string const& lhs,
  490. std::string const& rhs) const
  491. {
  492. auto mapping = StandardComputerMapping.find(lang);
  493. if (mapping != cm::cend(StandardComputerMapping)) {
  494. return mapping->second.IsLaterStandard(std::stoi(lhs), std::stoi(rhs));
  495. }
  496. return false;
  497. }