CMake should find python now without manual options being set on linux.
authorCampbell Barton <ideasman42@gmail.com>
Thu, 10 Mar 2011 00:25:35 +0000 (00:25 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 10 Mar 2011 00:25:35 +0000 (00:25 +0000)
cmake on *nix detects python ABI flags for debug and release mode.
searches /usr /usr/local /opt/py32

CMakeLists.txt
build_files/cmake/FindPythonLibsUnix.cmake [new file with mode: 0644]

index 0f84d61361f61ff6255429b20014d03bab39c2c6..eb3dc28c00826407fbfe45f7cc711f5b287cf79d 100644 (file)
@@ -265,43 +265,19 @@ if(UNIX AND NOT APPLE)
 
 
        if(WITH_PYTHON)
 
 
        if(WITH_PYTHON)
-               # No way to set py31. remove for now.
+               # No way to set py32. remove for now.
                # find_package(PythonLibs)
                # find_package(PythonLibs)
-               # set(PYTHON_BINARY python) # not used yet
 
 
-               set(PYTHON /usr)
-
-               # ABI can be any of these chars in this order 'dmu', debug/pymalloc/unicode
-               # TODO, detect available ABI's, may want to make this a module and detect this better
-               if(CMAKE_BUILD_TYPE STREQUAL Debug)
-                       set(PYTHON_ABI_FLAGS "d")
-               else()
-                       set(PYTHON_ABI_FLAGS "m")
-               endif()
+               # defines...
+               
+               #  PYTHON_VERSION
+               #  PYTHON_INCLUDE_DIRS
+               #  PYTHON_LIBRARY
+               #  PYTHON_LIBPATH
+               #  PYTHON_LINKFLAGS
 
 
-               set(PYTHON_VERSION 3.2 CACHE STRING "")
-               mark_as_advanced(PYTHON_VERSION)
-               set(PYTHON_INCLUDE_DIRS "${PYTHON}/include/python${PYTHON_VERSION}${PYTHON_ABI_FLAGS}" CACHE STRING "")
-               mark_as_advanced(PYTHON_INCLUDE_DIRS)
-               set(PYTHON_LIBRARY "python${PYTHON_VERSION}${PYTHON_ABI_FLAGS}" CACHE STRING "")
-               mark_as_advanced(PYTHON_LIBRARY)
-               set(PYTHON_LIBPATH ${PYTHON}/lib CACHE STRING "")
-               mark_as_advanced(PYTHON_LIBPATH)
-               # find_package(PythonInterp) # not used yet
-               # set(PYTHON_BINARY ${PYTHON_EXECUTABLE} CACHE STRING "")
-
-               set(PYTHON_LINKFLAGS "-Xlinker -export-dynamic")
-               mark_as_advanced(PYTHON_LINKFLAGS)
-
-               find_file(
-                       _Found_PYTHON_H
-                       Python.h
-                       ${PYTHON_INCLUDE_DIRS}
-               )
+               include(build_files/cmake/FindPythonLibsUnix.cmake)
 
 
-               if(NOT _Found_PYTHON_H)
-                       message(FATAL_ERROR "Python.h not found in  ${PYTHON_INCLUDE_DIRS}")
-               endif()
        endif()
 
        if(WITH_SDL)
        endif()
 
        if(WITH_SDL)
diff --git a/build_files/cmake/FindPythonLibsUnix.cmake b/build_files/cmake/FindPythonLibsUnix.cmake
new file mode 100644 (file)
index 0000000..9b25e4b
--- /dev/null
@@ -0,0 +1,72 @@
+# - Find python libraries
+#
+#  PYTHON_VERSION
+#  PYTHON_INCLUDE_DIRS
+#  PYTHON_LIBRARY
+#  PYTHON_LIBPATH
+#  PYTHON_LINKFLAGS
+
+#=============================================================================
+
+set(PYTHON_VERSION 3.2 CACHE STRING "")
+mark_as_advanced(PYTHON_VERSION)
+
+set(PYTHON_LINKFLAGS "-Xlinker -export-dynamic")
+mark_as_advanced(PYTHON_LINKFLAGS)
+
+set(_Python_ABI_FLAGS
+       "m;mu;u; ")
+
+string(REPLACE "." "" _PYTHON_VERSION_NO_DOTS ${PYTHON_VERSION})
+set(_Python_PATHS
+  "/opt/py${_PYTHON_VERSION_NO_DOTS}" "/usr" "/usr/local")
+
+if(NOT DEFINED PYTHON_INCLUDE_DIRS)
+       message(STATUS "Looking for include Python.h")
+       set(_Found_PYTHON_H OFF)
+
+       foreach(_CURRENT_PATH ${_Python_PATHS})
+               foreach(_CURRENT_ABI_FLAGS ${_Python_ABI_FLAGS})
+                       if(CMAKE_BUILD_TYPE STREQUAL Debug)
+                               set(_CURRENT_ABI_FLAGS "d${_CURRENT_ABI_FLAGS}")
+                       endif()
+                       string(REPLACE " " "" _CURRENT_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
+
+                       set(_Python_HEADER "${_CURRENT_PATH}/include/python${PYTHON_VERSION}${_CURRENT_ABI_FLAGS}/Python.h")
+
+                       if(EXISTS ${_Python_HEADER})
+                               message(STATUS "Checking for header: ${_Python_HEADER} - found")
+                               set(_Found_PYTHON_H ON)
+                               set(PYTHON ${_CURRENT_PATH})
+                               set(PYTHON_ABI_FLAGS ${_CURRENT_ABI_FLAGS})
+                               break()
+                       else()
+                               message(STATUS "Checking for header: ${_Python_HEADER}")
+                       endif()
+               endforeach()
+               
+               if(_Found_PYTHON_H)
+                       break()
+               endif()
+       endforeach()
+
+       if(NOT _Found_PYTHON_H)
+               message(FATAL_ERROR "Python.h not found")
+       endif()
+endif()
+
+#=============================================================================
+# now the python versions are found
+
+
+set(PYTHON_INCLUDE_DIRS "${PYTHON}/include/python${PYTHON_VERSION}${PYTHON_ABI_FLAGS}" CACHE STRING "")
+mark_as_advanced(PYTHON_INCLUDE_DIRS)
+set(PYTHON_LIBRARY "python${PYTHON_VERSION}${PYTHON_ABI_FLAGS}" CACHE STRING "")
+mark_as_advanced(PYTHON_LIBRARY)
+set(PYTHON_LIBPATH ${PYTHON}/lib CACHE STRING "")
+mark_as_advanced(PYTHON_LIBPATH)
+# set(PYTHON_BINARY ${PYTHON_EXECUTABLE} CACHE STRING "")
+
+if(NOT EXISTS "${PYTHON_INCLUDE_DIRS}/Python.h")
+       message(FATAL_ERROR " Missing python header: ${PYTHON_INCLUDE_DIRS}/Python.h")
+endif()