krizajb
krizajb

Reputation: 1814

Create dir & Copy files with qmake

I am trying to copy target binary file in multiple folders. In first step I must create those multiple folders. I have followed this example without success.

The only thing that worked was system(mkdir $$OUT_PWD/newFolder), but I'm trying to use QMAKE_EXTRA_TARGETS for $(MKDIR) followed by $(COPY) from this example.

Using Qt 4.8.6 with qmake 2.01a.

This is how I run qmake: qmake Server.pro -r -spec linux-g++ CONFIG+=debug CONFIG+=declarative_debug

Update: This is my current implementation, which creates a list of directories and copies the target binary to selected directories.

# List all server directories
DIRS = server-1 \
       server-2 \
       server-3 \
       server-4

INSTALL_PATH = $$OUT_PWD
# Shadow build detection
!equals(PWD, $$OUT_PWD) INSTALL_PATH = $$replace(INSTALL_PATH, build, install)

# Loop over all given directories and append the 'install' directory to make absolute paths
for(DIR, DIRS) ABS_DIRS += $$INSTALL_PATH/$$DIR
# Create 'copy' commands for $DIRS
for(DIR, ABS_DIRS) CP_CMD += $(COPY) $$OUT_PWD/$$TARGET $$DIR &&

# Ignore last &&
CP_CMD += true

install.commands = $(MKDIR) $$ABS_DIRS && $$CP_CMD

QMAKE_EXTRA_TARGETS += install
QMAKE_POST_LINK += install

The missing part for me was that I didn't execute make with correct arguments. After calling make install which also includes qmake INSTALLS files, the code executes. However this fails on clean build with given error: install: missing file operand. If I rename the install command with for example copy, I get this error: make: copy: Command not found. Any clues?

Upvotes: 6

Views: 10233

Answers (2)

krizajb
krizajb

Reputation: 1814

Got it working. Some side notes ... QtCreator by default creates build-project-kit-debug/release directory for building if shadow build is enabled. This code creates install-project-kit-debug/release directory on same level with listed DIRS as sub directories. Directories are created after compile with create command. Target binary is then copied to DIRS directories after linking.

Thanks to macetw for POST_TARGETDEPS which also lead me to QMAKE_POST_LINK. qmake and make are ran without any extra arguments.

# Sets target destination dir - platform independent
win32 {
    build_pass: CONFIG(debug, debug|release) {
        DESTDIR = $$OUT_PWD/debug
    }
    else: build_pass {
        DESTDIR = $$OUT_PWD/release
    }
}

unix {
    DESTDIR = $$OUT_PWD
}

# List all server directories
DIRS = server-1 \
       server-2 \
       server-3 \
       server-4

INSTALL_PATH = $$DESTDIR
# Shadow build detection
!equals(PWD, $$DESTDIR) INSTALL_PATH = $$replace(INSTALL_PATH, build, install)

# Loop over all given directories and append the 'install' directory to make absolute paths
for(DIR, DIRS) ABS_DIRS += $$INSTALL_PATH/$$DIR
# Create 'copy' commands for $DIRS
for(DIR, ABS_DIRS) CP_CMD += $(COPY) $$DESTDIR/$$TARGET $$DIR ;

create.commands = $(MKDIR) $$ABS_DIRS

QMAKE_EXTRA_TARGETS += create
POST_TARGETDEPS += create

QMAKE_POST_LINK += $$CP_CMD

Upvotes: 5

macetw
macetw

Reputation: 1850

This documentation appears to be missing the use of PRE_TARGETDEPS or POST_TARGETDEPS.

What that means is that the Makefile has the instructions to do what it needs to do, but those targets are not built into the dependency chain, so the Make tool never does them.

If the problem is about doing 2 things (instead of just one), try to put 2 commands in the same line. Like so:

QMAKE_EXTRA_TARGETS += foo
foo.target = $$OUT_PWD/newFolder
foo.commands = $(MKDIR) $$OUT_PWD/newFolder ; $(COPY_DIR) $SOURCE $$OUT_PWD/newFolder

... You might also try "&&" instead of ";", to get strong checks of return codes. You could choose to create 2 QMAKE_EXTRA_TARGETS that depend on one another.

QMAKE_EXTRA_TARGETS += foo bar
foo.target = $$OUT_PWD/newFolder
foo.commands = $(MKDIR) $$OUT_PWD/newFolder
bar.target = $$OUT_PWD/newFolder/file
bar.commands = $(COPY_DIR) $SOURCEOFFILE $$OUT_PWD/newFolder
bar.depends = foo

Upvotes: 3

Related Questions