Generate resource file before it is processed



  • Hi all,

    I have the script below in my qmake file, and the custom command works as expected. However, the resource file is processed before it is generated or updated.
    How can I make sure the listing.qrc file is created/updated before it is processed by qmake?

    RESOURCES += \
        $$PWD/listing.qrc
    
    webUi.target = listing.qrc
    webUi.commands = pushd $$PWD; npm run-script build -- --prod --base-href \"/ui/\"; popd
    QMAKE_EXTRA_TARGETS += webUi
    PRE_TARGETDEPS += listing.qrc
    

  • Moderators

    @AlexanderB
    see this

    Following is untested though:

    RESOURCES += \
        $$PWD/listing.qrc
    qrctarget.commands = pushd $$PWD; npm run-script build -- --prod --base-href \"/ui/\"; popd
    target.depends = qrctarget
    


  • @raven-worx said in Generate resource file before it is processed:

    RESOURCES +=
    $$PWD/listing.qrc
    qrctarget.commands = pushd $$PWD; npm run-script build -- --prod --base-href "/ui/"; popd
    target.depends = qrctarget

    This does not work. In the example, the qrctarget is added to the "target" target (which does not exist). If there is a target name I can use to reference the resource processing step, this might work. I hoped the PRE_TARGETDEPS would help with this, but it does not.


  • Moderators

    @AlexanderB
    the "target" target exists, but it might be that it is handled differently than ordinary targets.

    Anyway try the following (taken from here):

    # must use a variable as input
    PHONY_DEPS = .
    PreBuildEvent.input = PHONY_DEPS
    # use non-existing file here to execute every time
    PreBuildEvent.output = $$PWD/listing.qrc
    # the system call to the batch file
    PreBuildEvent.commands = pushd $$PWD; npm run-script build -- --prod --base-href \"/ui/\"; popd
    # some name that displays during execution
    PreBuildEvent.name = running Pre-Build steps...
    # "no_link" tells qmake we don’t need to add the output to the object files for linking, and "no_clean" means there is no clean step for them.
    # "target_predeps" tells qmake that the output of this needs to exist before we can do the rest of our compilation.
    PreBuildEvent.CONFIG += no_link no_clean target_predeps
    # Add the compiler to the list of 'extra compilers'.
    QMAKE_EXTRA_COMPILERS += PreBuildEvent
    


  • Hi @raven-worx ,

    Thanks again, I've got it working the way I want, though I had to do some extra things.

    For completeness and others:

    What happens is that if I use your example, the listing.qrc file is created the first time, and not on every build. If I use a fake file as output, the custom compiler is executed everytime, however, when the listing.qrc already exists, and due to not being the output it is probably?? not seen as a pre-target for RCC so it is executed before the custom compiler.

    I've tried to add a depends_command or a dependency to a custom target in which I remove the file. Removal of the file works, but now the custom compiler is stuck in a loop it seems. Not entirely sure why.

    What seems to work, is to have the files for the custom command explicitly listed in the input, which even makes sense, no input file change, no need to build. But, since the files are created outside the QT project, I do not have a full/explicit listing.
    Using $$files(path/*, true) works to get the listing, but now there are many warnings like:

    Makefile:1379: warning: overriding commands for target `../../Project/ui/ui.web/listing.qrc'
    Makefile:1376: warning: ignoring old commands for target `../../Project/ui/ui.web/listing.qrc'
    

    To get rid of this warning I added the combine option, since there are many inputs but only one output, which again makes sense.

    So now I have a setup which only executes the custom command if the input files have changes, or if the output file is missing.

    The resulting qmake lines:

    npmcompiler.name = NPM Compiler
    npmcompiler.input = WEBFILES
    npmcompiler.commands =  pushd $$PWD; npm run-script build -- --prod --base-href \"/ui/\"; popd
    npmcompiler.output = $$PWD/listing.qrc
    npmcompiler.CONFIG += combine ignore_no_exist no_link no_clean target_predeps
    
    QMAKE_EXTRA_COMPILERS += npmcompiler
    
    WEBFILES = $$files($$PWD/src/*, true)
    

Log in to reply