Hello there,

I was trying to develop a CMake module to add GResource (a GLib
component) support for CMake.

For people that doesn't know about GResource, it's a component that
helps to add files bundled to the binary file.

The component provides an utility called "glib-compile-resources" that
generates an .h and .c file with the bundle code, these files are
generated from an XML file that contains the list of files to be
bundled.

This utility also can be used to get the plain list of files that are
going to be bundled, separated by lines.

So, the module needs to works as the following:

 - It has function called gresource_add_file (I'm planning to add
support to add multiple files at once, but for now is fine), this
functions accepts the GResource XML file and returns a variable with
the name of generated .h and .c files to be used to compile an
executable.
 - When the XML file changes or any of the specified files in the XML
file changes, the .h and .c files needs to be regenerated on make.

I attach my current work, it partially works, my problem is that I
generate the files-to-be-bundled list at configure time (the
glib-compile-resources --generate-dependencies call) to get .h and .c
files dependencies, so, for example, let say I do the following:

1) Create my XML with file-a.png and file-b.png specified to be
bundled, add it to my CMakeLists.txt through gresource_add_file
function.
2) If I modify my XML, file-a.png or file-b.png, the GResource .h and
.c are regenerated on make as expected.
3) Let say I want to add file-c.png to my XML, I run make again, the
.h and .c files are regenerated since I modified the XML file.
4) Now I modify only file-c.png, I run make again, but the .h and .c
files are NOT regenerated.

If you see my code, the problem is obvious, as I mentioned, I am
generating the files-to-be-bundled list at configure time... so, when
I add file-c.png, this list is not refreshed since didn't run cmake
again.

Any ideas on how to do it right? It's quite tricky, I can't figure out
the working solution, can I generate dependencies for
add_custom_command dynamically with a shell call that is executed as
is by target buildsystem?

Thanks in advance!
Damián.
# Fetch executable path for glib-compile-resources
execute_process(
    COMMAND
        ${PKG_CONFIG_EXECUTABLE} gio-2.0 --variable glib_compile_resources
    OUTPUT_VARIABLE
        GLIB_COMPILE_RESOURCES_EXECUTABLE
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

function (gresource_add_file OUTPUT FILE)
    string(REPLACE ".gresource.xml" "" C_NAME ${FILE})
    string(MAKE_C_IDENTIFIER ${C_NAME} C_NAME)
    set(C_FILE "${CMAKE_CURRENT_BINARY_DIR}/${C_NAME}.c")
    set(H_FILE "${CMAKE_CURRENT_BINARY_DIR}/${C_NAME}.h")
    set(DEPS_FILE "${CMAKE_CURRENT_BINARY_DIR}/${C_NAME}.deps")
    set(${OUTPUT} "${C_FILE};${H_FILE}" PARENT_SCOPE)

    execute_process(
        COMMAND
            ${GLIB_COMPILE_RESOURCES_EXECUTABLE}
            ${FILE}
            --generate-dependencies
            --sourcedir=${CMAKE_CURRENT_SOURCE_DIR}
            --sourcedir=${CMAKE_CURRENT_BINARY_DIR}
        WORKING_DIRECTORY
            ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT_VARIABLE
            FILE_DEPS
    )

    string(REGEX REPLACE "(\r?\n)" ";" FILE_DEPS "${FILE_DEPS}")

    add_custom_command(
        OUTPUT
            ${C_FILE}
            ${H_FILE}
        COMMAND
            ${GLIB_COMPILE_RESOURCES_EXECUTABLE}
            ${FILE}
            --sourcedir=${CMAKE_CURRENT_SOURCE_DIR}
            --sourcedir=${CMAKE_CURRENT_BINARY_DIR}
            --generate-source
            --target=${C_FILE} --c-name=${C_NAME}
        COMMAND
            ${GLIB_COMPILE_RESOURCES_EXECUTABLE}
            ${FILE}
            --sourcedir=${CMAKE_CURRENT_SOURCE_DIR}
            --sourcedir=${CMAKE_CURRENT_BINARY_DIR}
            --generate-header
            --target=${H_FILE} --c-name=${C_NAME}
        DEPENDS
            ${FILE};${FILE_DEPS}
        COMMENT
            "Generating header and source files for ${FILE} GResource"
        WORKING_DIRECTORY
            ${CMAKE_CURRENT_SOURCE_DIR}
    )

endfunction ()
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to