cmProjectCommand.cxx 7.0 KB

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