Strayer
Strayer

Reputation: 3120

Copy a file to the build directory after compiling project with Qt

I have a file "settings.ini" which needs to reside next to the Qt executable.

I can add a custom build step for this in Qt Creator which calls something like this:

copy %{sourceDir}/settings.ini %{buildDir}/settings.ini

This works great so far, but I'd like to include this in the *.pro file so I can put this up in our SVN too.

How can I do this using qmake/.pro-files only?

Upvotes: 17

Views: 23837

Answers (6)

RandomGuy
RandomGuy

Reputation: 667

I ended up creating a python script where I do the copy of the files.

Then in the .pro file I add the following line:

QMAKE_POST_LINK += python $$PWD/copy_requirements.py $$PWD $$OUT_PWD

I pass the source directory with $$PWD and the build directory with $$OUT_PWD.

The python copy_requirements.py script then uses these variables to copy the files needed from the source directory to the build directory. This is executed after the code compilation.

This works on windows / linux. According to Qt documentation this doesn't work on Xcode projects.

Upvotes: 0

ce_nort
ce_nort

Reputation: 168

For anyone who is looking to copy multiple files or directories: Expanding on Paglian's answer, I found that adding another variable with commands worked.

copyconfig.commands = $(COPY_DIR) $$shell_path($$PWD\config) $$shell_path($$DESTDIR\config)
copystyle.commands = $(COPY_DIR) $$shell_path($$PWD\style) $$shell_path($$DESTDIR\style)
first.depends = $(first) copyconfig copystyle
export(first.depends)
export(copyconfig.commands)
export(copystyle.commands)
QMAKE_EXTRA_TARGETS += first copyconfig copystyle

Upvotes: 0

kingspp
kingspp

Reputation: 311

Compatible with Windows and Mac OSX Dev environments:

Change {AppName} to respective application name

# Define mac/windows specific target dirs
TARGETDIR = ''
macx {
  TARGETDIR      += $$OUT_PWD/{AppName}.app/Contents/MacOS/
}
else {
  TARGETDIR      += $$OUT_PWD
}

# Directories do not exist for the first build
# Without mkdata, build is successful after 5 tries. To avoid, use mkdata
mkdata.commands = $(MKDIR) $${TARGETDIR}
copydata.commands = $(COPY_FILE) $$PWD/settings.ini $${TARGETDIR}
first.depends = $(first) mkdata copydata
export(first.depends)
export(mkdata.commands)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first mkdata copydata

Happy to add Unix support if someone posts Unix solution in the comments.

Upvotes: 0

lumos0815
lumos0815

Reputation: 4326

for osx bundles you can handle it this way see Resource files in OS X bundle

add this to you project file:

APP_QML_FILES.files = path/to/file1.qml path/to/file2.qml
APP_QML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_QML_FILES

this example copies the files to Contents/Resources

Upvotes: 2

Paglian
Paglian

Reputation: 6752

To copy %{sourceDir}/settings.ini to the build directory without requiring to call make install use:

copydata.commands = $(COPY_DIR) $$PWD/settings.ini $$OUT_PWD
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

$$PWD is the path of current .pro file. If your settings.ini file is not located in the same directory than the project file, then use something like $$PWD/more_dirs_here/settings.ini

Note: I found this solution here. I recommend to read the whole article as it explains how it works.

Upvotes: 18

jwernerny
jwernerny

Reputation: 7048

You probably want to use the INSTALLS keyword in QMake. It will require you to run make install after your build, but it does work cross-platform.

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

INSTALLS += install_it

Upvotes: 11

Related Questions