[CMake] Inserting a dependency before Qt moc is run

Aurélien Gâteau agateau at kde.org
Fri Nov 15 09:13:15 EST 2013


Hi,

I am working on KDE Frameworks, which uses CMake.

We make use of Qt5 plugin system, where a plugin can be described by a .json 
file, as documented here:
http://doc-snapshot.qt-project.org/qt5-stable/qtplugin.html#Q_PLUGIN_METADATA

We want to generate the .json files from .desktop files. We have a tool to 
do so, called kservice_to_json.

I am trying to express the necessary dependencies in CMake but I fail. What 
I want to obtain is something like that:

    foo.json: foo.desktop
        kservice_to_json -i foo.desktop -o foo.json

    # foo.json must be generated before moc is run, because moc tries to
    # read the .json file mentioned in Q_PLUGIN_METADATA
    foo.moc: foo.cpp foo.json
        moc foo.cpp -o foo.moc

    foo.o: foo.cpp foo.moc
        g++ foo.cpp -o foo.o

I defined a custom command to generate the .json, like this (simplified):
My latest try looks like this:

macro(kservice_desktop_to_json desktop maintarget)
    string(REPLACE ".desktop" ".json" json ${desktop})

    string(REPLACE ".desktop" "_json" target ${desktop})
    string(REPLACE "." "_" target ${target})
    add_custom_target(${target} DEPENDS ${json})

    add_custom_command(
        OUTPUT ${json}
        COMMAND KF5::desktoptojson -i ${desktop} -o 
${CMAKE_CURRENT_BINARY_DIR}/${json}
        DEPENDS ${desktop}
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMENT "Generating ${json}"
        )

    # Does not work !
    add_dependencies(${maintarget}_automoc ${target})

endmacro(kservice_desktop_to_json)

With the idea that one would use it like this:

   add_library(foo ...)
   kservice_desktop_to_json(foo.desktop foo)

But this does not work because at the time the macro calls

    add_dependencies(${maintarget}_automoc ${target})

The "foo_automoc" target does not exist yet, so it can't add any dependency 
to it. I also made numerous attempts at adding the dependency to the 
generated moc file without success.

Do you have any solution for that problem?

Regards,
Aurélien

PS: You can find more information, including a test case there:

http://thread.gmane.org/gmane.comp.kde.devel.buildsystem/8068



More information about the CMake mailing list