[CMake] Various problems deploying a python module

Janosch Peters jp at binford3000.de
Thu Jun 17 10:23:09 EDT 2010


Hi list,

I try to write a cmake script for a python module.  Its finally working 
now, but the solution I came up with is not very nice.

1. Finding the correct python environment:

I have two python frameworks on my mac: Python2.5 which comes with OS 
X, and python2.6 from macports. If I just use 
FIND_PACKAGE(PythonInterp) and FIND_PACKAGE(PythonLibs) I end up 
getting the python2.6 interpreter from macports but the python2.5 libs 
from OS X.

I "solved" it by manually setting the path to python2.6 libraries and 
headers in INCLUDE_DIRECTORIES and TARGET_LINK_LIBRARIES but this is 
quite ugly because it may break on another system wit a different path 
prefix for macports.

Is there any way to set the desired python version for PythonInterp and 
PythonLibs in cmake?


2. Renaming the library file:

This is probably very simple, but I startet wih cmake today: How do I 
change the filename of my library/module? The module is called 
libvlfeat.so, however python expects C modules to be named _<name>.so. 
(In my case _vlfeat.so).

How do I do that?


3. Copy python files during installation

The python module needs some python files along with the compiled C 
module. Currently, I just copy all the stuff using cmakes file() 
command. That means, all files are created when the user types in 
"cmake .". It would be better if the files would not be copied until 
the user says "make install".

Is there a way that the "file()" commands are somehow converted to 
similar commands in the makefile?


Thanks in advance,
Janosch

=========== CMakeLists.txt ===============

cmake_minimum_required(VERSION 2.8)
project(vlfeat)
set(CMAKE_VERBOSE_MAKEFILE off)
set(LIBRARY_OUTPUT_DIRECTORY "python/vlfeat")
#set(CMAKE_C_FLAGS "-msse -dynamiclib")

file(GLOB CFILES vl/*.c)
file(GLOB MSER python/vlfeat/mser/*.cpp)
file(GLOB SIFT python/vlfeat/sift/*.cpp)
file(GLOB IMOP python/vlfeat/imop/*.cpp)
file(GLOB MISC python/vlfeat/misc/*.cpp)
file(GLOB KMEANS python/vlfeat/kmeans/*.cpp)

add_library( vlfeat MODULE
        ${CFILES} ${MSER} ${SIFT} ${IMOP} ${MISC} ${KMEANS} 
python/vlfeat/py_vlfeat.cpp
)

SET_TARGET_PROPERTIES(vlfeat PROPERTIES LINKER_LANGUAGE C)
FIND_PACKAGE(PythonInterp)
FIND_PACKAGE(PythonLibs)
find_package( Boost 1.36.0 COMPONENTS python)
execute_process( COMMAND python -c "from distutils.sysconfig import 
get_python_lib; print get_python_lib()" OUTPUT_VARIABLE 
PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE )

INCLUDE_DIRECTORIES(. 
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 
${PYTHON_INCLUDE_DIRS} ${PYTHON_SITE_PACKAGES}/numpy/core/include 
${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(vlfeat ${Boost_LIBRARIES} 
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/Python 
${PYTHON_LIBRARIES})

file(MAKE_DIRECTORY ${PYTHON_SITE_PACKAGES}/vlfeat)
file(GLOB PLOTOP_PY "python/vlfeat/plotop/*.py")
file(GLOB MSER_PY "python/vlfeat/mser/*.py")
file(GLOB KMEANS_PY "python/vlfeat/kmeans/*.py")

file(COPY ${PLOTOP_PY} DESTINATION ${PYTHON_SITE_PACKAGES}/vlfeat/plotop)
file(COPY ${MSER_PY} DESTINATION ${PYTHON_SITE_PACKAGES}/vlfeat/mser)
file(COPY ${KMEANS_PY} DESTINATION ${PYTHON_SITE_PACKAGES}/vlfeat/kmeans)
file(COPY "python/vlfeat/__init__.py" DESTINATION 
${PYTHON_SITE_PACKAGES}/vlfeat)

install(TARGETS vlfeat LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/vlfeat)






More information about the CMake mailing list