cmProjectCommand.cxx 7.0 KB

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