# - Find python libraries
# This module finds if Python is installed and determines where the
# include files and libraries are. It also determines what the name of
# the library is. This code sets the following variables:
#
#  PYTHONLIBS_FOUND           - have the Python libs been found
#  PYTHON_LIBRARIES           - path to the python library
#  PYTHON_INCLUDE_PATH        - path to where Python.h is found (deprecated)
#  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
#  PYTHON_DEBUG_LIBRARIES     - path to the debug library
#  PYTHON_VERSION             - python version string e.g. 2.7.1
#  PYTHON_MAJOR_VERSION       - python major version number
#  PYTHON_MINOR_VERSION       - python minor version number
#  PYTHON_MICRO_VERSION       - python release version number
#
# This code uses the following variables:
#  Python_ADDITIONAL_VERSIONS - list of additional Python versions to search for

#=============================================================================
# Copyright 2001-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)

INCLUDE(CMakeFindFrameworks)
# Search for the python framework on Apple.
CMAKE_FIND_FRAMEWORKS(Python)

set(_PythonInterp_VERSION)
if(PYTHONINTERP_FOUND)
  set(_PythonInterp_VERSION ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
endif()

# Set up the versions we know about, in the order we will search. Always add
# the user supplied additional versions to the front.
set(_Python_VERSIONS
  ${Python_ADDITIONAL_VERSIONS} ${_PythonInterp_VERSION}
  2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)

FOREACH(_CURRENT_VERSION ${_Python_VERSIONS})
  STRING(REPLACE "." "" _CURRENT_VERSION_NO_DOTS ${_CURRENT_VERSION})
  IF(WIN32)
    FIND_LIBRARY(PYTHON_DEBUG_LIBRARY
      NAMES python${_CURRENT_VERSION_NO_DOTS}_d python
      PATHS
      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs/Debug
      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs )
  ENDIF()

  FIND_LIBRARY(PYTHON_LIBRARY
    NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
    PATHS
      [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/libs
    # Avoid finding the .dll in the PATH.  We want the .lib.
    NO_SYSTEM_ENVIRONMENT_PATH
  )
  # Look for the static library in the Python config directory
  FIND_LIBRARY(PYTHON_LIBRARY
    NAMES python${_CURRENT_VERSION_NO_DOTS} python${_CURRENT_VERSION}
    # Avoid finding the .dll in the PATH.  We want the .lib.
    NO_SYSTEM_ENVIRONMENT_PATH
    # This is where the static library is usually located
    PATH_SUFFIXES python${_CURRENT_VERSION}/config
  )

  # Only look for include directory if library was found, this ensures
  # that version will be matched between include dir and library
  IF(PYTHON_LIBRARY)
    SET(PYTHON_FRAMEWORK_INCLUDES)
    IF(Python_FRAMEWORKS AND NOT PYTHON_INCLUDE_DIR)
      FOREACH(dir ${Python_FRAMEWORKS})
        SET(PYTHON_FRAMEWORK_INCLUDES ${PYTHON_FRAMEWORK_INCLUDES}
          ${dir}/Versions/${_CURRENT_VERSION}/include/python${_CURRENT_VERSION})
      ENDFOREACH()
    ENDIF()

    # Search frameworks and registry locations first
    FIND_PATH(PYTHON_INCLUDE_DIR
      NAMES Python.h
      PATHS
        ${PYTHON_FRAMEWORK_INCLUDES}
        [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]/include
      NO_DEFAULT_PATH
    )

    # Broaden to default paths as a second step (mainly for UNIX)
    FIND_PATH(PYTHON_INCLUDE_DIR
      NAMES Python.h
      PATH_SUFFIXES
        python${_CURRENT_VERSION}
    )
  ENDIF()

  # For backward compatibility, set PYTHON_INCLUDE_PATH
  SET(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}")

ENDFOREACH()

MARK_AS_ADVANCED(
  PYTHON_DEBUG_LIBRARY
  PYTHON_LIBRARY
  PYTHON_INCLUDE_DIR
)


# look in PYTHON_INCLUDE_DIR for patchlevel.h, which contains the
# version number macros in all versions of python from 1.5 through
# at least version 3.2, and set these vars:  PYTHON_VERSION,
# PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION, PYTHON_MICRO_VERSION.
IF(PYTHON_INCLUDE_DIR)
  SET(_VERSION_REGEX
      "^#define[ \t]+PY([A-Z_]*_VERSION)[ \t]+[\"]*([[0-9A-Za-z\\.]+)[\"]*[ \t]*$")
  FILE(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" _VERSION_STRINGS
       LIMIT_COUNT 10 REGEX ${_VERSION_REGEX})
  FOREACH(_VERSION_STRING ${_VERSION_STRINGS})
    STRING(REGEX REPLACE ${_VERSION_REGEX} "PYTHON\\1"
           _VERSION_VARIABLE "${_VERSION_STRING}")
    STRING(REGEX REPLACE ${_VERSION_REGEX} "\\2"
           _VERSION_NUMBER "${_VERSION_STRING}")
    SET(${_VERSION_VARIABLE} ${_VERSION_NUMBER})
  ENDFOREACH()
ENDIF()


# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
# cache entries because they are meant to specify the location of a single
# library. We now set the variables listed by the documentation for this
# module.
SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")


INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)
