blob: c80432a58b7d008ae0548ddfb67d3d577d4110d8 [file] [log] [blame]
Dean Moldovan928fff62016-05-22 19:48:47 +02001# - Find python libraries
2# This module finds the libraries corresponding to the Python interpeter
3# FindPythonInterp provides.
4# This code sets the following variables:
5#
6# PYTHONLIBS_FOUND - have the Python libs been found
7# PYTHON_PREFIX - path to the Python installation
8# PYTHON_LIBRARIES - path to the python library
9# PYTHON_INCLUDE_DIRS - path to where Python.h is found
10# PYTHON_SITE_PACKAGES - path to installation site-packages
11# PYTHON_IS_DEBUG - whether the Python interpreter is a debug build
12#
13# PYTHON_INCLUDE_PATH - path to where Python.h is found (deprecated)
14#
15# A function PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is defined
16# to build modules for python.
17#
18# Thanks to talljimbo for the patch adding the 'LDVERSION' config
19# variable usage.
20
21#=============================================================================
22# Copyright 2001-2009 Kitware, Inc.
23# Copyright 2012 Continuum Analytics, Inc.
24#
25# All rights reserved.
26#
27# Redistribution and use in source and binary forms, with or without
28# modification, are permitted provided that the following conditions
29# are met:
30#
31# * Redistributions of source code must retain the above copyright
32# notice, this list of conditions and the following disclaimer.
33#
34# * Redistributions in binary form must reproduce the above copyright
35# notice, this list of conditions and the following disclaimer in the
36# documentation and/or other materials provided with the distribution.
37#
38# * Neither the names of Kitware, Inc., the Insight Software Consortium,
39# nor the names of their contributors may be used to endorse or promote
40# products derived from this software without specific prior written
41# permission.
42#
43# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54#=============================================================================
55
56
57# Use the Python interpreter to find the libs.
58if(PythonLibsNew_FIND_REQUIRED)
59 find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
60else()
61 find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
62endif()
63
64if(NOT PYTHONINTERP_FOUND)
65 set(PYTHONLIBS_FOUND FALSE)
66 return()
67endif()
68
69# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
70# testing whether sys has the gettotalrefcount function is a reliable, cross-platform
71# way to detect a CPython debug interpreter.
72#
73# The library suffix is from the config var LDVERSION sometimes, otherwise
74# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
75execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
76 "from distutils import sysconfig as s;import sys;import struct;
77print('.'.join(str(v) for v in sys.version_info));
78print(sys.prefix);
79print(s.get_python_inc(plat_specific=True));
80print(s.get_python_lib(plat_specific=True));
81print(s.get_config_var('SO'));
82print(hasattr(sys, 'gettotalrefcount')+0);
83print(struct.calcsize('@P'));
84print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
85"
86 RESULT_VARIABLE _PYTHON_SUCCESS
87 OUTPUT_VARIABLE _PYTHON_VALUES
88 ERROR_VARIABLE _PYTHON_ERROR_VALUE
89 OUTPUT_STRIP_TRAILING_WHITESPACE)
90
91if(NOT _PYTHON_SUCCESS MATCHES 0)
92 if(PythonLibsNew_FIND_REQUIRED)
93 message(FATAL_ERROR
94 "Python config failure:\n${_PYTHON_ERROR_VALUE}")
95 endif()
96 set(PYTHONLIBS_FOUND FALSE)
97 return()
98endif()
99
100# Convert the process output into a list
101string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
102string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
103list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
104list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
105list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
106list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
107list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
108list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
109list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
110list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
111
112# Make sure the Python has the same pointer-size as the chosen compiler
113# Skip if CMAKE_SIZEOF_VOID_P is not defined
114if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
115 if(PythonLibsNew_FIND_REQUIRED)
116 math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
117 math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
118 message(FATAL_ERROR
119 "Python config failure: Python is ${_PYTHON_BITS}-bit, "
120 "chosen compiler is ${_CMAKE_BITS}-bit")
121 endif()
122 set(PYTHONLIBS_FOUND FALSE)
123 return()
124endif()
125
126# The built-in FindPython didn't always give the version numbers
127string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
128list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
129list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
130list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
131
132# Make sure all directory separators are '/'
133string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX})
134string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
135string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
136
137# TODO: All the nuances of CPython debug builds have not been dealt with/tested.
138if(PYTHON_IS_DEBUG)
139 set(PYTHON_MODULE_EXTENSION "_d${PYTHON_MODULE_EXTENSION}")
140endif()
141
142if(CMAKE_HOST_WIN32)
143 set(PYTHON_LIBRARY
144 "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
145elseif(APPLE)
146 set(PYTHON_LIBRARY
147 "${PYTHON_PREFIX}/lib/libpython${PYTHON_LIBRARY_SUFFIX}.dylib")
148else()
149 if(${PYTHON_SIZEOF_VOID_P} MATCHES 8)
150 set(_PYTHON_LIBS_SEARCH "${PYTHON_PREFIX}/lib64" "${PYTHON_PREFIX}/lib")
151 else()
152 set(_PYTHON_LIBS_SEARCH "${PYTHON_PREFIX}/lib")
153 endif()
154 #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
155 # Probably this needs to be more involved. It would be nice if the config
156 # information the python interpreter itself gave us were more complete.
157 find_library(PYTHON_LIBRARY
158 NAMES "python${PYTHON_LIBRARY_SUFFIX}"
159 PATHS ${_PYTHON_LIBS_SEARCH}
160 NO_DEFAULT_PATH)
161
162 # If all else fails, just set the name/version and let the linker figure out the path.
163 if(NOT PYTHON_LIBRARY)
164 set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
165 endif()
166endif()
167
168# For backward compatibility, set PYTHON_INCLUDE_PATH, but make it internal.
169SET(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}" CACHE INTERNAL
170 "Path to where Python.h is found (deprecated)")
171
172MARK_AS_ADVANCED(
173 PYTHON_LIBRARY
174 PYTHON_INCLUDE_DIR
175)
176
177# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
178# cache entries because they are meant to specify the location of a single
179# library. We now set the variables listed by the documentation for this
180# module.
181SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
182SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
183SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
184
185find_package_message(PYTHON
186 "Found PythonLibs: ${PYTHON_LIBRARY}"
187 "${PYTHON_EXECUTABLE}${PYTHON_VERSION}")