Getting qmake to run my code generator correctly

Here is my situation:

I have a typical Qt C ++ project that I create with a qmake (.pro file).

I also have a python script that generates some code. The Python script (call it update.py) generates three files, let them name gen.h, gen.cpp, gen.qml.

Now, what I'm doing now is manually updating.py executing to generate these files. Then I can run make, and everything builds OK. Gen.h and gen.cpp are just β€œregular” files in my .pro file, and they are checked in SVN.

Instead, I want to make sure that when make is launched, update.py will start and generate these files, and then they will be built with the project. This way I can remove them from SVN and avoid the extra manual step.

FYI: I already have update.py setting to update these files only when necessary, therefore, if you run update.py several times, it will not blindly change gen.h, gen.cpp, etc.

I spent a huge amount of time trying to get this job (seemingly embarrassing in fact). I was messing around with QMAKE_EXTRA_TARGETS, QMAKE_EXTRA_COMPILERS, PRE_TARGETDEPS, etc., but nothing of the kind works the way I want.

Oh, another piece of information: gen.cpp and gen.h have QObject-based classes, so I need to generate them before running MOC.

Thanks!

+4
source share
1 answer

If I understood, this would fit your needs:

mytarget.target = .buildfile mytarget.commands = ./update.py QMAKE_EXTRA_TARGETS += mytarget PRE_TARGETDEPS += .buildfile 

with the first statement, you define a new Makefile target called "buildfile", with the second you define what your buildfile target does (it calls update.py, which generates the code) with the third you define mytarget as the new qmake target and the last one you add the buildfile target for qmake target list.

Oh, I forgot. I found this in the qmake manual: http://qt-project.org/doc/qt-4.8/qmake-environment-reference.html in this section. Maybe this is helpful;)

And for moc you can define them:

 new_moc.output = moc_${QMAKE_FILE_BASE}.cpp new_moc.commands = moc ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT} new_moc.depend_command = g++ -E -M ${QMAKE_FILE_NAME} | sed "s,^.*: ,," new_moc.input = NEW_HEADERS QMAKE_EXTRA_COMPILERS += new_moc 

I did some tests. And this works with me:

 //.pro file: QT += core gui TARGET = test TEMPLATE = app SOURCES += main.cpp \ widget.cpp HEADERS += widget.h FORMS += widget.ui mytarget.target = .buildfile mytarget.commands = ./update.py QMAKE_EXTRA_TARGETS += mytarget PRE_TARGETDEPS += .buildfile new_moc.target = .mymoc new_moc.output = moc_widget.cpp new_moc.commands = moc widget.cpp -o moc_widget.o new_moc.depend_command = g++ -E -M widget | sed "s,^.*: ,," new_moc.input = moc_widget.h QMAKE_EXTRA_COMPILERS += new_moc //update.py: #!c:/Python/python.exe -u fd=open("widget.h",'w') fd.write("#ifndef WIDGET_H\n") fd.write("#define WIDGET_H\n") fd.write("#include <QWidget>\n") fd.write("namespace Ui {\n") fd.write("class Widget;\n") fd.write("}\n") fd.write("class Widget : public QWidget\n") fd.write("{\n") fd.write(" Q_OBJECT\n") fd.write("public:\n") fd.write(" explicit Widget(QWidget *parent = 0);\n") fd.write(" ~Widget();\n") fd.write("private:\n") fd.write(" Ui::Widget *ui;\n") fd.write("};\n") fd.write("#endif // WIDGET_H\n") fd.close() fd=open("widget.cpp",'w') fd.write("#include \"widget.h\"\n") fd.write("#include \"ui_widget.h\"\n") fd.write("Widget::Widget(QWidget *parent) :\n") fd.write(" QWidget(parent),\n") fd.write(" ui(new Ui::Widget)\n") fd.write("{\n") fd.write(" ui->setupUi(this);\n") fd.write("}\n") fd.write("\n") fd.write("Widget::~Widget()\n") fd.write("{\n") fd.write(" delete ui;\n") fd.write("}\n") fd.close() 
+3
source

Source: https://habr.com/ru/post/1439225/


All Articles