Skip to content

Commit

Permalink
Merge #101: cmake: Build test_bitcoin-qt executable
Browse files Browse the repository at this point in the history
e4b4c98 fixup! cmake: Migrate Guix build scripts to CMake (Hennadii Stepanov)
0be2225 cmake: Build `test_bitcoin-qt` executable (Hennadii Stepanov)
4da7634 fixup! cmake: Build `bitcoin-qt` executable (Hennadii Stepanov)

Pull request description:

  A new configuration option is `BUILD_GUI_TESTS`.

  ---

  **Native Windows / MSVC Notes:**
  1. vcpkg configures Qt with `-opengl dynamic`, which makes the "minimal" platform plugin unusable due to internal Qt bugs.
  2. `ctest` workarounds this issue by setting the environment variable `QT_QPA_PLATFORM=windows`.
  3. This approach works nice in the CI for both `x64-windows` and `x64-windows-static` triplets. Please note that `test_bitcoin-qt.exe` fails to run in the CI environment on the master branch.
  4. One who is willing to debug Qt with the "minimal" platform plugin could run the `test_bitcoin-qt.exe` directly (not driven by `ctest`).

ACKs for top commit:
  pablomartin4btc:
    re tACK e4b4c98

Tree-SHA512: 638de710aa722a168b9944089062b2da43f9f14cb0684acb8fc5922907e96b1e4d8c04c852826275d5cbbbd824a1f2905d550bdf5355cef9f22738414e7fbd6e
  • Loading branch information
hebasto committed Mar 13, 2024
2 parents 5e58e05 + e4b4c98 commit 83374fd
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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("")
Expand Down
1 change: 1 addition & 0 deletions cmake/optional_qt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
13 changes: 13 additions & 0 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 1 addition & 2 deletions contrib/guix/libexec/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 24 additions & 2 deletions src/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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 <QtGlobal>
#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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -192,3 +210,7 @@ install(TARGETS bitcoin-qt
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT GUI
)

if(BUILD_GUI_TESTS)
add_subdirectory(test)
endif()
46 changes: 46 additions & 0 deletions src/qt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_PROPERTY:Qt5::QMinimalIntegrationPlugin,LOCATION_$<UPPER_CASE:$<CONFIG>>> $<TARGET_FILE_DIR:test_bitcoin-qt>/plugins/platforms
VERBATIM
)
endif()

install(TARGETS test_bitcoin-qt
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT GUI
)

0 comments on commit 83374fd

Please sign in to comment.