cmake/Source/cmProjectCommand.cxx

352 lines
12 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmProjectCommand.h"
2017-07-20 19:35:53 +02:00
#include "cmsys/RegularExpression.hxx"
2018-08-09 18:06:22 +02:00
#include <functional>
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <stdio.h>
2018-08-09 18:06:22 +02:00
#include "cmAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmPolicies.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmake.h"
class cmExecutionStatus;
// cmProjectCommand
2016-07-09 11:21:54 +02:00
bool cmProjectCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("PROJECT called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
std::string const& projectName = args[0];
this->Makefile->SetProjectName(projectName);
std::string bindir = projectName;
bindir += "_BINARY_DIR";
2017-07-20 19:35:53 +02:00
std::string srcdir = projectName;
srcdir += "_SOURCE_DIR";
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(
bindir, this->Makefile->GetCurrentBinaryDirectory(),
2017-04-14 19:02:05 +02:00
"Value Computed by CMake", cmStateEnums::STATIC);
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(
srcdir, this->Makefile->GetCurrentSourceDirectory(),
2017-04-14 19:02:05 +02:00
"Value Computed by CMake", cmStateEnums::STATIC);
2013-03-16 19:13:01 +02:00
bindir = "PROJECT_BINARY_DIR";
srcdir = "PROJECT_SOURCE_DIR";
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(bindir,
2016-07-09 11:21:54 +02:00
this->Makefile->GetCurrentBinaryDirectory());
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(srcdir,
2016-07-09 11:21:54 +02:00
this->Makefile->GetCurrentSourceDirectory());
2017-07-20 19:35:53 +02:00
this->Makefile->AddDefinition("PROJECT_NAME", projectName.c_str());
// Set the CMAKE_PROJECT_NAME variable to be the highest-level
2012-02-18 12:40:36 +02:00
// 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.
2016-07-09 11:21:54 +02:00
if (!this->Makefile->GetDefinition("CMAKE_PROJECT_NAME") ||
(this->Makefile->IsRootMakefile())) {
2017-07-20 19:35:53 +02:00
this->Makefile->AddDefinition("CMAKE_PROJECT_NAME", projectName.c_str());
this->Makefile->AddCacheDefinition(
"CMAKE_PROJECT_NAME", projectName.c_str(), "Value Computed by CMake",
cmStateEnums::STATIC);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
bool haveVersion = false;
bool haveLanguages = false;
2017-07-20 19:35:53 +02:00
bool haveDescription = false;
2018-08-09 18:06:22 +02:00
bool haveHomepage = false;
bool injectedProjectCommand = false;
2014-08-03 19:52:23 +02:00
std::string version;
2017-07-20 19:35:53 +02:00
std::string description;
2018-08-09 18:06:22 +02:00
std::string homepage;
std::vector<std::string> languages;
2018-08-09 18:06:22 +02:00
std::function<void()> missedValueReporter;
auto resetReporter = [&missedValueReporter]() {
missedValueReporter = std::function<void()>();
};
2016-07-09 11:21:54 +02:00
enum Doing
{
2017-07-20 19:35:53 +02:00
DoingDescription,
2018-08-09 18:06:22 +02:00
DoingHomepage,
2016-07-09 11:21:54 +02:00
DoingLanguages,
DoingVersion
};
2014-08-03 19:52:23 +02:00
Doing doing = DoingLanguages;
2016-07-09 11:21:54 +02:00
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.");
2014-08-03 19:52:23 +02:00
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
haveLanguages = true;
2018-08-09 18:06:22 +02:00
if (missedValueReporter) {
missedValueReporter();
}
2014-08-03 19:52:23 +02:00
doing = DoingLanguages;
2018-08-09 18:06:22 +02:00
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);
}
2016-07-09 11:21:54 +02:00
} else if (args[i] == "VERSION") {
if (haveVersion) {
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"VERSION may be specified at most once.");
2014-08-03 19:52:23 +02:00
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
haveVersion = true;
2018-08-09 18:06:22 +02:00
if (missedValueReporter) {
missedValueReporter();
}
2014-08-03 19:52:23 +02:00
doing = DoingVersion;
2018-08-09 18:06:22 +02:00
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();
};
2017-07-20 19:35:53 +02:00
} else if (args[i] == "DESCRIPTION") {
if (haveDescription) {
this->Makefile->IssueMessage(
2018-04-23 21:13:27 +02:00
cmake::FATAL_ERROR, "DESCRIPTION may be specified at most once.");
2017-07-20 19:35:53 +02:00
cmSystemTools::SetFatalErrorOccured();
return true;
}
haveDescription = true;
2018-08-09 18:06:22 +02:00
if (missedValueReporter) {
missedValueReporter();
}
2017-07-20 19:35:53 +02:00
doing = DoingDescription;
2018-08-09 18:06:22 +02:00
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;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingVersion) {
2014-08-03 19:52:23 +02:00
doing = DoingLanguages;
version = args[i];
2018-08-09 18:06:22 +02:00
resetReporter();
2017-07-20 19:35:53 +02:00
} else if (doing == DoingDescription) {
doing = DoingLanguages;
description = args[i];
2018-08-09 18:06:22 +02:00
resetReporter();
} else if (doing == DoingHomepage) {
doing = DoingLanguages;
homepage = args[i];
resetReporter();
2016-07-09 11:21:54 +02:00
} else // doing == DoingLanguages
{
languages.push_back(args[i]);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2018-08-09 18:06:22 +02:00
if (missedValueReporter) {
missedValueReporter();
}
if ((haveVersion || haveDescription || haveHomepage) && !haveLanguages &&
!languages.empty()) {
2016-07-09 11:21:54 +02:00
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
2018-08-09 18:06:22 +02:00
"project with VERSION, DESCRIPTION or HOMEPAGE_URL must "
"use LANGUAGES before language names.");
2014-08-03 19:52:23 +02:00
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
if (haveLanguages && languages.empty()) {
2014-08-03 19:52:23 +02:00
languages.push_back("NONE");
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmPolicies::PolicyStatus cmp0048 =
this->Makefile->GetPolicyStatus(cmPolicies::CMP0048);
2016-07-09 11:21:54 +02:00
if (haveVersion) {
2014-08-03 19:52:23 +02:00
// Set project VERSION variables to given values
2016-07-09 11:21:54 +02:00
if (cmp0048 == cmPolicies::OLD || cmp0048 == cmPolicies::WARN) {
this->Makefile->IssueMessage(
cmake::FATAL_ERROR,
"VERSION not allowed unless CMP0048 is set to NEW");
2014-08-03 19:52:23 +02:00
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
cmsys::RegularExpression vx(
"^([0-9]+(\\.[0-9]+(\\.[0-9]+(\\.[0-9]+)?)?)?)?$");
if (!vx.find(version)) {
2014-08-03 19:52:23 +02:00
std::string e = "VERSION \"" + version + "\" format invalid.";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e);
cmSystemTools::SetFatalErrorOccured();
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string vs;
const char* sep = "";
char vb[4][64];
2016-07-09 11:21:54 +02:00
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) {
2014-08-03 19:52:23 +02:00
sprintf(vb[i], "%u", v[i]);
vs += sep;
vs += vb[i];
sep = ".";
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
vb[i][0] = 0;
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string vv;
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION";
2014-08-03 19:52:23 +02:00
this->Makefile->AddDefinition("PROJECT_VERSION", vs.c_str());
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(vv, vs.c_str());
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_MAJOR";
2014-08-03 19:52:23 +02:00
this->Makefile->AddDefinition("PROJECT_VERSION_MAJOR", vb[0]);
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(vv, vb[0]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_MINOR";
2014-08-03 19:52:23 +02:00
this->Makefile->AddDefinition("PROJECT_VERSION_MINOR", vb[1]);
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(vv, vb[1]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_PATCH";
2014-08-03 19:52:23 +02:00
this->Makefile->AddDefinition("PROJECT_VERSION_PATCH", vb[2]);
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(vv, vb[2]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_TWEAK";
2014-08-03 19:52:23 +02:00
this->Makefile->AddDefinition("PROJECT_VERSION_TWEAK", vb[3]);
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(vv, vb[3]);
2018-08-09 18:06:22 +02:00
// 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]);
2016-07-09 11:21:54 +02:00
} else if (cmp0048 != cmPolicies::OLD) {
2014-08-03 19:52:23 +02:00
// 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");
2017-07-20 19:35:53 +02:00
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");
2018-08-09 18:06:22 +02:00
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");
}
2014-08-03 19:52:23 +02:00
std::string vw;
2018-01-26 17:06:56 +01:00
for (std::string const& i : vv) {
const char* v = this->Makefile->GetDefinition(i);
2016-07-09 11:21:54 +02:00
if (v && *v) {
if (cmp0048 == cmPolicies::WARN) {
2018-08-09 18:06:22 +02:00
if (!injectedProjectCommand) {
vw += "\n ";
vw += i;
}
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
this->Makefile->AddDefinition(i, "");
2014-08-03 19:52:23 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
if (!vw.empty()) {
2015-04-27 22:25:09 +02:00
std::ostringstream w;
2015-08-17 11:37:30 +02:00
w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0048)
2014-08-03 19:52:23 +02:00
<< "\nThe following variable(s) would be set to empty:" << vw;
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2017-07-20 19:35:53 +02:00
if (haveDescription) {
this->Makefile->AddDefinition("PROJECT_DESCRIPTION", description.c_str());
2018-08-09 18:06:22 +02:00
this->Makefile->AddDefinition(projectName + "_DESCRIPTION",
description.c_str());
TopLevelCMakeVarCondSet("CMAKE_PROJECT_DESCRIPTION", description.c_str());
}
if (haveHomepage) {
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());
2017-07-20 19:35:53 +02:00
}
2016-07-09 11:21:54 +02:00
if (languages.empty()) {
// if no language is specified do c and c++
languages.push_back("C");
languages.push_back("CXX");
2016-07-09 11:21:54 +02:00
}
this->Makefile->EnableLanguage(languages, false);
2017-07-20 19:35:53 +02:00
std::string extraInclude = "CMAKE_PROJECT_" + projectName + "_INCLUDE";
2015-04-27 22:25:09 +02:00
const char* include = this->Makefile->GetDefinition(extraInclude);
2016-07-09 11:21:54 +02:00
if (include) {
bool readit = this->Makefile->ReadDependentFile(include);
if (!readit && !cmSystemTools::GetFatalErrorOccured()) {
std::string m = "could not find file:\n"
" ";
2012-04-19 19:04:21 +03:00
m += include;
2015-04-27 22:25:09 +02:00
this->SetError(m);
2012-04-19 19:04:21 +03:00
return false;
}
2016-07-09 11:21:54 +02:00
}
return true;
}
2018-08-09 18:06:22 +02:00
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);
}
}