From a85adbfe0f131f86a8173758c6193b37b4cc8c99 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 18 Feb 2018 23:01:02 +0100 Subject: [PATCH] Compile sdrbase resources as a binary resource file loaded dynamically --- CMakeLists.txt | 8 +++ cmake/include/Qt5ExternalResources.cmake | 80 ++++++++++++++++++++++++ sdrbase/CMakeLists.txt | 7 --- sdrgui/mainwindow.cpp | 9 +++ sdrsrv/maincore.cpp | 9 +++ 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 cmake/include/Qt5ExternalResources.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8392d93f6..f814dfa39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,6 +223,13 @@ include_directories( ${OPENGL_INCLUDE_DIR} ) +############################################################################## +# External binary resources +include(${CMAKE_SOURCE_DIR}/cmake/include/Qt5ExternalResources.cmake) + +qt5_add_external_resources(sdrbase.rcc sdrbase/resources/res.qrc) + + ############################################################################## # main GUI application @@ -318,6 +325,7 @@ install(TARGETS sdrangelsrv DESTINATION bin) #install files and directories install(DIRECTORY udev-rules DESTINATION share/sdrangel) install(FILES udev-rules/install.sh DESTINATION share/sdrangel/udev-rules PERMISSIONS WORLD_EXECUTE) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/sdrbase.rcc DESTINATION bin) ############################################################################## diff --git a/cmake/include/Qt5ExternalResources.cmake b/cmake/include/Qt5ExternalResources.cmake new file mode 100644 index 000000000..b11cc7c0e --- /dev/null +++ b/cmake/include/Qt5ExternalResources.cmake @@ -0,0 +1,80 @@ +# +# External resource support for CMake's Qt5 support, version 1.0 +# written by Grzegorz Antoniak +# http://github.com/antekone/qt5-external-resources-cmake +# +# Tested on CMake 3.2.2 + +# qt5_parse_rcc_depends function +# +# Based on original Qt5CoreMacros.cmake/qt5_add_resources function. + +function(QT5_PARSE_RCC_DEPENDS infile) + 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) + + set(out_depends_return ${out_depends} PARENT_SCOPE) + set(_RC_DEPENDS_RETURN ${_RC_DEPENDS} PARENT_SCOPE) + 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() +endfunction() + +# qt5_add_external_resources function +# +# Usage: +# +# qt5_add_external_resources(outfile res/infile.qrc) +# +# This should generate ${CMAKE_BINARY_DIR}/outfile.rcc, ready to be used inside +# the application. You can also use it like this: +# +# qt5_add_external_resources(outfile res/infile.qrc OPTIONS -someoption) +# +# if you would like to add some option to the RCC's command line. + +function(QT5_ADD_EXTERNAL_RESOURCES rccfilename qrcfilename) + set(options) + set(oneValueArgs) + set(multiValueArgs OPTIONS) + + cmake_parse_arguments(_RCC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + set(rcc_files ${_RCC_UNPARSED_ARGUMENTS}) + set(rcc_options ${_RCC_OPTIONS}) + + get_filename_component(outfilename ${rccfilename} NAME_WE) + get_filename_component(infile ${qrcfilename} ABSOLUTE) + get_filename_component(rc_path ${infile} PATH) + set(outfile ${CMAKE_CURRENT_BINARY_DIR}/${outfilename}.rcc) + + qt5_parse_rcc_depends(${infile}) + + add_custom_command(OUTPUT ${outfile} + COMMAND ${Qt5Core_RCC_EXECUTABLE} + ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile} -binary + MAIN_DEPENDENCY ${infile} + DEPENDS ${_RC_DEPENDS_RETURN} "${out_depends_return}" VERBATIM) + set(_RC_TARGETNAME "qrc_external_${outfilename}") + add_custom_target(${_RC_TARGETNAME} ALL DEPENDS ${outfile}) +endfunction() + +# vim:set et: diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index cdf237282..185fc1f0e 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -258,19 +258,12 @@ if (BUILD_DEBIAN) include_directories(${LIBSERIALDVSRC}) endif (BUILD_DEBIAN) -set(sdrbase_RESOURCES - resources/res.qrc -) - add_definitions(${QT_DEFINITIONS}) add_definitions(-DQT_SHARED) -qt5_add_resources(sdrbase_RESOURCES_RCC ${sdrbase_RESOURCES}) - add_library(sdrbase SHARED ${sdrbase_SOURCES} ${sdrbase_HEADERS_MOC} - ${sdrbase_RESOURCES_RCC} ) include_directories( diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index 56e12d8c8..073e11b16 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -180,6 +181,14 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parse connect(ui->tabInputsView, SIGNAL(currentChanged(int)), this, SLOT(tabInputViewIndexChanged())); + QString applicationDirPath = qApp->applicationDirPath(); + + if (QResource::registerResource(applicationDirPath + "/sdrbase.rcc")) { + qDebug("MainWindow::MainWindow: registered resource file %s/%s", qPrintable(applicationDirPath), "sdrbase.rcc"); + } else { + qWarning("MainWindow::MainWindow: could not register resource file %s/%s", qPrintable(applicationDirPath), "sdrbase.rcc"); + } + m_apiAdapter = new WebAPIAdapterGUI(*this); m_requestMapper = new WebAPIRequestMapper(this); m_requestMapper->setAdapter(m_apiAdapter); diff --git a/sdrsrv/maincore.cpp b/sdrsrv/maincore.cpp index bd9232e4a..22412f6bb 100644 --- a/sdrsrv/maincore.cpp +++ b/sdrsrv/maincore.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include "dsp/dspengine.h" @@ -68,6 +69,14 @@ MainCore::MainCore(qtwebapp::LoggerWithFile *logger, const MainParser& parser, Q loadSettings(); + QString applicationDirPath = QCoreApplication::instance()->applicationDirPath(); + + if (QResource::registerResource(applicationDirPath + "/sdrbase.rcc")) { + qDebug("MainCore::MainCore: registered resource file %s/%s", qPrintable(applicationDirPath), "sdrbase.rcc"); + } else { + qWarning("MainCore::MainCore: could not register resource file %s/%s", qPrintable(applicationDirPath), "sdrbase.rcc"); + } + m_apiAdapter = new WebAPIAdapterSrv(*this); m_requestMapper = new WebAPIRequestMapper(this); m_requestMapper->setAdapter(m_apiAdapter);