1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
18 # This module finds the libraries corresponding to the Python 3 interpreter
19 # and the NumPy package, and sets the following variables:
21 # - PYTHON_INCLUDE_DIRS
24 # - NUMPY_INCLUDE_DIRS
26 # Need CMake 3.15 or later for Python3_FIND_STRATEGY
27 if(${CMAKE_VERSION} VERSION_LESS "3.15.0")
28 # Use deprecated Python- and NumPy-finding code
29 if(Python3Alt_FIND_REQUIRED)
30 find_package(PythonLibsNew REQUIRED)
31 find_package(NumPy REQUIRED)
33 find_package(PythonLibsNew)
36 find_package_handle_standard_args(
37 Python3Alt REQUIRED_VARS PYTHON_EXECUTABLE PYTHON_INCLUDE_DIRS NUMPY_INCLUDE_DIRS)
41 if(${CMAKE_VERSION} VERSION_LESS "3.18.0" OR ARROW_BUILD_TESTS)
42 # When building arrow-python-test, we need libpython to be present, so ask for
43 # the full "Development" component. Also ask for it on CMake < 3.18,
44 # where "Development.Module" is not available.
45 if(Python3Alt_FIND_REQUIRED)
47 COMPONENTS Interpreter Development NumPy
50 find_package(Python3 COMPONENTS Interpreter Development NumPy)
53 if(Python3Alt_FIND_REQUIRED)
55 COMPONENTS Interpreter Development.Module NumPy
58 find_package(Python3 COMPONENTS Interpreter Development.Module NumPy)
66 set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
67 set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS})
68 set(PYTHON_LIBRARIES ${Python3_LIBRARIES})
69 set(PYTHON_OTHER_LIBS)
71 get_target_property(NUMPY_INCLUDE_DIRS Python3::NumPy INTERFACE_INCLUDE_DIRECTORIES)
73 # CMake's python3_add_library() doesn't apply the required extension suffix,
74 # detect it ourselves.
75 # (https://gitlab.kitware.com/cmake/cmake/issues/20408)
76 execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
77 "from distutils import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"
78 RESULT_VARIABLE _PYTHON_RESULT
79 OUTPUT_VARIABLE _PYTHON_STDOUT
80 ERROR_VARIABLE _PYTHON_STDERR)
82 if(NOT _PYTHON_RESULT MATCHES 0)
83 if(Python3Alt_FIND_REQUIRED)
84 message(FATAL_ERROR "Python 3 config failure:\n${_PYTHON_STDERR}")
88 string(STRIP ${_PYTHON_STDOUT} _EXT_SUFFIX)
90 function(PYTHON_ADD_MODULE name)
91 python3_add_library(${name} MODULE ${ARGN})
92 set_target_properties(${name} PROPERTIES SUFFIX ${_EXT_SUFFIX})
95 find_package_handle_standard_args(
96 Python3Alt REQUIRED_VARS PYTHON_EXECUTABLE PYTHON_INCLUDE_DIRS NUMPY_INCLUDE_DIRS)