1# - Find python libraries
2# This module finds if Python is installed and determines where the
3# include files and libraries are. It also determines what the name of
4# the library is. This code sets the following variables:
5#
6#  PYTHONLIBS_FOUND           - have the Python libs been found
7#  PYTHON_LIBRARIES           - path to the python library
8#  PYTHON_INCLUDE_PATH        - path to where Python.h is found (deprecated)
9#  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
10#  PYTHON_DEBUG_LIBRARIES     - path to the debug library
11#  PYTHON_VERSION             - python version string e.g. 2.7.1
12#  PYTHON_MAJOR_VERSION       - python major version number
13#  PYTHON_MINOR_VERSION       - python minor version number
14#  PYTHON_MICRO_VERSION       - python release version number
15#
16# This code uses the following variables:
17#  Python_ADDITIONAL_VERSIONS - list of additional Python versions to search for
18
19#=============================================================================
20# Copyright 2001-2009 Kitware, Inc.
21#
22# Distributed under the OSI-approved BSD License (the "License");
23# see accompanying file Copyright.txt for details.
24#
25# This software is distributed WITHOUT ANY WARRANTY; without even the
26# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27# See the License for more information.
28#=============================================================================
29# (To distribute this file outside of CMake, substitute the full
30#  License text for the above reference.)
31
32INCLUDE(CMakeFindFrameworks)
33# Search for the python framework on Apple.
34CMAKE_FIND_FRAMEWORKS(Python)
35
36set(_PythonInterp_VERSION)
37if(PYTHONINTERP_FOUND)
38  set(_PythonInterp_VERSION ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
39endif()
40
41# Set up the versions we know about, in the order we will search. Always add
42# the user supplied additional versions to the front.
43set(_Python_VERSIONS
44  ${Python_ADDITIONAL_VERSIONS} ${_PythonInterp_VERSION}
45  2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
46
47FOREACH(_CURRENT_VERSION ${_Python_VERSIONS})
48  STRING(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
49  IF(WIN32)
50    FIND_LIBRARY(PYTHON_DEBUG_LIBRARY
51      NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
52      PATHS
53      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
54      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs )
55  ENDIF()
56
57  FIND_LIBRARY(PYTHON_LIBRARY
58    NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
59    PATHS
60      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
61    # Avoid finding the .dll in the PATH.  We want the .lib.
62    NO_SYSTEM_ENVIRONMENT_PATH
63  )
64  # Look for the static library in the Python config directory
65  FIND_LIBRARY(PYTHON_LIBRARY
66    NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
67    # Avoid finding the .dll in the PATH.  We want the .lib.
68    NO_SYSTEM_ENVIRONMENT_PATH
69    # This is where the static library is usually located
70    PATH_SUFFIXES python${_CURRENT_VERSION}/config
71  )
72
73  # Only look for include directory if library was found, this ensures
74  # that version will be matched between include dir and library
75  IF(PYTHON_LIBRARY)
76    SET(PYTHON_FRAMEWORK_INCLUDES)
77    IF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
78      FOREACH(dir ${Python_FRAMEWORKS})
79        SET(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
80          ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
81      ENDFOREACH()
82    ENDIF()
83
84    # Search frameworks and registry locations first
85    FIND_PATH(PYTHON_INCLUDE_DIR
86      NAMES Python.h
87      PATHS
88        ${PYTHON_FRAMEWORK_INCLUDES}
89        [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
90      NO_DEFAULT_PATH
91    )
92
93    # Broaden to default paths as a second step (mainly for UNIX)
94    FIND_PATH(PYTHON_INCLUDE_DIR
95      NAMES Python.h
96      PATH_SUFFIXES
97        python${_CURRENT_VERSION}
98    )
99  ENDIF()
100
101  # For backward compatibility, set PYTHON_INCLUDE_PATH
102  SET(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")
103
104ENDFOREACH()
105
106MARK_AS_ADVANCED(
107  PYTHON_DEBUG_LIBRARY
108  PYTHON_LIBRARY
109  PYTHON_INCLUDE_DIR
110)
111
112
113# look in PYTHON_INCLUDE_DIR for patchlevel.h, which contains the
114# version number macros in all versions of python from 1.5 through
115# at least version 3.2, and set these vars:  PYTHON_VERSION,
116# PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION, PYTHON_MICRO_VERSION.
117IF(PYTHON_INCLUDE_DIR)
118  SET(_VERSION_REGEX
119      "^#define[ \t]+PY([A-Z_]*_VERSION)[ \t]+[\"]*([[0-9A-Za-z\\.]+)[\"]*[ \t]*$")
120  FILE(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" _VERSION_STRINGS
121       LIMIT_COUNT 10 REGEX ${_VERSION_REGEX})
122  FOREACH(_VERSION_STRING ${_VERSION_STRINGS})
123    STRING(REGEX REPLACE ${_VERSION_REGEX} "PYTHON\\1"
124           _VERSION_VARIABLE "${_VERSION_STRING}")
125    STRING(REGEX REPLACE ${_VERSION_REGEX} "\\2"
126           _VERSION_NUMBER "${_VERSION_STRING}")
127    SET(${_VERSION_VARIABLE} ${_VERSION_NUMBER})
128  ENDFOREACH()
129ENDIF()
130
131
132# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
133# cache entries because they are meant to specify the location of a single
134# library. We now set the variables listed by the documentation for this
135# module.
136SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
137SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
138SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
139
140
141INCLUDE(FindPackageHandleStandardArgs)
142FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
143