cmProjectCommand.cxx 15 KB

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