cmake/Source/cmDefinePropertyCommand.cxx

123 lines
4.0 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 "cmDefinePropertyCommand.h"
2016-07-09 11:21:54 +02:00
2022-03-29 21:10:50 +02:00
#include <algorithm>
#include <iterator>
#include <cmext/string_view>
#include "cmArgumentParser.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmProperty.h"
2022-03-29 21:10:50 +02:00
#include "cmRange.h"
2015-08-17 11:37:30 +02:00
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
bool cmDefinePropertyCommand(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("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
// Get the scope in which to define the property.
cmProperty::ScopeType scope;
2017-07-20 19:35:53 +02:00
std::string const& scope_arg = args[0];
if (scope_arg == "GLOBAL") {
scope = cmProperty::GLOBAL;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "DIRECTORY") {
scope = cmProperty::DIRECTORY;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "TARGET") {
scope = cmProperty::TARGET;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "SOURCE") {
scope = cmProperty::SOURCE_FILE;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "TEST") {
scope = cmProperty::TEST;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "VARIABLE") {
scope = cmProperty::VARIABLE;
2017-07-20 19:35:53 +02:00
} else if (scope_arg == "CACHED_VARIABLE") {
scope = cmProperty::CACHED_VARIABLE;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("given invalid scope ", scope_arg,
". Valid scopes are GLOBAL, DIRECTORY, TARGET, "
"SOURCE, TEST, VARIABLE, CACHED_VARIABLE."));
return false;
2016-07-09 11:21:54 +02:00
}
// Parse remaining arguments.
bool inherited = false;
2020-02-01 23:06:01 +01:00
std::string PropertyName;
2022-03-29 21:10:50 +02:00
std::vector<std::string> BriefDocs;
std::vector<std::string> FullDocs;
std::string initializeFromVariable;
cmArgumentParser<void> parser;
parser.Bind("PROPERTY"_s, PropertyName);
parser.Bind("BRIEF_DOCS"_s, BriefDocs);
parser.Bind("FULL_DOCS"_s, FullDocs);
parser.Bind("INHERITED"_s, inherited);
parser.Bind("INITIALIZE_FROM_VARIABLE"_s, initializeFromVariable);
std::vector<std::string> invalidArgs;
parser.Parse(cmMakeRange(args).advance(1), &invalidArgs);
if (!invalidArgs.empty()) {
status.SetError(
cmStrCat("given invalid argument \"", invalidArgs.front(), "\"."));
return false;
2016-07-09 11:21:54 +02:00
}
// Make sure a property name was found.
2020-02-01 23:06:01 +01:00
if (PropertyName.empty()) {
status.SetError("not given a PROPERTY <name> argument.");
return false;
2016-07-09 11:21:54 +02:00
}
2022-03-29 21:10:50 +02:00
if (!initializeFromVariable.empty()) {
// Make sure property scope is TARGET.
if (scope != cmProperty::TARGET) {
status.SetError(
"Scope must be TARGET if INITIALIZE_FROM_VARIABLE is specified");
return false;
}
// Make sure the variable has the property name as a suffix.
if (!cmHasSuffix(initializeFromVariable, PropertyName)) {
status.SetError(cmStrCat("Variable name \"", initializeFromVariable,
"\" does not end with property name \"",
PropertyName, "\""));
return false;
}
if (PropertyName.find('_') == std::string::npos) {
status.SetError(cmStrCat("Property name \"", PropertyName,
"\" defined with INITIALIZE_FROM_VARIABLE does "
"not contain underscore"));
return false;
}
// Make sure the variable is not reserved.
static constexpr const char* reservedPrefixes[] = {
"CMAKE_",
"_CMAKE_",
};
if (std::any_of(std::begin(reservedPrefixes), std::end(reservedPrefixes),
[&initializeFromVariable](const char* prefix) {
return cmHasPrefix(initializeFromVariable, prefix);
})) {
status.SetError(cmStrCat("variable name \"", initializeFromVariable,
"\" is reserved"));
return false;
}
2016-07-09 11:21:54 +02:00
}
// Actually define the property.
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetState()->DefineProperty(
2022-03-29 21:10:50 +02:00
PropertyName, scope, cmJoin(BriefDocs, ""), cmJoin(FullDocs, ""),
inherited, initializeFromVariable);
return true;
}