How to have qmake extra targets only run if the target is out of date

21 Views Asked by At

In qmake is there a way to tell extra targets when they are out of date?

For the following configuration I can call any one of the defined extra targets for example make copy_zip and the dependent target commands are run in the expected order but they run every time. In the qmake documentation it doesn't mention how to specify an out-of-date test.

TARGET = demo
SOURCES = main.cpp

add_resource.commands = mkdir -p $${OUT_PWD}/$${TARGET}.app/Contents/Resources/ $$escape_expand(\n\t)
add_resource.commands += cp $$PWD/resource.dat $${OUT_PWD}/$${TARGET}.app/Contents/Resources/
add_resource.depends += first

create_zip.commands = zip -r $${OUT_PWD}/$${TARGET}.zip $${OUT_PWD}/$${TARGET}.app
create_zip.depends += add_resource

copy_zip.commands = cp $${OUT_PWD}/$${TARGET}.zip $${OUT_PWD}/$${TARGET}.zip.dat
copy_zip.depends = create_zip

QMAKE_EXTRA_TARGETS += add_resource create_zip copy_zip

If this turns out to be not possible with extra targets then is there another way to do it?

1

There are 1 best solutions below

0
glennr On

This is how I did it (on MacOS).

QMake .pro file:

# main.cpp
# #include <iostream>
# int main(int argc, char* argv[])
# {
#     (void)argv;
#     std::cout << "argnum " << argc << std::endl;
#     return 0;
# }

TARGET = demo
SOURCES = main.cpp

# Build this target if the main executable has been changed
ADD_RESOURCE_FILES = $${OUT_PWD}/$${TARGET}.app/Contents/MacOS/$${TARGET}

# The .input field must be a qmake variable
add_resource.input = ADD_RESOURCE_FILES

# This is a flag file to trigger the out-of-date mechanism
add_resource.output = $$OUT_PWD/.add_resource

# Add commands here to execute the desired task. Must have "$$escape_expand(\n\t)"
# at the end of each line.
add_resource.commands = mkdir -p $${OUT_PWD}/$${TARGET}.app/Contents/Resources/ $$escape_expand(\n\t)
add_resource.commands += touch $$PWD/resource.dat $$escape_expand(\n\t)
add_resource.commands += cp $$PWD/resource.dat $${OUT_PWD}/$${TARGET}.app/Contents/Resources/ $$escape_expand(\n\t)

# Update the flag file
add_resource.commands += touch $$OUT_PWD/.add_resource

# Do not add this target to the linker
add_resource.CONFIG += no_link

# Add to qmake's compiler list
QMAKE_EXTRA_COMPILERS += add_resource

# Create flag file target and its dependencies 
.add_resource.output = $$OUT_PWD/.add_resource
.add_resource.depends = ADD_RESOURCE_FILES

# Build this target if the flag file for the previous step has been changed
CREATE_ZIP_FILES = $${OUT_PWD}/.add_resource
create_zip.input = CREATE_ZIP_FILES
create_zip.output = $${OUT_PWD}/.create_zip
create_zip.commands = zip -r $${OUT_PWD}/$${TARGET}.zip $${OUT_PWD}/$${TARGET}.app $$escape_expand(\n\t)
create_zip.commands += touch $$OUT_PWD/.create_zip
create_zip.CONFIG += no_link
QMAKE_EXTRA_COMPILERS += create_zip

.create_zip.output = $$OUT_PWD/.create_zip
.create_zip.depends = CREATE_ZIP_FILES

COPY_ZIP_FILES = $$OUT_PWD/.create_zip
copy_zip.input = COPY_ZIP_FILES
copy_zip.output = $$OUT_PWD/.copy_zip
copy_zip.commands = cp $${OUT_PWD}/$${TARGET}.zip $${OUT_PWD}/$${TARGET}.zip.dat $$escape_expand(\n\t)
copy_zip.commands += touch $$OUT_PWD/.copy_zip
copy_zip.CONFIG += no_link
QMAKE_EXTRA_COMPILERS += copy_zip

.copy_zip.output = $$OUT_PWD/.copy_zip
.copy_zip.depends = COPY_ZIP_FILES

After calling qmake <file>.pro the new targets can be built using e.g. make .copy_zip.