define_property(): Change constraints of INITIALIZE_FROM_VARIABLE

Remove the requirement that the variable name have a prefix while
keeping the suffix requirement. Require that the property name
contains an underscore. Update docs and tests accordingly.

Fixes: #23340
This commit is contained in:
Kyle Edwards
2022-03-21 16:34:15 -04:00
committed by Craig Scott
parent 2aad4cef97
commit 26caa97057
13 changed files with 31 additions and 29 deletions

View File

@@ -69,5 +69,7 @@ project via corresponding options to the :command:`get_property` command.
The ``INITIALIZE_FROM_VARIABLE`` option specifies a variable from which the
property should be initialized. It can only be used with target properties.
The ``<variable>`` name must end with the property name, must have a prefix
before the property name, and must not begin with ``CMAKE_`` or ``_CMAKE_``.
The ``<variable>`` name must end with the property name and must not begin
with ``CMAKE_`` or ``_CMAKE_``. The property name must contain at least one
underscore. It is recommended that the property name have a prefix specific
to the project.

View File

@@ -91,10 +91,10 @@ bool cmDefinePropertyCommand(std::vector<std::string> const& args,
PropertyName, "\""));
return false;
}
if (initializeFromVariable == PropertyName) {
status.SetError(cmStrCat(
"Variable name must have a non-empty prefix before property name \"",
PropertyName, "\""));
if (PropertyName.find('_') == std::string::npos) {
status.SetError(cmStrCat("Property name \"", PropertyName,
"\" defined with INITIALIZE_FROM_VARIABLE does "
"not contain underscore"));
return false;
}

View File

@@ -6,4 +6,4 @@ run_cmake(define_property-INITIALIZE_FROM_VARIABLE-invalid_1)
run_cmake(define_property-INITIALIZE_FROM_VARIABLE-invalid_2)
run_cmake(define_property-INITIALIZE_FROM_VARIABLE-non_target)
run_cmake(define_property-INITIALIZE_FROM_VARIABLE-wrong_suffix)
run_cmake(define_property-INITIALIZE_FROM_VARIABLE-no_prefix)
run_cmake(define_property-INITIALIZE_FROM_VARIABLE-no_underscore)

View File

@@ -1,4 +1,4 @@
^CMake Error at define_property-INITIALIZE_FROM_VARIABLE-invalid_1\.cmake:[0-9]+ \(define_property\):
define_property variable name "CMAKE_PROP1" is reserved
define_property variable name "CMAKE_Test_PROP1" is reserved
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -1,3 +1,3 @@
define_property(TARGET PROPERTY PROP1
INITIALIZE_FROM_VARIABLE CMAKE_PROP1
define_property(TARGET PROPERTY Test_PROP1
INITIALIZE_FROM_VARIABLE CMAKE_Test_PROP1
)

View File

@@ -1,4 +1,4 @@
^CMake Error at define_property-INITIALIZE_FROM_VARIABLE-invalid_2\.cmake:[0-9]+ \(define_property\):
define_property variable name "_CMAKE_PROP1" is reserved
define_property variable name "_CMAKE_Test_PROP1" is reserved
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -1,3 +1,3 @@
define_property(TARGET PROPERTY PROP1
INITIALIZE_FROM_VARIABLE _CMAKE_PROP1
define_property(TARGET PROPERTY Test_PROP1
INITIALIZE_FROM_VARIABLE _CMAKE_Test_PROP1
)

View File

@@ -1,5 +0,0 @@
^CMake Error at define_property-INITIALIZE_FROM_VARIABLE-no_prefix\.cmake:[0-9]+ \(define_property\):
define_property Variable name must have a non-empty prefix before property
name "PROP1"
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -0,0 +1,5 @@
^CMake Error at define_property-INITIALIZE_FROM_VARIABLE-no_underscore\.cmake:[0-9]+ \(define_property\):
define_property Property name "PROP1" defined with INITIALIZE_FROM_VARIABLE
does not contain underscore
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@@ -1,11 +1,11 @@
define_property(TARGET PROPERTY PROP2
define_property(TARGET PROPERTY Test_PROP2
INITIALIZE_FROM_VARIABLE Test_PROP2
)
define_property(TARGET PROPERTY PROP3
INITIALIZE_FROM_VARIABLE Test_PROP3
define_property(TARGET PROPERTY Test_PROP3
INITIALIZE_FROM_VARIABLE MyTest_PROP3
)
add_executable(sub_exe ../main.c)
assert_prop_eq(sub_exe PROP1 "Hello")
assert_prop_eq(sub_exe PROP2 "world")
assert_prop_eq(sub_exe PROP3 "!")
assert_prop_eq(sub_exe Test_PROP1 "Hello")
assert_prop_eq(sub_exe Test_PROP2 "world")
assert_prop_eq(sub_exe Test_PROP3 "!")

View File

@@ -16,14 +16,14 @@ endfunction()
set(Test_PROP1 "Hello")
set(Test_PROP2 "world")
set(Test_PROP3 "!")
define_property(TARGET PROPERTY PROP1
set(MyTest_PROP3 "!")
define_property(TARGET PROPERTY Test_PROP1
INITIALIZE_FROM_VARIABLE Test_PROP1
)
add_subdirectory(define_property-INITIALIZE_FROM_VARIABLE-subdirectory)
add_executable(top_exe main.c)
assert_prop_eq(top_exe PROP1 "Hello")
assert_prop_eq(top_exe PROP2 "world")
assert_prop_eq(top_exe PROP3 "!")
assert_prop_eq(top_exe Test_PROP1 "Hello")
assert_prop_eq(top_exe Test_PROP2 "world")
assert_prop_eq(top_exe Test_PROP3 "!")