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

Fixes: #24186
This commit is contained in:
Marc Chevrier 2022-12-08 14:52:56 +01:00
parent ff875ed859
commit 7480fa0a5f
18 changed files with 114 additions and 2 deletions

View File

@ -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
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``
.. include:: GENEX_NOTE.txt

View File

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

View File

@ -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
definitions on source files.
.. versionadded:: 3.26
Any leading ``-D`` on an item will be removed.
.. include:: /include/COMPILE_DEFINITIONS_DISCLAIMER.txt
Contents of ``COMPILE_DEFINITIONS`` may use :manual:`cmake-generator-expressions(7)`

View File

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

View File

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

View File

@ -4995,7 +4995,13 @@ void cmGlobalXCodeGenerator::AppendDefines(
std::string def;
for (auto const& define : defines) {
// 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.
std::string tmp;

View File

@ -3377,7 +3377,12 @@ void cmLocalGenerator::AppendDefines(
if (!this->CheckDefinition(d.Value)) {
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);
}
}
}

View File

@ -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
PROPERTY LABELS "CUDA")
add_RunCMake_test(add_compile_definitions)
add_RunCMake_test(target_compile_definitions)
add_RunCMake_test(target_compile_features)
add_RunCMake_test(target_compile_options

View File

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

View File

@ -1,3 +1,16 @@
include(RunCMake)
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)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,3 +2,16 @@ include(RunCMake)
run_cmake(empty_keyword_args)
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)

View File

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

View File

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