cmProjectCommand.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. cmPolicies::PolicyStatus cmp0180 = mf.GetPolicyStatus(cmPolicies::CMP0180);
  50. std::string varName = cmStrCat(projectName, "_BINARY_DIR"_s);
  51. bool nonCacheVarAlreadySet = mf.IsNormalDefinitionSet(varName);
  52. mf.AddCacheDefinition(varName, mf.GetCurrentBinaryDirectory(),
  53. "Value Computed by CMake", cmStateEnums::STATIC);
  54. if (cmp0180 == cmPolicies::NEW || nonCacheVarAlreadySet) {
  55. mf.AddDefinition(varName, mf.GetCurrentBinaryDirectory());
  56. }
  57. varName = cmStrCat(projectName, "_SOURCE_DIR"_s);
  58. nonCacheVarAlreadySet = mf.IsNormalDefinitionSet(varName);
  59. mf.AddCacheDefinition(varName, mf.GetCurrentSourceDirectory(),
  60. "Value Computed by CMake", cmStateEnums::STATIC);
  61. if (cmp0180 == cmPolicies::NEW || nonCacheVarAlreadySet) {
  62. mf.AddDefinition(varName, mf.GetCurrentSourceDirectory());
  63. }
  64. mf.AddDefinition("PROJECT_BINARY_DIR", mf.GetCurrentBinaryDirectory());
  65. mf.AddDefinition("PROJECT_SOURCE_DIR", mf.GetCurrentSourceDirectory());
  66. mf.AddDefinition("PROJECT_NAME", projectName);
  67. mf.AddDefinitionBool("PROJECT_IS_TOP_LEVEL", mf.IsRootMakefile());
  68. varName = cmStrCat(projectName, "_IS_TOP_LEVEL"_s);
  69. nonCacheVarAlreadySet = mf.IsNormalDefinitionSet(varName);
  70. mf.AddCacheDefinition(varName, mf.IsRootMakefile() ? "ON" : "OFF",
  71. "Value Computed by CMake", cmStateEnums::STATIC);
  72. if (cmp0180 == cmPolicies::NEW || nonCacheVarAlreadySet) {
  73. mf.AddDefinition(varName, mf.IsRootMakefile() ? "ON" : "OFF");
  74. }
  75. // Set the CMAKE_PROJECT_NAME variable to be the highest-level
  76. // project name in the tree. If there are two project commands
  77. // in the same CMakeLists.txt file, and it is the top level
  78. // CMakeLists.txt file, then go with the last one, so that
  79. // CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
  80. // will work.
  81. if (!mf.GetDefinition("CMAKE_PROJECT_NAME") || mf.IsRootMakefile()) {
  82. mf.RemoveDefinition("CMAKE_PROJECT_NAME");
  83. mf.AddCacheDefinition("CMAKE_PROJECT_NAME", projectName,
  84. "Value Computed by CMake", cmStateEnums::STATIC);
  85. }
  86. bool haveVersion = false;
  87. bool haveLanguages = false;
  88. bool haveDescription = false;
  89. bool haveHomepage = 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 (doing == DoingVersion) {
  181. doing = DoingLanguages;
  182. version = args[i];
  183. resetReporter();
  184. } else if (doing == DoingDescription) {
  185. doing = DoingLanguages;
  186. description = args[i];
  187. resetReporter();
  188. } else if (doing == DoingHomepage) {
  189. doing = DoingLanguages;
  190. homepage = args[i];
  191. resetReporter();
  192. } else // doing == DoingLanguages
  193. {
  194. languages.push_back(args[i]);
  195. }
  196. }
  197. if (missedValueReporter) {
  198. missedValueReporter();
  199. }
  200. if ((haveVersion || haveDescription || haveHomepage) && !haveLanguages &&
  201. !languages.empty()) {
  202. mf.IssueMessage(MessageType::FATAL_ERROR,
  203. "project with VERSION, DESCRIPTION or HOMEPAGE_URL must "
  204. "use LANGUAGES before language names.");
  205. cmSystemTools::SetFatalErrorOccurred();
  206. return true;
  207. }
  208. if (haveLanguages && languages.empty()) {
  209. languages.emplace_back("NONE");
  210. }
  211. if (haveVersion) {
  212. cmsys::RegularExpression vx(
  213. R"(^([0-9]+(\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?)?)?$)");
  214. if (!vx.find(version)) {
  215. std::string e = R"(VERSION ")" + version + R"(" format invalid.)";
  216. mf.IssueMessage(MessageType::FATAL_ERROR, e);
  217. cmSystemTools::SetFatalErrorOccurred();
  218. return true;
  219. }
  220. cmPolicies::PolicyStatus const cmp0096 =
  221. mf.GetPolicyStatus(cmPolicies::CMP0096);
  222. constexpr std::size_t MAX_VERSION_COMPONENTS = 4u;
  223. std::string version_string;
  224. std::array<std::string, MAX_VERSION_COMPONENTS> version_components;
  225. if (cmp0096 == cmPolicies::OLD || cmp0096 == cmPolicies::WARN) {
  226. constexpr size_t maxIntLength =
  227. std::numeric_limits<unsigned>::digits10 + 2;
  228. char vb[MAX_VERSION_COMPONENTS][maxIntLength];
  229. unsigned v[MAX_VERSION_COMPONENTS] = { 0, 0, 0, 0 };
  230. const int vc = std::sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1],
  231. &v[2], &v[3]);
  232. for (auto i = 0u; i < MAX_VERSION_COMPONENTS; ++i) {
  233. if (static_cast<int>(i) < vc) {
  234. std::snprintf(vb[i], maxIntLength, "%u", v[i]);
  235. version_string += &"."[static_cast<std::size_t>(i == 0)];
  236. version_string += vb[i];
  237. version_components[i] = vb[i];
  238. } else {
  239. vb[i][0] = '\x00';
  240. }
  241. }
  242. } else {
  243. // The regex above verified that we have a .-separated string of
  244. // non-negative integer components. Keep the original string.
  245. version_string = std::move(version);
  246. // Split the integer components.
  247. auto components = cmSystemTools::SplitString(version_string, '.');
  248. for (auto i = 0u; i < components.size(); ++i) {
  249. version_components[i] = std::move(components[i]);
  250. }
  251. }
  252. std::string vv;
  253. vv = projectName + "_VERSION";
  254. mf.AddDefinition("PROJECT_VERSION", version_string);
  255. mf.AddDefinition(vv, version_string);
  256. vv = projectName + "_VERSION_MAJOR";
  257. mf.AddDefinition("PROJECT_VERSION_MAJOR", version_components[0]);
  258. mf.AddDefinition(vv, version_components[0]);
  259. vv = projectName + "_VERSION_MINOR";
  260. mf.AddDefinition("PROJECT_VERSION_MINOR", version_components[1]);
  261. mf.AddDefinition(vv, version_components[1]);
  262. vv = projectName + "_VERSION_PATCH";
  263. mf.AddDefinition("PROJECT_VERSION_PATCH", version_components[2]);
  264. mf.AddDefinition(vv, version_components[2]);
  265. vv = projectName + "_VERSION_TWEAK";
  266. mf.AddDefinition("PROJECT_VERSION_TWEAK", version_components[3]);
  267. mf.AddDefinition(vv, version_components[3]);
  268. // Also, try set top level variables
  269. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION", version_string);
  270. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MAJOR",
  271. version_components[0]);
  272. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MINOR",
  273. version_components[1]);
  274. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_PATCH",
  275. version_components[2]);
  276. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_TWEAK",
  277. version_components[3]);
  278. } else {
  279. // Set project VERSION variables to empty
  280. std::vector<std::string> vv = { "PROJECT_VERSION",
  281. "PROJECT_VERSION_MAJOR",
  282. "PROJECT_VERSION_MINOR",
  283. "PROJECT_VERSION_PATCH",
  284. "PROJECT_VERSION_TWEAK",
  285. projectName + "_VERSION",
  286. projectName + "_VERSION_MAJOR",
  287. projectName + "_VERSION_MINOR",
  288. projectName + "_VERSION_PATCH",
  289. projectName + "_VERSION_TWEAK" };
  290. if (mf.IsRootMakefile()) {
  291. vv.emplace_back("CMAKE_PROJECT_VERSION");
  292. vv.emplace_back("CMAKE_PROJECT_VERSION_MAJOR");
  293. vv.emplace_back("CMAKE_PROJECT_VERSION_MINOR");
  294. vv.emplace_back("CMAKE_PROJECT_VERSION_PATCH");
  295. vv.emplace_back("CMAKE_PROJECT_VERSION_TWEAK");
  296. }
  297. for (std::string const& i : vv) {
  298. cmValue v = mf.GetDefinition(i);
  299. if (cmNonempty(v)) {
  300. mf.AddDefinition(i, "");
  301. }
  302. }
  303. }
  304. mf.AddDefinition("PROJECT_DESCRIPTION", description);
  305. mf.AddDefinition(projectName + "_DESCRIPTION", description);
  306. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_DESCRIPTION", description);
  307. mf.AddDefinition("PROJECT_HOMEPAGE_URL", homepage);
  308. mf.AddDefinition(projectName + "_HOMEPAGE_URL", homepage);
  309. TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_HOMEPAGE_URL", homepage);
  310. if (languages.empty()) {
  311. // if no language is specified do c and c++
  312. languages = { "C", "CXX" };
  313. }
  314. mf.EnableLanguage(languages, false);
  315. if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE")) {
  316. return false;
  317. }
  318. if (!IncludeByVariable(status,
  319. "CMAKE_PROJECT_" + projectName + "_INCLUDE")) {
  320. return false;
  321. }
  322. return true;
  323. }
  324. static bool IncludeByVariable(cmExecutionStatus& status,
  325. const std::string& variable)
  326. {
  327. cmMakefile& mf = status.GetMakefile();
  328. cmValue include = mf.GetDefinition(variable);
  329. if (!include) {
  330. return true;
  331. }
  332. cmList includeFiles{ *include };
  333. bool failed = false;
  334. for (auto filePath : includeFiles) {
  335. // Any relative path without a .cmake extension is checked for valid cmake
  336. // modules. This logic should be consistent with CMake's include() command.
  337. // Otherwise default to checking relative path w.r.t. source directory
  338. if (!cmSystemTools::FileIsFullPath(filePath) &&
  339. !cmHasLiteralSuffix(filePath, ".cmake")) {
  340. std::string mfile = mf.GetModulesFile(cmStrCat(filePath, ".cmake"));
  341. if (mfile.empty()) {
  342. status.SetError(
  343. cmStrCat("could not find requested module:\n ", filePath));
  344. failed = true;
  345. continue;
  346. }
  347. filePath = mfile;
  348. }
  349. std::string includeFile = cmSystemTools::CollapseFullPath(
  350. filePath, mf.GetCurrentSourceDirectory());
  351. if (!cmSystemTools::FileExists(includeFile)) {
  352. status.SetError(
  353. cmStrCat("could not find requested file:\n ", filePath));
  354. failed = true;
  355. continue;
  356. }
  357. if (cmSystemTools::FileIsDirectory(includeFile)) {
  358. status.SetError(
  359. cmStrCat("requested file is a directory:\n ", filePath));
  360. failed = true;
  361. continue;
  362. }
  363. const bool readit = mf.ReadDependentFile(filePath);
  364. if (readit) {
  365. // If the included file ran successfully, continue to the next file
  366. continue;
  367. }
  368. if (cmSystemTools::GetFatalErrorOccurred()) {
  369. failed = true;
  370. continue;
  371. }
  372. status.SetError(cmStrCat("could not load requested file:\n ", filePath));
  373. failed = true;
  374. }
  375. // At this point all files were processed
  376. return !failed;
  377. }
  378. static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
  379. std::string const& value)
  380. {
  381. // Set the CMAKE_PROJECT_XXX variable to be the highest-level
  382. // project name in the tree. If there are two project commands
  383. // in the same CMakeLists.txt file, and it is the top level
  384. // CMakeLists.txt file, then go with the last one.
  385. if (!mf.GetDefinition(name) || mf.IsRootMakefile()) {
  386. mf.RemoveDefinition(name);
  387. mf.AddCacheDefinition(name, value, "Value Computed by CMake",
  388. cmStateEnums::STATIC);
  389. }
  390. }