CMake: Use cmake_language(DEFER) when available for scope finalizers

If CMake version is 3.19 or greater, use cmake_language(DEFER CALL)
for Qt internal scope finalizers, instead of the homegrown
implementation.

Apart from not depending on the hacky homegrown solution, it
significantly improves the readability of --trace-redirect logs.

Pick-to: 6.0
Task-number: QTBUG-77377
Change-Id: I5ce374bb313865662c536826e86052bc762438b9
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2020-09-24 18:28:01 +02:00
parent 89b49e5231
commit 69d9b2b6fd
2 changed files with 31 additions and 14 deletions

View File

@ -494,5 +494,8 @@ if(ANDROID)
include(QtAndroidHelpers)
endif()
# This sets up the scope finalizer mechanism.
variable_watch(CMAKE_CURRENT_LIST_DIR qt_watch_current_list_dir)
# This sets up the poor man's scope finalizer mechanism.
# For newer CMake versions, we use cmake_language(DEFER CALL) instead.
if(CMAKE_VERSION VERSION_LESS "3.19.0")
variable_watch(CMAKE_CURRENT_LIST_DIR qt_watch_current_list_dir)
endif()

View File

@ -1,24 +1,38 @@
# Add a finalizer function for the current CMake list file.
# It will be processed just before leaving the current source directory scope.
#
# When using CMake 3.18 or lower:
# You may add up to nine arguments that are passed to the finalizer.
# A finalizer that is registered with qt_add_list_file_finalizer(foo bar baz)
# will be called with nine arguments: foo(bar baz IGNORE IGNORE IGNORE...),
# because CMake's handling of empty list elements is a cruel joke.
#
# For CMake < 3.18 the function qt_watch_current_list_dir must know about the finalizer.
#
# When using CMake 3.19 or higher, no more INGORE parameters are passed. Instead we
# use cmake_language(DEFER CALL) and pass arguments as usual.
# qt_watch_current_list_dir also doesn't need to know about the finalizer
function(qt_add_list_file_finalizer func)
set_property(GLOBAL APPEND
PROPERTY QT_LIST_FILE_FINALIZER_FILES "${CMAKE_CURRENT_LIST_FILE}")
set_property(GLOBAL APPEND
PROPERTY QT_LIST_FILE_FINALIZER_FUNCS ${func})
foreach(i RANGE 1 9)
set(arg "${ARGV${i}}")
if(i GREATER_EQUAL ARGC OR "${arg}" STREQUAL "")
set(arg "IGNORE")
endif()
set(use_cmake_defer_call TRUE)
if(CMAKE_VERSION VERSION_LESS "3.19.0")
set(use_cmake_defer_call FALSE)
endif()
if(use_cmake_defer_call)
cmake_language(EVAL CODE "cmake_language(DEFER CALL \"${func}\" ${ARGN}) ")
else()
set_property(GLOBAL APPEND
PROPERTY QT_LIST_FILE_FINALIZER_ARGV${i} "${arg}")
endforeach()
PROPERTY QT_LIST_FILE_FINALIZER_FILES "${CMAKE_CURRENT_LIST_FILE}")
set_property(GLOBAL APPEND
PROPERTY QT_LIST_FILE_FINALIZER_FUNCS ${func})
foreach(i RANGE 1 9)
set(arg "${ARGV${i}}")
if(i GREATER_EQUAL ARGC OR "${arg}" STREQUAL "")
set(arg "IGNORE")
endif()
set_property(GLOBAL APPEND
PROPERTY QT_LIST_FILE_FINALIZER_ARGV${i} "${arg}")
endforeach()
endif()
endfunction()
# Watcher function for the variable CMAKE_CURRENT_LIST_DIR.