16

I am making extra targets using qmake, and I'm trying to do two things at the same time: make a new folder, and copy a dll into that folder. Both action separate work fine, but the two together don't work.

something.target = this

# This works:
# something.commands =   mkdir newFolder
# This works too (if newFolder exists)
# something.commands =   copy /Y someFolder\\file.dll newFolder

# This doesn't work:
something.commands = mkdir newFolder; \
                     copy /Y someFolder\\file.dll newFolder

QMAKE_EXTRA_TARGETS += something
PRE_TARGETDEPS += this

I thought this was the right syntax (I found similar examples for example here and here), but I am getting the following error:

> mkdir newFolder; copy /Y someFolder\\file.dll newFolder
> The syntax of the command is incorrect.

Is the syntax different on different platforms or something? I'm working on Windows 7, with Qt 5.0.1.

Community
  • 1
  • 1
Yellow
  • 3,955
  • 6
  • 45
  • 74

3 Answers3

26

The value of .commands variable is pasted in the place of target commands in Makefile by qmake as is. qmake strips any whitespaces from values and changes them into single spaces so it's impossible to create multiline value without a special tool. And there is the tool: function escape_expand. Try this:

something.commands = mkdir newFolder $$escape_expand(\n\t) copy /Y someFolder\\file.dll newFolder

$$escape_expand(\n\t) adds new line character (ends previous command) and starts next command with a tab character as Makefile syntax dictates.

Sergey Skoblikov
  • 5,811
  • 6
  • 40
  • 49
  • Fantastic, works like a charm! But out of curiosity: is there a reason that other threads seem to employ the `; \ ` as a line break without problem. Is this platform dependent? – Yellow Aug 08 '13 at 09:36
  • It may be a shell-specific feature for joining multiple commands into one. On Windows && would work with standard cmd shell for example. I don't know unix-like shells well enough to answer with certainty. – Sergey Skoblikov Aug 08 '13 at 13:51
  • @SergeySkoblikov: Under Unix, the `&&` should work as well -> it runs the second command after the first, but only in case, the first one was successfully finished. Only `&` will run the second command anyway. Seems to be the same under windows? – mozzbozz Nov 10 '14 at 14:43
6

The and operator also works for me on Linux, and strangely windows.

something.commands = mkdir newFolder && copy /Y someFolder\\file.dll newFolder
Chris Desjardins
  • 2,631
  • 1
  • 23
  • 25
1

You can also append to the .commands variable if you want to avoid backslashes:

target.commands += mkdir toto
target.commands += && copy ...
# Result will be:
target:
    mkdir toto && copy ...

Or:

target.commands += mkdir toto;
target.commands += copy ...;
# Result will be:
target:
    mkdir toto; copy ...;
Chnossos
  • 9,971
  • 4
  • 28
  • 40