cmProjectCommand.cxx 12 KB

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