cmProjectCommand.cxx 12 KB

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