cmProjectCommand.cxx 12 KB

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