PDA

View Full Version : Generated code and lupdate...



chenz
28th October 2009, 12:48
Hi,

I have implemented code generation in qmake as shown here: http://wiki.qtcentre.org/index.php?title=Undocumented_qmake

Now I want to have translateable strings in the generated C++ source files. The only way this works right now is to manually specify all (generated) source files (as well as .ts files) on the command line when calling lupdate. Also I have to make sure to build the project right before calling lupdate... (otherwise, the generated sources might not exist, or might be out of date)...

I would like to change this so that l can just call "lupdate test.pro" and it will automatically figure out the generated source files and build them if needed...

Any ideas on how to do this?


Example code, using "cp" as a code generator :-)

test.pro:

SOURCES=main.cpp
TRANSLATIONS=test_de.ts

GEN_SOURCES = generated.gen
gen.input = GEN_SOURCES
gen.output = ${QMAKE_FILE_BASE}.cpp
gen.commands = cp ${QMAKE_FILE_NAME} ${QMAKE_FILE_OUT}
gen.variable_out = SOURCES
QMAKE_EXTRA_COMPILERS += gen


generated.gen:

#include <QCoreApplication>
#include <iostream>

void helloWorld()
{
std::cout << qPrintable( QCoreApplication::translate( "fooBar", "Hello world!" ) ) << std::endl;
}

main.cpp:

#include <QCoreApplication>

void helloWorld();

int main( int argc, char** argv )
{
QCoreApplication app( argc, argv );
helloWorld();
return 0;
}

chenz
29th October 2009, 08:15
I have changed the project file to make sure SOURCES always contains the generated files:

test.pro:

SOURCES = main.cpp
TRANSLATIONS=test_de.ts

GEN_SOURCES = generated.cpp
include( gen.pri )


gen.pri:

SOURCES += $$GEN_SOURCES
for( src, GEN_SOURCES ): GEN_INPUT += $$replace( src, .cpp, .gen )

gen.input = GEN_INPUT
gen.output = ${QMAKE_FILE_BASE}.cpp
gen.commands = cp ${QMAKE_FILE_NAME} ${QMAKE_FILE_OUT}
gen.variable_out =
QMAKE_EXTRA_COMPILERS += gen


Now when I invoke "lupdate test.pro" after the project was built, it works fine.

The remaining problem is that it will fail silently if the project was not built and "generated.cpp" does not exist (not even a warning)!
This is a problem to me since I might accidentally damage my translation files without even noticing if I forget to build the project before calling lupdate...