Debian Codegraph @

src/python/CMakeLists.txt

raw
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright Contributors to the OpenEXR Project.
3
4 #######################################
5 #######################################
6 # This declares all the configuration variables visible
7 # in cmake-gui or similar and the rest of the global
8 # project setup
9 #
10 # Please look at this file to see what is configurable
11 #######################################
12 #######################################
13 include(config/PyImathSetup.cmake)
14
15 # we are building a python extension, so of course we depend on
16 # python as well. Except we don't know which version...
17 # cmake 3.14 can also search for numpy, but we only depend on 3.12
18 # in the rest of OpenEXR right now...
19
20 # first make sure we find *some* python
21 find_package(Python COMPONENTS Interpreter Development)
22 if(NOT TARGET Python::Interpreter AND NOT TARGET Python::Python)
23 message(WARNING "Unable to find any python interpreter or libraries, disabling PyImath")
24 return()
25 endif()
26
27 # we don't need cmake_dependant_option here, because the file is
28 # included only if -DPYTHON=ON
29 option(USE_PYTHON2 "Whether to use Python 2.x or 3.x" OFF)
30
31 # now determine which (or both), and compile for both
32 if(USE_PYTHON2)
33 find_package(Python2 COMPONENTS Interpreter Development)
34 if(Python2_FOUND)
35 message(STATUS "Found Python ${Python2_VERSION}")
36 elseif(Python2::Python)
37 message(WARNING "Found Python ${Python2_VERSION} development libraries, but no interpreter")
38 elseif(Python2::Interpreter)
39 message(WARNING "Found Python ${Python2_VERSION} interpreter, but no development libraries")
40 else()
41 message(WARNING "Unable to find Python2 interpreter or development libraries")
42 endif()
43 set(PY_MAJOR_VERSION ${Python2_VERSION_MAJOR})
44 set(Python_VERSION_MAJOR ${Python2_VERSION_MAJOR})
45 set(Python_VERSION_MINOR ${Python2_VERSION_MINOR})
46 set(Python_EXECUTABLE ${Python2_EXECUTABLE})
47 set(Python_SITEARCH ${Python2_SITEARCH})
48 else()
49 find_package(Python3 COMPONENTS Interpreter Development)
50 if(Python3_FOUND)
51 message(STATUS "Found Python ${Python3_VERSION}")
52 elseif(Python3::Python)
53 message(WARNING "Found Python ${Python3_VERSION} development libraries, but no interpreter")
54 elseif(Python3::Interpreter)
55 message(WARNING "Found Python ${Python3_VERSION} interpreter, but no development libraries")
56 else()
57 message(WARNING "Unable to find Python3 interpreter or development libraries")
58 endif()
59 set(PY_MAJOR_VERSION ${Python3_VERSION_MAJOR})
60 set(Python_VERSION_MAJOR ${Python3_VERSION_MAJOR})
61 set(Python_VERSION_MINOR ${Python3_VERSION_MINOR})
62 set(Python_EXECUTABLE ${Python3_EXECUTABLE})
63 set(Python_SITEARCH ${Python3_SITEARCH})
64 endif()
65
66 if (NOT Python2_FOUND AND NOT Python3_FOUND)
67 message(WARNING "Disabling PyImath")
68 return()
69 endif()
70
71 # Now that we know what versions of python we have, let's look
72 # for our other dependency - boost.
73 # Boost Python has some .. annoyances in that the python module
74 # has version names attached to it
75 function(PYIMATH_EXTRACT_REL_SITEARCH varname pyver pyexe pysitearch)
76 if(PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR)
77 set(${varname} ${PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules")
78 message(STATUS "Will install to: ${PYIMATH_OVERRIDE_PYTHON_INSTALL_DIR}")
79 else()
80 get_filename_component(_exedir ${pyexe} DIRECTORY)
81 # we do this such that cmake will canonicalize the slashes
82 # so the directory search will work under windows and unix
83 # consistently
84 get_filename_component(_basedir ${pysitearch} DIRECTORY)
85 get_filename_component(_basename ${pysitearch} NAME)
86 set(_basedir "${_basedir}/${_basename}")
87 string(FIND ${_basedir} ${_exedir} _findloc)
88 string(LENGTH ${_exedir} _elen)
89 while(_findloc EQUAL -1 AND _elen GREATER 0)
90 get_filename_component(_nexedir ${_exedir} DIRECTORY)
91 string(FIND ${_basedir} ${_nexedir} _findloc)
92 if (_nexedir STREQUAL _exedir)
93 message(WARNING "Unable to get parent directory for ${_exedir}, using absolute python site arch folder ${pysitearch}")
94 set(_elen -1)
95 break()
96 else()
97 set(_exedir ${_nexedir})
98 endif()
99 string(LENGTH ${_exedir} _elen)
100 endwhile()
101 math(EXPR _elen "${_elen}+1")
102 string(SUBSTRING ${_basedir} ${_elen} -1 _reldir)
103 if(APPLE)
104 # on macOS, set install path to user's python package directory
105 # so that elevated privileges are not necessary
106 execute_process(
107 COMMAND "${pyexe}" -c "if True:
108 import sysconfig
109 print(sysconfig.get_path('platlib', 'posix_user'))"
110 OUTPUT_VARIABLE _reldir
111 OUTPUT_STRIP_TRAILING_WHITESPACE)
112 endif()
113 set(${varname} ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules")
114 message(STATUS "Will install to: ${_reldir}")
115 endif()
116 endfunction()
117
118 set(PYIMATH_BOOST_PY_COMPONENT "python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
119 message(STATUS "Found Python${Python_VERSION_MAJOR} libraries: ${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
120 # we can't just use the Python2_SITEARCH variable as that then will
121 # ignore CMAKE_INSTALL_PREFIX. Could extract this to a function somewhere
122 # if it is generally useful
123 pyimath_extract_rel_sitearch(PyImath_Python${Python_VERSION_MAJOR}_SITEARCH_REL ${Python_VERSION_MAJOR} ${Python_EXECUTABLE} ${Python_SITEARCH})
124
125 # different flavors of O.S. have multiple versions of python
126 # some of them have both. Then for boost, some versions of boost
127 # have just a python component, some it's by major version (python2/python3)
128 # and still others have maj/min (python27)
129 # let's run a search and see what we get instead of making it
130 # an explicit required. The older names are not portable, but
131 # we'll do the best we can
132
133 ### NB: We are turning this on globally by default as the boost
134 ### generated cmake config files seem to be broken and they
135 ### cross-wire python 2 with 3 in the same variables
136 option(Boost_NO_BOOST_CMAKE "Disable boost-provided cmake config" ON)
137 if(Boost_NO_BOOST_CMAKE)
138 message(STATUS "Disabling boost-provided cmake config. If this causes problems, consider setting Boost_NO_BOOST_CMAKE variable to OFF")
139 endif()
140
141 find_package(Boost OPTIONAL_COMPONENTS
142 python
143 python${Python_VERSION_MAJOR}
144 ${PYIMATH_BOOST_PY_COMPONENT})
145 set(_pyimath_have_perver_boost)
146 if(PYIMATH_BOOST_PY_COMPONENT)
147 string(TOUPPER ${PYIMATH_BOOST_PY_COMPONENT} PYIMATH_PY_UPPER)
148 else()
149 set(PYIMATH_BOOST_PY_COMPONENT python${Python_VERSION_MAJOR}x_NOTFOUND)
150 set(PYIMATH_PY_UPPER PYTHON${Python_VERSION_MAJOR}X_NOTFOUND)
151 endif()
152 if(Boost_PYTHON${Python_VERSION_MAJOR}_FOUND OR Boost_${PYIMATH_PY_UPPER}_FOUND)
153 set(_pyimath_have_perver_boost TRUE)
154 if(NOT Boost_${PYIMATH_PY_UPPER}_FOUND)
155 message(WARNING "Legacy Boost python${Python_VERSION_MAJOR} found, but does not include minor version, this is an old configuration and may not be portable")
156 set(PYIMATH_BOOST_PY_COMPONENT python${Python_VERSION_MAJOR})
157 endif()
158 endif()
159
160 if(Boost_PYTHON_FOUND AND NOT _pyimath_have_perver_boost)
161 # old boost case, I guess we just warn and assume it is python2 (likely)
162 message(WARNING "Ambiguous boost python module found, assuming python ${Python_VERSION_MAJOR}. If you have a new boost library, try cleaning the cmake cache and reconfigure with -DBoost_NO_BOOST_CMAKE=ON")
163 set(PYIMATH_BOOST_PY_COMPONENT python)
164 # set it to a bogus string but not empty so later we don't test against a namespace only target
165 # set(PYIMATH_BOOST_PY3_COMPONENT pythonIgnore)
166 elseif(NOT _pyimath_have_perver_boost)
167 message(WARNING "Unable to find boost::python library, disabling PyImath. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
168 return()
169 else()
170 if(TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT})
171 message(STATUS "Found Python ${Python_VERSION_MAJOR} boost: Boost::${PYIMATH_BOOST_PY_COMPONENT}")
172 elseif(Boost_PYTHON_FOUND OR Boost_${PYIMATH_PY_UPPER}_FOUND)
173 message(WARNING "Found boost for python ${Python_VERSION_MAJOR}, but FindBoost did not create an import library. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
174 return()
175 endif()
176 endif()
177 unset(PYIMATH_PY_UPPER)
178 unset(_pyimath_have_perver_boost)
179
180 # unfortunately, we can't use the boost numpy stuff, as that requires a
181 # version of boost that is newer than is mandated by many active versions
182 # of the VFX reference platform (numpy became active in 1.63 of boost).
183 # rather than make this an "official" find package thing
184 include(config/NumPyLocate.cmake)
185
186
187 # utility function for the repeated boilerplate of defining
188 # the libraries and/or python modules
189 include(config/ModuleDefine.cmake)
190
191 ##########################
192 add_subdirectory(config)
193
194 if(NOT BUILD_SHARED_LIBS)
195 message(WARNING "Forcing python bindings to be built with dynamic libraries")
196 endif()
197
198 add_subdirectory( PyImath )
199 if(TARGET Python2::ImathNumPy OR TARGET Python3::ImathNumPy)
200 add_subdirectory( PyImathNumpy )
201 endif()
202
203 ##########################
204 # Tests
205 ##########################
206 include(CTest)
207 if(BUILD_TESTING)
208 enable_testing()
209 add_subdirectory( PyImathTest )
210 add_subdirectory( PyImathSpeedTest )
211 if(TARGET Python2::ImathNumPy OR TARGET Python3::ImathNumPy)
212 add_subdirectory( PyImathNumpyTest )
213 endif()
214 endif()