Port qmake *.pri source-library to CMake - qt

I have small frequently modified *.pri source-libraries, purpose of those pri's is to add headers and sources, do aux tasks like copy file. Those libraries are at same or higher directory level than the project that will use them and must have access to parent project variables (e.g ANDROID_PACKAGE_SOURCE_DIR)
example of *.pri:
#AnFeature/AnFeature.pri
HEADERS += $$PWD/anfeature.h
SOURCES += $$PWD/anfeature.cpp
INCLUDEPATH += $$PWD
OTHER_FILES += $$PWD/android/src/example/anfeature/anfeature.java
QMAKE_POST_LINK += $$QMAKE_COPY_DIR $$shell_path($$PWD/android/src/example/anfeature) $$shell_path($$ANDROID_PACKAGE_SOURCE_DIR/src/example/anfeature)
Looking how to implement that kind of stuff via CMake 3.19 or newer

HEADERS and SOURCES are just variables, so you can use target_sources. You can even set the source property on targets using set_property.
For INCLUDEPATH, you can use target_include_directories. Or simple specifying your library dependency after finding it with find_package using target_link_libraries
For OTHER_FILES, you can just again set a cmake variable if you really need to.
For running custom commands, like QMAKE_POST_LINK does, you can use add_custom_command.

Related

How do I stop Qt Creator placing my executable in a "debug" subdirectory?

I'm building a project in Qt Creator, and while I don't care where the intermediate .obj files go it's important that the final executable be put in (and run from) a particular directory where the many dependency DLLs etc. are to be found.
So, in Qt Creator, I select the 'Shadow Build' option and specify the path to this directory.
What I always find, however, is that instead of being put into this directory, the final executable is always placed into
the_Directory_I_Actually_Want/debug
... which is no use to me because, when I then try to run or debug the program from within Qt Creator, it won't start because the DLLs that it depends on are all in the_Directory_I_Actually_Want and not in the /debug subdirectory.
I've tried setting DESTDIR within my .pro file to the the_Directory_I_Actually_Want, and I've tried setting TARGET within my .pro file to the_Directory_I_Actually_Want/projectName, and I've tried faddling around with the various options that are part of the 'kit' configuration, and nothing seems to let me have any control over this.
Is there a way of doing this, or am I going to have to change the rest of my build system around just for Qt Creator's benefit?
Three years later...
Just use:
CONFIG -= \
copy_dir_files \
debug_and_release \
debug_and_release_target
On Windows you can use DLLDESTDIR variable which specifies where to copy the target dll or exe. Just add this to your .pro :
CONFIG(release, debug|release): DLLDESTDIR += $$PWD/../exec
On Linux you can use QMAKE_POST_LINK variable which contains the command to execute after linking the TARGET together. So it is like:
CONFIG(release, debug|release): QMAKE_POST_LINK += $$quote(cp project $$PWD/../exec)
Here project is the name of the target file which you provide by TARGET = project
These will copy the executable binary to a directory named exec one level upper than the program working directory. You can have your arbitrary path.

Qt project file: Add libs depending on Kit

I have a QT project which runs on x86 linux and ARM linux embedded (yocto).
For each platform I defined a Kit within QtCreator referencing the appropriate compiler etc.
Now I want to add LIBS to my pro file, but I got different libraries on each platform. I didn't find a way to specify the LIBS-directive dependant on the compiling Kit.
I search something like:
if (Kit == "Desktop")
LIBS += ...
if (Kit == "Embedded Yocto")
LIBS += ...
How to achieve this?
Thank you in advance
I scraped together a solution to the OP's problem by using the answer suggested by #vsz in the comments found here.
I have two kits, one for the local Desktop and one for the Target_ARM device, and I wanted a way to easily build for both of those without having to specially modify the .pro file or anything else. I followed the linked answer and added the following:
In my Desktop kit (for both Debug and Release), I added CONFIG+=Desktop as an additional qmake argument in the qmake build step.
For the Target_ARM kit, I added CONFIG+=Target_ARM in the same spot.
Now, this is where things shifted from the linked answer to the OP's problem. I didn't simply want #defineed variables in my code, I wanted to alter the behavior of qmake based on the selected kit. I don't know if the CONFIG built-in test function supports block designations or not (ie, CONFIG { _several lines here_ }), but it turned out I could copy and paste the CONFIG test function in front of each line that I wanted to be conditional; in fact I could string multiple CONFIGs together, like this:
CONFIG(Desktop, Desktop|Target_ARM):unix:!macx:CONFIG(debug, debug|release): LIBS += /path/to/Desktop/debug/lib
else:CONFIG(Desktop, Desktop|Target_ARM):unix:!macx:CONFIG(release, debug|release): LIBS += /path/to/Destop/release/lib
As it suggests, the above statement will run qmake with the appropriate LIBS path depending on which kit and configuration I have selected. Desktop->debug will generate a Makefile with /path/to/Desktop/debug/lib in it whereas Desktop->release will generate a Makefile with /path/to/Desktop/release/lib. I have similar statements for the Target_ARM kit. Below is an example of selecting the correct INCLUDEPATH: Both tests will evaluate to true when Target_ARM->release is selected.
CONFIG(Target_ARM, Desktop|Target_ARM):CONFIG(release, debug|release): INCLUDEPATH += /include/path/for/Target_ARM/release
In all, I used this method to modify LIBS, INCLUDEPATH, DEPENDPATH, and PRE_TARGETDEPS. I have 4 possible configurations of include paths and libraries depending on which kit I select (Desktop or Target_ARM) and which build configuration I select (build or release). Once this is set up, there is no need to modify the .pro file, simply pick your kit, your build configuration, run qmake, then rebuild.
I don't know off the top of my head where the CONFIG+=Desktop (for example) data is stored, but I would guess in the .pro.user file. So if somebody pulls your .pro file from a repo, they may have to initially configure the project in this manner at least once, but not afterwards (as long as the .pro.user file persists). QT should really have an easy mechanism for doing this front-and-center, especially since one of their selling points is multiple-platform integration. If there's a better way of doing this, I haven't seen it on SO or in the QT documentation yet.
You have all qmake variables here: qt-project.org/doc/qt-4.8/qmake-function-reference.html
You can define a variable
KIT = Desktop
#KIT = EmbeddedYocto
And use contains function
contains( KIT, Desktop ) {
LIBS += ...
}
contains( KIT, EmbeddedYocto ) {
LIBS += ...
}
MY_QT_INSTALL_PREFIX=$$[QT_INSTALL_PREFIX]
equals(MY_QT_INSTALL_PREFIX,"C:/Qt/Qt5.3.1/5.3/msvc2010_opengl"){
message($$[QT_INSTALL_PREFIX])
}

Can I get qmake -project to add LIBS += .... to my .pro file?

I have a project that uses Qt. So I have "qmake" make my Makefile from the .pro file. But Qmake can also make that .pro file: qmake -project . This worked until I needed to add an external extra library to my project.
I get lots of hits on google that tell me to add LIBS += ... to my project file, but I want to tell qmake -project something that causes it to add it for me. In effect of course I'll be doing it myself, but I don't think that it's proper that I am editing the generated project file.
If for example I add files to the project directory, I'll have to recreate it and add in the library again, or I'll have to manually add the files to the (almost completely computer-generated) project file. I'm now using a script to auto-generate the project file, and then add in the LIBS += directive, but is there a proper way to do this?
When you are developing without the Qt Creator IDE, unless the IDE includes by itself some automatic utilities, you must edit manually the .pro configuration file.
The generated .pro file is a skeleton file which YOU must fill in with the libraries that you need, then the qmake system figures out the other dependencies and compiles your project. It is a essentially a much better version of pkg-config of gtk + Makefiles.
When you add more source and resource files to your project then manually you must add them to the .pro file.
Example:
QT += core gui
TARGET = qtcp1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
miwidget1.cpp \
lcdrange.cpp
HEADERS += mainwindow.h \
miwidget1.h \
lcdrange.h
FORMS += mainwindow.u
Alternately, you can issue qmake -project over and over again, but this can cause some unforseen accidents, since it includes everything that is in the current directory at the time, including the pre-processed files for conversion to standard C++ from QT dialect. This intermediate files must be erased (cleaned), before the remaking the project and the next make or can lead to tricky problems.
Using the official and free QT Creator IDE takes away most of this burden by adding automatically the new data to the .pro file and cleaning loose ends. Some other IDEs like Code::BLocks and Codelite provide some facilities for QT, but not to the level of QT creator. Some prefer to edit the .pro themselves for custom reasons, other like more other styles of IDEs, like Eclipse.
You should test the waters and decide by yourself what fits best to your needs.
ReEdited, to clarify a few things.

Is it possible to add a dependency to your Makefile in qmake?

Currently, I have a file which contains some version tagging information which is used by the .pro file and parsed by qmake, but the problem is that when that file changes qmake is not re-run.
Is it possible to add something to the .pro file so that qmake will treat this file as a dependency for the Makefile?
Sometheing like this should work:
depend_on_file.target = depend_on_file
depend_on_file.depends = path_to_your_txt_file
depend_on_file.CONFIG += recursive
QMAKE_EXTRA_TARGETS += depend_on_file
PRE_TARGETDEPS += depend_on_file
Rebuild will be triggered if file path_to_your_txt_file changes but keep in mind that if your qmake script that parses the file changes DEFINES variable then you out of luck. Defines are not listed in dependencies, you see. If defines are changed you have to do full rebuild by hand.

The right way to use a custom library in Qt?

I have a custom library to be included in Qt. Currently, it is included by adding the followings in .pro file.
INCLUDEPATH += ...
DEPENDPATH += ...
LIB += ...
Since the library is used in most of my projects. I am wondering that if there is some way to make my custom library to be INTEGRATED with the QtSDK? Maybe it can be included by syntax like the build-in components
QT += my_custom_library
and everything (header file include path, lib file include, etc...) will be done.
Is the integration possible?
You are probably looking for to create a mkspec file which then used with CONFIG option. So create a file to $QT_HOME/mkspec/features/mycoollib.prf that contains your instructions. There should be plenty of examples in that directory. The name of the file declares it's usage, so the file mycoollib.prf would be used like:
CONFIG += mycoollib

Resources