CMake: Add support for -binary option for generated resources

Add handling of -binary flag in qt6_add_resources for generated
resource file variant.

If -binary argument is provided in OPTIONS section of
qt6_add_resources function, it will be passed to rcc as argument.
File path to output .rcc could be additionally specified by
DESTINATION argument. Extra target generated_<resource_filename>
will be added to project's 'all' set.

Implement tests for new functionality.

Fixes: QTBUG-87644
Change-Id: Id1313da499d86f82859d1757c3cfae2d84e894d4
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Alexey Edelev 2020-11-10 15:18:49 +01:00
parent 29b17fa335
commit 558f5a2d7b
6 changed files with 126 additions and 20 deletions

View File

@ -1108,7 +1108,11 @@ endfunction()
#
function(_qt_internal_process_resource target resourceName)
cmake_parse_arguments(rcc "" "PREFIX;LANG;BASE;OUTPUT_TARGETS" "FILES;OPTIONS" ${ARGN})
cmake_parse_arguments(rcc "" "PREFIX;LANG;BASE;OUTPUT_TARGETS;DESTINATION" "FILES;OPTIONS" ${ARGN})
if("${rcc_OPTIONS}" MATCHES "-binary")
set(isBinary TRUE)
endif()
string(REPLACE "/" "_" resourceName ${resourceName})
string(REPLACE "." "_" resourceName ${resourceName})
@ -1162,8 +1166,8 @@ function(_qt_internal_process_resource target resourceName)
return()
endif()
list(APPEND output_targets ${output_target_quick})
set(generatedResourceFile "${CMAKE_CURRENT_BINARY_DIR}/.rcc/generated_${newResourceName}.qrc")
set(generatedSourceCode "${CMAKE_CURRENT_BINARY_DIR}/.rcc/qrc_${newResourceName}.cpp")
set(generatedBaseName "generated_${newResourceName}")
set(generatedResourceFile "${CMAKE_CURRENT_BINARY_DIR}/.rcc/${generatedBaseName}.qrc")
# Generate .qrc file:
@ -1210,10 +1214,8 @@ function(_qt_internal_process_resource target resourceName)
set(qt_core_configure_file_contents "${qrcContents}")
configure_file("${template_file}" "${generatedResourceFile}")
set_property(TARGET ${target} APPEND PROPERTY _qt_generated_qrc_files "${generatedResourceFile}")
set(rccArgs --name "${newResourceName}" "${generatedResourceFile}")
set(rccArgs --name "${newResourceName}"
--output "${generatedSourceCode}" "${generatedResourceFile}")
if(rcc_OPTIONS)
list(APPEND rccArgs ${rcc_OPTIONS})
endif()
@ -1228,10 +1230,29 @@ function(_qt_internal_process_resource target resourceName)
list(APPEND rccArgs "--no-zstd")
endif()
set_property(SOURCE "${generatedResourceFile}" PROPERTY SKIP_AUTOGEN ON)
# Set output file name for rcc command
if(isBinary)
set(generatedOutfile "${CMAKE_CURRENT_BINARY_DIR}/${generatedBaseName}.rcc")
if(rcc_DESTINATION)
# Add .rcc suffix if it's not specified by user
get_filename_component(destinationRccExt "${rcc_DESTINATION}" LAST_EXT)
if("${destinationRccExt}" STREQUAL ".rcc")
set(generatedOutfile "${rcc_DESTINATION}")
else()
set(generatedOutfile "${rcc_DESTINATION}.rcc")
endif()
endif()
else()
set(generatedOutfile "${CMAKE_CURRENT_BINARY_DIR}/.rcc/qrc_${newResourceName}.cpp")
endif()
list(PREPEND rccArgs --output "${generatedOutfile}")
# Process .qrc file:
add_custom_command(OUTPUT "${generatedSourceCode}"
COMMAND "${QT_CMAKE_EXPORT_NAMESPACE}::rcc"
ARGS ${rccArgs}
add_custom_command(OUTPUT "${generatedOutfile}"
COMMAND "${QT_CMAKE_EXPORT_NAMESPACE}::rcc" ${rccArgs}
DEPENDS
${resource_dependencies}
${generatedResourceFile}
@ -1239,18 +1260,25 @@ function(_qt_internal_process_resource target resourceName)
COMMENT "RCC ${newResourceName}"
VERBATIM)
get_target_property(type "${target}" TYPE)
# Only do this if newResourceName is the same as resourceName, since
# the resource will be chainloaded by the qt quickcompiler
# qml cache loader
if(newResourceName STREQUAL resourceName)
__qt_propagate_generated_resource(${target} ${resourceName} "${generatedSourceCode}" output_target)
list(APPEND output_targets ${output_target})
if(isBinary)
# Add generated .rcc target to 'all' set
add_custom_target(binary_resource_${generatedBaseName} ALL DEPENDS "${generatedOutfile}")
else()
target_sources(${target} PRIVATE "${generatedSourceCode}")
endif()
if (rcc_OUTPUT_TARGETS)
set(${rcc_OUTPUT_TARGETS} "${output_targets}" PARENT_SCOPE)
set_property(SOURCE "${generatedOutfile}" PROPERTY SKIP_AUTOGEN ON)
set_property(TARGET ${target} APPEND PROPERTY _qt_generated_qrc_files "${generatedResourceFile}")
# Only do this if newResourceName is the same as resourceName, since
# the resource will be chainloaded by the qt quickcompiler
# qml cache loader
if(newResourceName STREQUAL resourceName)
__qt_propagate_generated_resource(${target} ${resourceName} "${generatedOutfile}" output_target)
list(APPEND output_targets ${output_target})
else()
target_sources(${target} PRIVATE "${generatedOutfile}")
endif()
if (rcc_OUTPUT_TARGETS)
set(${rcc_OUTPUT_TARGETS} "${output_targets}" PARENT_SCOPE)
endif()
endif()
endfunction()

View File

@ -221,3 +221,6 @@ endif()
# FIXME: Needs porting of the qmake .pro files to create the modules and plugins in Qt6 CMake land.
# _qt_internal_test_expect_pass(test_import_plugins BINARY ${CMAKE_CTEST_COMMAND})
_qt_internal_test_expect_pass(test_versionless_targets)
_qt_internal_test_expect_pass(test_add_resources_binary_generated
BINARY test_add_resources_binary_generated)

View File

@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.14)
project(test_add_resources_binary_generated)
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/FindPackageHints.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/FindPackageHints.cmake")
endif()
find_package(Qt6Core REQUIRED HINTS ${Qt6Tests_PREFIX_PATH})
qt6_add_executable(test_add_resources_binary_generated main.cpp)
qt6_add_resources(test_add_resources_binary_generated resources1 FILES resource1.txt PREFIX "/"
OPTIONS "--binary")
qt6_add_resources(test_add_resources_binary_generated resources2 FILES resource2.txt PREFIX "/"
OPTIONS "--binary"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/resources2_custom.rcc")
target_compile_definitions(test_add_resources_binary_generated
PRIVATE
RESOURCE1_FULL_PATH="${CMAKE_CURRENT_BINARY_DIR}/generated_resources1.rcc")
target_compile_definitions(test_add_resources_binary_generated
PRIVATE
RESOURCE2_FULL_PATH="${CMAKE_CURRENT_BINARY_DIR}/resources2_custom.rcc")
if(NOT TARGET binary_resource_generated_resources1)
message(FATAL_ERROR "Generated resources binary target was not created")
endif()
if(NOT TARGET binary_resource_generated_resources2)
message(FATAL_ERROR "Generated resources binary target was not created")
endif()

View File

@ -0,0 +1,41 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QFile>
#include <QResource>
int main(int, char **)
{
if (!QResource::registerResource(RESOURCE1_FULL_PATH)
|| !QFile::exists(":/resource1.txt")
|| !QResource::registerResource(RESOURCE2_FULL_PATH)
|| !QFile::exists(":/resource2.txt")) {
return -1;
}
return 0;
}

View File

@ -0,0 +1 @@
Test resource1

View File

@ -0,0 +1 @@
Test resource2