Procházet zdrojové kódy

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
Kyle Edwards před 3 roky
rodič
revize
26caa97057

+ 4 - 2
Help/command/define_property.rst

@@ -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.

+ 4 - 4
Source/cmDefinePropertyCommand.cxx

@@ -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;
     }
 

+ 1 - 1
Tests/RunCMake/define_property/RunCMakeTest.cmake

@@ -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)

+ 1 - 1
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-invalid_1-stderr.txt

@@ -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\)$

+ 2 - 2
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-invalid_1.cmake

@@ -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
   )

+ 1 - 1
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-invalid_2-stderr.txt

@@ -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\)$

+ 2 - 2
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-invalid_2.cmake

@@ -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
   )

+ 0 - 5
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_prefix-stderr.txt

@@ -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\)$

+ 0 - 0
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_prefix-result.txt → Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_underscore-result.txt


+ 5 - 0
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_underscore-stderr.txt

@@ -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\)$

+ 0 - 0
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_prefix.cmake → Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-no_underscore.cmake


+ 6 - 6
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE-subdirectory/CMakeLists.txt

@@ -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 "!")

+ 5 - 5
Tests/RunCMake/define_property/define_property-INITIALIZE_FROM_VARIABLE.cmake

@@ -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 "!")