From 2af127763194c13c3f7ccce507c94eb2de6dbefe Mon Sep 17 00:00:00 2001 From: "A. Klitzing" Date: Thu, 13 Nov 2014 11:01:31 +0100 Subject: CMake: Introduce qt5_add_binary_resources Optional parameter DESTINATION to set target rcc file Example: qt5_add_binary_resources(GenerateFixture "fixture.qrc") Task-number: QTBUG-41728 Change-Id: I9dc2fe8e7d5e9ad3873b89f75ab84a2a1b9d1d29 Reviewed-by: Stephen Kelly --- src/corelib/Qt5CTestMacros.cmake | 2 + src/corelib/Qt5CoreMacros.cmake | 104 +++++++++++++++------ tests/auto/cmake/CMakeLists.txt | 1 + .../CMakeLists.txt | 24 +++++ .../existing.qrc | 6 ++ .../main.cpp | 46 +++++++++ .../resource_file.txt | 1 + .../resource_file_two.txt | 1 + 8 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 tests/auto/cmake/test_add_binary_resources_delayed_file/CMakeLists.txt create mode 100644 tests/auto/cmake/test_add_binary_resources_delayed_file/existing.qrc create mode 100644 tests/auto/cmake/test_add_binary_resources_delayed_file/main.cpp create mode 100644 tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file.txt create mode 100644 tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file_two.txt diff --git a/src/corelib/Qt5CTestMacros.cmake b/src/corelib/Qt5CTestMacros.cmake index cea514ff52..6451f65b55 100644 --- a/src/corelib/Qt5CTestMacros.cmake +++ b/src/corelib/Qt5CTestMacros.cmake @@ -55,6 +55,7 @@ foreach(module ${CMAKE_MODULES_UNDER_TEST}) endforeach() macro(expect_pass _dir) + cmake_parse_arguments(_ARGS "" "BINARY" "" ${ARGN}) string(REPLACE "(" "_" testname "${_dir}") string(REPLACE ")" "_" testname "${testname}") add_test(${testname} ${CMAKE_CTEST_COMMAND} @@ -66,6 +67,7 @@ macro(expect_pass _dir) --build-makeprogram ${CMAKE_MAKE_PROGRAM} --build-project ${_dir} --build-options "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" ${BUILD_OPTIONS_LIST} + --test-command ${_ARGS_BINARY} ) endmacro() diff --git a/src/corelib/Qt5CoreMacros.cmake b/src/corelib/Qt5CoreMacros.cmake index 02f1d313d7..249138e073 100644 --- a/src/corelib/Qt5CoreMacros.cmake +++ b/src/corelib/Qt5CoreMacros.cmake @@ -185,6 +185,77 @@ function(QT5_WRAP_CPP outfiles ) endfunction() + +# _qt5_parse_qrc_file(infile _out_depends _rc_depends) +# internal + +function(_QT5_PARSE_QRC_FILE infile _out_depends _rc_depends) + get_filename_component(rc_path ${infile} PATH) + + if(EXISTS "${infile}") + # parse file for dependencies + # all files are absolute paths or relative to the location of the qrc file + file(READ "${infile}" RC_FILE_CONTENTS) + string(REGEX MATCHALL "]*>" "" RC_FILE "${RC_FILE}") + if(NOT IS_ABSOLUTE "${RC_FILE}") + set(RC_FILE "${rc_path}/${RC_FILE}") + endif() + set(RC_DEPENDS ${RC_DEPENDS} "${RC_FILE}") + endforeach() + # Since this cmake macro is doing the dependency scanning for these files, + # let's make a configured file and add it as a dependency so cmake is run + # again when dependencies need to be recomputed. + qt5_make_output_file("${infile}" "" "qrc.depends" out_depends) + configure_file("${infile}" "${out_depends}" COPYONLY) + else() + # The .qrc file does not exist (yet). Let's add a dependency and hope + # that it will be generated later + set(out_depends) + endif() + + set(${_out_depends} ${out_depends} PARENT_SCOPE) + set(${_rc_depends} ${RC_DEPENDS} PARENT_SCOPE) +endfunction() + + +# qt5_add_binary_resources(target inputfiles ... ) + +function(QT5_ADD_BINARY_RESOURCES target ) + + set(options) + set(oneValueArgs DESTINATION) + set(multiValueArgs OPTIONS) + + cmake_parse_arguments(_RCC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + set(rcc_files ${_RCC_UNPARSED_ARGUMENTS}) + set(rcc_options ${_RCC_OPTIONS}) + set(rcc_destination ${_RCC_DESTINATION}) + + if(NOT rcc_destination) + set(rcc_destination ${CMAKE_CURRENT_BINARY_DIR}/${target}.rcc) + endif() + + foreach(it ${rcc_files}) + get_filename_component(infile ${it} ABSOLUTE) + + _QT5_PARSE_QRC_FILE(${infile} _out_depends _rc_depends) + set(infiles ${infiles} ${infile}) + set(out_depends ${out_depends} ${_out_depends}) + set(rc_depends ${rc_depends} ${_rc_depends}) + endforeach() + + add_custom_command(OUTPUT ${rcc_destination} + COMMAND ${Qt5Core_RCC_EXECUTABLE} + ARGS ${rcc_options} --binary --name ${target} --output ${rcc_destination} ${infiles} + DEPENDS ${rc_depends} ${out_depends} VERBATIM) + + add_custom_target(${target} ALL DEPENDS ${rcc_destination}) +endfunction() + + # qt5_add_resources(outfiles inputfile ... ) function(QT5_ADD_RESOURCES outfiles ) @@ -198,41 +269,22 @@ function(QT5_ADD_RESOURCES outfiles ) set(rcc_files ${_RCC_UNPARSED_ARGUMENTS}) set(rcc_options ${_RCC_OPTIONS}) + if(${rcc_options} MATCHES "-binary") + message(WARNING "Use qt5_add_binary_resources for binary option") + endif() + foreach(it ${rcc_files}) get_filename_component(outfilename ${it} NAME_WE) get_filename_component(infile ${it} ABSOLUTE) - get_filename_component(rc_path ${infile} PATH) set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cpp) - set(_RC_DEPENDS) - if(EXISTS "${infile}") - # parse file for dependencies - # all files are absolute paths or relative to the location of the qrc file - file(READ "${infile}" _RC_FILE_CONTENTS) - string(REGEX MATCHALL "]*>" "" _RC_FILE "${_RC_FILE}") - if(NOT IS_ABSOLUTE "${_RC_FILE}") - set(_RC_FILE "${rc_path}/${_RC_FILE}") - endif() - set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}") - endforeach() - # Since this cmake macro is doing the dependency scanning for these files, - # let's make a configured file and add it as a dependency so cmake is run - # again when dependencies need to be recomputed. - qt5_make_output_file("${infile}" "" "qrc.depends" out_depends) - configure_file("${infile}" "${out_depends}" COPYONLY) - else() - # The .qrc file does not exist (yet). Let's add a dependency and hope - # that it will be generated later - set(out_depends) - endif() + _QT5_PARSE_QRC_FILE(${infile} _out_depends _rc_depends) add_custom_command(OUTPUT ${outfile} COMMAND ${Qt5Core_RCC_EXECUTABLE} - ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile} + ARGS ${rcc_options} --name ${outfilename} --output ${outfile} ${infile} MAIN_DEPENDENCY ${infile} - DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM) + DEPENDS ${_rc_depends} "${out_depends}" VERBATIM) list(APPEND ${outfiles} ${outfile}) endforeach() set(${outfiles} ${${outfiles}} PARENT_SCOPE) diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt index 87d8a802f3..25bc8a5e45 100644 --- a/tests/auto/cmake/CMakeLists.txt +++ b/tests/auto/cmake/CMakeLists.txt @@ -74,6 +74,7 @@ if (NOT WIN32 OR (WIN32 AND NOT CMAKE_VERSION VERSION_LESS 2.8.11)) # Broken on windows on earlier CMake versions. # http://public.kitware.com/Bug/view.php?id=13392 expect_pass(test_add_resources_delayed_file) + expect_pass(test_add_binary_resources_delayed_file BINARY test_add_binary_resources_delayed_file) endif() expect_pass(test_private_includes) expect_pass(test_testlib_definitions) diff --git a/tests/auto/cmake/test_add_binary_resources_delayed_file/CMakeLists.txt b/tests/auto/cmake/test_add_binary_resources_delayed_file/CMakeLists.txt new file mode 100644 index 0000000000..e2478330ea --- /dev/null +++ b/tests/auto/cmake/test_add_binary_resources_delayed_file/CMakeLists.txt @@ -0,0 +1,24 @@ + +cmake_minimum_required(VERSION 2.8) + +project(test_add_binary_resources_delayed_file) + +find_package(Qt5Core REQUIRED) + +include_directories(${Qt5Core_INCLUDE_DIRS}) + +add_definitions(${Qt5Core_DEFINITIONS}) + +qt5_add_binary_resources(rcc_file "${CMAKE_CURRENT_BINARY_DIR}/test_add_binary_resources_delayed_file.qrc" "${CMAKE_CURRENT_SOURCE_DIR}/existing.qrc") + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test_add_binary_resources_delayed_file.qrc" " + + ${CMAKE_CURRENT_SOURCE_DIR}/resource_file.txt + + +") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}") + +add_executable(test_add_binary_resources_delayed_file main.cpp) +target_link_libraries(test_add_binary_resources_delayed_file ${Qt5Core_LIBRARIES}) diff --git a/tests/auto/cmake/test_add_binary_resources_delayed_file/existing.qrc b/tests/auto/cmake/test_add_binary_resources_delayed_file/existing.qrc new file mode 100644 index 0000000000..a4f448a0e2 --- /dev/null +++ b/tests/auto/cmake/test_add_binary_resources_delayed_file/existing.qrc @@ -0,0 +1,6 @@ + + + resource_file_two.txt + + + diff --git a/tests/auto/cmake/test_add_binary_resources_delayed_file/main.cpp b/tests/auto/cmake/test_add_binary_resources_delayed_file/main.cpp new file mode 100644 index 0000000000..86a3ecdcd6 --- /dev/null +++ b/tests/auto/cmake/test_add_binary_resources_delayed_file/main.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2015 André Klitzing +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL21$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +int main(int argc, char **argv) +{ + if (QResource::registerResource("rcc_file.rcc") && + QFile::exists("://resource_file.txt") && QFile::exists("://resource_file_two.txt")) + { + return 0; + } + + return -1; +} diff --git a/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file.txt b/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file.txt new file mode 100644 index 0000000000..2c604a4f18 --- /dev/null +++ b/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file.txt @@ -0,0 +1 @@ +Ken sent me. diff --git a/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file_two.txt b/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file_two.txt new file mode 100644 index 0000000000..980a0d5f19 --- /dev/null +++ b/tests/auto/cmake/test_add_binary_resources_delayed_file/resource_file_two.txt @@ -0,0 +1 @@ +Hello World! -- cgit v1.2.3