cmProjectCommand.cxx 7.4 KB

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