cmProjectCommand.cxx 14 KB

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