Commit 7e142c5a authored by Brad King's avatar Brad King
Browse files

project: Manage VERSION variables



Teach the project() command to set variables

  {PROJECT,<PROJECT-NAME>}_VERSION{,_MAJOR,_MINOR,_PATCH,_TWEAK}

holding the project version number and its components.  Add project()
command option "VERSION" to specify the version explicitly, and default
to the empty string when it is not given.

Since this clears variables when no VERSION is given, this may change
behavior for existing projects that set the version variables themselves
prior to calling project().  Add policy CMP0048 for compatibility.
Suggested-by: Alexander Neundorf's avatarAlex Neundorf <neundorf@kde.org>
parent 16d040c9
project
-------
Set a name and enable languages for the entire project.
Set a name, version, and enable languages for the entire project.
.. code-block:: cmake
project(<PROJECT-NAME> [LANGUAGES] [<language-name>...])
project(<PROJECT-NAME>
[VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
[LANGUAGES <language-name>...])
Sets the name of the project and stores the name in the
:variable:`PROJECT_NAME` variable. Additionally this sets variables
......@@ -15,6 +18,28 @@ Sets the name of the project and stores the name in the
* :variable:`PROJECT_BINARY_DIR`,
:variable:`<PROJECT-NAME>_BINARY_DIR`
If ``VERSION`` is specified, given components must be non-negative integers.
If ``VERSION`` is not specified, the default version is the empty string.
The ``VERSION`` option may not be used unless policy :policy:`CMP0048` is
set to ``NEW``.
The :command:`project()` command stores the version number and its components
in variables
* :variable:`PROJECT_VERSION`,
:variable:`<PROJECT-NAME>_VERSION`
* :variable:`PROJECT_VERSION_MAJOR`,
:variable:`<PROJECT-NAME>_VERSION_MAJOR`
* :variable:`PROJECT_VERSION_MINOR`,
:variable:`<PROJECT-NAME>_VERSION_MINOR`
* :variable:`PROJECT_VERSION_PATCH`,
:variable:`<PROJECT-NAME>_VERSION_PATCH`
* :variable:`PROJECT_VERSION_TWEAK`,
:variable:`<PROJECT-NAME>_VERSION_TWEAK`
Variables corresponding to unspecified versions are set to the empty string
(if policy :policy:`CMP0048` is set to ``NEW``).
Optionally you can specify which languages your project supports.
Example languages are ``C``, ``CXX`` (i.e. C++), ``Fortran``, etc.
By default ``C`` and ``CXX`` are enabled if no language options are
......
......@@ -99,3 +99,4 @@ All Policies
/policy/CMP0045
/policy/CMP0046
/policy/CMP0047
/policy/CMP0048
......@@ -79,7 +79,17 @@ Variables that Provide Information
/variable/PROJECT-NAME_BINARY_DIR
/variable/PROJECT_NAME
/variable/PROJECT-NAME_SOURCE_DIR
/variable/PROJECT-NAME_VERSION
/variable/PROJECT-NAME_VERSION_MAJOR
/variable/PROJECT-NAME_VERSION_MINOR
/variable/PROJECT-NAME_VERSION_PATCH
/variable/PROJECT-NAME_VERSION_TWEAK
/variable/PROJECT_SOURCE_DIR
/variable/PROJECT_VERSION
/variable/PROJECT_VERSION_MAJOR
/variable/PROJECT_VERSION_MINOR
/variable/PROJECT_VERSION_PATCH
/variable/PROJECT_VERSION_TWEAK
Variables that Change Behavior
==============================
......
CMP0048
-------
The :command:`project` command manages VERSION variables.
CMake version 3.0.0 introduced the ``VERSION`` option of the :command:`project`
command to specify a project version as well as the name. In order to keep
:variable:`PROJECT_VERSION` and related variables consistent with variable
:variable:`PROJECT_NAME` it is necessary to set the VERSION variables
to the empty string when no ``VERSION`` is given to :command:`project`.
However, this can change behavior for existing projects that set VERSION
variables themselves since :command:`project` may now clear them.
This policy controls the behavior for compatibility with such projects.
The OLD behavior for this policy is to leave VERSION variables untouched.
The NEW behavior for this policy is to set VERSION as documented by the
:command:`project` command.
This policy was introduced in CMake version 3.0.0.
CMake version |release| warns when the policy is not set and uses
OLD behavior. Use the cmake_policy command to set it to OLD or
NEW explicitly.
<PROJECT-NAME>_VERSION
----------------------
Value given to the ``VERSION`` option of the most recent call to the
:command:`project` command with project name ``<PROJECT-NAME>``, if any.
See also the component-wise version variables
:variable:`<PROJECT-NAME>_VERSION_MAJOR`,
:variable:`<PROJECT-NAME>_VERSION_MINOR`,
:variable:`<PROJECT-NAME>_VERSION_PATCH`, and
:variable:`<PROJECT-NAME>_VERSION_TWEAK`.
<PROJECT-NAME>_VERSION_MAJOR
----------------------------
First version number component of the :variable:`<PROJECT-NAME>_VERSION`
variable as set by the :command:`project` command.
<PROJECT-NAME>_VERSION_MINOR
----------------------------
Second version number component of the :variable:`<PROJECT-NAME>_VERSION`
variable as set by the :command:`project` command.
<PROJECT-NAME>_VERSION_PATCH
----------------------------
Third version number component of the :variable:`<PROJECT-NAME>_VERSION`
variable as set by the :command:`project` command.
<PROJECT-NAME>_VERSION_TWEAK
----------------------------
Fourth version number component of the :variable:`<PROJECT-NAME>_VERSION`
variable as set by the :command:`project` command.
PROJECT_VERSION
---------------
Value given to the ``VERSION`` option of the most recent call to the
:command:`project` command, if any.
See also the component-wise version variables
:variable:`PROJECT_VERSION_MAJOR`,
:variable:`PROJECT_VERSION_MINOR`,
:variable:`PROJECT_VERSION_PATCH`, and
:variable:`PROJECT_VERSION_TWEAK`.
PROJECT_VERSION_MAJOR
---------------------
First version number component of the :variable:`PROJECT_VERSION`
variable as set by the :command:`project` command.
PROJECT_VERSION_MINOR
---------------------
Second version number component of the :variable:`PROJECT_VERSION`
variable as set by the :command:`project` command.
PROJECT_VERSION_PATCH
---------------------
Third version number component of the :variable:`PROJECT_VERSION`
variable as set by the :command:`project` command.
PROJECT_VERSION_TWEAK
---------------------
Fourth version number component of the :variable:`PROJECT_VERSION`
variable as set by the :command:`project` command.
......@@ -341,6 +341,11 @@ cmPolicies::cmPolicies()
CMP0047, "CMP0047",
"Use QCC compiler id for the qcc drivers on QNX.",
3,0,0,0, cmPolicies::WARN);
this->DefinePolicy(
CMP0048, "CMP0048",
"project() command manages VERSION variables.",
3,0,0,0, cmPolicies::WARN);
}
cmPolicies::~cmPolicies()
......
......@@ -101,6 +101,7 @@ public:
CMP0045, ///< Error on non-existent target in get_target_property
CMP0046, ///< Error on non-existent dependency in add_dependencies
CMP0047, ///< Use QCC compiler id for the qcc drivers on QNX.
CMP0048, ///< project() command manages VERSION variables
/** \brief Always the last entry.
*
......
......@@ -62,8 +62,12 @@ bool cmProjectCommand
"Value Computed by CMake", cmCacheManager::STATIC);
}
bool haveVersion = false;
bool haveLanguages = false;
std::string version;
std::vector<std::string> languages;
enum Doing { DoingLanguages, DoingVersion };
Doing doing = DoingLanguages;
for(size_t i = 1; i < args.size(); ++i)
{
if(args[i] == "LANGUAGES")
......@@ -76,18 +80,149 @@ bool cmProjectCommand
return true;
}
haveLanguages = true;
doing = DoingLanguages;
}
else
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;
doing = DoingVersion;
}
else if(doing == DoingVersion)
{
doing = DoingLanguages;
version = args[i];
}
else // doing == DoingLanguages
{
languages.push_back(args[i]);
}
}
if (haveVersion && !haveLanguages && !languages.empty())
{
this->Makefile->IssueMessage
(cmake::FATAL_ERROR,
"project with VERSION 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 = args[0] + "_VERSION";
this->Makefile->AddDefinition("PROJECT_VERSION", vs.c_str());
this->Makefile->AddDefinition(vv.c_str(), vs.c_str());
vv = args[0] + "_VERSION_MAJOR";
this->Makefile->AddDefinition("PROJECT_VERSION_MAJOR", vb[0]);
this->Makefile->AddDefinition(vv.c_str(), vb[0]);
vv = args[0] + "_VERSION_MINOR";
this->Makefile->AddDefinition("PROJECT_VERSION_MINOR", vb[1]);
this->Makefile->AddDefinition(vv.c_str(), vb[1]);
vv = args[0] + "_VERSION_PATCH";
this->Makefile->AddDefinition("PROJECT_VERSION_PATCH", vb[2]);
this->Makefile->AddDefinition(vv.c_str(), vb[2]);
vv = args[0] + "_VERSION_TWEAK";
this->Makefile->AddDefinition("PROJECT_VERSION_TWEAK", vb[3]);
this->Makefile->AddDefinition(vv.c_str(), 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(args[0] + "_VERSION");
vv.push_back(args[0] + "_VERSION_MAJOR");
vv.push_back(args[0] + "_VERSION_MINOR");
vv.push_back(args[0] + "_VERSION_PATCH");
vv.push_back(args[0] + "_VERSION_TWEAK");
std::string vw;
for(std::vector<std::string>::iterator i = vv.begin();
i != vv.end(); ++i)
{
const char* v = this->Makefile->GetDefinition(i->c_str());
if(v && *v)
{
if(cmp0048 == cmPolicies::WARN)
{
vw += "\n ";
vw += *i;
}
else
{
this->Makefile->AddDefinition(i->c_str(), "");
}
}
}
if(!vw.empty())
{
cmOStringStream w;
w << (this->Makefile->GetPolicies()
->GetPolicyWarning(cmPolicies::CMP0048))
<< "\nThe following variable(s) would be set to empty:" << vw;
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
}
}
if (languages.empty())
{
// if no language is specified do c and c++
......
-- PROJECT_VERSION='1.2.3.4'
-- ProjectA_VERSION='1.2.3.4'
-- PROJECT_VERSION_MAJOR='1'
-- ProjectA_VERSION_MAJOR='1'
-- PROJECT_VERSION_MINOR='2'
-- ProjectA_VERSION_MINOR='2'
-- PROJECT_VERSION_PATCH='3'
-- ProjectA_VERSION_PATCH='3'
-- PROJECT_VERSION_TWEAK='4'
-- ProjectA_VERSION_TWEAK='4'
-- PROJECT_VERSION='0.1.2'
-- ProjectB_VERSION='0.1.2'
-- PROJECT_VERSION_MAJOR='0'
-- ProjectB_VERSION_MAJOR='0'
-- PROJECT_VERSION_MINOR='1'
-- ProjectB_VERSION_MINOR='1'
-- PROJECT_VERSION_PATCH='2'
-- ProjectB_VERSION_PATCH='2'
-- PROJECT_VERSION_TWEAK=''
-- ProjectB_VERSION_TWEAK=''
-- PROJECT_VERSION=''
-- ProjectC_VERSION=''
-- PROJECT_VERSION_MAJOR=''
-- ProjectC_VERSION_MAJOR=''
-- PROJECT_VERSION_MINOR=''
-- ProjectC_VERSION_MINOR=''
-- PROJECT_VERSION_PATCH=''
-- ProjectC_VERSION_PATCH=''
-- PROJECT_VERSION_TWEAK=''
-- ProjectC_VERSION_TWEAK=''
macro(print_versions name)
foreach(v "" _MAJOR _MINOR _PATCH _TWEAK)
message(STATUS "PROJECT_VERSION${v}='${PROJECT_VERSION${v}}'")
message(STATUS "${name}_VERSION${v}='${${name}_VERSION${v}}'")
endforeach()
endmacro()
cmake_policy(SET CMP0048 NEW)
project(ProjectA VERSION 1.2.3.4 LANGUAGES NONE)
print_versions(ProjectA)
project(ProjectB VERSION 0.1.2 LANGUAGES NONE)
print_versions(ProjectB)
set(PROJECT_VERSION 1)
set(ProjectC_VERSION 1)
project(ProjectC NONE)
print_versions(ProjectC)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment