CMake: Add option to link with Qt

The named option is BUILD_LINK_WITH_QT, which when set to ON will
generate the QtCreator.ini file needed for Qt Creator to find the
installed Qt.

Change-Id: If3a47a463510fbfc90a314829378c6c4e505d2f7
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Cristian Adam
2021-09-15 15:34:01 +02:00
parent 844cfe70b7
commit 3f0a54d41c
2 changed files with 21 additions and 0 deletions

View File

@@ -24,6 +24,9 @@ set(CMAKE_CXX_EXTENSIONS OFF)
qtc_handle_sccache_support()
option(BUILD_LINK_WITH_QT "Link with Qt from the parent Qt Creator" OFF)
qtc_link_with_qt()
option(WITH_TESTS "Build Tests" OFF)
add_feature_info("Build tests" ${WITH_TESTS} "")
option(WITH_DEBUG_CMAKE "Enabled CMake project debugging functionality (e.g. source file disk checking)" OFF)

View File

@@ -125,6 +125,24 @@ function(qtc_handle_sccache_support)
endif()
endfunction()
function(qtc_link_with_qt)
# When building with Qt Creator 4.15+ do the "Link with Qt..." automatically
if (BUILD_LINK_WITH_QT AND DEFINED CMAKE_PROJECT_INCLUDE_BEFORE)
get_filename_component(auto_setup_dir "${CMAKE_PROJECT_INCLUDE_BEFORE}" DIRECTORY)
set(qt_creator_ini "${auto_setup_dir}/../QtProject/QtCreator.ini")
if (EXISTS "${qt_creator_ini}")
file(STRINGS "${qt_creator_ini}" install_settings REGEX "^InstallSettings=.*$")
if (install_settings)
string(REPLACE "InstallSettings=" "" install_settings "${install_settings}")
else()
file(TO_CMAKE_PATH "${auto_setup_dir}/.." install_settings)
endif()
file(WRITE ${CMAKE_BINARY_DIR}/${_IDE_DATA_PATH}/QtProject/QtCreator.ini
"[Settings]\nInstallSettings=${install_settings}")
endif()
endif()
endfunction()
function(qtc_enable_release_for_debug_configuration)
if (MSVC)
string(REPLACE "/Od" "/O2" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")