cmProjectCommand.cxx 12 KB

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