Documentation/Labs/CMake-ified Python

From Slicer Wiki
Revision as of 13:23, 4 February 2013 by Pieper (talk | contribs)
Jump to: navigation, search
Home < Documentation < Labs < CMake-ified Python

Why

Within Slicer, we have been struggling to compile python on VS2008. Moving forward, we would like to also compile it on VS2010 and VS2012 ... 

Currently, we are building python 2.6.6. We would also like to move to a more recent version of python (2.7.x). 

It gonna very probably be a long and a painful process. [[ I am not even talking about building tkinter module allowing to bridge tcl and python ... ]]

I just found out that there is some work done regarding the CMake'ification of Python:

[1] https://github.com/davidsansome/python-cmake-buildsystem
[2]  http://www.cmake.org/Wiki/BuildingPythonWithCMake

Ideally, it would be great to contribute back the CMakeLists files. In the mean time, we could either help maintain the project [1]  or fork the python source code mirror available on github [3] and contribute to it.

[3] https://github.com/jonashaag/cpython
I did some experiment and the build system of "David Sansome" looks very promising. It allows a lot of flexibility in the way python can be built and embedded in existing application.

From the author:

This cmake-based buildsystem is better because:

  * It's much faster to compile: 7 seconds instead of 58 seconds in my
    unscientific test.
  * No compiled program for the target architecture is used in the build
    itself.  This makes cross-compiling possible.
  * Same project files for all platforms - there's no need to maintain the
    unix build separately from four different MSVC builds.


It also provided patches making cross-compilation very easy. (for example compilation for windows from Linux. See end of readme)

I think we should contribute and support that well documented build system :) 

In a nutshell, in addition to the option: 

 ENABLE_SHARED  "Build a shared libpython library"
 ENABLE_STATIC     "Build a static libpython library"


it also provides options like 

 BUILTIN_ARRAY, BUILTIN_AUDIOOP, ....,  BUILTIN_UNICODEDATA ,  BUILTIN_ZLIB
 allowing to selectively include python modules in either the static or shared python lib.

 ENABLE_ARRAY, ENABLE_AUDIOOP, ....,  ENABLE_UNICODEDATA ,  ENABLE_ZLIB
 allowing to specify with a given module should be built or not.


Let's also notice options like:

 EXTRA_PYTHONPATH  "A colon (:) separated list of extra paths to add to the PYTHONPATH"
 USE_LIB64   "Search for dependencies and install to prefix/lib64 instead of prefix/lib"
 USE_LIBEDIT  "Use libedit instead of readline"


Under the hood, it implements to CMake modules, that would probably make port of project like "numpy" easier ... 
  
  Assembler.cmake:  
  
    # CMake ignores .S files, so we have to add compile commands for them manually
    macro(add_assembler ADD_ASSEMBLER_OUTPUTVAR ADD_ASSEMBLER_FILE)

  
  Extensions.cmake
  
  # This function adds a python extension to the buildsystem.
#
# Usage:
#
# add_python_extension(
#     extension_name
#     SOURCES source1.c source2.c ...
#     [ REQUIRES variable1 variable2 ... ]
#     [ DEFINITIONS define1 define2 ... ]
#     [ LIBRARIES lib1 lib2 ... ]
#     [ INCLUDEDIRS dir1 dir2 ... ]
#     [ BUILTIN ]
# )

How

Python 2.7.1

  1. Download https://github.com/jonashaag/cpython/archive/v2.7.1.zip
  2. Download https://github.com/davidsansome/python-cmake-buildsystem/archive/master.zip
  3. Copy content of master.zip into the extracted folder corresponding to "v2.7.1"
  4. Build it as a regular CMake project

Build results

  • Ubuntu 10.04 / Default options => OK
  • Ubuntu 10.04 / Default options + ENABLE_SHARED:ON => OK


Python 2.7.3

  1. Download https://github.com/jonashaag/cpython/archive/v2.7.3.zip
  2. Download https://github.com/jcfr/python-cmake-buildsystem/archive/support-273-adding-random-module.zip
  3. Copy content of master.zip into the extracted folder corresponding to "v2.7.1"
  4. Build it as a regular CMake project

Build results

Windows