Create dir & Copy files with qmake - qt

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?

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

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

Related

qmake INSTALLS for a file not existing yet

Suppose I have a test.pro file with content as followings
unix {
inspro.path = /tmp
inspro.files += test.pro
}
!isEmpty(inspro.path) INSTALLS += inspro
unix {
insdoc.path = /tmp
insdoc.files += test.txt
}
!isEmpty(insdoc.path) INSTALLS += insdoc
Running qmake test.pro results in a Makefile. The file, test.pro, exists already, and the created Makefile contains install_inspro and uninstall_inspro for the file test.pro:
install_inspro: first FORCE
#test -d $(INSTALL_ROOT)/tmp || mkdir -p $(INSTALL_ROOT)/tmp
$(QINSTALL) /home/jianz/test/pro/test.pro $(INSTALL_ROOT)/tmp/test.pro
uninstall_inspro: FORCE
-$(DEL_FILE) -r $(INSTALL_ROOT)/tmp/test.pro
-$(DEL_DIR) $(INSTALL_ROOT)/tmp/
However, corresponding install_insdoc and install_insdoc are created if and only if the file test.txt exists.
In the case that the file test.txt is created as part of QMAKE_POST_LINK, is there a way to force qmake to create install_insdoc and uninstall_insdoc?
I think there's a custom install target CONFIG directive to help with this. Add:
insdoc.CONFIG += no_check_exist
Documented at https://doc.qt.io/qt-5/qmake-variable-reference.html#installs
More details and caveats at https://wiki.qt.io/Undocumented_QMake#Custom_install_config
Related Q/A: qmake copy files created while building

How to run multiple commands with an extra target in QMake

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.
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.
The and operator also works for me on Linux, and strangely windows.
something.commands = mkdir newFolder && copy /Y someFolder\\file.dll newFolder
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 ...;

Subdirectory in a Qt Project

I am looking for a way to organize my project's structure using QMake. I've found the subdirs template but I have quite a hard time understanding it. I have tried to do something like this. Can anyone please just tell me that I am correct or not.
edit: I have read the following thread How to use QMake's subdirs template? but I am still stuck
My project structure is as follows:
MultiFuncTester.pro
- DMM
(DMM.cpp, DMM.h and Multifunctester.pri)
-Safety
(Safety.cpp, Safety.h and Multifunctester.pri)
-Solar
(Solar.cpp, Solar.h and Multifunctester.pri)
Main
(Main.pro, Main.cpp and Multifunctester.pri)
Here Multifunctester.pri file has common stuff for all the sub directories.
I am pasting the MultiFuncTester.pro and .pri file and also the main.pro file
I have made project's pro file as MultiFuncTester.pro :
# build all components recursive
TEMPLATE = subdirs
######## normal build process ########
#
# Make sure your Main.pro is in the last line to ensure correct linking!
#
SUBDIRS = ../../MultiFuncTester/Components/Solar/Build/Solar.pro \
../../MultiFuncTester/Components/DMM/Build/DMM.pro \
../../MultiFuncTester/Components/Safety/Build/Safety.pro \
../../MultiFuncTester/Components/Setup/Build/Setup.pro \
../../MultiFuncTester/Components/Start/Build/Start.pro \
../../MultiFuncTester/Components/Main/Build/Main.pro \
CONFIG += ordered
MultiFunctester.pri file:
######################
# common stuff for all components
######################
TEMPLATE = lib
CONFIG += static \
warn_on \
qt \
thread \
rtti
QT += core \
gui
INCLUDEPATH +=/..\
../../MultiFuncTester/Components \
DEPENDPATH +=/..\
../../MultiFuncTester/Components \
CONFIG += debug_and_release
CONFIG += build_all
QMAKE_CXXFLAGS += -Wall
CONFIG(debug, debug|release) {
CONFIG_SUFFIX = dbg
} else {
CONFIG_SUFFIX = rel
DEFINES += QT_NO_DEBUG \
QT_NO_DEBUG_OUTPUT \
DBT_TRACE_DISCARD \
NDEBUG
CONFIG(gcov) {
QMAKE_CXXFLAGS_RELEASE += -fprofile-arcs -ftest-coverage
QMAKE_LFLAGS_RELEASE += -fprofile-arcs
QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE += -O0
}
}
CONFIG(crosstgt) {
### To be able to build Target run qmake as follows:
#qmake CONFIG+=crosstgt
CONFIG_SUFFIX = $${CONFIG_SUFFIX}_tgt
DEFINES += TARGET_BUILD
}
OBJECTS_DIR = obj_$${CONFIG_SUFFIX}
MOC_DIR = moc_$${CONFIG_SUFFIX}
DESTDIR = lib_$${CONFIG_SUFFIX}
Main.pro file:
################# include pri file #################
!include("Main.pri") {
error("Main.pri not found")
}
####################################################
################# override some pri settings #################
TEMPLATE = app
TARGET = MultiFuncTester
CONFIG -= static
QT += core \
gui
##############################################################
################# list used MultiFuncTester libraries #################
MultiFuncTester_COMPONENTS_DIR =../../MultiFuncTester/Components
################################################################
################# list MultiFunTester libraries #################
MultiFunTester_COMPONENTS_DIR =../../MultiFuncTester/Components
MultiFunTester_COMPONENTS = DMM \
SOLAR\
Safety\
Setup
################# own sources #################
INCLUDEPATH += ../../MultiFuncTester/Components \
SOURCES +=../Source/Main.cpp
################# set destination path
DESTDIR = bin_$$CONFIG_SUFFIX
################# edit include path
INCLUDEPATH += $$MultiFunctester_COMPONENTS_DIR \
################# start group
LIBS += -Wl,--start-group \
################# include MultiFunctester libraries and set dependencies
for(TheComponent, MultiFunctester_COMPONENTS) {
THELIBPATH = $$MultiFunctester_DIR/$${TheComponent}/Build/lib_$$CONFIG_SUFFIX
PRE_TARGETDEPS += $$THELIBPATH/lib$${TheComponent}.a
LIBS += $$THELIBPATH/lib$${TheComponent}.a
}
################# end group
LIBS += -Wl,--end-group
Each subdirectory has a .pro file which have headers and sources defined and also the common multifunctester.pri file
Please let me know that putting a common static library (MultiFunctester.pri file) is a right approach and what shall it do in the code.....and if not please help me in correcting me wherever I am wrong.
Thanks
There are some mistake :
TEMPLATE = lib in MultiFunctester.pri. .pri files are like header files, this will be added in every pro files which include it. It can be confusing, so avoid using the TEMPLATE variable in .pri
if I understand well your first point you have copies of Multifunctester.pri in each directory. This is also really confusing, you should only have one (or different pri files).
Overriding previous setting is not as a good idea as it seems. When you will have to debug to see which configuration is used in each pro file you will feel the pain. Rather declare all common settings in one place, and for variable settings declare them when they
apply.
I don't see Multifunctester.pri included anywhere...
You use the subdirs template when you want each sub-directory to be built separately (they can be interdependent). It is useful for instance when you have a framework with many executable. look at qwt source directory
In this case, you use
TEMPLATE = subdirs
CONFIG += ordered
### They MUST be subdirectories, no ../, and no .pro
SUBDIRS += dir1\
dir2\
...
dirn
for each level aside of the last one (where you have TEMPLATE = lib or app).
Now if you have directory structure but you want to build all together you can create a
pri file for each subdirectory where you include the source files. For example :
in the main src.pro file you have
include(MultiFunctester.pri) ## for the configurations
include($PATHTOSOLAR/Solar.pri)
include($PATHTODMM/dmm.pri)
include($PATHTOSAFETY/safety.pri)
include($PATHTOSETUP/setup.pri)
include($PATHTOSTART/start.pri)
include($PATHTOMAIN/main.pri)
where $PATHTO... is the relative path from thesrc.pro directory.
Now in each subdirectory you include the source files.
onesubdir.pri :
headers += /PATHTOTHISSUBDIR/file1.h ## again same relative path as above
....
source +=....

Running a program/script from QMake

We have a fairly large code-base. The vast majority of the code is compiled using qmake to produce the makefiles. However, there are some sub-projects that get produced by running batch files or running other programs.
I'd like to be able to have everything compiled using qmake, but I can't figure out how to get qmake to simply run a script.
One thing that I've tried is using QMAKE_EXTRA_TARGETS in my pro file, like so:
TEMPLATE = lib
SOURCES = placeholder.cpp
CONFIG += no_link staticlib
batch_runner.target = placeholder.cpp
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS = batch_runner
I then have to have the batch file produce placeholder.cpp like so:
# do the real work here
# ...
# create placeholder.cpp so qmake and nmake are happy
echo // dummy >> placeholder.cpp
This seems to work fine. The trouble is that it is somewhat hokey. If I don't specify batch_runner.target (i.e. I leave it blank) or don't put placeholder.cpp in SOURCES then the batch file never gets run. This is because qmake isn't making batch_runner.commands the action for any other dependency in the Makefile.
Is there any better way to get QMake to construct a Makefile such that a script is run when the Makefile executes?
It looks like QMAKE_POST_LINK works well for this sort of thing.
This seems to get the job done. my_batch_file.bat runs when nmake runs (rather than when qmake runs) and I don't need to do anything funny with placeholder targets or files.
It's quite likely that I don't need all of the items listed in 'CONFIG'.
TEMPLATE = lib
TARGET =
CONFIG += no_link target_predeps staticlib
QMAKE_POST_LINK = my_batch_file.bat
QMAKE_CLEAN += batch_output.obj
Try the system() command. For example:
system(pwd)
Here is another solution:
TEMPLATE = aux
OBJECTS_DIR = ./
DESTDIR = ./
first.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += first
QMAKE_CLEAN += batch_output.obj
The template aux basically produces a makefile which does nothing when run without specifying a target. The OBJECTS_DIR and DESTDIR variables are set to the current directory to prevent that qmake creates the debug and release directories (it's important to set them to ./ and not just to .; at least on Windows). Then, using QMAKE_EXTRA_TARGETS, we redefine the target first to run the custom command when the makefile is invoked without target.
It's a bit hacky but it gets the job done.
Addition:
If you want to prevent the generation of three makefiles (Makefile, Makefile.Debug, Makefile.Release), you can add
CONFIG -= debug_and_release
However, if you use this and depending on how the makefile is invoked (always invoked manually, invoked by parent directory's "subdirs" *.pro file, ...), it might be necessary to create fake debug and release targets to avoid "no rule to make target..." errors. For example:
release.target = release
release-clean.target = release-clean
release-install.target = release-install
[...]
debug.target = debug
debug-clean.target = debug-clean
debug-install.target = debug-install
[...]
QMAKE_EXTRA_TARGETS += release release-clean release-install [...]
QMAKE_EXTRA_TARGETS += debug debug-clean debug-install [...]
You could use the SUBDIRS configuration to run multiple different targets, even from the same makefile. This might work especially well with your extra targets, as a subdir configuration can specific a specific target in the makefile to run (see undocumented qmake for details). In this case, I would put all of the "regular" build commands in one .pro file, the external build commands in another, and a subdirs .pro file to build all of them. I haven't tested anything quite like this, but it should work.
regular.pro:
SOURCES += main.cpp
TARGET = regular.exe
external.pro:
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += batch_runner
other_runner.commands = other_batch_file.bat
QMAKE_EXTRA_TARGETS += other_runner
do_it_all.pro:
TEMPLATE = subdirs
CONFIG += ordered
regular.file = regular.pro
SUBDIRS += regular
batch.file = external.pro
batch.target = batch_runner
SUBDIRS += batch
other.file = external.pro
other.target = other_runner
SUBDIRS += other

Qmake project dependencies (linked libraries)

I have a project that links to a number of shared libraries.
Lets say project A depends on projects B and C
Ideally, I want to impose the following dependencies in my project file:
Rebuild project A if either B or C has been rebuilt since last time project A was built
Use the output for the relevant configuration (i.e. if building project A in debug mode, then use the debug versions of the libs for project B and C)
Does anyone know how I may explicitly express such dependencies in my project file?
After quite a bit of frustration with qmake, I have found what I think is the answer to your question. If not, then I have learned the way that I will use qmake until I find something better, because this is still a little bit ugly. I set up a demo project, this is my directory structure (files have extensions, folders do not):
MyProj
MyProj.pro
myproj-core
myproj-core.pro
globals.h
MyProjCore.h
MyProjCore.cpp
myproj-app
myproj-app.pro
main.cpp
We start with MyProj.pro as a subdirs project, which is the key to doing what you ask. Basically, instead of depending on other projects to specify debug/release and all sorts of other junk, you just set it on the one qmake file. It doesn't let you make only what you need, but it's the best solution I could come up with. Here are the contents:
TEMPLATE = subdirs
# Needed to ensure that things are built right, which you have to do yourself :(
CONFIG += ordered
# All the projects in your application are sub-projects of your solution
SUBDIRS = myproj-core \
myproj-app
# Use .depends to specify that a project depends on another.
myproj-app.depends = myproj-core
myproj-core.pro is your typical shared object library:
QT -= gui
TARGET = myproj-core
TEMPLATE = lib
DEFINES += MYPROJCORE_LIBRARY
SOURCES += MyProjCore.cpp
HEADERS += MyProjCore.h \
globals.h
myproj-app.pro is consumer application, where the little rebuild-when-needed trick is:
QT -= gui
TARGET = myproj-app
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
# Specify that we're lookin in myproj-core. Realistically, this should be put
# in some configuration file
INCLUDEPATH += ../myproj-core
# Link to the library generated by the project. Could use variables or
# something here to make it more bulletproof
LIBS += ../myproj-core/libmyproj-core.so
# Specify that we depend on the library (which, logically would be implicit from
# the fact that we are linking to it)
PRE_TARGETDEPS += ../myproj-core/libmyproj-core.so
SOURCES += main.cpp
I hope this solves your problem, as I know it solved mine!
EDIT: I made a file specifically for building the dependencies for me, I store it in a sibling folder of each of my projects (child of the MyProj in the directory structure specified above) called dependencies.pri:
# On windows, a shared object is a .dll
win32: SONAME=dll
else: SONAME=so
# This function sets up the dependencies for libraries that are built with
# this project. Specify the libraries you need to depend on in the variable
# DEPENDENCY_LIBRARIES and this will add
for(dep, DEPENDENCY_LIBRARIES) {
#message($$TARGET depends on $$dep ($${DESTDIR}/$${dep}.$${SONAME}))
LIBS += $${DESTDIR}/lib$${dep}.$${SONAME}
PRE_TARGETDEPS += $${DESTDIR}/lib$${dep}.$${SONAME}
}
So at the bottom of all the consuming applications, I can add the lines:
DEPENDENCY_LIBRARIES = myproj-core
include(../config/dependencies.pri)
This assumes that you are copying the libraries to some shared location and/or moving them around as needed, so my function might not work for you, but I figured I would add it to the solution.
I use the solution below. This works without the usage of an extra .pro file with subdir template.
TEMPLATE = app
TARGET = MyApp
PRE_TARGETDEPS = ../../libs/MyLib/MyLib.a
INCLUDEPATH += ../../libs/MyLib/include
HEADERS += src/MyApp.h \
../../libs/MyLib/incude/MyLib.h
SOURCES += src/MyApp.cpp
LIBS += ../../libs/MyLib/MyLib.a
MyLib.target = ../../libs/MyLib/MyLib.a
MyLib.commands = cd ../../libs/MyLib && make
MyLib.depends = ../../libs/MyLib/Makefile
QMAKE_EXTRA_TARGETS += MyLib
Check out this question: Force relink when building in QT Creator
Try adding something similar to this code to your pro file:
CONFIG(debug, debug|release) {
DESTDIR = ../../../bin/debug
OBJECTS_DIR = ./debug
}
else {
DESTDIR = ../../../bin/release
OBJECTS_DIR = ./release
}
Then you will have to specify the dependencies for each configuration:
CONFIG(debug, debug|release) {
LIBS += -L../../../lib/debug \
-L../../../bin/debug \
-llib1 \
-llib2
PRE_TARGETDEPS += ../../../lib/debug/liblib1.a \
../../../lib/debug/liblib2.a
else {
LIBS += -L../../../lib/release \
-L../../../bin/release \
-llib1 \
-llib2
PRE_TARGETDEPS += ../../../lib/release/liblib1.a \
../../../lib/release/liblib2.a
}
I had this problem when refactoring my project, after I moved in a new DLL (pqXDot) a reusable class (from pqGraphviz).
After adding a new DLL to my project, and adding the new DLL reference to others DLL and applications that needed it, I had in main .pro:
TEMPLATE = subdirs
SUBDIRS += \
pqConsole \
pqConsoleTest \
pqSource \
pqSourceTest \
fdqueens \
pqGraphviz \
pqGraphvizTest \
pqXDot
and the rebuild caused a linker error, because pqGraphviz, the DLL being restructured, can't find pqXDot, the new DLL.
It turns out it's sufficient to reorder the SUBDIRS list, moving the required DLL before the dependent one:
SUBDIRS += \
pqConsole \
pqConsoleTest \
pqSource \
pqSourceTest \
fdqueens \
pqXDot \
pqGraphviz \
pqGraphvizTest
For those that are interested into a template for your Qt/QML project, I have published one template on GitHub QmlAppTemplate.

Resources