cmStandardLevelResolver.cxx 18 KB

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