qmake command to copy files and folders into output directory
Asked Answered
W

2

9

I'm developing an app that should build on Windows, Linux and OS X using QtCreator and Qt 5.3. I want to copy all files and subfolders from a folder into output folder. I've got it working for Linux and OS X, but not for Windows. Here's the relevant section of my .pro file:

win32 {
    PWD_WIN = $${PWD}
    DESTDIR_WIN = $${OUT_PWD}
    copyfiles.commands = $$quote(cmd /c xcopy /S /I $${PWD_WIN}\copy_to_output $${DESTDIR_WIN})
}
macx {
    copyfiles.commands = cp -r $$PWD/copy_to_output/* $$OUT_PWD
}
linux {
    copyfiles.commands = cp -r $$PWD/copy_to_output/* $$OUT_PWD
}
QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

The error I'm getting on Windows is "Invalid number of parameters".

Wang answered 29/6, 2014 at 9:54 Comment(0)
P
10

If you look at the $${PWD} variable with message($${PWD}), you will see / as directory seperator, even in Windows. You have to convert it to native directory seperator :

PWD_WIN = $${PWD}
DESTDIR_WIN = $${OUT_PWD}
PWD_WIN ~= s,/,\\,g
DESTDIR_WIN ~= s,/,\\,g

copyfiles.commands = $$quote(cmd /c xcopy /S /I $${PWD_WIN}\\copy_to_output $${DESTDIR_WIN})

QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles
Perigon answered 29/6, 2014 at 16:27 Comment(1)
If you want to do this every time, you need to add also /Y to xcopy commands. Otherwise it will hang. (/Y - Suppresses prompting to confirm you want to overwrite an existing destination file).Gamali
I
4

Building off of @Murat's answer, Qt actually has built-in functions to convert the filepath to the local system's preference.

$$shell_path( <your path> ) //Converts to OS path divider preference.
$$clean_path( <your path> ) //Removes duplicate dividers.

Call it like: $$shell_path($$clean_path( <your path> )), or $$clean_path() will convert the dividers back to Linux-style dividers.

This works for me on Windows:

#For our copy command, we neeed to fix the filepaths to use Windows-style path dividers.
SHADER_SOURCE_PATH = $$shell_path($$clean_path("$${SOURCE_ROOT}\\Engine\\Shaders\\"))
SHADER_DESTINATION = $$shell_path($$clean_path("$${PROJECT_BIN}\\Shaders\\"))

#Create a command, using the 'cmd' command line and Window's 'xcopy', to copy our shaders folder
#into the Game/Bin/Shaders/ directory.
CopyShaders.commands = $$quote(cmd /c xcopy /Y /S /I $${SHADER_SOURCE_PATH} $${SHADER_DESTINATION})

#Add the command to Qt.
QMAKE_EXTRA_TARGETS += CopyShaders
POST_TARGETDEPS += CopyShaders
Isiahisiahi answered 27/9, 2017 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.