We are developing a application with Qt and in the server we have genkin server for auto build and auto tests.
We want to have a Coding-Convention Test tool like vera++. We have no experience in vera++ or other tools but all we want is to make vera++ process every files of the Qt project (.pro). Also, is it possible to integrate this tool in QtCreator?
I couldn't find anything in Internet.
Thanks.
Solution
I have integrated vera++ in a composite qmake project by adding the following lines in the file .qmake.conf in the root directory of the project:
defineReplace(absolute_paths) {
result =
for(path, ARGS) {
result += $$absolute_path($$path, $$_PRO_FILE_PWD_)
}
return($$result)
}
equals(TEMPLATE, subdirs): prepareRecursiveTarget(vera++)
else {
# We prefix the report file name with the project file base name to prevent name collisions.
VERA_TARGET = $$basename(_PRO_FILE_).vera++.xml
vera++.commands = vera++ --checkstyle-report $$VERA_TARGET --show-rule $$absolute_paths($$HEADERS) $$absolute_paths($$SOURCES)
QMAKE_CLEAN += $$VERA_TARGET
}
QMAKE_EXTRA_TARGETS += vera++
This adds a recursive make target vera++ that processes all the header and source files in each of the non-subdirs sub-projects.
To generate the XML reports, call the following commands:
qmake
make qmake_all
make vera++
Additional information
.qmake.conf is automatically included in all the .pro files in all subdirectories.
qmake
defineReplace()
prepareRecursiveTarget()
vera++
Related
I have project in QT Creator (as .pro file, not as CMake). Now I want to add tests (gtest) as another build configuration, so I've added build configuration "debug_tests" (Projects -> Add -> Debug with name debug_tests).
Now I want to add specific main file for "debug_tests" and exclude from build my normal main file when the build configuration is choosen.
So I've tried:
test_debug:{
message("Running tests:")
SOURCES += \
tests/MultiCellArticleModelTests.cpp \
tests/main_tests.cpp
LIBS += \
-lgtest
}
But it is not working, it is working when I do like that:
!debug_and_release:{
message("Running tests:")
SOURCES += \
tests/MultiCellArticleModelTests.cpp \
tests/main_tests.cpp
LIBS += \
-lgtest
}
But it is not good solution if I want to add more configurations.
Another solution which I see is to just add defines to compilation and have ifdef in my code.
Usually, tests are in their own sub-projects rather than part of some scope in your main source tree.
This allows you to keep them cleanly separated from your main code and you can run them easily. You can even call make check to run the whole test suite that you created for your library/application.
I made a QT GUI project in VS 2019. It ran perfectly through VS. Copied the entire project files directory to Linux partition. Ran qmake -project in the directory containing the 'test2.sln' file to create a 'test2.pro' file. Opened the '*.pro' file through QT Creator. It imported everything fine. But, when building it it errors out saying "ui_test2.h file not found".
The test2.pro contains:
TEMPLATE = app
TARGET = test2
INCLUDEPATH += .
DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += test2/test2.h test2/x64/Release/uic/ui_test2.h
FORMS += test2/test2.ui
SOURCES += test2/main.cpp test2/test2.cpp test2/x64/Release/rcc/qrc_test2.cpp
RESOURCES += test2/test2.qrc
What should I do to fix this?
1) remove folder with build (rm -r nameOfFolder)
2) rename your UI file (test2.ui -> mainwindow.ui) for example
3) check out name of own class and name of qt class in UI file (you can open it via vim or nano)
After that try to rebuild your project!
I hope you'll have done this with good results!
I know it is a topic already touched by a lot of user, but I don't find a valid solution; I have two form files created with QtDesigner:
interfaccia_test.ui
interfaccia.ui
I launch qmake -project command obtaing the following .pro file:
TEMPLATE = app
TARGET = qtgeo
INCLUDEPATH += . include
# Input
HEADERS += interfaccia.h include/localizzazione.hpp
FORMS += interfaccia.ui interfaccia_test.ui
SOURCES += interfaccia.cpp main.cpp src/localizzazione.cpp
src/SimpleSIFT.cpp
so i give qmake command but it doesn't generate ui_interfaccia.h and ui_interfaccia_test.h like I expected; then I try to make my project and I have
interfaccia.h:19:28: fatal error: ui_interfaccia.h: File o directory not found
#include "ui_interfaccia.h"
^
with interfaccia.h my file that use the GUI I made.
For any given project, you should ever only use qmake -project once. It's meant as a starting point if you have a bunch of files and want to get a project template. This template is then meant to be modified by a human being - you.
The normal way to build a qmake-based Qt project would be:
qmake
make
The ui_xxx.h files are generated by make, not qmake. Here's a list of what the various tools do:
qmake -project Generates a .pro file template for you to modify to suit the project. This should never be used by your end users, or by you after the project is going. It's your job to keep the .pro file up-to-date.
qmake or cmake Generates the makefile for the build system.
make or ninja Builds the project, generating all the other files.
There are two additional points:
The qmake won't generate the ui_xxx.h file if you've included a file that wouldn't be generated. So, for example, if it'd generate a file called ui_Interfaccia.h, but you've included ui_interfaccia.h, then the file with the wrong name nor the file with the correct name get generated.
This matters even if you're building everything on a case-insensitive OS/filesystem.
You're including the file with a wrong name. The correct name is ui_ClassName.h, where ClassName is the name of the class from the .ui file (look at the first few lines), with the same capitalization.
I have a qmake batch file which uses a .pri and .pro to create a visual studio C++ project which is used to create a dll. But I would like to setup the properties of this project automatically, particularly the debugging command and command line arguments is this possible in qmake?
It is possible, create a
add_qt_path.pri
file somewhere with the following contents:
# test if windows
win32 {
# test if already exists
VCXPROJ_USER_FILE = "$${OUT_PWD}/$${TARGET}.vcxproj.user"
!exists( $${VCXPROJ_USER_FILE}) {
# generate file contents
TEMPNAME = $${QMAKE_QMAKE} # contains full dir of qmake used
QTDIR = $$dirname(TEMPNAME) # gets only the path
# vcxproj.user template
VCXPROJ_USER = "<?xml version=\"1.0\" encoding=\"utf-8\"?>$$escape_expand(\\n)\
<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">$$escape_expand(\\n)\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">$$escape_expand(\\n)\
<LocalDebuggerEnvironment>PATH=$${QTDIR};%PATH%</LocalDebuggerEnvironment>$$escape_expand(\\n)\
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>$$escape_expand(\\n)\
</PropertyGroup>$$escape_expand(\\n)\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">$$escape_expand(\\n)\
<LocalDebuggerEnvironment>PATH=$${QTDIR};%PATH%</LocalDebuggerEnvironment>$$escape_expand(\\n)\
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>$$escape_expand(\\n)\
</PropertyGroup>$$escape_expand(\\n)\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">$$escape_expand(\\n)\
<LocalDebuggerEnvironment>PATH=$${QTDIR};%PATH%</LocalDebuggerEnvironment>$$escape_expand(\\n)\
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>$$escape_expand(\\n)\
</PropertyGroup>$$escape_expand(\\n)\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">$$escape_expand(\\n)\
<LocalDebuggerEnvironment>PATH=$${QTDIR};%PATH%</LocalDebuggerEnvironment>$$escape_expand(\\n)\
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>$$escape_expand(\\n)\
</PropertyGroup>$$escape_expand(\\n)\
</Project>$$escape_expand(\\n)\
"
# write file
write_file($${VCXPROJ_USER_FILE}, VCXPROJ_USER)
}
}
then include it into your qmake project (*.pro) file, after the TARGET deifnition:
QT += core
QT -= gui
TARGET = test3
CONFIG += console
CONFIG -= app_bundle
include(./../../add_qt_path.pri) # add qt path to vs project
# other qmake stuff
You can also add to the *.vcxproj.user any other entries such as debugging command and command line arguments, just take a look on how Visual Studio auto generates the in the *.vcxproj.user file when you set them up manually.
Most of the build environment properties can be setup through qmake options (you can find them in qmake sources e.g. *_objectmodel.* files ). Unfortunately, both options you need are in fact runtime options so I don't think you'll be able to set them in the .pri/.pro files. Afaik, they're not even stored in the .vcxproj file but in the .vcxproj.user file. If it was not for this then modifying qmake could have been an option, even though it wouldn't probably worth the effort.
I am putting together a build system for my Qt app using a qmake .pro file that uses the 'subdirs' template. This works fine, and allows me to specify the order that each target is built, so dependencies work nicely. However, I have now added a tool to the project that generates a version number (containing the build date, SVN revision, etc,) that is used by the main app - I can build this version tool first but when it is built I want to execute it before any more targets are built (it generates a header file containing the version number that the main app includes.)
For example, my simple qmake file looks like something this:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = version \
lib \
tests \
mainapp
When 'version' is built I want to execute it (passing some arguments on the command-line) before 'lib' is built.
Does anyone know if this is possible? I see that qmake has a 'system' command that can execute apps, but I don't know how I could leverage this.
A related question concerns my unit tests. These live in the 'test' project and use the QTest framework. I want to execute the tests exe before building 'mainapp' and if the tests fail (i.e. the tests exe doesn't return zero) I want to quit the build process.
I realise that qmake is designed to generate makefiles, so I may be wishing for a little too much here but if anyone can give me some pointers it would be very welcome.
I currently use qmake to exec my unit tests automatically for two years - and it works fine.
Have a look here - I made a mini-howto for that:
Qt: Automated Unit Tests with QMAKE
Abridged summary:
Structure
/myproject/
myproject.h
myproject.cpp
main.cpp
myproject.pro
/myproject/tests/
MyUnitTest.h
MyUnitTest.cpp
main.cpp
tests.pro
Using QMake to automatically run unit tests on build
The QMake target QMAKE_POST_LINK will run a user defined command after linking.
tests.pri (common file)
TEMPLATE = app
DEPENDPATH += . ../
INCLUDEPATH += . ../
DESTDIR = ./
CONFIG += qtestlib
unix:QMAKE_POST_LINK=./$$TARGET
win32:QMAKE_POST_LINK=$${TARGET}.exe
tests.pro (project-specific file)
TARGET = MyUnitTest
HEADERS += MyUnitTest.h
SOURCES += MyUnitTest.cpp main.cpp
include(tests.pri)
Running multiple unit tests in a single main()
main.cpp
#include "MyUnitTest1.h"
#include "MyUnitTest2.h"
int main(int argc, char** argv) {
QApplication app(argc, argv);
int retval(0);
retval +=QTest::qExec(&MyTest1(), argc, argv);
retval +=QTest::qExec(&MyTest2(), argc, argv);
return (retval ? 1 : 0);
}
This runs your tests on each build and aborts if an error is found.
Note
If you get linker errors such as "LNK2005: xxx already defined...", add a new .cpp file for each test class header and move some test method implementations.
You can use exactly that mechanism to exec your versioning tool after compile/building - so your questions should be solved :-)
If you have any further questions don't hesitate to ask me.
PS: Here you can find more (undocumented) tricks around QMake: Undocumented QMake
I posted a message on the Qt Interest mailing list about a 'pre build' step and it can be done using a combination of PRE_TARGETDEPS and QMAKE_EXTRA_TARGETS. Here is the response:
You can specify custom build steps,
eg. this would call makemyversion.sh
to create myversion.cpp every time
before it builds something:
versiontarget.target = myversion.cpp
versiontarget.commands = ./makemyversion.sh
versiontarget.depends = FORCE
PRE_TARGETDEPS += myversion.cpp
QMAKE_EXTRA_TARGETS += versiontarget
I am now using something similar to this to generate my app's version number each time it is built.
I've tried to do a lot of stuff with qmake as a build system over the years. Eventually I just resorted to having a pre-qmake step. Ie. a configure script.
You can build your version tool in there and then execute it before calling qmake to generate the Makefiles.
I found the easiest way to get data into the pro files, if you need that too, is to generate a .pro.inc file and include it from your main pro.
As 3DH mentioned, you want a QMAKE_POST_LINK option specified in your .pro files that contain something you want executed. So for your example, I would do something like this with the version.pro file:
TEMPLATE = app
TARGET = version
HEADERS = version.h
SOURCES = version.cpp
QMAKE_POST_LINK=./version
Something similar should work with your test directory.