rendered paste body# - Try to find the a valid boost+python combination
# Once done this will define
#
# BOOST_PYTHON_FOUND - system has a valid boost+python combination
# BOOST_PYTHON_INCLUDES - the include directory for boost+python
# BOOST_PYTHON_LIBS - the needed libs for boost+python
# Copyright (c) 2006, Pino Toscano, <toscano.pino@tiscali.it>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
if(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
# Already in cache, be silent
set(BOOST_PYTHON_FIND_QUIETLY TRUE)
endif(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
# some needed includes
include(CheckCXXSourceCompiles)
include(CheckIncludeFileCXX)
include(CheckLibraryExists)
find_package(Boost)
find_package(PythonLibs)
# reset vars
set(BOOST_PYTHON_INCLUDES)
set(BOOST_PYTHON_LIBS)
if (PYTHON_LIBRARY AND PYTHON_INCLUDE_PATH)
message(STATUS "++++++++ Found Python" ${PYTHON_LIBRARY} " " ${PYTHON_INCLUDE_PATH})
endif (PYTHON_LIBRARY AND PYTHON_INCLUDE_PATH)
# handy arrays
set(PYTHON_VERSIONS "python;python2.5;python25;python2.4;python24;python2.3;python23;python2.2;python22")
set(PYTHON_INCLUDE_DIRS "/usr/include/$pyver;/usr/local/include/$pyver;/usr/$pyver/include;/usr/local/$pyver/include;$prefix/include/$pyver;$prefix/$pyver/include")
set(PYTHON_LIBRARY_DIRS "/usr/lib;/usr/local/lib;/usr/lib/$pyver/config")
set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_PATH}" )
set(PYTHON_LIBRARY_DIRS "${PYTHON_INCLUDE_PATH}/../libs")
set(PYTHON_LIBS "boost_python-gcc-mt;boost_python-mt;boost_python-gcc-mt-1_33;boost_python-gcc-mt-1_33_1;boost_python;boost_python-gcc-mt-1_32")
if (Boost_FOUND)
# 1st: check for boost/shared_ptr.hpp
# set()
set(my_BOOST_ROOT $ENV{BOOST_ROOT})
set(my_BOOST_ROOT "${CMAKE_INSTALL_PREFIX}/")
message(STATUS "++++++++" ${CMAKE_INSTALL_PREFIX} ${Boost_INCLUDE_DIR})
if (my_BOOST_ROOT)
message(STATUS "++++++++" ${my_BOOST_ROOT} ${Boost_INCLUDE_DIR})
set(CMAKE_REQUIRED_INCLUDES "${my_BOOST_ROOT}/include")
link_directories(${my_BOOST_ROOT}/bin)
endif (my_BOOST_ROOT)
check_include_file_cxx(boost/shared_ptr.hpp HAVE_BOOST_SHARED_PTR_HPP)
endif (Boost_FOUND)
if(HAVE_BOOST_SHARED_PTR_HPP)
if (my_BOOST_ROOT)
set(PYTHON_INCLUDE_DIRS "${my_BOOST_ROOT}/include;${PYTHON_INCLUDE_DIRS}")
set(PYTHON_LIBRARY_DIRS "${my_BOOST_ROOT}/bin;${PYTHON_LIBRARY_DIRS}")
endif (my_BOOST_ROOT)
set(_found FALSE)
# save the old flags and setting the new ones
set(_save_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
foreach(_pyver ${PYTHON_VERSIONS})
if(NOT _found)
foreach(_pydir ${PYTHON_INCLUDE_DIRS})
# message(STATUS "++++++++ include dir: " ${_pydir})
if(NOT _found)
string(REPLACE "$pyver" "${_pyver}" _pydir ${_pydir})
string(REPLACE "$prefix" "${CMAKE_INSTALL_PREFIX}" _pydir ${_pydir})
if(EXISTS ${_pydir})
if(EXISTS ${_pydir}/Python.h)
foreach(_pylibdir ${PYTHON_LIBRARY_DIRS})
if(NOT _found)
string(REPLACE "$pyver" ${_pyver} _pylibdir ${_pylibdir})
foreach(_pblib ${PYTHON_LIBS})
if(NOT _found)
set(CMAKE_REQUIRED_FLAGS "-L${_pylibdir} -L${my_BOOST_ROOT}/bin")
set(CMAKE_REQUIRED_INCLUDES ${_pydir} ${my_BOOST_ROOT}/include)
set(CMAKE_REQUIRED_LIBRARIES ${_pblib} ${_pyver})
#${_pblib} ${_pyver}
message(STATUS "++++++++ all dirs: " ${_pydir} " " ${_pylibdir} " " ${_pblib} " " ${_pyver})
check_cxx_source_compiles("
#include <boost/python.hpp>
const char* greet() { return \"Hello world!\"; }
BOOST_PYTHON_MODULE(hello) { boost::python::def(\"greet\", greet); }
int main() { return 0; }
// some vars, in case of the compilation fail...
// python include dir: ${_pydir}
// python lib dir: ${_pylibdir}
// boost python lib: ${_pblib}
" bp_${_pylibdir}_${_pydir}_${_pblib}_${_pyver}_compile )
set(CMAKE_REQUIRED_FLAGS)
set(CMAKE_REQUIRED_INCLUDES)
set(CMAKE_REQUIRED_LIBRARIES)
if(bp_${_pylibdir}_${_pydir}_${_pblib}_${_pyver}_compile)
message(STATUS "++++++++ compiled: " ${_pydir} " " ${_pylibdir} " " ${_pblib} " " ${_pyver})
set(_found TRUE)
set(BOOST_PYTHON_INCLUDES ${_pydir})
set(BOOST_PYTHON_LIBS "-l${_pyver} -L${_pylibdir} -l${_pblib} -L${my_BOOST_ROOT}/bin")
endif(bp_${_pylibdir}_${_pydir}_${_pblib}_${_pyver}_compile)
endif(NOT _found)
endforeach(_pblib ${PYTHON_LIBS})
endif(NOT _found)
endforeach(_pylibdir ${PYTHON_LIBRARY_DIRS})
endif(EXISTS ${_pydir}/Python.h)
endif(EXISTS ${_pydir})
endif(NOT _found)
endforeach(_pydir ${PYTHON_INCLUDE_DIRS})
endif(NOT _found)
endforeach(_pyver ${PYTHON_VERSIONS})
set(CMAKE_CXX_FLAGS ${_save_CMAKE_CXX_FLAGS})
endif(HAVE_BOOST_SHARED_PTR_HPP)
if(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
set(BOOST_PYTHON_FOUND TRUE)
endif(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
if(BOOST_PYTHON_FOUND)
if(NOT BoostPython_FIND_QUIETLY)
message(STATUS "Found Boost+Python: ${BOOST_PYTHON_LIBS}")
endif(NOT BoostPython_FIND_QUIETLY)
set(KIG_ENABLE_PYTHON_SCRIPTING 1)
else (BOOST_PYTHON_FOUND)
if (BoostPython_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find Boost+Python")
endif(BoostPython_FIND_REQUIRED)
set(KIG_ENABLE_PYTHON_SCRIPTING 0)
endif(BOOST_PYTHON_FOUND)
mark_as_advanced(BOOST_PYTHON_INCLUDES BOOST_PYTHON_LIBS)