cmProjectCommand.cxx 13 KB

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