cmProjectCommand.cxx 16 KB

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