You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
8.7 KiB

  1. # - Find python interpreter
  2. # This module finds if Python interpreter is installed and determines where the
  3. # executables are. This code sets the following variables:
  4. #
  5. # PYTHONINTERP_FOUND - Was the Python executable found
  6. # PYTHON_EXECUTABLE - path to the Python interpreter
  7. #
  8. # PYTHON_VERSION_STRING - Python version found e.g. 2.5.2
  9. # PYTHON_VERSION_MAJOR - Python major version found e.g. 2
  10. # PYTHON_VERSION_MINOR - Python minor version found e.g. 5
  11. # PYTHON_VERSION_PATCH - Python patch version found e.g. 2
  12. #
  13. # The Python_ADDITIONAL_VERSIONS variable can be used to specify a list of
  14. # version numbers that should be taken into account when searching for Python.
  15. # You need to set this variable before calling find_package(PythonInterp).
  16. #
  17. # You can point to a preferred python install to use by setting the following
  18. # to the point at the root directory of the python install:
  19. #
  20. # PYTHON_ROOT_DIR - The root directory of the python install
  21. #=============================================================================
  22. # Copyright 2005-2010 Kitware, Inc.
  23. # Copyright 2011 Bjoern Ricks <bjoern.ricks@gmail.com>
  24. # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
  25. #
  26. # Distributed under the OSI-approved BSD License (the "License");
  27. # see accompanying file Copyright.txt for details.
  28. #
  29. # This software is distributed WITHOUT ANY WARRANTY; without even the
  30. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  31. # See the License for more information.
  32. #=============================================================================
  33. # (To distribute this file outside of CMake, substitute the full
  34. # License text for the above reference.)
  35. unset(_Python_NAMES)
  36. set(_PYTHON1_VERSIONS 1.6 1.5)
  37. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  38. set(_PYTHON3_VERSIONS 3.14 3.13 3.12 3.11 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
  39. # Disabling the "search every possible place" code for now
  40. # see https://gitlab.com/kicad/code/kicad/-/issues/8553
  41. #if(PythonInterp_FIND_VERSION)
  42. # if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  43. # string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
  44. # string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
  45. # list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
  46. # unset(_PYTHON_FIND_OTHER_VERSIONS)
  47. # if(NOT PythonInterp_FIND_VERSION_EXACT)
  48. # foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
  49. # if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  50. # list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  51. # endif()
  52. # endforeach()
  53. # endif()
  54. # unset(_PYTHON_FIND_MAJ_MIN)
  55. # unset(_PYTHON_FIND_MAJ)
  56. # else()
  57. # list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
  58. # set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
  59. # endif()
  60. #else()
  61. # set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  62. #endif()
  63. list(APPEND _Python_NAMES python3 python)
  64. # Search for the preferred executable first
  65. if( ${PYTHON_ROOT_DIR} )
  66. # Search for any of the executable names solely in the directory we've
  67. # been pointed to. Failure to find the python executable here is a fatal
  68. # fail.
  69. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES}
  70. PATHS ${PYTHON_ROOT_DIR}
  71. NO_DEFAULT_PATH )
  72. elseif(VCPKG_TOOLCHAIN)
  73. # this is a hack for arm64 builds for now
  74. # the main problem being nobody seems to actually cross-compile kicad
  75. # and insteads compiles it painfully on slow hardware
  76. set(INTERP_TRIPLET "${VCPKG_TARGET_TRIPLET}")
  77. if(NOT "${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "ARM64")
  78. if(${VCPKG_TARGET_TRIPLET} STREQUAL "arm64-windows")
  79. set(INTERP_TRIPLET "x64-windows")
  80. endif()
  81. endif()
  82. # Our VCPKG usage will always place it in a known location
  83. find_program(PYTHON_EXECUTABLE
  84. NAMES ${_Python_NAMES}
  85. PATHS "${VCPKG_INSTALLED_DIR}/${INTERP_TRIPLET}/tools/python3"
  86. NO_DEFAULT_PATH
  87. NO_PACKAGE_ROOT_PATH
  88. NO_CMAKE_PATH
  89. NO_CMAKE_ENVIRONMENT_PATH
  90. NO_CMAKE_SYSTEM_PATH
  91. NO_SYSTEM_ENVIRONMENT_PATH
  92. )
  93. else()
  94. # If there is no specific path given, look for python in the path
  95. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  96. endif()
  97. # Set up the versions we know about, in the order we will search. Always add
  98. # the user supplied additional versions to the front.
  99. set(_Python_VERSIONS
  100. ${Python_ADDITIONAL_VERSIONS}
  101. ${_PYTHON_FIND_OTHER_VERSIONS}
  102. )
  103. unset(_PYTHON_FIND_OTHER_VERSIONS)
  104. unset(_PYTHON1_VERSIONS)
  105. unset(_PYTHON2_VERSIONS)
  106. unset(_PYTHON3_VERSIONS)
  107. # Search for newest python version if python executable isn't found
  108. if(NOT PYTHON_EXECUTABLE)
  109. # If using the MINGW compiler, we mustn't find the standard python
  110. # distribution because of multiple C-Runtime errors. We must instead
  111. # use the Python-a-mingw-us distribution
  112. if(MINGW)
  113. list( APPEND _Python_PPATHS ${PYTHON_ROOT_DIR} )
  114. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.9" )
  115. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.8" )
  116. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.7" )
  117. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.6" )
  118. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.5" )
  119. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.4" )
  120. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.3" )
  121. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.2" )
  122. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.1" )
  123. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.0" )
  124. else()
  125. list( APPEND _Python_PPATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath] )
  126. endif()
  127. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  128. set(_Python_NAMES python${_CURRENT_VERSION})
  129. if(WIN32)
  130. list(APPEND _Python_NAMES python)
  131. endif()
  132. find_program(PYTHON_EXECUTABLE
  133. NAMES ${_Python_NAMES}
  134. PATHS ${_Python_PPATHS}
  135. )
  136. endforeach()
  137. endif()
  138. # determine python version string
  139. if(PYTHON_EXECUTABLE)
  140. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  141. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  142. OUTPUT_VARIABLE _VERSION
  143. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  144. ERROR_QUIET)
  145. if(NOT _PYTHON_VERSION_RESULT)
  146. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  147. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  148. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  149. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  150. if(PYTHON_VERSION_PATCH EQUAL 0)
  151. # it's called "Python 2.7", not "2.7.0"
  152. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  153. endif()
  154. else()
  155. # sys.version predates sys.version_info, so use that
  156. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
  157. OUTPUT_VARIABLE _VERSION
  158. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  159. ERROR_QUIET)
  160. if(NOT _PYTHON_VERSION_RESULT)
  161. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  162. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  163. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  164. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
  165. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
  166. else()
  167. set(PYTHON_VERSION_PATCH "0")
  168. endif()
  169. else()
  170. # sys.version was first documented for Python 1.5, so assume
  171. # this is older.
  172. set(PYTHON_VERSION_STRING "1.4")
  173. set(PYTHON_VERSION_MAJOR "1")
  174. set(PYTHON_VERSION_MAJOR "4")
  175. set(PYTHON_VERSION_MAJOR "0")
  176. endif()
  177. endif()
  178. unset(_PYTHON_VERSION_RESULT)
  179. unset(_VERSION)
  180. endif()
  181. # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
  182. # all listed variables are TRUE
  183. include(FindPackageHandleStandardArgs)
  184. find_package_handle_standard_args(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  185. mark_as_advanced(PYTHON_EXECUTABLE)