From 05bb85523cc743c92de2e146dbbc7ed3406e20ec Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Fri, 10 Dec 2021 15:06:30 +0100 Subject: [PATCH] cmake: improve the check of optimization level against CMAKE_BUILD_TYPE Fixes: #39626 The CMAKE_C_FLAGS_ is a string with arguments separated by spaces and not a list, for example "-O3 -DNDEBUG". Therefore update the `if()` check to do a regex match to determine if the optimization level specified through Kconfig matches the optimization level that would be defined by the CMAKE_BUILD_TYPE setting. Signed-off-by: Torsten Rasmussen --- CMakeLists.txt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b27826afce0..b34bdf021a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1715,8 +1715,11 @@ set(build_types None Debug Release RelWithDebInfo MinSizeRel) if((CMAKE_BUILD_TYPE IN_LIST build_types) AND (NOT NO_BUILD_TYPE_WARNING)) string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_uppercase) - - if(NOT (${OPTIMIZATION_FLAG} IN_LIST CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase})) + # The CMAKE_C_FLAGS_ is a string, so we do a regex to see if the + # optimization flag is present in that string. + # To avoid false-positive matches, the flag must either be matched first + # or last in string, or come after / followed by minimum a space. + if(NOT (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_uppercase} MATCHES "(^| )${OPTIMIZATION_FLAG}($| )")) message(WARNING " The CMake build type was set to '${CMAKE_BUILD_TYPE}', but the optimization flag was set to '${OPTIMIZATION_FLAG}'. This may be intentional and the warning can be turned off by setting the CMake variable 'NO_BUILD_TYPE_WARNING'"