FindPython.cmake 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # 防止重复引入
  2. if(GJDM_Python_ALREADY_INCLUDED)
  3. return()
  4. endif()
  5. set(GJDM_Python_ALREADY_INCLUDED 1)
  6. set(Python_VERSION_MAJOR 3)
  7. set(Python_VERSION_MINOR 12)
  8. set(Python_VERSION_PATCH 0)
  9. # find_path 搜索包含某个文件的路径
  10. # 如果在某个路径下发现了该文件,该结果会被存储到该变量中;如果没有找到,存储的结果将会是<VAR>-NOTFOUND
  11. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  12. set(_python_h_dir "include/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m")
  13. elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  14. set(_python_h_dir "include")
  15. endif()
  16. find_path(Python_DIRS
  17. NAMES
  18. "${_python_h_dir}/Python.h"
  19. PATHS
  20. ${CMAKE_SOURCE_DIR}/extlib/Python
  21. NO_SYSTEM_ENVIRONMENT_PATH
  22. NO_CMAKE_SYSTEM_PATH
  23. )
  24. find_path(Python_INCLUDE_DIRS
  25. NAMES
  26. Python.h
  27. HINTS
  28. ${Python_DIRS}/${_python_h_dir}
  29. )
  30. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  31. set(Python_EXECUTABLE "${Python_DIRS}/python.exe")
  32. else()
  33. set(Python_EXECUTABLE "${Python_DIRS}/bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
  34. endif()
  35. set(Python_VERSION "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}.${Python_VERSION_PATCH}")
  36. find_path(Python_LIBRARY_DIRS
  37. NAMES
  38. python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.lib libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so
  39. HINTS
  40. ${Python_DIRS}/libs ${Python_DIRS}/lib
  41. )
  42. set(Python_LIBRARIES GJDM::PYTHON)
  43. add_library(GJDM::PYTHON SHARED IMPORTED)
  44. set_property(TARGET GJDM::PYTHON PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Python_INCLUDE_DIRS})
  45. set_property(TARGET GJDM::PYTHON APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  46. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  47. add_library(GJDM::FFI SHARED IMPORTED)
  48. set_property(TARGET GJDM::FFI APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  49. set_target_properties(GJDM::FFI PROPERTIES
  50. IMPORTED_LOCATION_RELEASE "${Python_LIBRARY_DIRS}/libffi.so.8.1.0"
  51. IMPORTED_SONAME_RELEASE "libffi.so.8"
  52. )
  53. set_target_properties(GJDM::PYTHON PROPERTIES
  54. IMPORTED_LOCATION_RELEASE "${Python_LIBRARY_DIRS}/libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so.1.0"
  55. IMPORTED_SONAME_RELEASE "libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so"
  56. )
  57. elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  58. set_target_properties(GJDM::PYTHON PROPERTIES
  59. IMPORTED_IMPLIB_RELEASE "${Python_LIBRARY_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.lib"
  60. IMPORTED_LOCATION_RELEASE "${Python_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.dll"
  61. )
  62. endif()
  63. include(FindPackageHandleStandardArgs)
  64. # 如果找到所有需要的变量,并且版本匹配,则将Python_FOUND变量设置为TRUE
  65. find_package_handle_standard_args(Python
  66. FOUND_VAR
  67. Python_FOUND
  68. REQUIRED_VARS
  69. Python_DIRS
  70. Python_INCLUDE_DIRS
  71. Python_LIBRARY_DIRS
  72. Python_LIBRARIES
  73. Python_EXECUTABLE
  74. VERSION_VAR
  75. Python_VERSION
  76. )