How to have qmake generate dependencies to custom targets?

  add-custom-target, c++, qmake

I am trying to set up an out-of-tree build, so I can clone my repository and leave the sources untouched.
I expect qmake to allow this. The intended use is

git clone <my-sources>
mkdir build && cd build
qmake ../my-sources
make

I have a source file referring to a generated header file.

#include "custom/dir/custom.h"
int main(){}

This custom.h should be generated, so in my test.pro I defined

custom_target_generated_dirs.target = custom/dir
custom_target_generated_dirs.commands = mkdir -p $$custom_target_generated_dirs.target

custom_target_headers.target = $$custom_target_generated_dirs.target/custom.h
custom_target_headers.depends = $$custom_target_generated_dirs.target
custom_target_headers.commands = cp custom.h.template > $$custom_target_headers.target

QMAKE_EXTRA_TARGETS += custom_target_headers custom_target_generated_dirs

SOURCES = client.cpp

When I run qmake, the resulting makefile defines a dependency to a file relative to the source dir:

client.o: ../client.cpp ../custom/dir/custom.h
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o client.o ../client.cpp

This makes it impossible to build out-of-tree: I must not put generated code inside the source tree. Indeed, this doesn’t build, either:

~/build$ qmake ../qmaketest/ && make
make: *** No rule to make target '../qmaketest/custom/dir/custom.h', needed by 'client.o'.  Stop.

How can I tell qmake to generate C++ compilation dependencies towards the build tree?

Source: Windows Questions C++

LEAVE A COMMENT