cmProjectCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "cmProperty.h"
  16. #include "cmStateTypes.h"
  17. #include "cmStringAlgorithms.h"
  18. #include "cmSystemTools.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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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::SetFatalErrorOccured();
  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. char vb[MAX_VERSION_COMPONENTS]
  217. [std::numeric_limits<unsigned>::digits10 + 2];
  218. unsigned v[MAX_VERSION_COMPONENTS] = { 0, 0, 0, 0 };
  219. const int vc = std::sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1],
  220. &v[2], &v[3]);
  221. for (auto i = 0u; i < MAX_VERSION_COMPONENTS; ++i) {
  222. if (int(i) < vc) {
  223. std::sprintf(vb[i], "%u", v[i]);
  224. version_string += &"."[std::size_t(i == 0)];
  225. version_string += vb[i];
  226. version_components[i] = vb[i];
  227. } else {
  228. vb[i][0] = '\x00';
  229. }
  230. }
  231. } else {
  232. // The regex above verified that we have a .-separated string of
  233. // non-negative integer components. Keep the original string.
  234. version_string = std::move(version);
  235. // Split the integer components.
  236. auto components = cmSystemTools::SplitString(version_string, '.');
  237. for (auto i = 0u; i < components.size(); ++i) {
  238. version_components[i] = std::move(components[i]);
  239. }
  240. }
  241. std::string vv;
  242. vv = projectName + "_VERSION";
  243. mf.AddDefinition("PROJECT_VERSION", version_string);
  244. mf.AddDefinition(vv, version_string);
  245. vv = projectName + "_VERSION_MAJOR";
  246. mf.AddDefinition("PROJECT_VERSION_MAJOR", version_components[0]);
  247. mf.AddDefinition(vv, version_components[0]);
  248. vv = projectName + "_VERSION_MINOR";
  249. mf.AddDefinition("PROJECT_VERSION_MINOR", version_components[1]);
  250. mf.AddDefinition(vv, version_components[1]);
  251. vv = projectName + "_VERSION_PATCH";
  252. mf.AddDefinition("PROJECT_VERSION_PATCH", version_components[2]);
  253. mf.AddDefinition(vv, version_components[2]);
  254. vv = projectName + "_VERSION_TWEAK";
  255. mf.AddDefinition("PROJECT_VERSION_TWEAK", version_components[3]);
  256. mf.AddDefinition(vv, version_components[3]);
  257. // Also, try set top level variables
  258. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION", version_string);
  259. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MAJOR",
  260. version_components[0]);
  261. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MINOR",
  262. version_components[1]);
  263. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_PATCH",
  264. version_components[2]);
  265. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_TWEAK",
  266. version_components[3]);
  267. } else if (cmp0048 != cmPolicies::OLD) {
  268. // Set project VERSION variables to empty
  269. std::vector<std::string> vv = { "PROJECT_VERSION",
  270. "PROJECT_VERSION_MAJOR",
  271. "PROJECT_VERSION_MINOR",
  272. "PROJECT_VERSION_PATCH",
  273. "PROJECT_VERSION_TWEAK",
  274. projectName + "_VERSION",
  275. projectName + "_VERSION_MAJOR",
  276. projectName + "_VERSION_MINOR",
  277. projectName + "_VERSION_PATCH",
  278. projectName + "_VERSION_TWEAK" };
  279. if (mf.IsRootMakefile()) {
  280. vv.emplace_back("CMAKE_PROJECT_VERSION");
  281. vv.emplace_back("CMAKE_PROJECT_VERSION_MAJOR");
  282. vv.emplace_back("CMAKE_PROJECT_VERSION_MINOR");
  283. vv.emplace_back("CMAKE_PROJECT_VERSION_PATCH");
  284. vv.emplace_back("CMAKE_PROJECT_VERSION_TWEAK");
  285. }
  286. std::string vw;
  287. for (std::string const& i : vv) {
  288. cmProp v = mf.GetDefinition(i);
  289. if (cmNonempty(v)) {
  290. if (cmp0048 == cmPolicies::WARN) {
  291. if (!injectedProjectCommand) {
  292. vw += "\n ";
  293. vw += i;
  294. }
  295. } else {
  296. mf.AddDefinition(i, "");
  297. }
  298. }
  299. }
  300. if (!vw.empty()) {
  301. mf.IssueMessage(
  302. MessageType::AUTHOR_WARNING,
  303. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0048),
  304. "\nThe following variable(s) would be set to empty:", vw));
  305. }
  306. }
  307. mf.AddDefinition("PROJECT_DESCRIPTION", description);
  308. mf.AddDefinition(projectName + "_DESCRIPTION", description);
  309. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_DESCRIPTION", description);
  310. mf.AddDefinition("PROJECT_HOMEPAGE_URL", homepage);
  311. mf.AddDefinition(projectName + "_HOMEPAGE_URL", homepage);
  312. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_HOMEPAGE_URL", homepage);
  313. if (languages.empty()) {
  314. // if no language is specified do c and c++
  315. languages = { "C", "CXX" };
  316. }
  317. mf.EnableLanguage(languages, false);
  318. if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE")) {
  319. return false;
  320. }
  321. if (!IncludeByVariable(status,
  322. "CMAKE_PROJECT_" + projectName + "_INCLUDE")) {
  323. return false;
  324. }
  325. return true;
  326. }
  327. static bool IncludeByVariable(cmExecutionStatus& status,
  328. const std::string& variable)
  329. {
  330. cmMakefile& mf = status.GetMakefile();
  331. cmProp include = mf.GetDefinition(variable);
  332. if (!include) {
  333. return true;
  334. }
  335. std::string includeFile =
  336. cmSystemTools::CollapseFullPath(*include, mf.GetCurrentSourceDirectory());
  337. if (!cmSystemTools::FileExists(includeFile)) {
  338. status.SetError(cmStrCat("could not find requested file:\n ", *include));
  339. return false;
  340. }
  341. if (cmSystemTools::FileIsDirectory(includeFile)) {
  342. status.SetError(cmStrCat("requested file is a directory:\n ", *include));
  343. return false;
  344. }
  345. const bool readit = mf.ReadDependentFile(*include);
  346. if (readit) {
  347. return true;
  348. }
  349. if (cmSystemTools::GetFatalErrorOccured()) {
  350. return true;
  351. }
  352. status.SetError(cmStrCat("could not load requested file:\n ", *include));
  353. return false;
  354. }
  355. static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
  356. std::string const& value)
  357. {
  358. // Set the CMAKE_PROJECT_XXX variable to be the highest-level
  359. // project name in the tree. If there are two project commands
  360. // in the same CMakeLists.txt file, and it is the top level
  361. // CMakeLists.txt file, then go with the last one.
  362. if (!mf.GetDefinition(name) || mf.IsRootMakefile()) {
  363. mf.RemoveDefinition(name);
  364. mf.AddCacheDefinition(name, value, "Value Computed by CMake",
  365. cmStateEnums::STATIC);
  366. }
  367. }