Skip to content

Commit

Permalink
tests cmake reuse PCH
Browse files Browse the repository at this point in the history
The databasemanager test case that will provide PCH for all other test
cases, what means all other test cases will use this PCH without
compilation.

 - added a new PROVIDES_PCH for tiny_configure_test()
 - this new implementation will be enabled from Qt v6.8.0 because of
   https://bugreports.qt.io/projects/QTBUG/issues/QTBUG-126729
  • Loading branch information
silverqx committed Jul 22, 2024
1 parent 39cafde commit ade98d5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmake/CommonModules/TinyToolchainRequirement.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ in ${CMAKE_CURRENT_FUNCTION}().")

set(TINY_QT_VERSION "${CMAKE_MATCH_0}" CACHE INTERNAL
"Qt version used to determine whether a minimum required Qt version was \
satisfied.")
satisfied (also used by tiny_configure_test_pch()).")

if(TINY_QT_VERSION VERSION_GREATER_EQUAL minReqQtVersion)
set(${out_variable} TRUE PARENT_SCOPE)
Expand Down
84 changes: 76 additions & 8 deletions cmake/Modules/TinyTestCommon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function(tiny_configure_test name)

set(options
DEPENDS_ON_UNITTESTS INCLUDE_MIGRATIONS INCLUDE_MODELS INCLUDE_PROJECT_SOURCE_DIR
RUN_SERIAL
PROVIDES_PCH RUN_SERIAL
)
cmake_parse_arguments(PARSE_ARGV 1 TINY "${options}" "" "")

Expand All @@ -15,13 +15,8 @@ function(tiny_configure_test name)
${TINY_UNPARSED_ARGUMENTS}")
endif()

target_precompile_headers(${name} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:"${${TinyOrm_ns}_SOURCE_DIR}/include/pch.h">
)

if(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_compile_definitions(${name} PRIVATE TINYORM_USING_PCH)
endif()
# Configure PCH for the given test case
tiny_configure_test_pch(${name} ${TINY_PROVIDES_PCH})

set_target_properties(${name}
PROPERTIES
Expand Down Expand Up @@ -132,3 +127,76 @@ sqlite_schemabuilder;mysql_tinybuilder"
)

endfunction()

# Configure PCH for the given test case
# The PROVIDES_PCH parameter from the tiny_configure_test() function tags a test case that
# will provide PCH for all other test cases, only one test case can be tagged with it and
# all other test cases will use this PCH without compilation.
function(tiny_configure_test_pch name provides_pch)

# Qt <v6.8.0 breaks REUSE_FROM
# See: https://bugreports.qt.io/projects/QTBUG/issues/QTBUG-126729
if(NOT TINY_QT_VERSION VERSION_GREATER_EQUAL "6.8.0")
target_precompile_headers(${name} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:"${${TinyOrm_ns}_SOURCE_DIR}/include/pch.h">
)
return()
endif()

if(provides_pch)
# Throw an exception if CACHE{TINY_TESTS_PCH_REUSE_FROM} isn't equal to the name
tiny_throw_if_wrong_reuse_from(${name})

target_precompile_headers(${name} PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:"${${TinyOrm_ns}_SOURCE_DIR}/include/pch.h">
)

set(TINY_TESTS_PCH_REUSE_FROM ${name} CACHE INTERNAL
"The name of the first test case that provides PCH for all other test cases \
(used by REUSE_FROM).")

else()
# This should never happen :/
# Throw an exception if the CACHE{TINY_TESTS_PCH_REUSE_FROM} isn't DEFINED
tiny_throw_if_no_cache_reuse_from()

# TODO REUSE_FROM will fail if NOT $<COMPILE_LANGUAGE:CXX> silverqx
target_precompile_headers(${name} REUSE_FROM $CACHE{TINY_TESTS_PCH_REUSE_FROM})

return()
endif()

if(NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
target_compile_definitions(${name} PRIVATE TINYORM_USING_PCH)
endif()

endfunction()

# Throw an exception if the CACHE{TINY_TESTS_PCH_REUSE_FROM} is not equal to the name
function(tiny_throw_if_wrong_reuse_from name)

# Nothing to do
if(NOT DEFINED CACHE{TINY_TESTS_PCH_REUSE_FROM} OR
$CACHE{TINY_TESTS_PCH_REUSE_FROM} STREQUAL name
)
return()
endif()

message(FATAL_ERROR "The '${name}' test case can't set the PROVIDES_PCH because \
the \$CACHE{TINY_TESTS_PCH_REUSE_FROM} is already set \
for the '$CACHE{TINY_TESTS_PCH_REUSE_FROM}' test case, in ${CMAKE_CURRENT_FUNCTION}().")

endfunction()

# Throw an exception if the CACHE{TINY_TESTS_PCH_REUSE_FROM} isn't DEFINED
function(tiny_throw_if_no_cache_reuse_from)

# Nothing to do
if(DEFINED CACHE{TINY_TESTS_PCH_REUSE_FROM})
return()
endif()

message(FATAL_ERROR "The \$CACHE{TINY_TESTS_PCH_REUSE_FROM} is NOT DEFINED, \
the first compiled test case must be tagged with the PROVIDES_PCH argument.")

endfunction()
2 changes: 1 addition & 1 deletion tests/auto/functional/orm/databasemanager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ add_executable(databasemanager
add_test(NAME databasemanager COMMAND databasemanager)

include(TinyTestCommon)
tiny_configure_test(databasemanager DEPENDS_ON_UNITTESTS)
tiny_configure_test(databasemanager DEPENDS_ON_UNITTESTS PROVIDES_PCH)

0 comments on commit ade98d5

Please sign in to comment.