Adding the cmake subdir
[kdeedu-porting.git] / cmake / modules / FindBoostPython.cmake
blob97696bd8dcb047c435ec814e147a72e9127d88ef
1 # - Try to find the a valid boost+python combination
2 # Once done this will define
4 #  BOOST_PYTHON_FOUND - system has a valid boost+python combination
5 #  BOOST_PYTHON_INCLUDES - the include directory for boost+python
6 #  BOOST_PYTHON_LIBS - the needed libs for boost+python
8 # Copyright (c) 2006, Pino Toscano, <toscano.pino@tiscali.it>
10 # Redistribution and use is allowed according to the terms of the BSD license.
11 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
13 if(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
14     # Already in cache, be silent
15         set(BOOST_PYTHON_FIND_QUIETLY TRUE)
16 endif(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
18 # some needed includes
19 include(CheckCXXSourceCompiles)
20 include(CheckIncludeFileCXX)
21 include(CheckLibraryExists)
23 # reset vars
24 set(BOOST_PYTHON_INCLUDES)
25 set(BOOST_PYTHON_LIBS)
27 # handy arrays
28 set(PYTHON_VERSIONS "python;python2.5;python2.4;python2.3;python2.2")
29 set(PYTHON_INCLUDE_DIRS "/usr/include/$pyver;/usr/local/include/$pyver;/usr/$pyver/include;/usr/local/$pyver/include;$prefix/include/$pyver;$prefix/$pyver/include")
30 set(PYTHON_LIBRARY_DIRS "/usr/lib${LIB_SUFFIX};/usr/local/lib${LIB_SUFFIX};/usr/lib${LIB_SUFFIX}/$pyver/config")
31 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;boost_python")
33 # 1st: check for boost/shared_ptr.hpp
34 check_include_file_cxx(boost/shared_ptr.hpp HAVE_BOOST_SHARED_PTR_HPP)
36 if(HAVE_BOOST_SHARED_PTR_HPP)
38   set(_found FALSE)
40   # save the old flags and setting the new ones
41   set(_save_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
42   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
44   foreach(_pyver ${PYTHON_VERSIONS})
45     if(NOT _found)
46       foreach(_pydir ${PYTHON_INCLUDE_DIRS})
48         if(NOT _found)
49           string(REPLACE "$pyver" "${_pyver}" _pydir ${_pydir})
50           string(REPLACE "$prefix" "${CMAKE_INSTALL_PREFIX}" _pydir ${_pydir})
52           if(EXISTS ${_pydir})
53           if(EXISTS ${_pydir}/Python.h)
55             foreach(_pylibdir ${PYTHON_LIBRARY_DIRS})
57               if(NOT _found)
58                 string(REPLACE "$pyver" ${_pyver} _pylibdir ${_pylibdir})
60                 foreach(_pblib ${PYTHON_LIBS})
62                   if(NOT _found)
64                     set(CMAKE_REQUIRED_FLAGS "-L${_pylibdir}")
65                     set(CMAKE_REQUIRED_INCLUDES ${_pydir})
66                     set(CMAKE_REQUIRED_LIBRARIES ${_pblib} ${_pyver})
68                     check_cxx_source_compiles("
69 #include <boost/python.hpp>
70 const char* greet() { return \"Hello world!\"; }
71 BOOST_PYTHON_MODULE(hello) { boost::python::def(\"greet\", greet); }
73 int main() { return 0; }
75 // some vars, in case of the compilation fail...
76 // python include dir: ${_pydir}
77 // python lib dir: ${_pylibdir}
78 // boost python lib: ${_pblib}
80 " boost_python_${_pylibdir}_${_pydir}_${_pblib}_compile )
82                     set(CMAKE_REQUIRED_FLAGS)
83                     set(CMAKE_REQUIRED_INCLUDES)
84                     set(CMAKE_REQUIRED_LIBRARIES)
86                     if(boost_python_${_pylibdir}_${_pydir}_${_pblib}_compile)
88                         set(_found TRUE)
90                         set(BOOST_PYTHON_INCLUDES ${_pydir})
91                         set(BOOST_PYTHON_LIBS "-l${_pyver} -L${_pylibdir} -l${_pblib}")
93                     endif(boost_python_${_pylibdir}_${_pydir}_${_pblib}_compile)
95                   endif(NOT _found)
97                 endforeach(_pblib ${PYTHON_LIBS})
99               endif(NOT _found)
101             endforeach(_pylibdir ${PYTHON_LIBRARY_DIRS})
103           endif(EXISTS ${_pydir}/Python.h)
104           endif(EXISTS ${_pydir})
106         endif(NOT _found)
108       endforeach(_pydir ${PYTHON_INCLUDE_DIRS})
110     endif(NOT _found)
112   endforeach(_pyver ${PYTHON_VERSIONS})
114   set(CMAKE_CXX_FLAGS ${_save_CMAKE_CXX_FLAGS})
116 endif(HAVE_BOOST_SHARED_PTR_HPP)
118 if(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
119   set(BOOST_PYTHON_FOUND TRUE)
120 endif(BOOST_PYTHON_INCLUDES AND BOOST_PYTHON_LIBS)
122 if(BOOST_PYTHON_FOUND)
123   if(NOT BoostPython_FIND_QUIETLY)
124     message(STATUS "Found Boost+Python: ${BOOST_PYTHON_LIBS}")
125   endif(NOT BoostPython_FIND_QUIETLY)
126   set(KIG_ENABLE_PYTHON_SCRIPTING 1)
127 else (BOOST_PYTHON_FOUND)
128   if (BoostPython_FIND_REQUIRED)
129     message(FATAL_ERROR "Could NOT find Boost+Python")
130   endif(BoostPython_FIND_REQUIRED)
131   set(KIG_ENABLE_PYTHON_SCRIPTING 0)
132 endif(BOOST_PYTHON_FOUND)
134 mark_as_advanced(BOOST_PYTHON_INCLUDES BOOST_PYTHON_LIBS)