aboutsummaryrefslogtreecommitdiff
path: root/modules/FindPythonLibrary.cmake
blob: 99fefdc227d5e480bbc2058cd50f0f61b96122fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Find Python
# ~~~~~~~~~~~
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
# Find the Python interpreter and related Python directories.
#
# This file defines the following variables:
# 
# PYTHON_EXECUTABLE - The path and filename of the Python interpreter.
#
# PYTHON_SHORT_VERSION - The version of the Python interpreter found,
#     excluding the patch version number. (e.g. 2.5 and not 2.5.1))
# 
# PYTHON_LONG_VERSION - The version of the Python interpreter found as a human
#     readable string.
#
# PYTHON_SITE_PACKAGES_DIR - Location of the Python site-packages directory.
#
# PYTHON_INCLUDE_PATH - Directory holding the python.h include file.
#
# PYTHON_LIBRARY, PYTHON_LIBRARIES- Location of the Python library.

INCLUDE(CMakeFindFrameworks)

IF(EXISTS PYTHON_LIBRARY)
   # Already in cache, be silent
   SET(PYTHONLIBRARY_FOUND TRUE)
ELSE(EXISTS PYTHON_LIBRARY)

  FIND_PACKAGE(PythonInterp REQUIRED)

  FIND_FILE(_find_lib_python_py FindLibPython.py PATHS ${CMAKE_MODULE_PATH})

  EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE}  ${_find_lib_python_py} OUTPUT_VARIABLE python_config)
  IF(python_config)
    STRING(REGEX REPLACE ".*exec_prefix:([^\n]+).*$" "\\1" PYTHON_PREFIX ${python_config})
    STRING(REGEX REPLACE ".*\nshort_version:([^\n]+).*$" "\\1" PYTHON_SHORT_VERSION ${python_config})
    STRING(REGEX REPLACE ".*\nlong_version:([^\n]+).*$" "\\1" PYTHON_LONG_VERSION ${python_config})
    STRING(REGEX REPLACE ".*\npy_inc_dir:([^\n]+).*$" "\\1" PYTHON_INCLUDE_PATH ${python_config})
    STRING(REGEX REPLACE ".*\nsite_packages_dir:([^\n]+).*$" "\\1" PYTHON_SITE_PACKAGES_DIR ${python_config})
    STRING(REGEX REPLACE "([0-9]+).([0-9]+)" "\\1\\2" PYTHON_SHORT_VERSION_NO_DOT ${PYTHON_SHORT_VERSION})
    set(PYTHON_LIBRARY_NAMES python${PYTHON_SHORT_VERSION} python${PYTHON_SHORT_VERSION_NO_DOT})
    if(WIN32)
        STRING(REPLACE "\\" "/" PYTHON_SITE_PACKAGES_DIR ${PYTHON_SITE_PACKAGES_DIR})
    endif(WIN32)
    FIND_LIBRARY(PYTHON_LIBRARY NAMES ${PYTHON_LIBRARY_NAMES} PATHS ${PYTHON_PREFIX}/lib ${PYTHON_PREFIX}/libs NO_DEFAULT_PATH)
    SET(PYTHONLIBRARY_FOUND TRUE)
  ENDIF(python_config)

  # adapted from cmake's builtin FindPythonLibs
  if(APPLE)
    CMAKE_FIND_FRAMEWORKS(Python)
    SET(PYTHON_FRAMEWORK_INCLUDES)
    IF(Python_FRAMEWORKS)
      # If a framework has been selected for the include path,
      # make sure "-framework" is used to link it.
      IF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
        SET(PYTHON_LIBRARY "")
        SET(PYTHON_DEBUG_LIBRARY "")
      ENDIF("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework")
      IF(NOT PYTHON_LIBRARY)
        SET (PYTHON_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE)
      ENDIF(NOT PYTHON_LIBRARY)
      SET(PYTHONLIBRARY_FOUND TRUE)
    ENDIF(Python_FRAMEWORKS)
  ENDIF(APPLE)

  IF(PYTHONLIBRARY_FOUND)
    SET(PYTHON_LIBRARIES ${PYTHON_LIBRARY})
    IF(NOT PYTHONLIBRARY_FIND_QUIETLY)
      MESSAGE(STATUS "Found Python executable: ${PYTHON_EXECUTABLE}")
      MESSAGE(STATUS "Found Python version: ${PYTHON_LONG_VERSION}")
      MESSAGE(STATUS "Found Python library: ${PYTHON_LIBRARY}")
    ENDIF(NOT PYTHONLIBRARY_FIND_QUIETLY)
  ELSE(PYTHONLIBRARY_FOUND)
    IF(PYTHONLIBRARY_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find Python")
    ENDIF(PYTHONLIBRARY_FIND_REQUIRED)
  ENDIF(PYTHONLIBRARY_FOUND)

ENDIF (EXISTS PYTHON_LIBRARY)