blob: 994bb1e03b636bc4075672ee7ee6cdaac16a0272 [file] [log] [blame]
Dean Moldovan928fff62016-05-22 19:48:47 +02001# - Find python libraries
luz.paz28cb6762018-01-09 12:30:19 -05002# This module finds the libraries corresponding to the Python interpreter
Dean Moldovan928fff62016-05-22 19:48:47 +02003# 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
Dean Moldovane782eb82016-06-12 00:37:56 +020010# PYTHON_MODULE_EXTENSION - lib extension, e.g. '.so' or '.pyd'
11# PYTHON_MODULE_PREFIX - lib name prefix: usually an empty string
Dean Moldovan928fff62016-05-22 19:48:47 +020012# PYTHON_SITE_PACKAGES - path to installation site-packages
13# PYTHON_IS_DEBUG - whether the Python interpreter is a debug build
14#
Dean Moldovan928fff62016-05-22 19:48:47 +020015# Thanks to talljimbo for the patch adding the 'LDVERSION' config
16# variable usage.
17
18#=============================================================================
19# Copyright 2001-2009 Kitware, Inc.
20# Copyright 2012 Continuum Analytics, Inc.
21#
22# All rights reserved.
23#
24# Redistribution and use in source and binary forms, with or without
25# modification, are permitted provided that the following conditions
26# are met:
27#
28# * Redistributions of source code must retain the above copyright
29# notice, this list of conditions and the following disclaimer.
30#
31# * Redistributions in binary form must reproduce the above copyright
32# notice, this list of conditions and the following disclaimer in the
33# documentation and/or other materials provided with the distribution.
34#
35# * Neither the names of Kitware, Inc., the Insight Software Consortium,
36# nor the names of their contributors may be used to endorse or promote
37# products derived from this software without specific prior written
38# permission.
39#
40# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51#=============================================================================
52
Dean Moldovan2e37fe02017-06-29 11:42:54 +020053# Checking for the extension makes sure that `LibsNew` was found and not just `Libs`.
54if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION)
Dean Moldovane782eb82016-06-12 00:37:56 +020055 return()
56endif()
Dean Moldovan928fff62016-05-22 19:48:47 +020057
58# Use the Python interpreter to find the libs.
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +020059if(NOT PythonLibsNew_FIND_VERSION)
60 set(PythonLibsNew_FIND_VERSION "")
61endif()
Dean Moldovan928fff62016-05-22 19:48:47 +020062if(PythonLibsNew_FIND_REQUIRED)
63 find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
64else()
65 find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
66endif()
67
68if(NOT PYTHONINTERP_FOUND)
69 set(PYTHONLIBS_FOUND FALSE)
Jamie Snapea0b8f702019-05-12 13:43:44 -040070 set(PythonLibsNew_FOUND FALSE)
Dean Moldovan928fff62016-05-22 19:48:47 +020071 return()
72endif()
73
74# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
75# testing whether sys has the gettotalrefcount function is a reliable, cross-platform
76# way to detect a CPython debug interpreter.
77#
78# The library suffix is from the config var LDVERSION sometimes, otherwise
79# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
80execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
81 "from distutils import sysconfig as s;import sys;import struct;
82print('.'.join(str(v) for v in sys.version_info));
83print(sys.prefix);
84print(s.get_python_inc(plat_specific=True));
85print(s.get_python_lib(plat_specific=True));
86print(s.get_config_var('SO'));
87print(hasattr(sys, 'gettotalrefcount')+0);
88print(struct.calcsize('@P'));
89print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
Jason Rhinelander2ac2d692016-11-22 15:59:18 -050090print(s.get_config_var('LIBDIR') or '');
91print(s.get_config_var('MULTIARCH') or '');
Dean Moldovan928fff62016-05-22 19:48:47 +020092"
93 RESULT_VARIABLE _PYTHON_SUCCESS
94 OUTPUT_VARIABLE _PYTHON_VALUES
Jason Rhinelander2ac2d692016-11-22 15:59:18 -050095 ERROR_VARIABLE _PYTHON_ERROR_VALUE)
Dean Moldovan928fff62016-05-22 19:48:47 +020096
97if(NOT _PYTHON_SUCCESS MATCHES 0)
98 if(PythonLibsNew_FIND_REQUIRED)
99 message(FATAL_ERROR
100 "Python config failure:\n${_PYTHON_ERROR_VALUE}")
101 endif()
102 set(PYTHONLIBS_FOUND FALSE)
Jamie Snapea0b8f702019-05-12 13:43:44 -0400103 set(PythonLibsNew_FOUND FALSE)
Dean Moldovan928fff62016-05-22 19:48:47 +0200104 return()
105endif()
106
107# Convert the process output into a list
Ryota Suzuki1377fbf2018-10-24 18:18:04 +0900108if(WIN32)
109 string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
110endif()
Dean Moldovan928fff62016-05-22 19:48:47 +0200111string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
112string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
113list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
114list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
115list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
116list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
117list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
118list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
119list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
120list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500121list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
122list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
Dean Moldovan928fff62016-05-22 19:48:47 +0200123
124# Make sure the Python has the same pointer-size as the chosen compiler
125# Skip if CMAKE_SIZEOF_VOID_P is not defined
126if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
127 if(PythonLibsNew_FIND_REQUIRED)
128 math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
129 math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
130 message(FATAL_ERROR
131 "Python config failure: Python is ${_PYTHON_BITS}-bit, "
132 "chosen compiler is ${_CMAKE_BITS}-bit")
133 endif()
134 set(PYTHONLIBS_FOUND FALSE)
Jamie Snapea0b8f702019-05-12 13:43:44 -0400135 set(PythonLibsNew_FOUND FALSE)
Dean Moldovan928fff62016-05-22 19:48:47 +0200136 return()
137endif()
138
139# The built-in FindPython didn't always give the version numbers
140string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
141list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
142list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
143list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
144
145# Make sure all directory separators are '/'
Matthew Dawkins6f113472019-11-14 02:53:06 -0500146string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
147string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
148string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200149
Liam Keeganc4fd1fd2020-07-21 19:17:22 +0200150if(CMAKE_HOST_WIN32)
Dean Moldovan928fff62016-05-22 19:48:47 +0200151 set(PYTHON_LIBRARY
Takahiro Ishikawa36188072020-05-04 19:40:42 +0900152 "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500153
154 # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
Christian Ewaldb81c5002016-08-08 08:31:08 +0200155 # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
156 if(NOT EXISTS "${PYTHON_LIBRARY}")
157 get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
158 set(PYTHON_LIBRARY
Takahiro Ishikawa36188072020-05-04 19:40:42 +0900159 "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
Christian Ewaldb81c5002016-08-08 08:31:08 +0200160 endif()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500161
Liam Keeganc4fd1fd2020-07-21 19:17:22 +0200162 # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib
163 if(DEFINED ENV{MSYSTEM} AND MINGW AND NOT EXISTS "${PYTHON_LIBRARY}")
164 if(PYTHON_MULTIARCH)
165 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
166 else()
167 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
168 endif()
169 unset(PYTHON_LIBRARY)
170 find_library(PYTHON_LIBRARY
171 NAMES "python${PYTHON_LIBRARY_SUFFIX}"
172 PATHS ${_PYTHON_LIBS_SEARCH}
173 NO_DEFAULT_PATH)
174 endif()
175
Christian Ewaldb81c5002016-08-08 08:31:08 +0200176 # raise an error if the python libs are still not found.
177 if(NOT EXISTS "${PYTHON_LIBRARY}")
178 message(FATAL_ERROR "Python libraries not found")
179 endif()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500180
Dean Moldovan928fff62016-05-22 19:48:47 +0200181else()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500182 if(PYTHON_MULTIARCH)
183 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200184 else()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500185 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200186 endif()
187 #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
188 # Probably this needs to be more involved. It would be nice if the config
189 # information the python interpreter itself gave us were more complete.
190 find_library(PYTHON_LIBRARY
191 NAMES "python${PYTHON_LIBRARY_SUFFIX}"
192 PATHS ${_PYTHON_LIBS_SEARCH}
193 NO_DEFAULT_PATH)
194
195 # If all else fails, just set the name/version and let the linker figure out the path.
196 if(NOT PYTHON_LIBRARY)
197 set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
198 endif()
199endif()
200
Dean Moldovan928fff62016-05-22 19:48:47 +0200201MARK_AS_ADVANCED(
202 PYTHON_LIBRARY
203 PYTHON_INCLUDE_DIR
204)
205
206# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
207# cache entries because they are meant to specify the location of a single
208# library. We now set the variables listed by the documentation for this
209# module.
210SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
211SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200212if(NOT PYTHON_DEBUG_LIBRARY)
213 SET(PYTHON_DEBUG_LIBRARY "")
214endif()
Dean Moldovan928fff62016-05-22 19:48:47 +0200215SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
216
217find_package_message(PYTHON
218 "Found PythonLibs: ${PYTHON_LIBRARY}"
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200219 "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
Wenzel Jakobf6661012016-10-09 14:12:17 +0200220
221set(PYTHONLIBS_FOUND TRUE)
Jamie Snapea0b8f702019-05-12 13:43:44 -0400222set(PythonLibsNew_FOUND TRUE)
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200223
224if(NOT PYTHON_MODULE_PREFIX)
225 SET(PYTHON_MODULE_PREFIX "")
226endif()