cmProjectCommand.cxx 12 KB

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