Thursday 6 June 2013

Compiling x64 PySide using VS2010, Python 2.7.4 and Qt.4.8.4 for Windows

I am for the most part a 3D generalist and TD by profession. I enjoy doing tool development, but I try to avoid low level (i.e. C++) programming as much as possible in order to avoid the time consuming process of coding, compiling, restarting the software...ad infinitum.
This is why I use Python as much as possible, and this is also the reason I want to replace the .Net/win32-based menu renderer of our Qmenu for Softimage plugin with one based on Qt, and hence PySide. Coincidentally, there are a couple of precompiled packages of various flavors of PySide downloadable from several official and unofficial sites (like http://qt-project.org/wiki/PySide_Binaries_Windows, http://www.lfd.uci.edu/~gohlke/pythonlibs/ ).
However, due to a bug in the build scripts that has been fixed only recently, to this date none of them contains the shiboken Python module, which is the equivalent of the SIP module of PyQt, (which is for example also needed to get Steven Caron's PyQt for Softimage plugin up and running with PySide).
Even more suprisingly, none of these available packages has been compiled with Visual Studio 2010!
The latter is of importance since most major 3D packages specify VS2010 as the designated compiler for plugin development, including Softimage 2014 and Maya 2014, and mixing compilers is generally not advisable, if not prohibitive in most cases.

Note: The following guidelines are not meant to aide in compiling PySide for Maya, which requires a special version of Qt from Autodesk. The whole procedure has been discussed here by Cyrille Fauvel, in case this is what you are looking for.

Long story short: If you need x64 PySide compiled with the Microsoft VS2010 compiler against your preferred version of Python using the (presumably) latest versions of Qt including the shiboken.pyd Python module, you must build it yourself.

 The catch is that there are a couple of sites telling you how to do it, but none of them tells you the whole story. Most assume a certain existing setup or knowledge of the process, or employ a semi-automatic build- and install procedure that won't let you choose specific versions of the required packages (esp. Qt).
It took me several days to figure out the whole process, and I ended up using most of the information from the following two sites. Thanks and credits go to the individual creators and maintainers of these pages:

Building PySide:
https://pypi.python.org/pypi/PySide#installing-prerequisities

Building Qt:
http://www.holoborodko.com/pavel/2011/02/01/how-to-compile-qt-4-7-with-visual-studio-2010/

The following is a summary of those pages including explanations and experiences from my own build attempts. The build process essentially consists of two parts: compiling Qt and compiling PySide.

Compiling of Qt is, unfortunately, also required since there is no publicly available x64 binary version available anywhere that was built using VS2010, not even from here.
Depending on your own working speed and that of your computer (a fast raid or SSD really speeds things up) this can take half a day to a day, including setting up the required tools and the actual build times.

Prerequisites

It is assumed that you have Visual Studio 2010 already installed and that you know how to set up and change environment variables.
Python (ideally the version you want to compile PySide (in my case this was the installer version 2.7.4 for x64 from http://www.python.org/download/releases/2.7.4/) needs to be installed too.
Make sure that the Python installation path is registered with your PATH environment variable, and in case you have multiple versions of Python installed, that the path to the version you want to use is listed before any other Python folder in the PATH variable.

Note: PySide requires Python 2.6 or later and Qt 4.6 or later.

Building Qt

Install Perl if you are going to compile Qt version 4.8.0 or higher from
http://www.perl.org/get.html
That's right, you need to install yet another scripting language to compile Qt!
Whoever made the build scripts thought it was a smart idea to implement them using Perl :-/

Visual C++ 2010 contains all necessary SDKs for Qt compilation. However if you plan to use Qt with Phonon you need to install the DirectX SDK. The June 2010 version is the last official standalone DX SDK.

Download your desired version of Qt (as of this time of writing the latest 4.8.x version is 4.8.4) from
http://download.qt-project.org/official_releases/qt/4.8/4.8.4/qt-everywhere-opensource-src-4.8.4.zip

Download and extract the archive, then copy contents of the folder qt-everywhere-opensource-src-4.8.4 of this archive to the directory where you intend to install Qt. In my case this was C:\Qt\4.8.4.
That is: The folder C:\Qt\4.8.4 should now contain files and folders like \bin, \doc, configure.exe etc., not the "qt-everywhere-opensource-src-4.8.4" folder itself!

Set up environment variables. In case you chose different folders or software versions you might need to adjust the following lines accordingly. Add the following variables:

QTDIR=C:\Qt\4.8.4
QMAKESPEC=win32-msvc2010
Update the PATH variable to include %QTDIR%\bin

Download the latest version of jom from http://qt-project.org/wiki/jom
It will accelerate the build process by using all available cores at build time, more on this further down.
Extract jom files to C:\Qt\jom

Start Visual Studio 2010 x64 Command Prompt as an administrator. 
On Win7 this can be done by right-clicking Start -> Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools  -> Visual Studio x64 Win64 Command Prompt (2010) and choosing "Run as Administrator" from the popup menu.
On Win8 you can reach the shortcut by typing "Visual Studio" to get a list of app and shortcuts known to the system, it should list this particular shortcut to. Right-click it and choose to run as administrator from the bottom ribbon.

Run the following commands in it (every line is a different command. Type it, then press Enter):

>> cd c:\Qt\4.8.4

To compile both debug and release versions type or copy/paste:

C:\Qt\4.8.4>configure -debug-and-release -opensource -platform win32-msvc2010

I chose to only compile the release version like so:

C:\Qt\4.8.4>configure -release -opensource -platform win32-msvc2010 

Note: Don't get irritated by the -platform flag reading "win32-msvc2010". It simply means that the MS Visual Studio compiler will be used and does not specify the architecture (x64 or x32, which was in fact already chosen by starting the Visual Studio x64 command prompt above). In other words:  "-platform" is always "win32-msvc2010" for us in the context of this example, even for win64 builds.

This will take a few minutes, depending on your hardware. When it's done it should report back by printing:
"Qt is now configured for building. Just run nmake.
To reconfigure, run nmake confclean and configure."

Build Qt using jom
Jom is an nmake replacement for Qt compilation on multi-core CPUs. Its parameter -j N allows to setup the number of parallel processes for compilation. Larger is better, where N is the number of CPU cores you want to utilize for Qt compilation. The number of physical CPU cores is a good choice for N, e.g. 4 on a typical quad-core CPU.
Usually compilation takes about 10 hours on a single core, with jom and an SSD drive it took only a bit more than 1 hour on my machine (Windows 8, 64bits)


Still in the same command prompt, enter and execute the following:

C:\Qt\4.8.4>..\jom\jom.exe -j 4

You might want to use a different number in the end if you have a system with more or less physical CPU cores. 

Building PySide

Install Cmake

Install OpenSSL (optional)
This is optional, PySide will compile and run fine without it. Since I only intend to use the GUI libraries of Qt I skipped installing it.

Install latest "distribute" distribution into the folder where Python is installed.by downloading the distribute_setup.py script from  http://python-distribute.org/distribute_setup.py
Run it using the python interpreter using the command prompt we have still open from building Qt, or open a new one.
C:\>C:\Python27\python distribute_setup.py
It should print:
Building PySide distribution

Install the PySide setup scripts.
in case you have Git installed, you can clone the PySide setup scripts from git repository into a folder of your choice, e.g.  c:\pyside-setup like so:
C:\>git clone https://github.com/PySide/pyside-setup.git pyside-setup

Alternatively you can download them from https://github.com/PySide/pyside-setup/archive/master.zip
and uncompress the archive to a folder of your choice, e.g. c:\pyside-setup


Switch to the pyside-setup directory:
c:\> cd pyside-setup

Build the PySide windows installer:
C:\pyside-setup>C:\Python27\python.exe setup.py bdist_wininst --msvc-version=10.0 --qmake=c:\Qt\4.8.4\bin\qmake.exe

In case you have OpenSSL installed, the line can be appended with  "--openssl=c:\OpenSSL32bit\bin" , which gives us:
C:\pyside-setup>C:\Python27\python.exe setup.py bdist_wininst --msvc-version=10.0 --qmake=c:\Qt\4.8.4\bin\qmake.exe --openssl=c:\OpenSSL32bit\bin

This can take about an hour on modern hardware.

After the successful build, install the distribution with easy_install and run the post-install script:
C:\pyside-setup>C:\Python27\Scripts\easy_install dist\PySide-1.1.2.win32-py2.7.exe
C:\pyside-setup>C:\Python27\python.exe C:\Python27\Scripts\pyside_postinstall.py -install

After successfull install, the post-install script should print the following information to the console:


Generating file c:\Python27\qt.conf...
The PySide extensions were successfully installed.


This is it.

Cleanup (Optional)

Note: Do the following step after compiling PySide only, otherwise files required by the PySide build process will be deleted too!
After compilation the Qt folder is huge in size (approx. 3.5GB). One can safely shrink its size by deleting temporary files created during the process. Once compiled ,Qt doesn’t depend on these intermediate files – and they can be erased without affecting Qt functionality.

Run this from the command prompt:

>> cd  C:\Qt\jom
>> jom.exe clean

I wrote this article in hope that it is a useful time safer for others. In case it was useful to you, or if you found anything that's wrong, incomplete, misleading, needs additional workarounds, or if you know a more elegant solution, please feel free to leave a comment below.

6 comments:

  1. I have been looking for days, and this is the first clear set of instructions I found. Thank you, finally got the magic words "The PySide extensions were successfully installed" ! Thank you!

    ReplyDelete
  2. Hi Plutometro! Thanks a lot for your pretty and detailed steps on how to compile PySide!!!
    Is there a way to extend it to cover PyQt for Maya 2014?

    I'm asking this as PySide already comes compiled by Adesk in Maya 2014, but I am having a bad patch trying to figure out how to compile the PyQt sources from riverbank to be able to launch QT stuffs from inside Maya.
    Thanks a Ton for your help!

    Gerardo Verrone
    animatorstudio@hotmail.com

    ReplyDelete
  3. Hi Gerardo,

    Sorry, I have never looked into compiling PyQt for Maya, I'm pretty sure there are other sites out there that explain the process. Just out of curiosity: WHat's your motivation of using PyQt over PySide? Did you encounter any major problems/incompatibilities with PySide?

    Stefan

    ReplyDelete
    Replies
    1. Hi Stefean! I just found a pre-compiled version of PySide for Maya 2014 http://jason-parks.com/Downloads/PyQt4_Maya2014/

      I was looking to be able to run PyQt codes as I am following a course were they explain all by using PyQt.

      I am pretty sure in a near future I will do the twist to PySide as it is free and comes pre-installed. This is a really plus on the side of PySide.
      Just hope Adesk will keep pushing on the PySide development to prevent the closure of that project.

      cheers

      Delete
  4. Good to hear you found a binary.
    Why would PySide be discontinued? Isn't it actively being developed and used?
    Do you have any concrete information on this?

    ReplyDelete