Search code examples
qtqmake

Copy a file to build directory after compiling project with Qt


I have seen several suggestions, but nothing really worked for me as I want. I just need to copy a file to the desired destination directory.

Say, for example from this answer:

install_it.path = %{buildDir}
install_it.files += %{sourceDir}/settings.ini

INSTALLS += install_it

Variables %{buildDir} and %{sourceDir} should be defined, to make this work. Ok, there's no problem with %{sourceDir}: it is just .. But how can I get %{buildDir}?

EDIT1

Say, I have a project my_project here:

/path/to/my_project

So, release build path is this: /path/to/my_project-build-Desktop-release,

debug build path is this: /path/to/my_project-build-Desktop-debug

I have files to be copied to destination directory here: /path/to/my_project/copy_to_install_dir

So, I want all files from /path/to/my_project/copy_to_install_dir to be copied to /path/to/my_project-build-Desktop-release when I do release build. And, the same way for debug build.

I can't find variable which contain full destination path, i.e. /path/to/my_project-build-Desktop-release for debug build.

Just in case: I use Windows, but anyway I'm looking for crossplatform solution.

EDIT2

Exact solution, for future readers:

install_it.path = $$OUT_PWD
install_it.files = copy_to_install_dir/*

INSTALLS += \
    install_it

Solution

  • This is what we are using in QtSerialPort:

    target_headers.files  = $$PUBLIC_HEADERS
    target_headers.path   = $$[QT_INSTALL_HEADERS]/QtSerialPort
    INSTALLS              += target_headers
    
    mkspecs_features.files    = $$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf
    mkspecs_features.path     = $$[QT_INSTALL_DATA]/mkspecs/features
    INSTALLS                  += mkspecs_features
    

    Basically, you set the files and path for the target, and then append that into the INSTALLS variable. What you still need is the $$OUT_PWD variable which we are also using extensively in QtSerialPort. That will provide you the root of the build directory.

    It is one of those undocumented qmake features, but it is very useful.

    Also, for the source directory in general at large, you should not assume "." and so forth because that may be different as you run through a wrapper application in which the "." will point to that and not what you expect: the qmake source project root. In those cases, it is safer to use the PWD variable which points to the source as opposed OUT_PWD which points to the build folder.

    Just to give a rough example about the difference o those two variables with a real world scenario, here you can find what we are doing in QtSerialPort:

    system("echo QTSERIALPORT_PROJECT_ROOT = $$PWD >> $$OUT_PWD/.qmake.cache")
    system("echo QTSERIALPORT_BUILD_ROOT = $$OUT_PWD >> $$OUT_PWD/.qmake.cache")
    

    where the former is the root for the source project, and the latter for the build directory. They may be the same, but in many cases they are not, e.g. when building through QtCreator just for one of those.