cmProjectCommand.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <sstream>
  6. #include <stdio.h>
  7. #include "cmMakefile.h"
  8. #include "cmPolicies.h"
  9. #include "cmStateTypes.h"
  10. #include "cmSystemTools.h"
  11. #include "cmake.h"
  12. class cmExecutionStatus;
  13. // cmProjectCommand
  14. bool cmProjectCommand::InitialPass(std::vector<std::string> const& args,
  15. cmExecutionStatus&)
  16. {
  17. if (args.empty()) {
  18. this->SetError("PROJECT called with incorrect number of arguments");
  19. return false;
  20. }
  21. std::string const& projectName = args[0];
  22. this->Makefile->SetProjectName(projectName);
  23. std::string bindir = projectName;
  24. bindir += "_BINARY_DIR";
  25. std::string srcdir = projectName;
  26. srcdir += "_SOURCE_DIR";
  27. this->Makefile->AddCacheDefinition(
  28. bindir, this->Makefile->GetCurrentBinaryDirectory(),
  29. "Value Computed by CMake", cmStateEnums::STATIC);
  30. this->Makefile->AddCacheDefinition(
  31. srcdir, this->Makefile->GetCurrentSourceDirectory(),
  32. "Value Computed by CMake", cmStateEnums::STATIC);
  33. bindir = "PROJECT_BINARY_DIR";
  34. srcdir = "PROJECT_SOURCE_DIR";
  35. this->Makefile->AddDefinition(bindir,
  36. this->Makefile->GetCurrentBinaryDirectory());
  37. this->Makefile->AddDefinition(srcdir,
  38. this->Makefile->GetCurrentSourceDirectory());
  39. this->Makefile->AddDefinition("PROJECT_NAME", projectName.c_str());
  40. // Set the CMAKE_PROJECT_NAME variable to be the highest-level
  41. // project name in the tree. If there are two project commands
  42. // in the same CMakeLists.txt file, and it is the top level
  43. // CMakeLists.txt file, then go with the last one, so that
  44. // CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
  45. // will work.
  46. if (!this->Makefile->GetDefinition("CMAKE_PROJECT_NAME") ||
  47. (this->Makefile->IsRootMakefile())) {
  48. this->Makefile->AddDefinition("CMAKE_PROJECT_NAME", projectName.c_str());
  49. this->Makefile->AddCacheDefinition(
  50. "CMAKE_PROJECT_NAME", projectName.c_str(), "Value Computed by CMake",
  51. cmStateEnums::STATIC);
  52. }
  53. bool haveVersion = false;
  54. bool haveLanguages = false;
  55. bool haveDescription = false;
  56. std::string version;
  57. std::string description;
  58. std::vector<std::string> languages;
  59. enum Doing
  60. {
  61. DoingDescription,
  62. DoingLanguages,
  63. DoingVersion
  64. };
  65. Doing doing = DoingLanguages;
  66. for (size_t i = 1; i < args.size(); ++i) {
  67. if (args[i] == "LANGUAGES") {
  68. if (haveLanguages) {
  69. this->Makefile->IssueMessage(
  70. cmake::FATAL_ERROR, "LANGUAGES may be specified at most once.");
  71. cmSystemTools::SetFatalErrorOccured();
  72. return true;
  73. }
  74. haveLanguages = true;
  75. doing = DoingLanguages;
  76. } else if (args[i] == "VERSION") {
  77. if (haveVersion) {
  78. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  79. "VERSION may be specified at most once.");
  80. cmSystemTools::SetFatalErrorOccured();
  81. return true;
  82. }
  83. haveVersion = true;
  84. doing = DoingVersion;
  85. } else if (args[i] == "DESCRIPTION") {
  86. if (haveDescription) {
  87. this->Makefile->IssueMessage(
  88. cmake::FATAL_ERROR, "DESCRIPTION may be specified at most once.");
  89. cmSystemTools::SetFatalErrorOccured();
  90. return true;
  91. }
  92. haveDescription = true;
  93. doing = DoingDescription;
  94. } else if (doing == DoingVersion) {
  95. doing = DoingLanguages;
  96. version = args[i];
  97. } else if (doing == DoingDescription) {
  98. doing = DoingLanguages;
  99. description = args[i];
  100. } else // doing == DoingLanguages
  101. {
  102. languages.push_back(args[i]);
  103. }
  104. }
  105. if (haveVersion && !haveLanguages && !languages.empty()) {
  106. this->Makefile->IssueMessage(
  107. cmake::FATAL_ERROR,
  108. "project with VERSION must use LANGUAGES before language names.");
  109. cmSystemTools::SetFatalErrorOccured();
  110. return true;
  111. }
  112. if (haveLanguages && languages.empty()) {
  113. languages.push_back("NONE");
  114. }
  115. cmPolicies::PolicyStatus cmp0048 =
  116. this->Makefile->GetPolicyStatus(cmPolicies::CMP0048);
  117. if (haveVersion) {
  118. // Set project VERSION variables to given values
  119. if (cmp0048 == cmPolicies::OLD || cmp0048 == cmPolicies::WARN) {
  120. this->Makefile->IssueMessage(
  121. cmake::FATAL_ERROR,
  122. "VERSION not allowed unless CMP0048 is set to NEW");
  123. cmSystemTools::SetFatalErrorOccured();
  124. return true;
  125. }
  126. cmsys::RegularExpression vx(
  127. "^([0-9]+(\\.[0-9]+(\\.[0-9]+(\\.[0-9]+)?)?)?)?$");
  128. if (!vx.find(version)) {
  129. std::string e = "VERSION \"" + version + "\" format invalid.";
  130. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
  131. cmSystemTools::SetFatalErrorOccured();
  132. return true;
  133. }
  134. std::string vs;
  135. const char* sep = "";
  136. char vb[4][64];
  137. unsigned int v[4] = { 0, 0, 0, 0 };
  138. int vc =
  139. sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1], &v[2], &v[3]);
  140. for (int i = 0; i < 4; ++i) {
  141. if (i < vc) {
  142. sprintf(vb[i], "%u", v[i]);
  143. vs += sep;
  144. vs += vb[i];
  145. sep = ".";
  146. } else {
  147. vb[i][0] = 0;
  148. }
  149. }
  150. std::string vv;
  151. vv = projectName + "_VERSION";
  152. this->Makefile->AddDefinition("PROJECT_VERSION", vs.c_str());
  153. this->Makefile->AddDefinition(vv, vs.c_str());
  154. vv = projectName + "_VERSION_MAJOR";
  155. this->Makefile->AddDefinition("PROJECT_VERSION_MAJOR", vb[0]);
  156. this->Makefile->AddDefinition(vv, vb[0]);
  157. vv = projectName + "_VERSION_MINOR";
  158. this->Makefile->AddDefinition("PROJECT_VERSION_MINOR", vb[1]);
  159. this->Makefile->AddDefinition(vv, vb[1]);
  160. vv = projectName + "_VERSION_PATCH";
  161. this->Makefile->AddDefinition("PROJECT_VERSION_PATCH", vb[2]);
  162. this->Makefile->AddDefinition(vv, vb[2]);
  163. vv = projectName + "_VERSION_TWEAK";
  164. this->Makefile->AddDefinition("PROJECT_VERSION_TWEAK", vb[3]);
  165. this->Makefile->AddDefinition(vv, vb[3]);
  166. } else if (cmp0048 != cmPolicies::OLD) {
  167. // Set project VERSION variables to empty
  168. std::vector<std::string> vv;
  169. vv.push_back("PROJECT_VERSION");
  170. vv.push_back("PROJECT_VERSION_MAJOR");
  171. vv.push_back("PROJECT_VERSION_MINOR");
  172. vv.push_back("PROJECT_VERSION_PATCH");
  173. vv.push_back("PROJECT_VERSION_TWEAK");
  174. vv.push_back(projectName + "_VERSION");
  175. vv.push_back(projectName + "_VERSION_MAJOR");
  176. vv.push_back(projectName + "_VERSION_MINOR");
  177. vv.push_back(projectName + "_VERSION_PATCH");
  178. vv.push_back(projectName + "_VERSION_TWEAK");
  179. std::string vw;
  180. for (std::string const& i : vv) {
  181. const char* v = this->Makefile->GetDefinition(i);
  182. if (v && *v) {
  183. if (cmp0048 == cmPolicies::WARN) {
  184. vw += "\n ";
  185. vw += i;
  186. } else {
  187. this->Makefile->AddDefinition(i, "");
  188. }
  189. }
  190. }
  191. if (!vw.empty()) {
  192. std::ostringstream w;
  193. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0048)
  194. << "\nThe following variable(s) would be set to empty:" << vw;
  195. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  196. }
  197. }
  198. if (haveDescription) {
  199. this->Makefile->AddDefinition("PROJECT_DESCRIPTION", description.c_str());
  200. // Set the CMAKE_PROJECT_DESCRIPTION variable to be the highest-level
  201. // project name in the tree. If there are two project commands
  202. // in the same CMakeLists.txt file, and it is the top level
  203. // CMakeLists.txt file, then go with the last one.
  204. if (!this->Makefile->GetDefinition("CMAKE_PROJECT_DESCRIPTION") ||
  205. (this->Makefile->IsRootMakefile())) {
  206. this->Makefile->AddDefinition("CMAKE_PROJECT_DESCRIPTION",
  207. description.c_str());
  208. this->Makefile->AddCacheDefinition(
  209. "CMAKE_PROJECT_DESCRIPTION", description.c_str(),
  210. "Value Computed by CMake", cmStateEnums::STATIC);
  211. }
  212. }
  213. if (languages.empty()) {
  214. // if no language is specified do c and c++
  215. languages.push_back("C");
  216. languages.push_back("CXX");
  217. }
  218. this->Makefile->EnableLanguage(languages, false);
  219. std::string extraInclude = "CMAKE_PROJECT_" + projectName + "_INCLUDE";
  220. const char* include = this->Makefile->GetDefinition(extraInclude);
  221. if (include) {
  222. bool readit = this->Makefile->ReadDependentFile(include);
  223. if (!readit && !cmSystemTools::GetFatalErrorOccured()) {
  224. std::string m = "could not find file:\n"
  225. " ";
  226. m += include;
  227. this->SetError(m);
  228. return false;
  229. }
  230. }
  231. return true;
  232. }