diff --git a/CMakeLists.txt b/CMakeLists.txt index 1436db836ff32..f69113e8b0d45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,7 @@ tristate_option(WITH_EXTERNAL_SIGNER ) option(BUILD_TESTS "Build test_bitcoin executable." ON) +cmake_dependent_option(BUILD_GUI_TESTS "Build test_bitcoin-qt executable." ON "BUILD_TESTS" OFF) option(BUILD_BENCH "Build bench_bitcoin executable." ON) cmake_dependent_option(BUILD_FUZZ_BINARY "Build fuzz binary." ON "NOT MSVC" OFF) cmake_dependent_option(FUZZ "Build for fuzzing. Enabling this will disable all other targets and override BUILD_FUZZ_BINARY." OFF "NOT MSVC" OFF) @@ -128,6 +129,7 @@ if(FUZZ) set(WITH_MINIUPNPC OFF) set(WITH_ZMQ OFF) set(BUILD_TESTS OFF) + set(BUILD_GUI_TESTS OFF) set(BUILD_BENCH OFF) set(BUILD_FUZZ_BINARY ON) @@ -484,6 +486,7 @@ message(" ZeroMQ .............................. ${WITH_ZMQ}") message(" USDT tracing ........................ ${WITH_USDT}") message("Tests:") message(" test_bitcoin ........................ ${BUILD_TESTS}") +message(" test_bitcoin-qt ..................... ${BUILD_GUI_TESTS}") message(" bench_bitcoin ....................... ${BUILD_BENCH}") message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}") message("") diff --git a/cmake/optional_qt.cmake b/cmake/optional_qt.cmake index bf46f8413b13b..cdb80d9766f5f 100644 --- a/cmake/optional_qt.cmake +++ b/cmake/optional_qt.cmake @@ -41,6 +41,7 @@ if(WITH_GUI) message(WARNING "Qt not found, disabling.\n" "To skip this warning check, use \"-DWITH_GUI=OFF\".\n") set(WITH_GUI OFF) + set(BUILD_GUI_TESTS OFF) endif() endif() endif() diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 0215633e77808..a29b23eec7a4f 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -67,3 +67,16 @@ if(TARGET object) COMMAND object ) endif() + +if(TARGET test_bitcoin-qt) + add_test(NAME test_bitcoin-qt + COMMAND test_bitcoin-qt + ) + if(WIN32 AND VCPKG_TARGET_TRIPLET) + # On Windows, vcpkg configures Qt with `-opengl dynamic`, which makes + # the "minimal" platform plugin unusable due to internal Qt bugs. + set_tests_properties(test_bitcoin-qt PROPERTIES + ENVIRONMENT "QT_QPA_PLATFORM=windows" + ) + endif() +endif() diff --git a/contrib/guix/libexec/build.sh b/contrib/guix/libexec/build.sh index 0db04c1aa5edf..ad189d591fc5c 100755 --- a/contrib/guix/libexec/build.sh +++ b/contrib/guix/libexec/build.sh @@ -215,8 +215,7 @@ mkdir -p "$OUTDIR" ########################### # CONFIGFLAGS -# TODO: Re-add CMake's analogue of --disable-gui-tests. -CONFIGFLAGS="-DREDUCE_EXPORTS=ON -DBUILD_BENCH=OFF -DBUILD_FUZZ_BINARY=OFF" +CONFIGFLAGS="-DREDUCE_EXPORTS=ON -DBUILD_BENCH=OFF -DBUILD_GUI_TESTS=OFF -DBUILD_FUZZ_BINARY=OFF" # CFLAGS HOST_CFLAGS="-O2 -g" diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index 553919e68d439..d008bbb5165fd 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -3,7 +3,7 @@ # file COPYING or https://opensource.org/license/mit/. function(import_plugins target) - if(CMAKE_CROSSCOMPILING OR VCPKG_TARGET_TRIPLET MATCHES "-static") + if(QT_IS_STATIC) set(plugins Qt5::QMinimalIntegrationPlugin) if(CMAKE_SYSTEM_NAME STREQUAL "Linux") list(APPEND plugins Qt5::QXcbIntegrationPlugin) @@ -31,6 +31,9 @@ set(CMAKE_AUTOUIC_SEARCH_PATHS forms) set(qt_minimum_required_version 5.11.3) set(qt_components Core Gui Widgets Network LinguistTools) +if(BUILD_GUI_TESTS) + list(APPEND qt_components Test) +endif() if(CMAKE_CROSSCOMPILING) # The find_package(Qt ...) function internally uses find_library() @@ -51,6 +54,21 @@ unset(qt_components) message(STATUS "Found Qt: ${Qt5_DIR} (found suitable version \"${Qt5_VERSION}\", minimum required is \"${qt_minimum_required_version}\")") unset(qt_minimum_required_version) +include(CMakePushCheckState) +cmake_push_check_state(RESET) +set(CMAKE_REQUIRED_LIBRARIES Qt5::Core) +include(CheckCXXSourceCompiles) +check_cxx_source_compiles(" + #include + + #if !defined(QT_STATIC) + #error Qt is not static + #endif + int main() {} + " QT_IS_STATIC +) +cmake_pop_check_state() + # TODO: The file(GLOB ...) command should be replaced with an explicit # file list. Such a change must be synced with the corresponding change # to https://github.com/bitcoin-core/bitcoin-maintainer-tools/blob/main/update-translations.py @@ -161,7 +179,7 @@ if(ENABLE_WALLET) ) endif() -if(CMAKE_CROSSCOMPILING OR VCPKG_TARGET_TRIPLET MATCHES "-static") +if(QT_IS_STATIC) # We want to define static plugins to link ourselves, thus preventing # automatic linking against a "sane" set of default static plugins. qt5_import_plugins(bitcoinqt @@ -192,3 +210,7 @@ install(TARGETS bitcoin-qt RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT GUI ) + +if(BUILD_GUI_TESTS) + add_subdirectory(test) +endif() diff --git a/src/qt/test/CMakeLists.txt b/src/qt/test/CMakeLists.txt new file mode 100644 index 0000000000000..8c316108f9085 --- /dev/null +++ b/src/qt/test/CMakeLists.txt @@ -0,0 +1,46 @@ +# Copyright (c) 2024-present The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or https://opensource.org/license/mit/. + +add_executable(test_bitcoin-qt + apptests.cpp + optiontests.cpp + rpcnestedtests.cpp + test_main.cpp + uritests.cpp + util.cpp + ../../init/bitcoin-qt.cpp +) + +target_link_libraries(test_bitcoin-qt + core_interface + bitcoinqt + test_util + bitcoin_node + Boost::headers + Qt5::Test +) + +import_plugins(test_bitcoin-qt) + +if(ENABLE_WALLET) + target_sources(test_bitcoin-qt + PRIVATE + addressbooktests.cpp + wallettests.cpp + ../../wallet/test/wallet_test_fixture.cpp + ) +endif() + +if(NOT QT_IS_STATIC) + add_custom_command( + TARGET test_bitcoin-qt POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $>> $/plugins/platforms + VERBATIM + ) +endif() + +install(TARGETS test_bitcoin-qt + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + COMPONENT GUI +)