Skip to content

Commit

Permalink
CMake: Allow usage of QtStandaloneTestTemplateProject as package comp…
Browse files Browse the repository at this point in the history
…onent

Currently in order to compile a Qt6 test standalone one needs to use
the qt-cmake-standalone-test script which will load the
Qt6BuildInternals/QtStandaloneTestTemplateProject/CMakeLists.txt project
with the current test source directory to create a complete CMake project.

This commit will allow a test to have these lines at top:

cmake_minimum_required(VERSION 3.16)

if (NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
    project(a_qt_test LANGUAGES C CXX ASM)
    find_package(Qt6BuildInternals COMPONENTS STANDALONE_TEST)
endif()

and be standalone and work with any IDE capable of loading CMake projects.

Fixes: QTCREATORBUG-25389
Pick-to: 6.1
Change-Id: If3f878b7e560a8bfbb604a8f1aa30b7962742e66
Reviewed-by: Alexandru Croitor <[email protected]>
  • Loading branch information
cristianadam committed Apr 23, 2021
1 parent 66acee6 commit 0a1256a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
2 changes: 1 addition & 1 deletion bin/qt-cmake-standalone-test.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@__qt_cmake_standalone_test_os_prelude@

@__qt_cmake_standalone_test_script_relpath@
@__qt_cmake_private_relpath@ @__qt_cmake_standalone_test_relpath@ -DQT_STANDALONE_TEST_PATH=@__qt_cmake_standalone_passed_args@
@__qt_cmake_private_relpath@ @__qt_cmake_standalone_test_relpath@ -DQT_BUILD_STANDALONE_TESTS=ON -DQT_STANDALONE_TEST_PATH=@__qt_cmake_standalone_passed_args@
13 changes: 13 additions & 0 deletions cmake/QtBuildInternals/QtBuildInternalsConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,16 @@ macro(qt_examples_build_end)

set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ${BACKUP_CMAKE_FIND_ROOT_PATH_MODE_PACKAGE})
endmacro()

if ("STANDALONE_TEST" IN_LIST Qt6BuildInternals_FIND_COMPONENTS)
include(${CMAKE_CURRENT_LIST_DIR}/QtStandaloneTestTemplateProject/Main.cmake)
if (NOT PROJECT_VERSION_MAJOR)
get_property(_qt_major_version TARGET ${QT_CMAKE_EXPORT_NAMESPACE}::Core PROPERTY INTERFACE_QT_MAJOR_VERSION)
set(PROJECT_VERSION ${Qt${_qt_major_version}Core_VERSION})

string(REPLACE "." ";" _qt_core_version_list ${PROJECT_VERSION})
list(GET _qt_core_version_list 0 PROJECT_VERSION_MAJOR)
list(GET _qt_core_version_list 1 PROJECT_VERSION_MINOR)
list(GET _qt_core_version_list 2 PROJECT_VERSION_PATCH)
endif()
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,7 @@ project(qt_single_test VERSION 6.0.0 LANGUAGES C CXX ASM)

find_package(Qt6 REQUIRED COMPONENTS BuildInternals)

# Includes QtSetup and friends for private CMake API.
qt_build_internals_set_up_private_api()

# Find all StandaloneTestsConfig.cmake files, and include them
# This will find all Qt packages that are required for standalone tests.
# It will find more packages that needed for a certain test, but will ensure any test can
# be built.
qt_get_standalone_tests_confg_files_path(standalone_tests_config_path)

file(GLOB config_files "${standalone_tests_config_path}/*")
foreach(file ${config_files})
include("${file}")
endforeach()
include(${CMAKE_CURRENT_LIST_DIR}/Main.cmake)

# Get the absolute path of the passed-in project dir, relative to the current working directory
# of the calling script, rather than relative to this source directory.
Expand All @@ -27,10 +15,5 @@ else()
set(absolute_project_path "${QT_STANDALONE_TEST_PATH}")
endif()

# Just before adding the test, change the local (non-cache) install prefix to something other than
# the Qt install prefix, so that tests don't try to install and pollute the Qt install prefix.
# Needs to be called after qt_get_standalone_tests_confg_files_path().
qt_set_up_fake_standalone_tests_install_prefix()

# Add the test project path as a subdirectory project.
add_subdirectory("${absolute_project_path}" "build_dir")
18 changes: 18 additions & 0 deletions cmake/QtBuildInternals/QtStandaloneTestTemplateProject/Main.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Includes QtSetup and friends for private CMake API.
qt_build_internals_set_up_private_api()

# Find all StandaloneTestsConfig.cmake files, and include them
# This will find all Qt packages that are required for standalone tests.
# It will find more packages that needed for a certain test, but will ensure any test can
# be built.
qt_get_standalone_tests_confg_files_path(standalone_tests_config_path)

file(GLOB config_files "${standalone_tests_config_path}/*")
foreach(file ${config_files})
include("${file}")
endforeach()

# Just before adding the test, change the local (non-cache) install prefix to something other than
# the Qt install prefix, so that tests don't try to install and pollute the Qt install prefix.
# Needs to be called after qt_get_standalone_tests_confg_files_path().
qt_set_up_fake_standalone_tests_install_prefix()
26 changes: 14 additions & 12 deletions cmake/QtTestHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,20 @@ function(qt_internal_add_test name)
"${CMAKE_CURRENT_SOURCE_DIR}")
qt_path_join(testdata_install_dir ${QT_INSTALL_DIR}
"${relative_path_to_test_project}")
foreach(testdata IN LISTS arg_TESTDATA)
set(testdata "${CMAKE_CURRENT_SOURCE_DIR}/${testdata}")
if (IS_DIRECTORY "${testdata}")
qt_install(
DIRECTORY "${testdata}"
DESTINATION "${testdata_install_dir}")
else()
qt_install(
FILES "${testdata}"
DESTINATION "${testdata_install_dir}")
endif()
endforeach()
if (testdata_install_dir)
foreach(testdata IN LISTS arg_TESTDATA)
set(testdata "${CMAKE_CURRENT_SOURCE_DIR}/${testdata}")
if (IS_DIRECTORY "${testdata}")
qt_install(
DIRECTORY "${testdata}"
DESTINATION "${testdata_install_dir}")
else()
qt_install(
FILES "${testdata}"
DESTINATION "${testdata_install_dir}")
endif()
endforeach()
endif()
endif()

endfunction()
Expand Down

0 comments on commit 0a1256a

Please sign in to comment.