Friday, June 20, 2008

Automating Qt compilation with MSBuild scripts

Qt introduces some special mechanisms to introduce some kind of "reflection" and a similar concept to events/delegates in C#, called Signals and Slots in Qt. This is very useful indeed, but when developing under windows with Visual Studio, the compilation process becomes a bit cumbersome:

1a) create *.pro file, using 'qmake -project'
1b) derive a makefile from the *.pro, using 'qmake -makefile'
2) generate *.vcproj (to work in Visual Studio)
3) compile *.vcproj

This can be perfectly automated using MSBuild scripts (*.proj files). With MSBuild scripts you can do anything that you can do with *.bat files, and a whole lot more. They are typically used to automate compilation (and 'Continuous Integration') of .Net projects, but they can be perfectly abused for C++/Qt stuff too! :-)

Prequisites:
- Install .Net 2.0 SDK (it's needed for executing VCBuild tasks in MSBuild files)
- Append 'C:\Program Files\Microsoft Visual Studio 9.0\VC\vcpackages' to your Path Environment Variable.
- 'Qt-ready' environment

We put Steps 1a, 1b, and 2 in a 'compile_qt.proj' MSBuild script:


We put Step 3 in a separated 'compile_vc.proj' MSBuild script (it MUST be in a separated MSBuild script, because the vcproj files are recognized danymically with msbuilds 'ItemGroup' mechanism, which is always invoked at the startup of the script, hence only working in our case properly if we start this script AFTER steps 1 and 2):


It would be possible to call compile_vc.proj directly from within compile_qt.proj, but somehow I find it more convenient to have a spearate third 'compile.proj' that calls all other *.proj (many other custom build steps can be added later, and clearly separated in different *.proj files for easy maintenance):


Now the whole conversion/compilation process can be run with one single command:

C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe compile.proj

I recommend to define C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe as the default application to run *.proj files, then you only need to double-click compile.proj and that's it.

If run with the 'just clicking the *.proj', then if there is an error in the conversion/compilation process, the shell closes immediately after the error was displayed, making it impossible to investigate the problem. The simplest solution to that is to have a little 'compile.bat' file:

C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe compile.proj @pause
...and to just click compile.bat instead of compile.proj when facing errors.

Important note: All *.proj and *.bat files in this article must be located in the root directory of your Qt project.

PS: Sorry for the pictures... ...can anybody tell me how to add ordinary 'code blocks' with the blogspot engine?!

No comments: