FindPython.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # FindPython
  2. # --------
  3. #
  4. # Find the Python libraries(Only for Python provided by FastCAE)
  5. #
  6. # Result Variables
  7. # ^^^^^^^^^^^^^^^^
  8. #
  9. # The following variables will be defined:
  10. #
  11. # ``Python_FOUND`` True if Python found on the local system
  12. #
  13. # ``Python_VERSION`` Python Version(x.x.x)
  14. #
  15. # ``Python_VERSION_MAJOR`` Python Major Version
  16. #
  17. # ``Python_VERSION_MINOR`` Python Minor Version
  18. #
  19. # ``Python_VERSION_PATCH`` Python Patch Version
  20. #
  21. # ``Python_DIRS`` Location of Python(root dir)
  22. #
  23. # ``Python_INCLUDE_DIRS`` Location of Python header files
  24. #
  25. # ``Python_LIBRARY_DIRS`` Location of Python libraries
  26. #
  27. # ``Python_LIBRARIES`` List of the Python libraries found
  28. #
  29. # ``Python_EXECUTABLE`` Location of Python program
  30. #
  31. # 防止重复引入
  32. if(GJDM_Python_ALREADY_INCLUDED)
  33. return()
  34. endif()
  35. set(GJDM_Python_ALREADY_INCLUDED 1)
  36. set(Python_VERSION_MAJOR 3)
  37. set(Python_VERSION_MINOR 7)
  38. set(Python_VERSION_PATCH 0)
  39. # find_path 搜索包含某个文件的路径
  40. # 如果在某个路径下发现了该文件,该结果会被存储到该变量中;如果没有找到,存储的结果将会是<VAR>-NOTFOUND
  41. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  42. set(_python_h_dir "include/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m")
  43. elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  44. set(_python_h_dir "include")
  45. endif()
  46. find_path(Python_DIRS
  47. NAMES
  48. "${_python_h_dir}/Python.h"
  49. PATHS
  50. ${CMAKE_SOURCE_DIR}/extlib/Python
  51. NO_SYSTEM_ENVIRONMENT_PATH
  52. NO_CMAKE_SYSTEM_PATH
  53. )
  54. find_path(Python_INCLUDE_DIRS
  55. NAMES
  56. Python.h
  57. HINTS
  58. ${Python_DIRS}/${_python_h_dir}
  59. )
  60. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  61. set(Python_EXECUTABLE "${Python_DIRS}/python.exe")
  62. else()
  63. set(Python_EXECUTABLE "${Python_DIRS}/bin/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}")
  64. endif()
  65. #[[if(EXISTS ${Python_EXECUTABLE})
  66. message(STATUS "Python_EXECUTABLE: ${Python_EXECUTABLE}")
  67. else()
  68. set(Python_EXECUTABLE "Python_EXECUTABLE-NOTFOUND")
  69. endif()]]
  70. #[[execute_process(
  71. COMMAND
  72. ${Python_EXECUTABLE} "-c" "import sys\nprint(sys.version_info)"
  73. RESULT_VARIABLE _status
  74. OUTPUT_VARIABLE _version
  75. ERROR_QUIET
  76. OUTPUT_STRIP_TRAILING_WHITESPACE
  77. )
  78. function(_findPythonVersion _key _output)
  79. # 初始化
  80. set(CMAKE_MATCH_1 "0")
  81. # 定义正则
  82. set(_version_expr "${_key}=([0-9]+),")
  83. # 匹配
  84. string(REGEX MATCH "${_version_expr}" Python_ver "${_version}")
  85. # PARENT_SCOPE选项,意味着该变量的作用域会传递到上一层(函数的调用者)
  86. set(${_output} "${CMAKE_MATCH_1}" PARENT_SCOPE)
  87. endfunction(_findPythonVersion)
  88. _findPythonVersion("major" Python_VERSION_MAJOR)
  89. _findPythonVersion("minor" Python_VERSION_MINOR)
  90. _findPythonVersion("micro" Python_VERSION_PATCH)]]
  91. set(Python_VERSION "${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}.${Python_VERSION_PATCH}")
  92. find_path(Python_LIBRARY_DIRS
  93. NAMES
  94. python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.lib libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so
  95. HINTS
  96. ${Python_DIRS}/libs ${Python_DIRS}/lib
  97. )
  98. set(Python_LIBRARIES GJDM::PYTHON)
  99. add_library(GJDM::PYTHON SHARED IMPORTED)
  100. set_property(TARGET GJDM::PYTHON PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Python_INCLUDE_DIRS})
  101. set_property(TARGET GJDM::PYTHON APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  102. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  103. add_library(GJDM::FFI SHARED IMPORTED)
  104. set_property(TARGET GJDM::FFI APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
  105. set_target_properties(GJDM::FFI PROPERTIES
  106. IMPORTED_LOCATION_RELEASE "${Python_LIBRARY_DIRS}/libffi.so.8.1.0"
  107. IMPORTED_SONAME_RELEASE "libffi.so.8"
  108. )
  109. set_target_properties(GJDM::PYTHON PROPERTIES
  110. IMPORTED_LOCATION_RELEASE "${Python_LIBRARY_DIRS}/libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so.1.0"
  111. IMPORTED_SONAME_RELEASE "libpython${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}m.so"
  112. )
  113. elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  114. set_target_properties(GJDM::PYTHON PROPERTIES
  115. IMPORTED_IMPLIB_RELEASE "${Python_LIBRARY_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.lib"
  116. IMPORTED_LOCATION_RELEASE "${Python_DIRS}/python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}.dll"
  117. )
  118. endif()
  119. include(FindPackageHandleStandardArgs)
  120. # 如果找到所有需要的变量,并且版本匹配,则将Python_FOUND变量设置为TRUE
  121. find_package_handle_standard_args(Python
  122. FOUND_VAR
  123. Python_FOUND
  124. REQUIRED_VARS
  125. Python_DIRS
  126. Python_INCLUDE_DIRS
  127. Python_LIBRARY_DIRS
  128. Python_LIBRARIES
  129. Python_EXECUTABLE
  130. VERSION_VAR
  131. Python_VERSION
  132. )