1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmProjectCommand.h"
#include "cmsys/RegularExpression.hxx"
#include <functional>
#include <sstream>
#include <stdio.h>
#include "cmAlgorithms.h"
#include "cmMakefile.h"
#include "cmPolicies.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmake.h"
class cmExecutionStatus;
// cmProjectCommand
bool cmProjectCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
if (args.empty()) {
this->SetError("PROJECT called with incorrect number of arguments");
return false;
}
std::string const& projectName = args[0];
this->Makefile->SetProjectName(projectName);
std::string bindir = projectName;
bindir += "_BINARY_DIR";
std::string srcdir = projectName;
srcdir += "_SOURCE_DIR";
this->Makefile->AddCacheDefinition(
bindir, this->Makefile->GetCurrentBinaryDirectory().c_str(),
"Value Computed by CMake", cmStateEnums::STATIC);
this->Makefile->AddCacheDefinition(
srcdir, this->Makefile->GetCurrentSourceDirectory().c_str(),
"Value Computed by CMake", cmStateEnums::STATIC);
bindir = "PROJECT_BINARY_DIR";
srcdir = "PROJECT_SOURCE_DIR";
this->Makefile->AddDefinition(
bindir, this->Makefile->GetCurrentBinaryDirectory().c_str());
this->Makefile->AddDefinition(
srcdir, this->Makefile->GetCurrentSourceDirectory().c_str());
this->Makefile->AddDefinition("PROJECT_NAME", projectName.c_str());
// Set the CMAKE_PROJECT_NAME variable to be the highest-level
// project name in the tree. If there are two project commands
// in the same CMakeLists.txt file, and it is the top level
// CMakeLists.txt file, then go with the last one, so that
// CMAKE_PROJECT_NAME will match PROJECT_NAME, and cmake --build
// will work.
if (!this->Makefile->GetDefinition("CMAKE_PROJECT_NAME") ||
(this->Makefile->IsRootMakefile())) {
this->Makefile->AddDefinition("CMAKE_PROJECT_NAME", projectName.c_str());
this->Makefile->AddCacheDefinition(
"CMAKE_PROJECT_NAME", projectName.c_str(), "Value Computed by CMake",
cmStateEnums::STATIC);
}
bool haveVersion = false;
bool haveLanguages = false;
bool haveDescription = false;
bool haveHomepage = false;
bool injectedProjectCommand = false;
std::string version;
std::string description;
std::string homepage;
std::vector<std::string> languages;
std::function<void()> missedValueReporter;
auto resetReporter = [&missedValueReporter]() {
missedValueReporter = std::function<void()>();
};
enum Doing
{
DoingDescription,
DoingHomepage,
DoingLanguages,
DoingVersion
};
Doing doing = DoingLanguages;
for (size_t i = 1; i < args.size(); ++i) {
if (args[i] == "LANGUAGES") {
if (haveLanguages) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR, "LANGUAGES may be specified at most once.");
cmSystemTools::SetFatalErrorOccured();
return true;
}
haveLanguages = true;
if (missedValueReporter) {
missedValueReporter();
}
doing = DoingLanguages;
if (!languages.empty()) {
std::string msg =
"the following parameters must be specified after LANGUAGES "
"keyword: ";
msg += cmJoin(languages, ", ");
msg += '.';
this->Makefile->IssueMessage(cmake::WARNING, msg);
}
} else if (args[i] == "VERSION") {
if (haveVersion) {
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"VERSION may be specified at most once.");
cmSystemTools::SetFatalErrorOccured();
return true;
}
haveVersion = true;
if (missedValueReporter) {
missedValueReporter();
}
doing = DoingVersion;
missedValueReporter = [this, &resetReporter]() {
this->Makefile->IssueMessage(
cmake::WARNING,
"VERSION keyword not followed by a value or was followed by a "
"value that expanded to nothing.");
resetReporter();
};
} else if (args[i] == "DESCRIPTION") {
if (haveDescription) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR, "DESCRIPTION may be specified at most once.");
cmSystemTools::SetFatalErrorOccured();
return true;
}
haveDescription = true;
if (missedValueReporter) {
missedValueReporter();
}
doing = DoingDescription;
missedValueReporter = [this, &resetReporter]() {
this->Makefile->IssueMessage(
cmake::WARNING,
"DESCRIPTION keyword not followed by a value or was followed "
"by a value that expanded to nothing.");
resetReporter();
};
} else if (args[i] == "HOMEPAGE_URL") {
if (haveHomepage) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR, "HOMEPAGE_URL may be specified at most once.");
cmSystemTools::SetFatalErrorOccured();
return true;
}
haveHomepage = true;
doing = DoingHomepage;
missedValueReporter = [this, &resetReporter]() {
this->Makefile->IssueMessage(
cmake::WARNING,
"HOMEPAGE_URL keyword not followed by a value or was followed "
"by a value that expanded to nothing.");
resetReporter();
};
} else if (i == 1 && args[i] == "__CMAKE_INJECTED_PROJECT_COMMAND__") {
injectedProjectCommand = true;
} else if (doing == DoingVersion) {
doing = DoingLanguages;
version = args[i];
resetReporter();
} else if (doing == DoingDescription) {
doing = DoingLanguages;
description = args[i];
resetReporter();
} else if (doing == DoingHomepage) {
doing = DoingLanguages;
homepage = args[i];
resetReporter();
} else // doing == DoingLanguages
{
languages.push_back(args[i]);
}
}
if (missedValueReporter) {
missedValueReporter();
}
if ((haveVersion || haveDescription || haveHomepage) && !haveLanguages &&
!languages.empty()) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
"project with VERSION, DESCRIPTION or HOMEPAGE_URL must "
"use LANGUAGES before language names.");
cmSystemTools::SetFatalErrorOccured();
return true;
}
if (haveLanguages && languages.empty()) {
languages.push_back("NONE");
}
cmPolicies::PolicyStatus cmp0048 =
this->Makefile->GetPolicyStatus(cmPolicies::CMP0048);
if (haveVersion) {
// Set project VERSION variables to given values
if (cmp0048 == cmPolicies::OLD || cmp0048 == cmPolicies::WARN) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
"VERSION not allowed unless CMP0048 is set to NEW");
cmSystemTools::SetFatalErrorOccured();
return true;
}
cmsys::RegularExpression vx(
"^([0-9]+(\\.[0-9]+(\\.[0-9]+(\\.[0-9]+)?)?)?)?$");
if (!vx.find(version)) {
std::string e = "VERSION \"" + version + "\" format invalid.";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
cmSystemTools::SetFatalErrorOccured();
return true;
}
std::string vs;
const char* sep = "";
char vb[4][64];
unsigned int v[4] = { 0, 0, 0, 0 };
int vc =
sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1], &v[2], &v[3]);
for (int i = 0; i < 4; ++i) {
if (i < vc) {
sprintf(vb[i], "%u", v[i]);
vs += sep;
vs += vb[i];
sep = ".";
} else {
vb[i][0] = 0;
}
}
std::string vv;
vv = projectName + "_VERSION";
this->Makefile->AddDefinition("PROJECT_VERSION", vs.c_str());
this->Makefile->AddDefinition(vv, vs.c_str());
vv = projectName + "_VERSION_MAJOR";
this->Makefile->AddDefinition("PROJECT_VERSION_MAJOR", vb[0]);
this->Makefile->AddDefinition(vv, vb[0]);
vv = projectName + "_VERSION_MINOR";
this->Makefile->AddDefinition("PROJECT_VERSION_MINOR", vb[1]);
this->Makefile->AddDefinition(vv, vb[1]);
vv = projectName + "_VERSION_PATCH";
this->Makefile->AddDefinition("PROJECT_VERSION_PATCH", vb[2]);
this->Makefile->AddDefinition(vv, vb[2]);
vv = projectName + "_VERSION_TWEAK";
this->Makefile->AddDefinition("PROJECT_VERSION_TWEAK", vb[3]);
this->Makefile->AddDefinition(vv, vb[3]);
// Also, try set top level variables
TopLevelCMakeVarCondSet("CMAKE_PROJECT_VERSION", vs.c_str());
TopLevelCMakeVarCondSet("CMAKE_PROJECT_VERSION_MAJOR", vb[0]);
TopLevelCMakeVarCondSet("CMAKE_PROJECT_VERSION_MINOR", vb[1]);
TopLevelCMakeVarCondSet("CMAKE_PROJECT_VERSION_PATCH", vb[2]);
TopLevelCMakeVarCondSet("CMAKE_PROJECT_VERSION_TWEAK", vb[3]);
} else if (cmp0048 != cmPolicies::OLD) {
// Set project VERSION variables to empty
std::vector<std::string> vv;
vv.push_back("PROJECT_VERSION");
vv.push_back("PROJECT_VERSION_MAJOR");
vv.push_back("PROJECT_VERSION_MINOR");
vv.push_back("PROJECT_VERSION_PATCH");
vv.push_back("PROJECT_VERSION_TWEAK");
vv.push_back(projectName + "_VERSION");
vv.push_back(projectName + "_VERSION_MAJOR");
vv.push_back(projectName + "_VERSION_MINOR");
vv.push_back(projectName + "_VERSION_PATCH");
vv.push_back(projectName + "_VERSION_TWEAK");
if (this->Makefile->IsRootMakefile()) {
vv.push_back("CMAKE_PROJECT_VERSION");
vv.push_back("CMAKE_PROJECT_VERSION_MAJOR");
vv.push_back("CMAKE_PROJECT_VERSION_MINOR");
vv.push_back("CMAKE_PROJECT_VERSION_PATCH");
vv.push_back("CMAKE_PROJECT_VERSION_TWEAK");
}
std::string vw;
for (std::string const& i : vv) {
const char* v = this->Makefile->GetDefinition(i);
if (v && *v) {
if (cmp0048 == cmPolicies::WARN) {
if (!injectedProjectCommand) {
vw += "\n ";
vw += i;
}
} else {
this->Makefile->AddDefinition(i, "");
}
}
}
if (!vw.empty()) {
std::ostringstream w;
w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0048)
<< "\nThe following variable(s) would be set to empty:" << vw;
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
}
this->Makefile->AddDefinition("PROJECT_DESCRIPTION", description.c_str());
this->Makefile->AddDefinition(projectName + "_DESCRIPTION",
description.c_str());
TopLevelCMakeVarCondSet("CMAKE_PROJECT_DESCRIPTION", description.c_str());
this->Makefile->AddDefinition("PROJECT_HOMEPAGE_URL", homepage.c_str());
this->Makefile->AddDefinition(projectName + "_HOMEPAGE_URL",
homepage.c_str());
TopLevelCMakeVarCondSet("CMAKE_PROJECT_HOMEPAGE_URL", homepage.c_str());
if (languages.empty()) {
// if no language is specified do c and c++
languages.push_back("C");
languages.push_back("CXX");
}
this->Makefile->EnableLanguage(languages, false);
std::string extraInclude = "CMAKE_PROJECT_" + projectName + "_INCLUDE";
const char* include = this->Makefile->GetDefinition(extraInclude);
if (include) {
bool readit = this->Makefile->ReadDependentFile(include);
if (!readit && !cmSystemTools::GetFatalErrorOccured()) {
std::string m = "could not find file:\n"
" ";
m += include;
this->SetError(m);
return false;
}
}
return true;
}
void cmProjectCommand::TopLevelCMakeVarCondSet(const char* const name,
const char* const value)
{
// Set the CMAKE_PROJECT_XXX variable to be the highest-level
// project name in the tree. If there are two project commands
// in the same CMakeLists.txt file, and it is the top level
// CMakeLists.txt file, then go with the last one.
if (!this->Makefile->GetDefinition(name) ||
(this->Makefile->IsRootMakefile())) {
this->Makefile->AddDefinition(name, value);
this->Makefile->AddCacheDefinition(name, value, "Value Computed by CMake",
cmStateEnums::STATIC);
}
}
|