cmake/Source/cmProjectCommand.cxx

413 lines
14 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"
2020-02-01 23:06:01 +01:00
#include <array>
#include <cstddef>
#include <cstdio>
2018-08-09 18:06:22 +02:00
#include <functional>
2020-02-01 23:06:01 +01:00
#include <limits>
#include <utility>
#include "cmsys/RegularExpression.hxx"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmPolicies.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
static bool IncludeByVariable(cmExecutionStatus& status,
const std::string& variable);
static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
std::string const& value);
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
bool cmProjectCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("PROJECT called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
2023-05-23 16:38:00 +02:00
if (mf.IsRootMakefile() &&
!mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
mf.IssueMessage(
MessageType::AUTHOR_WARNING,
"cmake_minimum_required() should be called prior to this top-level "
"project() call. Please see the cmake-commands(7) manual for usage "
"documentation of both commands.");
}
2020-02-01 23:06:01 +01:00
if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE_BEFORE")) {
2019-11-11 23:01:05 +01:00
return false;
}
2017-07-20 19:35:53 +02:00
std::string const& projectName = args[0];
2020-08-30 11:54:41 +02:00
if (!IncludeByVariable(status,
"CMAKE_PROJECT_" + projectName + "_INCLUDE_BEFORE")) {
return false;
}
2020-02-01 23:06:01 +01:00
mf.SetProjectName(projectName);
2013-03-16 19:13:01 +02:00
2020-02-01 23:06:01 +01:00
mf.AddCacheDefinition(projectName + "_BINARY_DIR",
2020-08-30 11:54:41 +02:00
mf.GetCurrentBinaryDirectory(),
2020-02-01 23:06:01 +01:00
"Value Computed by CMake", cmStateEnums::STATIC);
mf.AddCacheDefinition(projectName + "_SOURCE_DIR",
2020-08-30 11:54:41 +02:00
mf.GetCurrentSourceDirectory(),
2020-02-01 23:06:01 +01:00
"Value Computed by CMake", cmStateEnums::STATIC);
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_BINARY_DIR", mf.GetCurrentBinaryDirectory());
mf.AddDefinition("PROJECT_SOURCE_DIR", mf.GetCurrentSourceDirectory());
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_NAME", projectName);
2021-09-14 00:13:48 +02:00
mf.AddDefinitionBool("PROJECT_IS_TOP_LEVEL", mf.IsRootMakefile());
mf.AddCacheDefinition(projectName + "_IS_TOP_LEVEL",
mf.IsRootMakefile() ? "ON" : "OFF",
"Value Computed by CMake", cmStateEnums::STATIC);
// 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.
2020-02-01 23:06:01 +01:00
if (!mf.GetDefinition("CMAKE_PROJECT_NAME") || mf.IsRootMakefile()) {
2021-09-14 00:13:48 +02:00
mf.RemoveDefinition("CMAKE_PROJECT_NAME");
2020-08-30 11:54:41 +02:00
mf.AddCacheDefinition("CMAKE_PROJECT_NAME", projectName,
2020-02-01 23:06:01 +01:00
"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) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"LANGUAGES may be specified at most once.");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2014-08-03 19:52:23 +02:00
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()) {
2020-02-01 23:06:01 +01:00
std::string msg = cmStrCat(
2018-08-09 18:06:22 +02:00
"the following parameters must be specified after LANGUAGES "
2020-02-01 23:06:01 +01:00
"keyword: ",
cmJoin(languages, ", "), '.');
mf.IssueMessage(MessageType::WARNING, msg);
2018-08-09 18:06:22 +02:00
}
2016-07-09 11:21:54 +02:00
} else if (args[i] == "VERSION") {
if (haveVersion) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"VERSION may be specified at most once.");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2014-08-03 19:52:23 +02:00
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;
2020-02-01 23:06:01 +01:00
missedValueReporter = [&mf, &resetReporter]() {
mf.IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::WARNING,
2018-08-09 18:06:22 +02:00
"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) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"DESCRIPTION may be specified at most once.");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2017-07-20 19:35:53 +02:00
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;
2020-02-01 23:06:01 +01:00
missedValueReporter = [&mf, &resetReporter]() {
mf.IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::WARNING,
2018-08-09 18:06:22 +02:00
"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) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"HOMEPAGE_URL may be specified at most once.");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2018-08-09 18:06:22 +02:00
return true;
}
haveHomepage = true;
doing = DoingHomepage;
2020-02-01 23:06:01 +01:00
missedValueReporter = [&mf, &resetReporter]() {
mf.IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::WARNING,
2018-08-09 18:06:22 +02:00
"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()) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"project with VERSION, DESCRIPTION or HOMEPAGE_URL must "
"use LANGUAGES before language names.");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2014-08-03 19:52:23 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
if (haveLanguages && languages.empty()) {
2019-11-11 23:01:05 +01:00
languages.emplace_back("NONE");
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
cmPolicies::PolicyStatus const cmp0048 =
mf.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) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::FATAL_ERROR,
"VERSION not allowed unless CMP0048 is set to NEW");
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2014-08-03 19:52:23 +02:00
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(
2019-11-11 23:01:05 +01:00
R"(^([0-9]+(\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?)?)?$)");
2016-07-09 11:21:54 +02:00
if (!vx.find(version)) {
2020-02-01 23:06:01 +01:00
std::string e = R"(VERSION ")" + version + R"(" format invalid.)";
mf.IssueMessage(MessageType::FATAL_ERROR, e);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2014-08-03 19:52:23 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
cmPolicies::PolicyStatus const cmp0096 =
mf.GetPolicyStatus(cmPolicies::CMP0096);
constexpr std::size_t MAX_VERSION_COMPONENTS = 4u;
std::string version_string;
std::array<std::string, MAX_VERSION_COMPONENTS> version_components;
if (cmp0096 == cmPolicies::OLD || cmp0096 == cmPolicies::WARN) {
2022-03-29 21:10:50 +02:00
constexpr size_t maxIntLength =
std::numeric_limits<unsigned>::digits10 + 2;
char vb[MAX_VERSION_COMPONENTS][maxIntLength];
2020-02-01 23:06:01 +01:00
unsigned v[MAX_VERSION_COMPONENTS] = { 0, 0, 0, 0 };
const int vc = std::sscanf(version.c_str(), "%u.%u.%u.%u", &v[0], &v[1],
&v[2], &v[3]);
for (auto i = 0u; i < MAX_VERSION_COMPONENTS; ++i) {
2022-08-04 22:12:04 +02:00
if (static_cast<int>(i) < vc) {
2022-03-29 21:10:50 +02:00
std::snprintf(vb[i], maxIntLength, "%u", v[i]);
2022-08-04 22:12:04 +02:00
version_string += &"."[static_cast<std::size_t>(i == 0)];
2020-02-01 23:06:01 +01:00
version_string += vb[i];
version_components[i] = vb[i];
} else {
vb[i][0] = '\x00';
}
}
} else {
// The regex above verified that we have a .-separated string of
// non-negative integer components. Keep the original string.
version_string = std::move(version);
// Split the integer components.
auto components = cmSystemTools::SplitString(version_string, '.');
for (auto i = 0u; i < components.size(); ++i) {
version_components[i] = std::move(components[i]);
2014-08-03 19:52:23 +02:00
}
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";
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_VERSION", version_string);
mf.AddDefinition(vv, version_string);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_MAJOR";
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_VERSION_MAJOR", version_components[0]);
mf.AddDefinition(vv, version_components[0]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_MINOR";
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_VERSION_MINOR", version_components[1]);
mf.AddDefinition(vv, version_components[1]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_PATCH";
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_VERSION_PATCH", version_components[2]);
mf.AddDefinition(vv, version_components[2]);
2017-07-20 19:35:53 +02:00
vv = projectName + "_VERSION_TWEAK";
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_VERSION_TWEAK", version_components[3]);
mf.AddDefinition(vv, version_components[3]);
2018-08-09 18:06:22 +02:00
// Also, try set top level variables
2020-02-01 23:06:01 +01:00
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION", version_string);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MAJOR",
version_components[0]);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_MINOR",
version_components[1]);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_PATCH",
version_components[2]);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_VERSION_TWEAK",
version_components[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
2020-02-01 23:06:01 +01:00
std::vector<std::string> vv = { "PROJECT_VERSION",
"PROJECT_VERSION_MAJOR",
"PROJECT_VERSION_MINOR",
"PROJECT_VERSION_PATCH",
"PROJECT_VERSION_TWEAK",
projectName + "_VERSION",
projectName + "_VERSION_MAJOR",
projectName + "_VERSION_MINOR",
projectName + "_VERSION_PATCH",
projectName + "_VERSION_TWEAK" };
if (mf.IsRootMakefile()) {
2019-11-11 23:01:05 +01:00
vv.emplace_back("CMAKE_PROJECT_VERSION");
vv.emplace_back("CMAKE_PROJECT_VERSION_MAJOR");
vv.emplace_back("CMAKE_PROJECT_VERSION_MINOR");
vv.emplace_back("CMAKE_PROJECT_VERSION_PATCH");
vv.emplace_back("CMAKE_PROJECT_VERSION_TWEAK");
2018-08-09 18:06:22 +02:00
}
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) {
2021-11-20 13:41:27 +01:00
cmValue v = mf.GetDefinition(i);
2021-09-14 00:13:48 +02:00
if (cmNonempty(v)) {
2016-07-09 11:21:54 +02:00
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 {
2020-02-01 23:06:01 +01:00
mf.AddDefinition(i, "");
2014-08-03 19:52:23 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
if (!vw.empty()) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(
MessageType::AUTHOR_WARNING,
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0048),
"\nThe following variable(s) would be set to empty:", vw));
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_DESCRIPTION", description);
mf.AddDefinition(projectName + "_DESCRIPTION", description);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_DESCRIPTION", description);
2018-08-09 18:06:22 +02:00
2020-02-01 23:06:01 +01:00
mf.AddDefinition("PROJECT_HOMEPAGE_URL", homepage);
mf.AddDefinition(projectName + "_HOMEPAGE_URL", homepage);
TopLevelCMakeVarCondSet(mf, "CMAKE_PROJECT_HOMEPAGE_URL", homepage);
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++
2020-02-01 23:06:01 +01:00
languages = { "C", "CXX" };
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
mf.EnableLanguage(languages, false);
2019-11-11 23:01:05 +01:00
2020-02-01 23:06:01 +01:00
if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE")) {
2019-11-11 23:01:05 +01:00
return false;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
2020-02-01 23:06:01 +01:00
if (!IncludeByVariable(status,
"CMAKE_PROJECT_" + projectName + "_INCLUDE")) {
2019-11-11 23:01:05 +01:00
return false;
}
return true;
}
2018-08-09 18:06:22 +02:00
2020-02-01 23:06:01 +01:00
static bool IncludeByVariable(cmExecutionStatus& status,
const std::string& variable)
2019-11-11 23:01:05 +01:00
{
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
2021-11-20 13:41:27 +01:00
cmValue include = mf.GetDefinition(variable);
2019-11-11 23:01:05 +01:00
if (!include) {
return true;
}
2021-09-14 00:13:48 +02:00
std::string includeFile =
cmSystemTools::CollapseFullPath(*include, mf.GetCurrentSourceDirectory());
if (!cmSystemTools::FileExists(includeFile)) {
status.SetError(cmStrCat("could not find requested file:\n ", *include));
return false;
}
if (cmSystemTools::FileIsDirectory(includeFile)) {
status.SetError(cmStrCat("requested file is a directory:\n ", *include));
return false;
}
const bool readit = mf.ReadDependentFile(*include);
2019-11-11 23:01:05 +01:00
if (readit) {
return true;
}
2022-08-04 22:12:04 +02:00
if (cmSystemTools::GetFatalErrorOccurred()) {
2019-11-11 23:01:05 +01:00
return true;
}
2021-09-14 00:13:48 +02:00
status.SetError(cmStrCat("could not load requested file:\n ", *include));
2019-11-11 23:01:05 +01:00
return false;
}
2020-02-01 23:06:01 +01:00
static void TopLevelCMakeVarCondSet(cmMakefile& mf, std::string const& name,
std::string const& value)
2018-08-09 18:06:22 +02:00
{
// 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.
2020-02-01 23:06:01 +01:00
if (!mf.GetDefinition(name) || mf.IsRootMakefile()) {
2021-09-14 00:13:48 +02:00
mf.RemoveDefinition(name);
2020-08-30 11:54:41 +02:00
mf.AddCacheDefinition(name, value, "Value Computed by CMake",
2020-02-01 23:06:01 +01:00
cmStateEnums::STATIC);
2018-08-09 18:06:22 +02:00
}
}