summaryrefslogtreecommitdiffstats
path: root/Source/cmDefinePropertyCommand.cxx
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2022-01-13 20:59:48 (GMT)
committerKyle Edwards <kyle.edwards@kitware.com>2022-01-20 14:05:35 (GMT)
commitfce24e4f102686c5c103db301bb698e0ea82765f (patch)
treee5effdc88ea773c899b08b084aaf89dc936885dc /Source/cmDefinePropertyCommand.cxx
parent1ca83ae2bbf9e474c46b6ea094c5035420bbfd45 (diff)
downloadCMake-fce24e4f102686c5c103db301bb698e0ea82765f.zip
CMake-fce24e4f102686c5c103db301bb698e0ea82765f.tar.gz
CMake-fce24e4f102686c5c103db301bb698e0ea82765f.tar.bz2
define_property(): Add INITIALIZE_FROM_VARIABLE argument
Fixes: #20698
Diffstat (limited to 'Source/cmDefinePropertyCommand.cxx')
-rw-r--r--Source/cmDefinePropertyCommand.cxx44
1 files changed, 43 insertions, 1 deletions
diff --git a/Source/cmDefinePropertyCommand.cxx b/Source/cmDefinePropertyCommand.cxx
index 10c36cd..7a2f34f 100644
--- a/Source/cmDefinePropertyCommand.cxx
+++ b/Source/cmDefinePropertyCommand.cxx
@@ -2,6 +2,9 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDefinePropertyCommand.h"
+#include <algorithm>
+#include <iterator>
+
#include <cmext/string_view>
#include "cmArgumentParser.h"
@@ -50,12 +53,14 @@ bool cmDefinePropertyCommand(std::vector<std::string> const& args,
std::string PropertyName;
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);
@@ -71,10 +76,47 @@ bool cmDefinePropertyCommand(std::vector<std::string> const& args,
return false;
}
+ 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 (initializeFromVariable == PropertyName) {
+ status.SetError(cmStrCat(
+ "Variable name must have a non-empty prefix before property name \"",
+ PropertyName, "\""));
+ 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;
+ }
+
// Actually define the property.
status.GetMakefile().GetState()->DefineProperty(
PropertyName, scope, cmJoin(BriefDocs, ""), cmJoin(FullDocs, ""),
- inherited);
+ inherited, initializeFromVariable);
return true;
}