cmProjectCommand.cxx 16 KB

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