cmProjectCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 "cmProjectCommand.h"
  4. #include <array>
  5. #include <cstddef>
  6. #include <cstdio>
  7. #include <functional>
  8. #include <limits>
  9. #include <utility>
  10. #include "cmsys/RegularExpression.hxx"
  11. #include "cmExecutionStatus.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmPolicies.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmValue.h"
  19. static bool IncludeByVariable(cmExecutionStatus& status,
  20. const std::string& variable);
  21. static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
  22. std::string const& value);
  23. bool cmProjectCommand(std::vector<std::string> const& args,
  24. cmExecutionStatus& status)
  25. {
  26. if (args.empty()) {
  27. status.SetError("PROJECT called with incorrect number of arguments");
  28. return false;
  29. }
  30. cmMakefile& mf = status.GetMakefile();
  31. if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE_BEFORE")) {
  32. return false;
  33. }
  34. std::string const& projectName = args[0];
  35. if (!IncludeByVariable(status,
  36. "CMAKE_PROJECT_" + projectName + "_INCLUDE_BEFORE")) {
  37. return false;
  38. }
  39. mf.SetProjectName(projectName);
  40. mf.AddCacheDefinition(projectName + "_BINARY_DIR",
  41. mf.GetCurrentBinaryDirectory(),
  42. "Value Computed by CMake", cmStateEnums::STATIC);
  43. mf.AddCacheDefinition(projectName + "_SOURCE_DIR",
  44. mf.GetCurrentSourceDirectory(),
  45. "Value Computed by CMake", cmStateEnums::STATIC);
  46. mf.AddDefinition("PROJECT_BINARY_DIR", mf.GetCurrentBinaryDirectory());
  47. mf.AddDefinition("PROJECT_SOURCE_DIR", mf.GetCurrentSourceDirectory());
  48. mf.AddDefinition("PROJECT_NAME", projectName);
  49. mf.AddDefinitionBool("PROJECT_IS_TOP_LEVEL", mf.IsRootMakefile());
  50. mf.AddCacheDefinition(projectName + "_IS_TOP_LEVEL",
  51. mf.IsRootMakefile() ? "ON" : "OFF",
  52. "Value Computed by CMake", cmStateEnums::STATIC);
  53. // Set the CMAKE_PROJECT_NAME variable to be the highest-level
  54. // project name in the tree. If there are two project commands
  55. // in the same CMakeLists.txt file, and it is the top level
  56. // CMakeLists.txt file, then go with the last one, so that
  57. // CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
  58. // will work.
  59. if (!mf.GetDefinition("CMAKE_PROJECT_NAME") || mf.IsRootMakefile()) {
  60. mf.RemoveDefinition("CMAKE_PROJECT_NAME");
  61. mf.AddCacheDefinition("CMAKE_PROJECT_NAME", projectName,
  62. "Value Computed by CMake", cmStateEnums::STATIC);
  63. }
  64. bool haveVersion = false;
  65. bool haveLanguages = false;
  66. bool haveDescription = false;
  67. bool haveHomepage = false;
  68. bool injectedProjectCommand = false;
  69. std::string version;
  70. std::string description;
  71. std::string homepage;
  72. std::vector<std::string> languages;
  73. std::function<void()> missedValueReporter;
  74. auto resetReporter = [&missedValueReporter]() {
  75. missedValueReporter = std::function<void()>();
  76. };
  77. enum Doing
  78. {
  79. DoingDescription,
  80. DoingHomepage,
  81. DoingLanguages,
  82. DoingVersion
  83. };
  84. Doing doing = DoingLanguages;
  85. for (size_t i = 1; i < args.size(); ++i) {
  86. if (args[i] == "LANGUAGES") {
  87. if (haveLanguages) {
  88. mf.IssueMessage(MessageType::FATAL_ERROR,
  89. "LANGUAGES may be specified at most once.");
  90. cmSystemTools::SetFatalErrorOccurred();
  91. return true;
  92. }
  93. haveLanguages = true;
  94. if (missedValueReporter) {
  95. missedValueReporter();
  96. }
  97. doing = DoingLanguages;
  98. if (!languages.empty()) {
  99. std::string msg = cmStrCat(
  100. "the following parameters must be specified after LANGUAGES "
  101. "keyword: ",
  102. cmJoin(languages, ", "), '.');
  103. mf.IssueMessage(MessageType::WARNING, msg);
  104. }
  105. } else if (args[i] == "VERSION") {
  106. if (haveVersion) {
  107. mf.IssueMessage(MessageType::FATAL_ERROR,
  108. "VERSION may be specified at most once.");
  109. cmSystemTools::SetFatalErrorOccurred();
  110. return true;
  111. }
  112. haveVersion = true;
  113. if (missedValueReporter) {
  114. missedValueReporter();
  115. }
  116. doing = DoingVersion;
  117. missedValueReporter = [&mf, &resetReporter]() {
  118. mf.IssueMessage(
  119. MessageType::WARNING,
  120. "VERSION keyword not followed by a value or was followed by a "
  121. "value that expanded to nothing.");
  122. resetReporter();
  123. };
  124. } else if (args[i] == "DESCRIPTION") {
  125. if (haveDescription) {
  126. mf.IssueMessage(MessageType::FATAL_ERROR,
  127. "DESCRIPTION may be specified at most once.");
  128. cmSystemTools::SetFatalErrorOccurred();
  129. return true;
  130. }
  131. haveDescription = true;
  132. if (missedValueReporter) {
  133. missedValueReporter();
  134. }
  135. doing = DoingDescription;
  136. missedValueReporter = [&mf, &resetReporter]() {
  137. mf.IssueMessage(
  138. MessageType::WARNING,
  139. "DESCRIPTION keyword not followed by a value or was followed "
  140. "by a value that expanded to nothing.");
  141. resetReporter();
  142. };
  143. } else if (args[i] == "HOMEPAGE_URL") {
  144. if (haveHomepage) {
  145. mf.IssueMessage(MessageType::FATAL_ERROR,
  146. "HOMEPAGE_URL may be specified at most once.");
  147. cmSystemTools::SetFatalErrorOccurred();
  148. return true;
  149. }
  150. haveHomepage = true;
  151. doing = DoingHomepage;
  152. missedValueReporter = [&mf, &resetReporter]() {
  153. mf.IssueMessage(
  154. MessageType::WARNING,
  155. "HOMEPAGE_URL keyword not followed by a value or was followed "
  156. "by a value that expanded to nothing.");
  157. resetReporter();
  158. };
  159. } else if (i == 1 && args[i] == "__CMAKE_INJECTED_PROJECT_COMMAND__") {
  160. injectedProjectCommand = true;
  161. } else if (doing == DoingVersion) {
  162. doing = DoingLanguages;
  163. version = args[i];
  164. resetReporter();
  165. } else if (doing == DoingDescription) {
  166. doing = DoingLanguages;
  167. description = args[i];
  168. resetReporter();
  169. } else if (doing == DoingHomepage) {
  170. doing = DoingLanguages;
  171. homepage = args[i];
  172. resetReporter();
  173. } else // doing == DoingLanguages
  174. {
  175. languages.push_back(args[i]);
  176. }
  177. }
  178. if (missedValueReporter) {
  179. missedValueReporter();
  180. }
  181. if ((haveVersion || haveDescription || haveHomepage) && !haveLanguages &&
  182. !languages.empty()) {
  183. mf.IssueMessage(MessageType::FATAL_ERROR,
  184. "project with VERSION, DESCRIPTION or HOMEPAGE_URL must "
  185. "use LANGUAGES before language names.");
  186. cmSystemTools::SetFatalErrorOccurred();
  187. return true;
  188. }
  189. if (haveLanguages && languages.empty()) {
  190. languages.emplace_back("NONE");
  191. }
  192. cmPolicies::PolicyStatus const cmp0048 =
  193. mf.GetPolicyStatus(cmPolicies::CMP0048);
  194. if (haveVersion) {
  195. // Set project VERSION variables to given values
  196. if (cmp0048 == cmPolicies::OLD || cmp0048 == cmPolicies::WARN) {
  197. mf.IssueMessage(MessageType::FATAL_ERROR,
  198. "VERSION not allowed unless CMP0048 is set to NEW");
  199. cmSystemTools::SetFatalErrorOccurred();
  200. return true;
  201. }
  202. cmsys::RegularExpression vx(
  203. R"(^([0-9]+(\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?)?)?$)");
  204. if (!vx.find(version)) {
  205. std::string e = R"(VERSION ")" + version + R"(" format invalid.)";
  206. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  207. cmSystemTools::SetFatalErrorOccurred();
  208. return true;
  209. }
  210. cmPolicies::PolicyStatus const cmp0096 =
  211. mf.GetPolicyStatus(cmPolicies::CMP0096);
  212. constexpr std::size_t MAX_VERSION_COMPONENTS = 4u;
  213. std::string version_string;
  214. std::array<std::string, MAX_VERSION_COMPONENTS> version_components;
  215. if (cmp0096 == cmPolicies::OLD || cmp0096 == cmPolicies::WARN) {
  216. constexpr size_t maxIntLength =
  217. std::numeric_limits<unsigned>::digits10 + 2;
  218. char vb[MAX_VERSION_COMPONENTS][maxIntLength];
  219. unsigned v[MAX_VERSION_COMPONENTS] = { 0, 0, 0, 0 };
  220. const int vc = std::sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1],
  221. &v[2], &v[3]);
  222. for (auto i = 0u; i < MAX_VERSION_COMPONENTS; ++i) {
  223. if (static_cast<int>(i) < vc) {
  224. std::snprintf(vb[i], maxIntLength, "%u", v[i]);
  225. version_string += &"."[static_cast<std::size_t>(i == 0)];
  226. version_string += vb[i];
  227. version_components[i] = vb[i];
  228. } else {
  229. vb[i][0] = '\x00';
  230. }
  231. }
  232. } else {
  233. // The regex above verified that we have a .-separated string of
  234. // non-negative integer components. Keep the original string.
  235. version_string = std::move(version);
  236. // Split the integer components.
  237. auto components = cmSystemTools::SplitString(version_string, '.');
  238. for (auto i = 0u; i < components.size(); ++i) {
  239. version_components[i] = std::move(components[i]);
  240. }
  241. }
  242. std::string vv;
  243. vv = projectName + "_VERSION";
  244. mf.AddDefinition("PROJECT_VERSION", version_string);
  245. mf.AddDefinition(vv, version_string);
  246. vv = projectName + "_VERSION_MAJOR";
  247. mf.AddDefinition("PROJECT_VERSION_MAJOR", version_components[0]);
  248. mf.AddDefinition(vv, version_components[0]);
  249. vv = projectName + "_VERSION_MINOR";
  250. mf.AddDefinition("PROJECT_VERSION_MINOR", version_components[1]);
  251. mf.AddDefinition(vv, version_components[1]);
  252. vv = projectName + "_VERSION_PATCH";
  253. mf.AddDefinition("PROJECT_VERSION_PATCH", version_components[2]);
  254. mf.AddDefinition(vv, version_components[2]);
  255. vv = projectName + "_VERSION_TWEAK";
  256. mf.AddDefinition("PROJECT_VERSION_TWEAK", version_components[3]);
  257. mf.AddDefinition(vv, version_components[3]);
  258. // Also, try set top level variables
  259. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION", version_string);
  260. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MAJOR",
  261. version_components[0]);
  262. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MINOR",
  263. version_components[1]);
  264. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_PATCH",
  265. version_components[2]);
  266. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_TWEAK",
  267. version_components[3]);
  268. } else if (cmp0048 != cmPolicies::OLD) {
  269. // Set project VERSION variables to empty
  270. std::vector<std::string> vv = { "PROJECT_VERSION",
  271. "PROJECT_VERSION_MAJOR",
  272. "PROJECT_VERSION_MINOR",
  273. "PROJECT_VERSION_PATCH",
  274. "PROJECT_VERSION_TWEAK",
  275. projectName + "_VERSION",
  276. projectName + "_VERSION_MAJOR",
  277. projectName + "_VERSION_MINOR",
  278. projectName + "_VERSION_PATCH",
  279. projectName + "_VERSION_TWEAK" };
  280. if (mf.IsRootMakefile()) {
  281. vv.emplace_back("CMAKE_PROJECT_VERSION");
  282. vv.emplace_back("CMAKE_PROJECT_VERSION_MAJOR");
  283. vv.emplace_back("CMAKE_PROJECT_VERSION_MINOR");
  284. vv.emplace_back("CMAKE_PROJECT_VERSION_PATCH");
  285. vv.emplace_back("CMAKE_PROJECT_VERSION_TWEAK");
  286. }
  287. std::string vw;
  288. for (std::string const& i : vv) {
  289. cmValue v = mf.GetDefinition(i);
  290. if (cmNonempty(v)) {
  291. if (cmp0048 == cmPolicies::WARN) {
  292. if (!injectedProjectCommand) {
  293. vw += "\n ";
  294. vw += i;
  295. }
  296. } else {
  297. mf.AddDefinition(i, "");
  298. }
  299. }
  300. }
  301. if (!vw.empty()) {
  302. mf.IssueMessage(
  303. MessageType::AUTHOR_WARNING,
  304. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0048),
  305. "\nThe following variable(s) would be set to empty:", vw));
  306. }
  307. }
  308. mf.AddDefinition("PROJECT_DESCRIPTION", description);
  309. mf.AddDefinition(projectName + "_DESCRIPTION", description);
  310. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_DESCRIPTION", description);
  311. mf.AddDefinition("PROJECT_HOMEPAGE_URL", homepage);
  312. mf.AddDefinition(projectName + "_HOMEPAGE_URL", homepage);
  313. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_HOMEPAGE_URL", homepage);
  314. if (languages.empty()) {
  315. // if no language is specified do c and c++
  316. languages = { "C", "CXX" };
  317. }
  318. mf.EnableLanguage(languages, false);
  319. if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE")) {
  320. return false;
  321. }
  322. if (!IncludeByVariable(status,
  323. "CMAKE_PROJECT_" + projectName + "_INCLUDE")) {
  324. return false;
  325. }
  326. return true;
  327. }
  328. static bool IncludeByVariable(cmExecutionStatus& status,
  329. const std::string& variable)
  330. {
  331. cmMakefile& mf = status.GetMakefile();
  332. cmValue include = mf.GetDefinition(variable);
  333. if (!include) {
  334. return true;
  335. }
  336. std::string includeFile =
  337. cmSystemTools::CollapseFullPath(*include, mf.GetCurrentSourceDirectory());
  338. if (!cmSystemTools::FileExists(includeFile)) {
  339. status.SetError(cmStrCat("could not find requested file:\n ", *include));
  340. return false;
  341. }
  342. if (cmSystemTools::FileIsDirectory(includeFile)) {
  343. status.SetError(cmStrCat("requested file is a directory:\n ", *include));
  344. return false;
  345. }
  346. const bool readit = mf.ReadDependentFile(*include);
  347. if (readit) {
  348. return true;
  349. }
  350. if (cmSystemTools::GetFatalErrorOccurred()) {
  351. return true;
  352. }
  353. status.SetError(cmStrCat("could not load requested file:\n ", *include));
  354. return false;
  355. }
  356. static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
  357. std::string const& value)
  358. {
  359. // Set the CMAKE_PROJECT_XXX variable to be the highest-level
  360. // project name in the tree. If there are two project commands
  361. // in the same CMakeLists.txt file, and it is the top level
  362. // CMakeLists.txt file, then go with the last one.
  363. if (!mf.GetDefinition(name) || mf.IsRootMakefile()) {
  364. mf.RemoveDefinition(name);
  365. mf.AddCacheDefinition(name, value, "Value Computed by CMake",
  366. cmStateEnums::STATIC);
  367. }
  368. }