cmProjectCommand.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. this->Makefile->SetProjectName(args[0]);
  22. std::string bindir = args[0];
  23. bindir += "_BINARY_DIR";
  24. std::string srcdir = args[0];
  25. srcdir += "_SOURCE_DIR";
  26. this->Makefile->AddCacheDefinition(
  27. bindir, this->Makefile->GetCurrentBinaryDirectory(),
  28. "Value Computed by CMake", cmStateEnums::STATIC);
  29. this->Makefile->AddCacheDefinition(
  30. srcdir, this->Makefile->GetCurrentSourceDirectory(),
  31. "Value Computed by CMake", cmStateEnums::STATIC);
  32. bindir = "PROJECT_BINARY_DIR";
  33. srcdir = "PROJECT_SOURCE_DIR";
  34. this->Makefile->AddDefinition(bindir,
  35. this->Makefile->GetCurrentBinaryDirectory());
  36. this->Makefile->AddDefinition(srcdir,
  37. this->Makefile->GetCurrentSourceDirectory());
  38. this->Makefile->AddDefinition("PROJECT_NAME", args[0].c_str());
  39. // Set the CMAKE_PROJECT_NAME variable to be the highest-level
  40. // project name in the tree. If there are two project commands
  41. // in the same CMakeLists.txt file, and it is the top level
  42. // CMakeLists.txt file, then go with the last one, so that
  43. // CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
  44. // will work.
  45. if (!this->Makefile->GetDefinition("CMAKE_PROJECT_NAME") ||
  46. (this->Makefile->IsRootMakefile())) {
  47. this->Makefile->AddDefinition("CMAKE_PROJECT_NAME", args[0].c_str());
  48. this->Makefile->AddCacheDefinition("CMAKE_PROJECT_NAME", args[0].c_str(),
  49. "Value Computed by CMake",
  50. cmStateEnums::STATIC);
  51. }
  52. bool haveVersion = false;
  53. bool haveLanguages = false;
  54. bool haveDescription = false;
  55. std::string version;
  56. std::string description;
  57. std::vector<std::string> languages;
  58. enum Doing
  59. {
  60. DoingDescription,
  61. DoingLanguages,
  62. DoingVersion
  63. };
  64. Doing doing = DoingLanguages;
  65. for (size_t i = 1; i < args.size(); ++i) {
  66. if (args[i] == "LANGUAGES") {
  67. if (haveLanguages) {
  68. this->Makefile->IssueMessage(
  69. cmake::FATAL_ERROR, "LANGUAGES may be specified at most once.");
  70. cmSystemTools::SetFatalErrorOccured();
  71. return true;
  72. }
  73. haveLanguages = true;
  74. doing = DoingLanguages;
  75. } else if (args[i] == "VERSION") {
  76. if (haveVersion) {
  77. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  78. "VERSION may be specified at most once.");
  79. cmSystemTools::SetFatalErrorOccured();
  80. return true;
  81. }
  82. haveVersion = true;
  83. doing = DoingVersion;
  84. } else if (args[i] == "DESCRIPTION") {
  85. if (haveDescription) {
  86. this->Makefile->IssueMessage(
  87. cmake::FATAL_ERROR, "DESCRITPION may be specified at most once.");
  88. cmSystemTools::SetFatalErrorOccured();
  89. return true;
  90. }
  91. haveDescription = true;
  92. doing = DoingDescription;
  93. } else if (doing == DoingVersion) {
  94. doing = DoingLanguages;
  95. version = args[i];
  96. } else if (doing == DoingDescription) {
  97. doing = DoingLanguages;
  98. description = args[i];
  99. } else // doing == DoingLanguages
  100. {
  101. languages.push_back(args[i]);
  102. }
  103. }
  104. if (haveVersion && !haveLanguages && !languages.empty()) {
  105. this->Makefile->IssueMessage(
  106. cmake::FATAL_ERROR,
  107. "project with VERSION must use LANGUAGES before language names.");
  108. cmSystemTools::SetFatalErrorOccured();
  109. return true;
  110. }
  111. if (haveLanguages && languages.empty()) {
  112. languages.push_back("NONE");
  113. }
  114. cmPolicies::PolicyStatus cmp0048 =
  115. this->Makefile->GetPolicyStatus(cmPolicies::CMP0048);
  116. if (haveVersion) {
  117. // Set project VERSION variables to given values
  118. if (cmp0048 == cmPolicies::OLD || cmp0048 == cmPolicies::WARN) {
  119. this->Makefile->IssueMessage(
  120. cmake::FATAL_ERROR,
  121. "VERSION not allowed unless CMP0048 is set to NEW");
  122. cmSystemTools::SetFatalErrorOccured();
  123. return true;
  124. }
  125. cmsys::RegularExpression vx(
  126. "^([0-9]+(\\.[0-9]+(\\.[0-9]+(\\.[0-9]+)?)?)?)?$");
  127. if (!vx.find(version)) {
  128. std::string e = "VERSION \"" + version + "\" format invalid.";
  129. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
  130. cmSystemTools::SetFatalErrorOccured();
  131. return true;
  132. }
  133. std::string vs;
  134. const char* sep = "";
  135. char vb[4][64];
  136. unsigned int v[4] = { 0, 0, 0, 0 };
  137. int vc =
  138. sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1], &v[2], &v[3]);
  139. for (int i = 0; i < 4; ++i) {
  140. if (i < vc) {
  141. sprintf(vb[i], "%u", v[i]);
  142. vs += sep;
  143. vs += vb[i];
  144. sep = ".";
  145. } else {
  146. vb[i][0] = 0;
  147. }
  148. }
  149. std::string vv;
  150. vv = args[0] + "_VERSION";
  151. this->Makefile->AddDefinition("PROJECT_VERSION", vs.c_str());
  152. this->Makefile->AddDefinition(vv, vs.c_str());
  153. vv = args[0] + "_VERSION_MAJOR";
  154. this->Makefile->AddDefinition("PROJECT_VERSION_MAJOR", vb[0]);
  155. this->Makefile->AddDefinition(vv, vb[0]);
  156. vv = args[0] + "_VERSION_MINOR";
  157. this->Makefile->AddDefinition("PROJECT_VERSION_MINOR", vb[1]);
  158. this->Makefile->AddDefinition(vv, vb[1]);
  159. vv = args[0] + "_VERSION_PATCH";
  160. this->Makefile->AddDefinition("PROJECT_VERSION_PATCH", vb[2]);
  161. this->Makefile->AddDefinition(vv, vb[2]);
  162. vv = args[0] + "_VERSION_TWEAK";
  163. this->Makefile->AddDefinition("PROJECT_VERSION_TWEAK", vb[3]);
  164. this->Makefile->AddDefinition(vv, vb[3]);
  165. } else if (cmp0048 != cmPolicies::OLD) {
  166. // Set project VERSION variables to empty
  167. std::vector<std::string> vv;
  168. vv.push_back("PROJECT_VERSION");
  169. vv.push_back("PROJECT_VERSION_MAJOR");
  170. vv.push_back("PROJECT_VERSION_MINOR");
  171. vv.push_back("PROJECT_VERSION_PATCH");
  172. vv.push_back("PROJECT_VERSION_TWEAK");
  173. vv.push_back(args[0] + "_VERSION");
  174. vv.push_back(args[0] + "_VERSION_MAJOR");
  175. vv.push_back(args[0] + "_VERSION_MINOR");
  176. vv.push_back(args[0] + "_VERSION_PATCH");
  177. vv.push_back(args[0] + "_VERSION_TWEAK");
  178. std::string vw;
  179. for (std::vector<std::string>::iterator i = vv.begin(); i != vv.end();
  180. ++i) {
  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_" + args[0] + "_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. }