blob: c1c72c763c6cec6f2fa517f549a553d550ba49d0 [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)
Henry Schreiner94db5c52020-07-30 16:20:10 -040055 return()
Dean Moldovane782eb82016-06-12 00:37:56 +020056endif()
Dean Moldovan928fff62016-05-22 19:48:47 +020057
Henry Schreiner1729aae2020-08-19 12:26:26 -040058if(PythonLibsNew_FIND_QUIETLY)
59 set(_pythonlibs_quiet QUIET)
60endif()
61
62if(PythonLibsNew_FIND_REQUIRED)
63 set(_pythonlibs_required REQUIRED)
64endif()
65
66# Check to see if the `python` command is present and from a virtual
67# environment, conda, or GHA activation - if it is, try to use that.
68
69if(NOT DEFINED PYTHON_EXECUTABLE)
70 if(DEFINED ENV{VIRTUAL_ENV})
71 find_program(
72 PYTHON_EXECUTABLE python
73 PATHS "$ENV{VIRTUAL_ENV}" "$ENV{VIRTUAL_ENV}/bin"
74 NO_DEFAULT_PATH)
75 elseif(DEFINED ENV{CONDA_PREFIX})
76 find_program(
77 PYTHON_EXECUTABLE python
78 PATHS "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/bin"
79 NO_DEFAULT_PATH)
80 elseif(DEFINED ENV{pythonLocation})
81 find_program(
82 PYTHON_EXECUTABLE python
83 PATHS "$ENV{pythonLocation}" "$ENV{pythonLocation}/bin"
84 NO_DEFAULT_PATH)
85 endif()
86 if(NOT PYTHON_EXECUTABLE)
87 unset(PYTHON_EXECUTABLE)
88 endif()
89endif()
90
Dean Moldovan928fff62016-05-22 19:48:47 +020091# Use the Python interpreter to find the libs.
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +020092if(NOT PythonLibsNew_FIND_VERSION)
Henry Schreiner94db5c52020-07-30 16:20:10 -040093 set(PythonLibsNew_FIND_VERSION "")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +020094endif()
Henry Schreiner1729aae2020-08-19 12:26:26 -040095
96find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
97 ${_pythonlibs_quiet})
Dean Moldovan928fff62016-05-22 19:48:47 +020098
99if(NOT PYTHONINTERP_FOUND)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400100 set(PYTHONLIBS_FOUND FALSE)
101 set(PythonLibsNew_FOUND FALSE)
102 return()
Dean Moldovan928fff62016-05-22 19:48:47 +0200103endif()
104
Henry Schreiner1729aae2020-08-19 12:26:26 -0400105# According to https://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
Dean Moldovan928fff62016-05-22 19:48:47 +0200106# testing whether sys has the gettotalrefcount function is a reliable, cross-platform
107# way to detect a CPython debug interpreter.
108#
109# The library suffix is from the config var LDVERSION sometimes, otherwise
110# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
Henry Schreiner94db5c52020-07-30 16:20:10 -0400111execute_process(
112 COMMAND
113 "${PYTHON_EXECUTABLE}" "-c" "from distutils import sysconfig as s;import sys;import struct;
Dean Moldovan928fff62016-05-22 19:48:47 +0200114print('.'.join(str(v) for v in sys.version_info));
115print(sys.prefix);
116print(s.get_python_inc(plat_specific=True));
117print(s.get_python_lib(plat_specific=True));
118print(s.get_config_var('SO'));
119print(hasattr(sys, 'gettotalrefcount')+0);
120print(struct.calcsize('@P'));
121print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500122print(s.get_config_var('LIBDIR') or '');
123print(s.get_config_var('MULTIARCH') or '');
Dean Moldovan928fff62016-05-22 19:48:47 +0200124"
Henry Schreiner94db5c52020-07-30 16:20:10 -0400125 RESULT_VARIABLE _PYTHON_SUCCESS
126 OUTPUT_VARIABLE _PYTHON_VALUES
127 ERROR_VARIABLE _PYTHON_ERROR_VALUE)
Dean Moldovan928fff62016-05-22 19:48:47 +0200128
129if(NOT _PYTHON_SUCCESS MATCHES 0)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400130 if(PythonLibsNew_FIND_REQUIRED)
131 message(FATAL_ERROR "Python config failure:\n${_PYTHON_ERROR_VALUE}")
132 endif()
133 set(PYTHONLIBS_FOUND FALSE)
134 set(PythonLibsNew_FOUND FALSE)
135 return()
Dean Moldovan928fff62016-05-22 19:48:47 +0200136endif()
137
138# Convert the process output into a list
Ryota Suzuki1377fbf2018-10-24 18:18:04 +0900139if(WIN32)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400140 string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
Ryota Suzuki1377fbf2018-10-24 18:18:04 +0900141endif()
Dean Moldovan928fff62016-05-22 19:48:47 +0200142string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
143string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
144list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
145list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
146list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
147list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
148list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
149list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
150list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
151list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500152list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
153list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
Dean Moldovan928fff62016-05-22 19:48:47 +0200154
155# Make sure the Python has the same pointer-size as the chosen compiler
156# Skip if CMAKE_SIZEOF_VOID_P is not defined
157if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
Henry Schreiner94db5c52020-07-30 16:20:10 -0400158 if(PythonLibsNew_FIND_REQUIRED)
159 math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
160 math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
161 message(FATAL_ERROR "Python config failure: Python is ${_PYTHON_BITS}-bit, "
162 "chosen compiler is ${_CMAKE_BITS}-bit")
163 endif()
164 set(PYTHONLIBS_FOUND FALSE)
165 set(PythonLibsNew_FOUND FALSE)
166 return()
Dean Moldovan928fff62016-05-22 19:48:47 +0200167endif()
168
169# The built-in FindPython didn't always give the version numbers
170string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
171list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
172list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
173list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
Henry Schreinerdf115972020-07-31 22:45:19 -0400174set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}.${PYTHON_VERSION_PATCH}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200175
176# Make sure all directory separators are '/'
Matthew Dawkins6f113472019-11-14 02:53:06 -0500177string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
178string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
179string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200180
Liam Keeganc4fd1fd2020-07-21 19:17:22 +0200181if(CMAKE_HOST_WIN32)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400182 set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500183
Henry Schreiner94db5c52020-07-30 16:20:10 -0400184 # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
185 # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
186 if(NOT EXISTS "${PYTHON_LIBRARY}")
187 get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
188 set(PYTHON_LIBRARY "${_PYTHON_ROOT}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
189 endif()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500190
Henry Schreiner94db5c52020-07-30 16:20:10 -0400191 # if we are in MSYS & MINGW, and we didn't find windows python lib, look for system python lib
192 if(DEFINED ENV{MSYSTEM}
193 AND MINGW
194 AND NOT EXISTS "${PYTHON_LIBRARY}")
195 if(PYTHON_MULTIARCH)
196 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
197 else()
198 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
Liam Keeganc4fd1fd2020-07-21 19:17:22 +0200199 endif()
Henry Schreiner94db5c52020-07-30 16:20:10 -0400200 unset(PYTHON_LIBRARY)
201 find_library(
202 PYTHON_LIBRARY
203 NAMES "python${PYTHON_LIBRARY_SUFFIX}"
204 PATHS ${_PYTHON_LIBS_SEARCH}
205 NO_DEFAULT_PATH)
206 endif()
Liam Keeganc4fd1fd2020-07-21 19:17:22 +0200207
Henry Schreiner94db5c52020-07-30 16:20:10 -0400208 # raise an error if the python libs are still not found.
209 if(NOT EXISTS "${PYTHON_LIBRARY}")
210 message(FATAL_ERROR "Python libraries not found")
211 endif()
Jason Rhinelander2ac2d692016-11-22 15:59:18 -0500212
Dean Moldovan928fff62016-05-22 19:48:47 +0200213else()
Henry Schreiner94db5c52020-07-30 16:20:10 -0400214 if(PYTHON_MULTIARCH)
215 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
216 else()
217 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
218 endif()
219 #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
220 # Probably this needs to be more involved. It would be nice if the config
221 # information the python interpreter itself gave us were more complete.
222 find_library(
223 PYTHON_LIBRARY
224 NAMES "python${PYTHON_LIBRARY_SUFFIX}"
Henry Schreinered6de122020-08-01 23:47:47 -0400225 PATHS ${_PYTHON_LIBS_SEARCH}
226 NO_DEFAULT_PATH)
Dean Moldovan928fff62016-05-22 19:48:47 +0200227
Henry Schreiner94db5c52020-07-30 16:20:10 -0400228 # If all else fails, just set the name/version and let the linker figure out the path.
229 if(NOT PYTHON_LIBRARY)
230 set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
231 endif()
Dean Moldovan928fff62016-05-22 19:48:47 +0200232endif()
233
Henry Schreiner94db5c52020-07-30 16:20:10 -0400234mark_as_advanced(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
Dean Moldovan928fff62016-05-22 19:48:47 +0200235
236# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
237# cache entries because they are meant to specify the location of a single
238# library. We now set the variables listed by the documentation for this
239# module.
Henry Schreiner94db5c52020-07-30 16:20:10 -0400240set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
241set(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200242if(NOT PYTHON_DEBUG_LIBRARY)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400243 set(PYTHON_DEBUG_LIBRARY "")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200244endif()
Henry Schreiner94db5c52020-07-30 16:20:10 -0400245set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
Dean Moldovan928fff62016-05-22 19:48:47 +0200246
Henry Schreiner94db5c52020-07-30 16:20:10 -0400247find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARY}"
248 "${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
Wenzel Jakobf6661012016-10-09 14:12:17 +0200249
250set(PYTHONLIBS_FOUND TRUE)
Jamie Snapea0b8f702019-05-12 13:43:44 -0400251set(PythonLibsNew_FOUND TRUE)
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200252
253if(NOT PYTHON_MODULE_PREFIX)
Henry Schreiner94db5c52020-07-30 16:20:10 -0400254 set(PYTHON_MODULE_PREFIX "")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200255endif()