ソースを参照

COMPILE_DEFINITIONS property: ensure leading -D is removed in all cases

Fixes: #24186
Marc Chevrier 2 年 前
コミット
7480fa0a5f

+ 3 - 0
Help/command/add_compile_definitions.rst

@@ -21,6 +21,9 @@ Function-style definitions are not supported. CMake will automatically
 escape the value correctly for the native build system (note that CMake
 escape the value correctly for the native build system (note that CMake
 language syntax may require escapes to specify some values).
 language syntax may require escapes to specify some values).
 
 
+.. versionadded:: 3.26
+  Any leading ``-D`` on an item will be removed.
+
 .. |command_name| replace:: ``add_compile_definitions``
 .. |command_name| replace:: ``add_compile_definitions``
 .. include:: GENEX_NOTE.txt
 .. include:: GENEX_NOTE.txt
 
 

+ 3 - 0
Help/prop_dir/COMPILE_DEFINITIONS.rst

@@ -19,6 +19,9 @@ directory's parent.
 CMake will automatically drop some definitions that are not supported
 CMake will automatically drop some definitions that are not supported
 by the native build tool.
 by the native build tool.
 
 
+.. versionadded:: 3.26
+  Any leading ``-D`` on an item will be removed.
+
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 
 
 Contents of ``COMPILE_DEFINITIONS`` may use "generator expressions" with
 Contents of ``COMPILE_DEFINITIONS`` may use "generator expressions" with

+ 3 - 0
Help/prop_sf/COMPILE_DEFINITIONS.rst

@@ -16,6 +16,9 @@ CMake will automatically drop some definitions that are not supported
 by the native build tool.  Xcode does not support per-configuration
 by the native build tool.  Xcode does not support per-configuration
 definitions on source files.
 definitions on source files.
 
 
+.. versionadded:: 3.26
+  Any leading ``-D`` on an item will be removed.
+
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 
 
 Contents of ``COMPILE_DEFINITIONS`` may use :manual:`cmake-generator-expressions(7)`
 Contents of ``COMPILE_DEFINITIONS`` may use :manual:`cmake-generator-expressions(7)`

+ 3 - 0
Help/prop_tgt/COMPILE_DEFINITIONS.rst

@@ -13,6 +13,9 @@ values).
 CMake will automatically drop some definitions that are not supported
 CMake will automatically drop some definitions that are not supported
 by the native build tool.
 by the native build tool.
 
 
+.. versionadded:: 3.26
+  Any leading ``-D`` on an item will be removed.
+
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 .. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
 
 
 Contents of ``COMPILE_DEFINITIONS`` may use "generator expressions" with the
 Contents of ``COMPILE_DEFINITIONS`` may use "generator expressions" with the

+ 6 - 0
Help/release/dev/COMPILE_DEFINITIONS-property-cleanup.rst

@@ -0,0 +1,6 @@
+COMPILE_DEFINITIONS-property-cleanup
+------------------------------------
+
+* For all ``COMPILE_DEFINITIONS`` properties, any leading ``-D`` on an item
+  will be removed regardless how to was defined: as is or inside a generator
+  expression.

+ 7 - 1
Source/cmGlobalXCodeGenerator.cxx

@@ -4995,7 +4995,13 @@ void cmGlobalXCodeGenerator::AppendDefines(
   std::string def;
   std::string def;
   for (auto const& define : defines) {
   for (auto const& define : defines) {
     // Start with -D if requested.
     // Start with -D if requested.
-    def = cmStrCat(dflag ? "-D" : "", define);
+    if (dflag && !cmHasLiteralPrefix(define, "-D")) {
+      def = cmStrCat("-D", define);
+    } else if (!dflag && cmHasLiteralPrefix(define, "-D")) {
+      def = define.substr(2);
+    } else {
+      def = define;
+    }
 
 
     // Append the flag with needed escapes.
     // Append the flag with needed escapes.
     std::string tmp;
     std::string tmp;

+ 6 - 1
Source/cmLocalGenerator.cxx

@@ -3377,7 +3377,12 @@ void cmLocalGenerator::AppendDefines(
     if (!this->CheckDefinition(d.Value)) {
     if (!this->CheckDefinition(d.Value)) {
       continue;
       continue;
     }
     }
-    defines.insert(d);
+    // remove any leading -D
+    if (cmHasLiteralPrefix(d.Value, "-D")) {
+      defines.emplace(d.Value.substr(2), d.Backtrace);
+    } else {
+      defines.insert(d);
+    }
   }
   }
 }
 }
 
 

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -693,6 +693,7 @@ add_RunCMake_test(target_link_options -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_I
 set_property(TEST RunCMake.target_link_options APPEND
 set_property(TEST RunCMake.target_link_options APPEND
   PROPERTY LABELS "CUDA")
   PROPERTY LABELS "CUDA")
 
 
+add_RunCMake_test(add_compile_definitions)
 add_RunCMake_test(target_compile_definitions)
 add_RunCMake_test(target_compile_definitions)
 add_RunCMake_test(target_compile_features)
 add_RunCMake_test(target_compile_features)
 add_RunCMake_test(target_compile_options
 add_RunCMake_test(target_compile_options

+ 11 - 0
Tests/RunCMake/CompileDefinitions/RemoveLeadingMinusD.cmake

@@ -0,0 +1,11 @@
+
+enable_language(C)
+
+set_property(SOURCE foo.c PROPERTY COMPILE_DEFINITIONS -DDEF0 "$<1:-DDEF1>")
+
+add_library(lib1 foo.c)
+set_property(TARGET lib1 PROPERTY COMPILE_DEFINITIONS -DDEF2 "$<1:-DDEF3>")
+set_property(TARGET lib1 PROPERTY INTERFACE_COMPILE_DEFINITIONS -DDEF4 "$<1:-DDEF5>")
+
+add_library(lib2 foo.c)
+target_link_libraries(lib2 PRIVATE lib1)

+ 13 - 0
Tests/RunCMake/CompileDefinitions/RunCMakeTest.cmake

@@ -1,3 +1,16 @@
 include(RunCMake)
 include(RunCMake)
 
 
 run_cmake(SetEmpty)
 run_cmake(SetEmpty)
+
+
+macro(run_cmake_build test)
+  set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build)
+  set(RunCMake_TEST_NO_CLEAN 1)
+  run_cmake_command(${test} ${CMAKE_COMMAND} --build . --config Release)
+
+  unset(RunCMake_TEST_BINARY_DIR)
+  unset(RunCMake_TEST_NO_CLEAN)
+endmacro()
+
+run_cmake(RemoveLeadingMinusD)
+run_cmake_build(RemoveLeadingMinusD)

+ 4 - 0
Tests/RunCMake/CompileDefinitions/foo.c

@@ -0,0 +1,4 @@
+
+void foo()
+{
+}

+ 5 - 0
Tests/RunCMake/add_compile_definitions/CMakeLists.txt

@@ -0,0 +1,5 @@
+cmake_minimum_required(VERSION 3.11)
+
+project(${RunCMake_TEST} LANGUAGES NONE)
+
+include(${RunCMake_TEST}.cmake)

+ 13 - 0
Tests/RunCMake/add_compile_definitions/RunCMakeTest.cmake

@@ -0,0 +1,13 @@
+include(RunCMake)
+
+macro(run_cmake_build test)
+  set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build)
+  set(RunCMake_TEST_NO_CLEAN 1)
+  run_cmake_command(${test} ${CMAKE_COMMAND} --build . --config Release)
+
+  unset(RunCMake_TEST_BINARY_DIR)
+  unset(RunCMake_TEST_NO_CLEAN)
+endmacro()
+
+run_cmake(remove_leading_minusD)
+run_cmake_build(remove_leading_minusD)

+ 4 - 0
Tests/RunCMake/add_compile_definitions/foo.c

@@ -0,0 +1,4 @@
+
+void foo()
+{
+}

+ 6 - 0
Tests/RunCMake/add_compile_definitions/remove_leading_minusD.cmake

@@ -0,0 +1,6 @@
+
+enable_language(C)
+
+add_compile_definitions(-DDEF0 "$<1:-DDEF1>")
+
+add_library(lib1 foo.c)

+ 13 - 0
Tests/RunCMake/target_compile_definitions/RunCMakeTest.cmake

@@ -2,3 +2,16 @@ include(RunCMake)
 
 
 run_cmake(empty_keyword_args)
 run_cmake(empty_keyword_args)
 run_cmake(unknown_imported_target)
 run_cmake(unknown_imported_target)
+
+
+macro(run_cmake_build test)
+  set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build)
+  set(RunCMake_TEST_NO_CLEAN 1)
+  run_cmake_command(${test} ${CMAKE_COMMAND} --build . --config Release)
+
+  unset(RunCMake_TEST_BINARY_DIR)
+  unset(RunCMake_TEST_NO_CLEAN)
+endmacro()
+
+run_cmake(remove_leading_minusD)
+run_cmake_build(remove_leading_minusD)

+ 4 - 0
Tests/RunCMake/target_compile_definitions/foo.c

@@ -0,0 +1,4 @@
+
+void foo()
+{
+}

+ 9 - 0
Tests/RunCMake/target_compile_definitions/remove_leading_minusD.cmake

@@ -0,0 +1,9 @@
+
+enable_language(C)
+
+add_library(lib1 foo.c)
+target_compile_definitions(lib1 PRIVATE -DDEF0 "$<1:-DDEF1>")
+target_compile_definitions(lib1 PUBLIC -DDEF2 "$<1:-DDEF3>")
+
+add_library(lib2 foo.c)
+target_link_libraries(lib2 PRIVATE lib1)