user1949113
user1949113

Reputation:

Qmake copy multiple files while building

I have a list of files in my Qmake project. And I want them copied in build directory at build time.

My qmake file is below

OTHER_FILES += \
    input1 \
    input2 \
    input3 \

I'm using Linux. I've read a few stack overflow questions and googled about my problem but cannot find the exact solution.

Upvotes: 1

Views: 509

Answers (2)

Vinu Raja Kumar C
Vinu Raja Kumar C

Reputation: 146

I have a requirement of copying openssl 1.1 dlls (libcrypto-1_1-x64.dll & libssl-1_1-x64.dll) into the build directory in order to have network communication. I am using Qt 5.13.0 on a windows 11 64 Bit machine with gcc 7.3.0 compiler. My project structure is,

Project Root D:\Qt\MyProject
Project file D:\Qt\MyProject\MyProject.pro
Debug Build D:\Qt\MyProject\debug
Release Build D:\Qt\MyProject\release
Files to copy D:\Qt\MyProject\library

I tried multiple solutions from SO but only the following snippet worked for me.

win32 {
CONFIG( debug, debug|release ) {
    DEST_DIR = debug          # debug
}
else {
    DEST_DIR = release        # release
}
}
TARGET_DIR = $${OUT_PWD}/$${DEST_DIR}
# List of files to be copied to target directory
FILELIST  = "library/libcrypto-1_1-x64.dll"       # ssl 1.1.x (qt>=5.12.4)
FILELIST += "library/libssl-1_1-x64.dll"
win32:TARGET_DIR ~= s,/,\\,g
for(FILE, FILELIST) {
    win32:FILE ~= s,/,\\,g
    $$basename(FILE).depends = $$shell_quote($$FILE)
    $$basename(FILE).target = $${TARGET_DIR}/$$basename(FILE)
    $$basename(FILE).commands = $(COPY_FILE) $$shell_quote($$FILE) $$shell_quote($${TARGET_DIR})
    QMAKE_EXTRA_TARGETS += $$basename(FILE)
    PRE_TARGETDEPS += $${TARGET_DIR}/$$basename(FILE)
}

In the above case, for the debug build, QMake generates two rules in the Makefile,

D:/Qt/MyProject/debug/libcrypto-1_1-x64.dll: library/libcrypto-1_1-x64.dll
    $(COPY_FILE) library\libcrypto-1_1-x64.dll D:\Qt\MyProject\debug

D:/Qt/MyProject/debug/libssl-1_1-x64.dll: library/libssl-1_1-x64.dll
    $(COPY_FILE) library\libssl-1_1-x64.dll D:\Qt\MyProject\debug

This ensures that the files are getting copied only if the files in the library directory is updated.

References :

  1. Qt5 - copy additional files on build time - must work in many situations
  2. QMake - how to copy a file to the output
  3. Copy a file to build directory after compiling project with Qt
  4. https://forum.qt.io/topic/124864/how-to-copy-a-folder-from-source-to-build-directory-in-qmake/3
  5. https://www.toptal.com/qt/vital-guide-qmake
  6. https://web.archive.org/web/20210620205822/http://blog.mgsxx.com/?p=2201
  7. https://www.reddit.com/r/QtFramework/comments/mpjl9z/copying_data_files_to_the_build_directory_with/
  8. https://dragly.org/2013/11/05/copying-data-files-to-the-build-directory-when-working-with-qmake/
  9. Copy a file to build directory after compiling project with Qt

Upvotes: 0

svlasov
svlasov

Reputation: 10455

Can be done using for() loop. You may need to adjust the BUILD_DIR variable. The "other" files are takes from the current directory.

OTHER_FILES += \
    input1 \
    input2 \
    input3 \

BUILD_DIR = build

for(file, OTHER_FILES) {
    eval($${file}.depends = $$file)
    eval($${file}.target = $$BUILD_DIR/$$file)
    eval($${file}.commands = cp $$file $$BUILD_DIR/)

    QMAKE_EXTRA_TARGETS += $${file}
    PRE_TARGETDEPS += $$BUILD_DIR/$$file
}

Upvotes: 3

Related Questions