Generate the gresource xml file based on a list of resources

This inverts the logic from
- having an xml file and extracting dependencies
  from it to
- having a list of dependencies and generating
  an xml file from it

In the original configuration adding or removing a
resource to/from the gresources.xml file would not
be detected by cmake as a change in dependencies.
The user would have to remember to rerun cmake manually.
By explicitly listing the dependencies, cmake will
properly recongifure and regenerate if that list is
updated. The remainder of the dependency configuration
also ensures proper rebuilds of gnucash, libaqbanking
and libofx if any of the resource files change, a new
one is added or an existing one is removed.

For reusability the code to generate the gresource related
files as been extracted into a separate function.
This commit is contained in:
Geert Janssens
2022-12-31 16:31:05 +01:00
parent bdde17115d
commit a543143689
10 changed files with 127 additions and 109 deletions

View File

@@ -1,9 +1,16 @@
set(cmake_FILES
GncAddGSchemaTargets.cmake GncAddSchemeTargets.cmake
GncAddSwigCommand.cmake GncAddTest.cmake GncFindLibm.cmake
MacroAddSourceFileCompileFlags.cmake MacroAppendForeach.cmake
MakeDist.cmake MakeDistFiles.cmake MakeDistCheck.cmake
)
GncAddGSchemaTargets.cmake
GncAddSchemeTargets.cmake
GncAddSwigCommand.cmake
GncAddTest.cmake
GncFindLibm.cmake
GncGenerateGResources.cmake
MacroAddSourceFileCompileFlags.cmake
MacroAppendForeach.cmake
MakeDist.cmake
MakeDistFiles.cmake
MakeDistCheck.cmake
)
set_dist_list(cmake_modules_DIST CMakeLists.txt COPYING-CMAKE-SCRIPTS.txt ${cmake_FILES})

View File

@@ -0,0 +1,62 @@
# gnc_generate_gresources (BASE filename
# RESOURCE_FILES resource1 resource2 ...)
#
# Function to generate two files in CMAKE_CURRENT_BINARY_DIR:
# - a gresource xml file that serves as an input to the glib_resource_compiler
# - a c source file to be compiled and linked with a library or executable
# to include the resources listed in RESOURCE_FILES in that library or
# executable
#
# To link the resources, add
#
# ${CMAKE_CURRENT_BINARY_DIR}/<filename>.c
#
# as additional source to the relevant "add_library" or "add_executable" call.
#
#
# BASE filename
# the base filename without extension for all output files to generate
#
# RESOURCE_FILES
# a list of files that you want compiled into a gresource
#
# The XML file will be generated in the current cmake binary directory
function(gnc_generate_gresources)
set(singleValues BASE)
set(multiValues RESOURCE_FILES)
cmake_parse_arguments(GR "" "${singleValues}" "${multiValues}" ${ARGN})
string(STRIP GR_BASE "${GR_BASE}")
set(XML_FILE "${GR_BASE}.xml")
set(TMP_FILE "${CMAKE_CURRENT_BINARY_DIR}/${XML_FILE}-tmp")
set(XML_PATH "${CMAKE_CURRENT_BINARY_DIR}/${XML_FILE}")
file(WRITE ${TMP_FILE} "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
file(APPEND ${TMP_FILE} "<gresources>\n")
file(APPEND ${TMP_FILE} " <gresource prefix=\"/org/gnucash\">\n")
foreach(res_file ${GR_RESOURCE_FILES})
file(APPEND ${TMP_FILE} " <file>${res_file}</file>\n")
endforeach()
file(APPEND ${TMP_FILE} " </gresource>\n")
file(APPEND ${TMP_FILE} "</gresources>\n")
# Regenerate target file only if something changed
configure_file(${TMP_FILE} ${XML_PATH} COPYONLY)
file(REMOVE ${TMP_FILE})
set(C_FILE "${GR_BASE}.c")
add_custom_command(
OUTPUT ${C_FILE}
COMMAND
"${GLIB_COMPILE_RESOURCES_EXECUTABLE}"
--target=${C_FILE}
--sourcedir=${CMAKE_CURRENT_SOURCE_DIR}
--generate-source
${XML_PATH}
DEPENDS
${XML_PATH} ${GR_RESOURCE_FILES}
)
endfunction()